Krolyxon
Commit 6b66a44

rmptuisrclist.rs

46 lines · 957 B · Rust

1#[derive(Debug)]
2pub struct ContentList<T> {
3 pub list: Vec<T>,
4 pub index: usize,
5}
6
7impl<T> ContentList<T> {
8 pub fn new() -> Self {
9 ContentList {
10 list: Vec::new(),
11 index: 0,
12 }
13 }
14
15 // Go to next item in list
16 pub fn next(&mut self) {
17 // if self.index < self.list.len() - 1 {
18 // self.index += 1;
19 // }
20
21 let len = self.list.len();
22 if len != 0 {
23 if self.index == self.list.len() - 1 {
24 self.index = 0;
25 } else {
26 self.index += 1;
27 }
28 }
29 }
30
31 /// Go to previous item in list
32 pub fn prev(&mut self) {
33 if self.index == 0 {
34 let len = self.list.len();
35 if len != 0 {
36 self.index = len - 1;
37 }
38 } else {
39 self.index -= 1;
40 }
41 }
42
43 pub fn reset_index(&mut self) {
44 self.index = 0;
45 }
46}