Struct holodeque::slice_deque::SliceDeque[][src]

pub struct SliceDeque<'a, T> where
    T: Default
{ /* fields omitted */ }
Expand description

A double-ended queue with fixed capacity, backed by a slice.

The capacity of the deque is determined by the length of the slice.

Implementations

Creates an empty SliceDeque backed by the provided slice.

The elements in the slice are dropped and replaced with the default value of T.

Example

let mut slice = ["these", "values", "will", "disappear"];
let mut deque = SliceDeque::new_in(&mut slice);

assert!(deque.is_empty());
assert_eq!(deque.capacity(), 4);

Returns the maximum number of elements the deque may hold.

This is the length of the backing slice.

Example

let mut slice = [(), (), (), ()];
let mut deque = SliceDeque::new_in(&mut slice);

assert_eq!(deque.capacity(), 4);

Returns the number of elements in the deque.

Example

let mut slice = [0, 0, 0];
let mut deque = SliceDeque::new_in(&mut slice);

assert_eq!(deque.len(), 0);
deque.push_back(1)?;
deque.push_back(2)?;
deque.push_back(3)?;
assert_eq!(deque.len(), 3);

Returns true if the deque contains no elements.

Example

let mut slice = [0, 0, 0];
let mut deque = SliceDeque::new_in(&mut slice);

deque.push_back(42)?;
assert!(!deque.is_empty());
deque.pop_front();
assert!(deque.is_empty());

Returns true if the deque is at capacity.

Example

let mut slice = [0, 0, 0, 0];
let mut deque = SliceDeque::new_in(&mut slice);

deque.push_back(1)?;
deque.push_back(2)?;
deque.push_back(3)?;
assert!(!deque.is_full());

deque.push_back(4)?;
assert!(deque.is_full());

Returns a reference to the first element in the deque.

If the deque is empty, None is returned.

Example

let mut slice = ['\0', '\0', '\0'];
let mut deque = SliceDeque::new_in(&mut slice);

deque.push_back('a')?;
deque.push_back('b')?;
deque.push_back('c')?;

assert_eq!(deque.front(), Some(&'a'));

Returns a mutable reference to the first element in the deque.

If the deque is empty, None is returned.

Example

let mut slice = ["", "", "", ""];
let mut deque = SliceDeque::new_in(&mut slice);

deque.push_front("old")?;
deque.front_mut().map(|mut val| {
    *val = "new";
});

assert_eq!(deque.front(), Some(&"new"));

Returns a reference to the last element in the deque.

If the deque is empty, None is returned.

Example

let mut slice = ['\0', '\0', '\0'];
let mut deque = SliceDeque::new_in(&mut slice);

deque.push_back('a')?;
deque.push_back('b')?;
deque.push_back('c')?;

assert_eq!(deque.back(), Some(&'c'));

Returns a mutable reference to the last element in the deque.

If the deque is empty, None is returned.

Example

let mut slice = ["", "", "", ""];
let mut deque = SliceDeque::new_in(&mut slice);

deque.push_back("old")?;
deque.back_mut().map(|mut val| {
    *val = "new";
});

assert_eq!(deque.back(), Some(&"new"));

Returns a pair of slices which contain, in order, the elements of the SliceDeque.

Example

let mut slice = [0, 0, 0, 0, 0, 0];
let mut deque = SliceDeque::new_in(&mut slice);

deque.push_front(3)?;
deque.push_front(6)?;
deque.push_front(9)?;
deque.push_back(5)?;
deque.push_back(10)?;
deque.push_back(15)?;

let (first, second) = deque.as_slices();
assert_eq!(first, &[9, 6, 3]);
assert_eq!(second, &[5, 10, 15]);

Returns a pair of mutable slices which contain, in order, the elements of the SliceDeque.

Example

let mut slice = [0, 0, 0, 0, 0, 0];
let mut deque = SliceDeque::new_in(&mut slice);

deque.push_front(3)?;
deque.push_front(6)?;
deque.push_front(9)?;
deque.push_back(5)?;
deque.push_back(10)?;
deque.push_back(15)?;

let (first_mut, second_mut) = deque.as_mut_slices();
for item in first_mut {
    *item -= 1;
}
for item in second_mut {
    *item += 1;
}

let (first, second) = deque.as_slices();
assert_eq!(first, &[8, 5, 2]);
assert_eq!(second, &[6, 11, 16]);

Prepends an element to the deque.

If the deque is at capacity, an Err containing the pushed value is returned.

Example

let mut slice = [0, 0, 0];
let mut deque = SliceDeque::new_in(&mut slice);

deque.push_front(1)?;
deque.push_front(2)?;
deque.push_front(3)?;

assert_eq!(deque.front(), Some(&3));
assert_eq!(deque.back(), Some(&1));

// Another element would exceed capacity, so this fails.
let err = deque.push_front(4).unwrap_err();
assert_eq!(err.into_inner(), 4);

Appends an element to the deque.

If the deque is at capacity, an Err containing the pushed value is returned.

