Ribbon

Trait Ribbon 

Source
pub trait Ribbon<T> {
Show 14 methods // Required methods fn progress(&mut self) -> Option<T>; fn expand(&mut self) -> bool; fn expand_while<F>(&mut self, f: F) -> bool where F: Fn(&T) -> bool; fn pop_front(&mut self) -> Option<T>; fn peek_front_mut(&mut self) -> Option<&mut T>; fn pop_back(&mut self) -> Option<T>; fn peek_back_mut(&mut self) -> Option<&mut T>; fn peek_at(&self, index: usize) -> Option<&T>; fn peek_at_mut(&mut self, index: usize) -> Option<&mut T>; fn len(&self) -> usize; // Provided methods fn expand_n(&mut self, n: usize) -> bool { ... } fn peek_front(&self) -> Option<&T> { ... } fn peek_back(&self) -> Option<&T> { ... } fn is_empty(&self) -> bool { ... }
}

Required Methods§

Source

fn progress(&mut self) -> Option<T>

Tries to stream the iterator forward through the Ribbon without expanding it. Underlying iterator is polled for the next element. Returns the head of the Ribbon, and the new item from the iterator is appended to the tail.

Is a no-op if iterator stops producing values. In that case None is returned.

§Example
use ribbon::{Ribbon, Tape};

let mut tape = Tape::new(0..10);

tape.expand_n(5);
let item = tape.progress();

assert_eq!(item, Some(0));
assert_eq!(tape.len(), 5);
assert_eq!(tape.peek_front(), Some(&1));
assert_eq!(tape.peek_back(), Some(&5));
Source

fn expand(&mut self) -> bool

Expands the Ribbon by consuming the next available item and appending it to the tail. Returns true if Ribbon is expanded.

§Example
 use ribbon::{Ribbon, Tape};

 let mut tape = Tape::new(0..2);

 assert!(tape.expand());
 assert_eq!(tape.len(), 1);
 assert_eq!(tape.peek_front(), Some(&0));
 assert_eq!(tape.peek_back(), Some(&0));

 assert!(tape.expand());
 assert_eq!(tape.len(), 2);
 assert_eq!(tape.peek_front(), Some(&0));
 assert_eq!(tape.peek_back(), Some(&1));

 // no more elements, expansion fails
 assert_eq!(tape.expand(), false);
Source

fn expand_while<F>(&mut self, f: F) -> bool
where F: Fn(&T) -> bool,

Expands the Ribbon by consuming items from the iterator while some condition holds and appending them to the end. Returns true if Ribbon is expanded by at least one element.

§Example
 use ribbon::{Ribbon, Tape};

 let mut tape = Tape::new(0..10);

 assert!(tape.expand_while(|item| *item < 5));
 assert_eq!(tape.len(), 5);
 assert_eq!(tape.peek_front(), Some(&0));
 assert_eq!(tape.peek_back(), Some(&4));

 assert!(tape.expand_while(|item| *item < 6));
 assert_eq!(tape.len(), 6);
 assert_eq!(tape.peek_front(), Some(&0));
 assert_eq!(tape.peek_back(), Some(&5));

 // no more elements smaller than 6, expansion fails
 assert_eq!(tape.expand_while(|item| *item < 6), false);
Source

fn pop_front(&mut self) -> Option<T>

Removes the item stored at the head of Ribbon and returns it (if available).

§Example
use ribbon::{Ribbon, Tape};

let mut tape = Tape::new(0..10);

tape.expand_n(2);
assert_eq!(tape.len(), 2);
assert_eq!(tape.pop_front(), Some(0));
assert_eq!(tape.pop_front(), Some(1));
assert_eq!(tape.pop_front(), None);
Source

fn peek_front_mut(&mut self) -> Option<&mut T>

Returns a mutable reference to the item stored at the head of Ribbon if item exists. Returns None otherwise.

§Example
use ribbon::{Ribbon, Tape};

let mut tape = Tape::new(0..10);

tape.expand_n(2);
assert_eq!(tape.len(), 2);
assert_eq!(tape.peek_front(), Some(&0));
assert_eq!(tape.len(), 2);
Source

fn pop_back(&mut self) -> Option<T>

Removes the item stored at the tail of Ribbon and returns it if it exists. Returns None otherwise.

§Example
use ribbon::{Ribbon, Tape};

let mut tape = Tape::new(0..10);

tape.expand_n(3);
assert_eq!(tape.len(), 3);
assert_eq!(tape.pop_back(), Some(2));
assert_eq!(tape.pop_back(), Some(1));
assert_eq!(tape.pop_back(), Some(0));
assert_eq!(tape.pop_back(), None);
Source

