Struct Entries

Source
pub struct Entries<'a, T> { /* private fields */ }
Expand description

A view into the elements of a Vec

Implementations§

Source§

impl<'a, T> Entries<'a, T>

Source

pub fn remaining(&mut self) -> &mut [T]

Get the remaining elements of the view as a mutable slice.

§Examples
use vec_entries::EntriesExt;

let mut v = vec![0, 1, 2];
v.entries(.., |e| {
    assert_eq!(e.remaining(), [0, 1, 2]);
    e.shift();
    assert_eq!(e.remaining(), [1, 2]);
    e.shift();
    assert_eq!(e.remaining(), [2]);
});
Source

pub const fn remaining_len(&self) -> usize

Get the number of elements still present in the view.

§Examples
use vec_entries::EntriesExt;

let mut v = vec![0, 1, 2];
v.entries(.., |e| {
    assert_eq!(e.remaining_len(), 3);
    e.shift();
    assert_eq!(e.remaining_len(), 2);
    e.shift();
    assert_eq!(e.remaining_len(), 1);
});
Source

pub const fn len_before(&self) -> usize

Get the length of portion of the underlying Vec before the view.

§Examples
use vec_entries::EntriesExt;

let mut v = vec![0, 1, 2];
v.entries(.., |e| {
    assert_eq!(e.len_before(), 0);
    e.shift();
    assert_eq!(e.len_before(), 1);
    e.shift();
    assert_eq!(e.len_before(), 2);
});
Source

pub fn len_after(&self) -> usize

Get the length of portion of the underlying Vec after the view.

§Examples
use vec_entries::EntriesExt;

let mut v = vec![0, 1, 2];
v.entries(.., |e| {
    assert_eq!(e.len_after(), 0);
    e.shift_back();
    assert_eq!(e.len_after(), 1);
    e.shift_back();
    assert_eq!(e.len_after(), 2);
});
Source

pub fn clear(&mut self)

Remove and drop all remaining elements in the view.

§Examples
use vec_entries::EntriesExt;

let mut v = vec![0, 1, 2, 3, 4];
v.entries(.., |e| {
    e.shift(); // shift `0` out of view
    e.shift_back(); // shift `4` out of view
    e.clear(); // Drop 1, 2, and 3
});
assert_eq!(v, [0, 4]);
Source

pub fn front<'b>(&'b mut self) -> Option<Entry<'a, 'b, T>>

Get an entry for the element at the head of the view.

Returns None if the view is empty.

§Examples
use vec_entries::EntriesExt;

let mut v = vec![0, 1, 2];
v.entries(.., |e| {
    let f = e.front().unwrap();
    assert_eq!(*f, 0);
    f.shift();

    let f = e.front().unwrap();
    assert_eq!(*f, 1);
    f.shift();

    let f = e.front().unwrap();
    assert_eq!(*f, 2);
    f.shift();

    assert!(e.front().is_none());
});
Source

pub fn back<'b>(&'b mut self) -> Option<Entry<'a, 'b, T>>

Get an entry for the element at the tail of the view.

Returns None if the view is empty.

§Examples
use vec_entries::EntriesExt;

let mut v = vec![0, 1, 2];
v.entries(.., |e| {
    let b = e.back().unwrap();
    assert_eq!(*b, 2);
    b.shift();

    let b = e.back().unwrap();
    assert_eq!(*b, 1);
    b.shift();

    let b = e.back().unwrap();
    assert_eq!(*b, 0);
    b.shift();

    assert!(e.back().is_none());
});
Source

pub fn shift(&mut self) -> Option<&'a mut T>

Shift the head of the view inward by one, returning a mutable reference to the now excluded item.

Returns None if the view is empty.

§Examples
use vec_entries::EntriesExt;

let mut v = vec![0, 1, 2];
v.entries(.., |e| {
    assert_eq!(e.shift(), Some(&mut 0));
    assert_eq!(e.shift(), Some(&mut 1));
    assert_eq!(e.shift(), Some(&mut 2));
    assert_eq!(e.shift(), None);
});
Source

pub fn shift_back(&mut self) -> Option<&'a mut T>

Shift the tail of the view inward by one, returning a mutable reference to the now excluded item.

§Examples
use vec_entries::EntriesExt;