Example

let mut slice = [0, 0, 0];
let mut deque = SliceDeque::new_in(&mut slice);

deque.push_back(1)?;
deque.push_back(2)?;
deque.push_back(3)?;

assert_eq!(deque.front(), Some(&1));
assert_eq!(deque.back(), Some(&3));

// Another element would exceed capacity, so this fails.
let err = deque.push_back(4).unwrap_err();
assert_eq!(err.into_inner(), 4);

Removes and returns the first element of the deque.

If the deque is empty, None is returned.

Example

let mut slice = [0, 0, 0];
let mut deque = SliceDeque::new_in(&mut slice);

deque.push_back(1)?;
deque.push_back(2)?;
deque.push_back(3)?;

assert_eq!(deque.pop_front(), Some(1));
assert_eq!(deque.pop_front(), Some(2));
assert_eq!(deque.pop_front(), Some(3));
assert_eq!(deque.pop_front(), None);

Removes and returns the last element of the deque.

If the deque is empty, None is returned.

Example

let mut slice = [0, 0, 0];
let mut deque = SliceDeque::new_in(&mut slice);

deque.push_back(1)?;
deque.push_back(2)?;
deque.push_back(3)?;

assert_eq!(deque.pop_back(), Some(3));
assert_eq!(deque.pop_back(), Some(2));
assert_eq!(deque.pop_back(), Some(1));
assert_eq!(deque.pop_back(), None);

Clears the SliceDeque, removing all values.

Example

let mut slice = [0, 0, 0, 0, 0, 0, 0, 0];
let mut deque = SliceDeque::new_in(&mut slice);

for i in 0..deque.capacity() / 2 {
    deque.push_front(i)?;
    deque.push_back(i)?;
}

assert_eq!(deque.len(), 8);
deque.clear();
assert!(deque.is_empty());

Shortens the SliceDeque, keeping the first len elements and dropping the rest.

If len is greater than the SliceDeque’s current length, this has no effect.

Example

let mut slice = [0, 0, 0, 0, 0, 0, 0, 0];
let mut deque = SliceDeque::new_in(&mut slice);

deque.push_back(5)?;
deque.push_back(10)?;
deque.push_back(15)?;
deque.push_back(20)?;
deque.push_back(25)?;

assert_eq!(deque.len(), 5);
deque.truncate(2);
assert_eq!(deque.len(), 2);

Returns an iterator over the elements of the deque.

Example

let mut slice = ["", "", "", "", ""];
let mut deque = SliceDeque::new_in(&mut slice);

deque.push_back("ideas")?;
deque.push_front("green")?;
deque.push_back("sleep")?;
deque.push_front("colorless")?;
deque.push_back("furiously")?;

let sentence = deque.iter().cloned().collect::<Vec<_>>();

assert_eq!(
    sentence,
    &["colorless", "green", "ideas", "sleep", "furiously"],
);

Drains n elements from the front of the deque.

If n exceeds self.len(), None is returned.

When this method is called, n elements are immediately removed from the front of the deque. If the returned iterator is dropped before yielding all its items, they are dropped along with it.

If the returned iterator is leaked (e.g. with mem::forget), the drained elements will not be dropped immediately. They may be dropped as a result of subsequent operations on the deque; otherwise, they will be dropped when the deque itself is dropped.

Example

let mut slice = [0, 0, 0, 0, 0];
let mut deque = SliceDeque::new_in(&mut slice);

deque.push_back(0)?;
deque.push_back(1)?;
deque.push_back(2)?;
deque.push_back(3)?;
deque.push_back(4)?;

let mut drain = deque.drain_front(3).unwrap();

assert_eq!(drain.next(), Some(0));
assert_eq!(drain.next(), Some(1));
assert_eq!(drain.next(), Some(2));
assert_eq!(drain.next(), None);
drop(drain);

assert_eq!(deque.len(), 2);

Drains n elements from the back of the deque.

If n exceeds self.len(), None is returned.

When this method is called, n elements are immediately removed from the back of the deque. If the returned iterator is dropped before yielding all its items, they are dropped along with it.

If the returned iterator is leaked (e.g. with mem::forget), the drained elements will not be dropped immediately. They may be dropped as a result of subsequent operations on the deque; otherwise, they will be dropped when the deque itself is dropped.

Example

let mut slice = [0, 0, 0, 0, 0];
let mut deque = SliceDeque::new_in(&mut slice);

deque.push_back(0)?;
deque.push_back(1)?;
deque.push_back(2)?;
deque.push_back(3)?;
deque.push_back(4)?;

let mut drain = deque.drain_back(3).unwrap();

assert_eq!(drain.next(), Some(4));
assert_eq!(drain.next(), Some(3));
assert_eq!(drain.next(), Some(2));
assert_eq!(drain.next(), None);
drop(drain);

assert_eq!(deque.len(), 2);

Extends the deque with the contents of a deserializer.

Trait Implementations

Formats the value using the given formatter. Read more

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.