Infinitive iteration with back option

Thanks, guys

I won't use nightly version for linked_list just because upgrade required for app users.

Finally, created this function in few rows.
If somebody want working example, here is the source:

struct Cursor {
    current: usize,
    last: usize,
}

impl Cursor {
    pub fn back(&mut self) {
        self.current = if self.current > 0 {
            self.current - 1
        } else {
            self.last
        }
    }

    pub fn next(&mut self) {
        self.current = if self.current < self.last {
            self.current + 1
        } else {
            0
        }
    }

    pub fn as_index(&self) -> usize {
        if self.current > 0 {
            self.current - 1
        } else {
            0
        }
    }
}

pub struct Model<T> {
    cursor: Cursor,
    vector: Vec<T>,
}

impl<T> Model<T> {
    pub fn new(vector: Vec<T>) -> Self {
        Self {
            cursor: Cursor {
                current: 0,
                last: vector.len(),
            },
            vector,
        }
    }

    pub fn back(&mut self) -> Option<&T> {
        self.cursor.back();
        self.vector.get(self.cursor.as_index())
    }

    pub fn next(&mut self) -> Option<&T> {
        self.cursor.next();
        self.vector.get(self.cursor.as_index())
    }
}
  • it will infinitively move the Cursor by one step for Vec<T> , according to direction
  • should I make it as the new crate? surprise me if does not exist yet.
1 Like