let mut v = vec![0, 1, 2];
v.entries(.., |e| {
    assert_eq!(e.shift_back(), Some(&mut 2));
    assert_eq!(e.shift_back(), Some(&mut 1));
    assert_eq!(e.shift_back(), Some(&mut 0));
    assert_eq!(e.shift_back(), None);
});
Source

pub fn remove(&mut self) -> Option<T>

Remove the item at the head of the view from the underlying Vec, returning the now removed item.

Returns None if the view is empty.

§Examples
use vec_entries::EntriesExt;

let mut v = vec![0, 1, 2];
v.entries(.., |e| {
    e.shift();
    assert_eq!(e.remove(), Some(1));
});
assert_eq!(v, [0, 2]);
Examples found in repository?
examples/merkle.rs (line 22)
20fn merkle_step<T>(v: &mut Vec<T>, mut join: impl FnMut(T, T) -> T) {
21    v.entries(.., |e| {
22        while let Some(left) = e.remove() {
23            let Some(right) = e.remove() else {
24                // If we can't find a right, just insert the left and stop
25                let Ok(_) = e.try_insert_outside(left) else {
26                    unreachable!()
27                };
28                return;
29            };
30
31            // Combine the two items and insert the result
32            let joined = join(left, right);
33            let Ok(_) = e.try_insert_outside(joined) else {
34                unreachable!()
35            };
36        }
37    });
38}
More examples
Hide additional examples
examples/sparse_merkle.rs (line 22)
20fn sparse_merkle_step<T>(v: &mut Vec<T>, mut maybe_join: impl FnMut(T, T) -> (T, Option<T>)) {
21    v.entries(.., |e| {
22        let Some(mut left) = e.remove() else { return };
23
24        while let Some(right) = e.remove() {
25            // Combine the two items and insert the result
26            // If the join succeeded this is the joined item
27            // If the join failed, this is the left item
28            let (joined, failed) = maybe_join(left, right);
29            let Ok(_) = e.try_insert_outside(joined) else {
30                unreachable!()
31            };
32
33            // Try to get the next left item, either from the join-failure or the next entry
34            // Stop if we can't get any more items
35            left = if let Some(next) = failed {
36                next
37            } else if let Some(next) = e.remove() {
38                next
39            } else {
40                return;
41            };
42        }
43
44        // Re-insert any unused left items
45        let Ok(_) = e.try_insert_outside(left) else {
46            unreachable!()
47        };
48    })
49}
Source

pub fn remove_back(&mut self) -> Option<T>

Remove the item at the tail of the view from the underlying Vec, returning the now removed item.

Returns None if the view is empty.

§Examples
use vec_entries::EntriesExt;

let mut v = vec![0, 1, 2];
v.entries(.., |e| {
    assert_eq!(e.remove_back(), Some(2));
});
assert_eq!(v, [0, 1]);
Source

pub fn try_insert_inside(&mut self, v: T) -> Result<&mut T, T>

Try to insert an item into the underlying Vec, just before the head of the view. The view is then shifted include the added item.

Returns Err if the there is no empty space for the insert to occur.

§Examples
use vec_entries::EntriesExt;

let mut v = vec![0, 1, 2];
v.entries(.., |e| {
    // Can't insert if you didn't remove
    assert_eq!(e.try_insert_inside(5), Err(5));

    assert_eq!(e.remove(), Some(0));
    assert_eq!(e.remaining(), [1, 2]);
    assert_eq!(e.try_insert_inside(5), Ok(&mut 5));
    assert_eq!(e.remaining(), [5, 1, 2]);
});
assert_eq!(v, [5, 1, 2]);
Source

pub fn try_insert_inside_back(&mut self, v: T) -> Result<&mut T, T>

Try to insert an item into the underlying Vec, just after the tail of the view. The view is then shifted include the added item.

Returns Err if the there is no empty space for the insert to occur.

§Examples
use vec_entries::EntriesExt;

let mut v = vec![0, 1, 2];
v.entries(.., |e| {
    // Can't insert if you didn't remove
    assert_eq!(e.try_insert_inside_back(5), Err(5));

    assert_eq!(e.remove_back(), Some(2));
    assert_eq!(e.remaining(), [0, 1]);
    assert_eq!(e.try_insert_inside_back(5), Ok(&mut 5));
    assert_eq!(e.remaining(), [0, 1, 5]);
});
assert_eq!(v, [0, 1, 5]);
Source