fn peek_back_mut(&mut self) -> Option<&mut T>

Returns a mutable reference to the item stored at the tail of Ribbon if item exists. Returns None otherwise.

§Example
use ribbon::{Ribbon, Tape};

let mut tape = Tape::new(0..10);

tape.expand_n(3);
assert_eq!(tape.len(), 3);
assert_eq!(tape.peek_back(), Some(&2));

if let Some(item) = tape.peek_back_mut() { *item = 42; }
assert_eq!(tape.peek_back(), Some(&42));

tape.expand();
assert_eq!(tape.peek_back(), Some(&3));

tape.expand();
assert_eq!(tape.peek_back(), Some(&4));
Source

fn peek_at(&self, index: usize) -> Option<&T>

Returns a reference to the item stored at the given index of Ribbon if item exists. Returns None if index out of bounds.

§Example
use ribbon::{Ribbon, Tape};

let mut tape = Tape::new(0..10);

tape.expand_n(5);
assert_eq!(tape.len(), 5);
assert_eq!(tape.peek_at(0), Some(&0));
assert_eq!(tape.peek_at(2), Some(&2));
assert_eq!(tape.peek_at(3), Some(&3));
Source

fn peek_at_mut(&mut self, index: usize) -> Option<&mut T>

Returns a mutable reference to the item stored at the given index of Ribbon if item exists. Returns None otherwise, or if index out of bounds.

§Example
use ribbon::{Ribbon, Tape};

let mut tape = Tape::new(0..10);

tape.expand_n(5);
assert_eq!(tape.len(), 5);
assert_eq!(tape.peek_at(0), Some(&0));

if let Some(item) = tape.peek_at_mut(0) { *item = 42; }

assert_eq!(tape.peek_at(0), Some(&42));
assert_eq!(tape.peek_at(3), Some(&3));
Source

fn len(&self) -> usize

Returns the number of items currently found on the Ribbon.

§Example
use ribbon::{Ribbon, Tape};

let mut tape = Tape::new(0..10);

tape.expand_n(5);
assert_eq!(tape.len(), 5);

tape.expand_n(2);
assert_eq!(tape.len(), 7);

Provided Methods§

Source

fn expand_n(&mut self, n: usize) -> bool

Expands the Ribbon by consuming the n next available item and appending them to the end. Returns true if Ribbon is expanded by at least one element.

§Example
 use ribbon::{Ribbon, Tape};

 let mut tape = Tape::new(0..10);

 assert!(tape.expand_n(5));
 assert_eq!(tape.len(), 5);
 assert_eq!(tape.peek_front(), Some(&0));
 assert_eq!(tape.peek_back(), Some(&4));

 assert!(tape.expand_n(7));
 assert_eq!(tape.len(), 10);
 assert_eq!(tape.peek_front(), Some(&0));
 assert_eq!(tape.peek_back(), Some(&9));

 // not expanding anymore, returns false
 assert_eq!(tape.expand_n(1), false);
Source

fn peek_front(&self) -> Option<&T>

Returns a reference to the item stored at the head of Ribbon if item exists. Returns None otherwise.

§Example
use ribbon::{Ribbon, Tape};

let mut tape = Tape::new(0..10);

tape.expand_n(2);
assert_eq!(tape.len(), 2);
assert_eq!(tape.peek_front(), Some(&0));
assert_eq!(tape.len(), 2);
Source

fn peek_back(&self) -> Option<&T>

Returns a reference to the item stored at the tail of Ribbon if item exists. Returns None otherwise.

§Example
use ribbon::{Ribbon, Tape};

let mut tape = Tape::new(0..10);

tape.expand_n(3);
assert_eq!(tape.len(), 3);
assert_eq!(tape.peek_back(), Some(&2));

tape.expand();
assert_eq!(tape.peek_back(), Some(&3));

tape.expand();
assert_eq!(tape.peek_back(), Some(&4));
Source

fn is_empty(&self) -> bool

Returns true if Ribbon does not contain any items at the moment.

§Example
use ribbon::{Ribbon, Tape};

let mut tape = Tape::new(0..10);
assert_eq!(tape.len(), 0);

tape.expand();
assert_eq!(tape.len(), 1);

tape.expand_n(5);
assert_eq!(tape.len(), 6);

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

Source§

impl<I> Ribbon<<I as Iterator>::Item> for Tape<I>
where I: Iterator,

Source§

impl<const LEN: usize, I> Ribbon<<I as Iterator>::Item> for Band<LEN, I>
where I: Iterator,