[][src]Struct shifted_vec::ShiftedVec

pub struct ShiftedVec<T> { /* fields omitted */ }

Wrapping the std::vec::Vec that keeps track of the offset that can handle any Type

let mut l: ShiftedVec<String> = ShiftedVec::with_offset(-2);

l.push("first".to_string());
l.push("second".to_string());

for (shifted, element) in l.iter_shifted() {
    println!("{}: {}", shifted, element);
}
assert_eq!(2, l.len());

Implementations

impl<T> ShiftedVec<T>[src]

pub fn with_offset(offset: isize) -> ShiftedVec<T>[src]

Create a ShiftedVec backed by a std::vec::Vec with capacity 10

let l: ShiftedVec<(usize, isize)> = ShiftedVec::with_offset(2);

pub fn with_offset_and_capacity(offset: isize, capacity: usize) -> ShiftedVec<T>[src]

Create a ShiftedVec

let mut l: ShiftedVec<(usize, isize)> = ShiftedVec::with_offset(2);
assert_eq!(0, l.len());

l.push((42, -42));
assert_eq!(1, l.len());

assert_eq!((42, -42), l[2]);

pub fn len(&self) -> usize[src]

pub fn push(&mut self, thing: T)[src]

Append to the current list

let l = {
    let mut l = ShiftedVec::with_offset(2);
    l.push((42, 42));
    l
};
assert_eq!(1, l.len());

pub fn get(&self, i: isize) -> Option<&T>[src]

Checked access to elements, see get_mut

pub fn get_mut(&mut self, i: isize) -> Option<&mut T>[src]

Checked writeable access to elements

let mut l = ShiftedVec::with_offset(2);

l.push(42);

assert_eq!(None, l.get_mut(0));
assert_eq!(None, l.get_mut(1));
assert_eq!(Some(&mut 42), l.get_mut(2));
assert_eq!(None, l.get_mut(3));

match l.get_mut(2) {
    Some(v) => *v = 64,
    None => unreachable!("we made sure that we do not miss"),
}

assert_eq!(64, l[2]);

impl<'a, T> ShiftedVec<T>[src]

pub fn iter(&self) -> Iter<T>[src]

Get an iterator

let mut l = ShiftedVec::with_offset(-2);
let first = "first";

l.push(first);

let mut it = l.iter();
assert_eq!(Some(&first), it.next());
assert_eq!(None, it.next());

pub fn iter_mut(&mut self) -> IterMut<T>[src]

Get a mutable iterator

let mut l = ShiftedVec::with_offset(-2);
let first = "first".to_string();
let second = "second".to_string();

l.push(first);

let mut it = l.iter_mut();
if let Some(val) = it.next() {
    *val = second.clone();
}
assert_eq!(None, it.next());

assert_eq!(second, l[-2]);

pub fn iter_shifted(&self) -> ShiftedIterator<T>[src]

Get an iterator including the offset

let mut l: ShiftedVec<String> = ShiftedVec::with_offset(-2);

l.push("first".to_string());
l.push("second".to_string());

let mut it = l.iter_shifted();
assert_eq!(Some((-2, &"first".to_string())), it.next());
assert_eq!(Some((-1, &"second".to_string())), it.next());
assert_eq!(None, it.next());

pub fn iter_shifted_mut(&mut self) -> ShiftedIteratorMut<T>[src]

Get a mutable iterator including the offset

let mut l: ShiftedVec<String> = ShiftedVec::with_offset(-2);

l.push("first".to_string());
l.push("second".to_string());

for (offset,  element) in l.iter_shifted_mut() {
    println!("{}: {}", offset, element);
    if offset == -2 {
        *element = "replaced".to_string();
    }
    if offset == -1 {
        element.push_str(" modified");
    }
}

assert_eq!("replaced", l[-2]);
assert_eq!("second modified", l[-1]);

Trait Implementations

impl<T: Debug> Debug for ShiftedVec<T>[src]

impl<T> Index<isize> for ShiftedVec<T>[src]

type Output = T

The returned type after indexing.

impl<T> IndexMut<isize> for ShiftedVec<T>[src]

Auto Trait Implementations

impl<T> RefUnwindSafe for ShiftedVec<T> where
    T: RefUnwindSafe

impl<T> Send for ShiftedVec<T> where
    T: Send

impl<T> Sync for ShiftedVec<T> where
    T: Sync

impl<T> Unpin for ShiftedVec<T> where
    T: Unpin

impl<T> UnwindSafe for ShiftedVec<T> where
    T: UnwindSafe

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.