pub fn try_insert_outside(&mut self, v: T) -> Result<&'a mut T, T>

Try to insert an item into the underlying Vec, just before the head of the view.

Returns Err if the there is no empty space for the insert to occur.

§Examples
use vec_entries::EntriesExt;

let mut v = vec![0, 1, 2];
v.entries(.., |e| {
    // Can't insert if you didn't remove
    assert_eq!(e.try_insert_outside(5), Err(5));

    assert_eq!(e.remove(), Some(0));
    assert_eq!(e.remaining(), [1, 2]);
    assert_eq!(e.try_insert_outside(5), Ok(&mut 5));
    assert_eq!(e.remaining(), [1, 2]);
});
assert_eq!(v, [5, 1, 2]);
Examples found in repository?
examples/merkle.rs (line 25)
20fn merkle_step<T>(v: &mut Vec<T>, mut join: impl FnMut(T, T) -> T) {
21    v.entries(.., |e| {
22        while let Some(left) = e.remove() {
23            let Some(right) = e.remove() else {
24                // If we can't find a right, just insert the left and stop
25                let Ok(_) = e.try_insert_outside(left) else {
26                    unreachable!()
27                };
28                return;
29            };
30
31            // Combine the two items and insert the result
32            let joined = join(left, right);
33            let Ok(_) = e.try_insert_outside(joined) else {
34                unreachable!()
35            };
36        }
37    });
38}
More examples
Hide additional examples
examples/sparse_merkle.rs (line 29)
20fn sparse_merkle_step<T>(v: &mut Vec<T>, mut maybe_join: impl FnMut(T, T) -> (T, Option<T>)) {
21    v.entries(.., |e| {
22        let Some(mut left) = e.remove() else { return };
23
24        while let Some(right) = e.remove() {
25            // Combine the two items and insert the result
26            // If the join succeeded this is the joined item
27            // If the join failed, this is the left item
28            let (joined, failed) = maybe_join(left, right);
29            let Ok(_) = e.try_insert_outside(joined) else {
30                unreachable!()
31            };
32
33            // Try to get the next left item, either from the join-failure or the next entry
34            // Stop if we can't get any more items
35            left = if let Some(next) = failed {
36                next
37            } else if let Some(next) = e.remove() {
38                next
39            } else {
40                return;
41            };
42        }
43
44        // Re-insert any unused left items
45        let Ok(_) = e.try_insert_outside(left) else {
46            unreachable!()
47        };
48    })
49}
Source

pub fn try_insert_outside_back(&mut self, v: T) -> Result<&'a mut T, T>

Try to insert an item into the underlying Vec, just after the tail of the view.

Returns Err if the there is no empty space for the insert to occur.

§Examples
use vec_entries::EntriesExt;

let mut v = vec![0, 1, 2];
v.entries(.., |e| {
    // Can't insert if you didn't remove
    assert_eq!(e.try_insert_outside_back(5), Err(5));

    assert_eq!(e.remove_back(), Some(2));
    assert_eq!(e.remaining(), [0, 1]);
    assert_eq!(e.try_insert_outside_back(5), Ok(&mut 5));
    assert_eq!(e.remaining(), [0, 1]);
});
assert_eq!(v, [0, 1, 5]);
Source

pub fn shift_iter<'b>(&'b mut self) -> ShiftIter<'a, 'b, T>

Get an iterator which gives mutable references to entries.

Source

pub fn remove_iter<'b>(&'b mut self) -> RemoveIter<'a, 'b, T>

Get an iterator which removes entries.

Trait Implementations§

Source§

impl<'a, T> Drop for Entries<'a, T>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl<'a, T> Freeze for Entries<'a, T>

§

impl<'a, T> RefUnwindSafe for Entries<'a, T>
where T: RefUnwindSafe,

§

impl<'a, T> Send for Entries<'a, T>
where T: Send,

§

impl<'a, T> Sync for Entries<'a, T>
where T: Sync,

§

impl<'a, T> Unpin for Entries<'a, T>

§

impl<'a, T> !UnwindSafe for Entries<'a, T>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

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

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.