Trait dodo::Cursor[][src]

pub trait Cursor {
    type Item;
    fn next(&mut self) -> Result<Option<Self::Item>>;

    fn skip(self, nb_items: usize) -> Skip<Self>
    where
        Self: Sized
, { ... }
fn find<F>(&mut self, filter: F) -> Result<Option<Self::Item>>
    where
        Self: Sized,
        F: FnMut(&Self::Item) -> bool
, { ... }
fn filter<F>(self, filter: F) -> Filter<Self, F>
    where
        Self: Sized,
        F: FnMut(&Self::Item) -> bool
, { ... }
fn map<B, F>(self, filter: F) -> Map<Self, F>
    where
        Self: Sized,
        F: FnMut(Self::Item) -> B
, { ... }
fn first(self) -> Result<Option<Self::Item>>
    where
        Self: Sized
, { ... }
fn nth(&mut self, index: usize) -> Result<Option<Self::Item>>
    where
        Self: Sized
, { ... }
fn take(self, nb_items: usize) -> Result<Vec<Self::Item>>
    where
        Self: Sized
, { ... }
fn collect(self) -> Result<Vec<Self::Item>>
    where
        Self: Sized
, { ... } }

Cursor over the contents of a collection.

Cursors deal with errors in a nicer way than iterators. Instead of providing results when filtering, cursors provide entities when possible. This doesn’t mean that cursors ignore errors: you still have to manage them when consuming the cursor.

// This is what you would have to do if cursors provided "Results".
let persons : Vec<&Result<Person,()>> = collection
    .iter()
    .filter(|result| result.as_ref().map_or(false, |person| person.age > 20))
    .collect();
//This is what you can do instead.
let persons = collection
    .find_all()?
    .filter(|person| person.age > 20)
    .collect()?;

Associated Types

type Item[src]

Yielded items.

Loading content...

Required methods

fn next(&mut self) -> Result<Option<Self::Item>>[src]

Advances the cursor and returns the next element.

Returns Ok(None) when iteration is finished.

Examples

collection.insert(&mut Person::new())?;

let mut cursor = collection.find_all()?;

assert!(matches!(cursor.next(), Ok(Some(person))), "Should exist.");
assert!(matches!(cursor.next(), Ok(None)), "Should not exist.");
Loading content...

Provided methods

fn skip(self, nb_items: usize) -> Skip<Self> where
    Self: Sized
[src]

Skip the first n elements.

Examples

collection.insert(&mut Person::new())?;
collection.insert(&mut Person::new())?;

let entities = collection.find_all()?.skip(1).collect()?;

assert_eq!(entities.len(), 1, "Should yield only one entity.");

fn find<F>(&mut self, filter: F) -> Result<Option<Self::Item>> where
    Self: Sized,
    F: FnMut(&Self::Item) -> bool
[src]

Searches for an element that satisfies a predicate.

Advances the cursor until it finds a candidate. Returns Ok(None) if no candidate is found.

Examples

collection.insert(&mut Person::with_age(42))?;
collection.insert(&mut Person::with_age(18))?;

let mut cursor = collection.find_all()?;

assert!(matches!(cursor.find(|person| person.age < 20), Ok(Some(person))), "Should exist.");
assert!(matches!(cursor.find(|person| person.age < 20), Ok(None)), "Should not exist.");

fn filter<F>(self, filter: F) -> Filter<Self, F> where
    Self: Sized,
    F: FnMut(&Self::Item) -> bool
[src]

Filters the cursor using a predicate.

Examples

collection.insert(&mut Person::with_age(42))?;
collection.insert(&mut Person::with_age(18))?;

let entities = collection.find_all()?.filter(|person| person.age > 20).collect()?;

assert_eq!(entities.len(), 1, "Should yield only one entity.");

fn map<B, F>(self, filter: F) -> Map<Self, F> where
    Self: Sized,
    F: FnMut(Self::Item) -> B, 
[src]

Maps the cursor using a mapper.

Examples

collection.insert(&mut Person::with_age(42))?;
collection.insert(&mut Person::with_age(18))?;

let entities = collection.find_all()?.map(|person| person.age).collect()?;

assert!(entities.contains(&42), "Should contain person age.");
assert!(entities.contains(&18), "Should contain person age.");

fn first(self) -> Result<Option<Self::Item>> where
    Self: Sized
[src]

Consumes the cursor, only taking the first element.

Returns Ok(None) if iteration is finished.

Examples

collection.insert(&mut Person::new())?;

let person = collection.find_all()?.first();

assert!(matches!(person, Ok(Some(person))), "Should have yielded the entity");

fn nth(&mut self, index: usize) -> Result<Option<Self::Item>> where
    Self: Sized
[src]

Take the nth element.

Returns Ok(None) if iteration is finished. This will not skip errors.

Examples

collection.insert(&mut Person::new())?; //Position 0
collection.insert(&mut Person::new())?; //Position 1

let mut cursor = collection.find_all()?;
assert!(matches!(cursor.nth(1), Ok(Some(person))), "Should yield the second person.");
assert!(matches!(cursor.next(), Ok(None)), "Should have no one left.");

fn take(self, nb_items: usize) -> Result<Vec<Self::Item>> where
    Self: Sized
[src]

Consumes the cursor into a Vec, taking only n elements.

If an error is encountered, this discards every element collected so far and returns only the error.

Examples

collection.insert(&mut Person::new())?;
collection.insert(&mut Person::new())?;

let persons : Vec<Person> = collection.find_all()?.take(1)?;

assert_eq!(persons.len(), 1, "Should only be one person in the results.");

fn collect(self) -> Result<Vec<Self::Item>> where
    Self: Sized
[src]

Consumes the cursor into a Vec.

If an error is encountered, this discards every element collected so far and returns only the error.

Examples

let mut person1 = Person::with_age(42);
let mut person2 = Person::with_age(18);
collection.insert(&mut person1)?;
collection.insert(&mut person2)?;

let persons : Vec<Person> = collection.find_all()?
                                      .filter(|person| person.age > 20)
                                      .collect()?;

assert!(persons.contains(&person1), "Person1 should be in the results.");
assert!(!persons.contains(&person2), "Person2 should not be in the results.");
Loading content...

Implementors

impl<'a, T, S, I, R> Cursor for CollectionCursor<'a, T, S, I, R> where
    T: Entity,
    S: Storage,
    I: Iterator<Item = Result<Uuid>>,
    R: Serializer
[src]

type Item = T

impl<B, C, F> Cursor for Map<C, F> where
    C: Cursor,
    F: FnMut(C::Item) -> B, 
[src]

type Item = B

impl<C> Cursor for Skip<C> where
    C: Cursor
[src]

type Item = C::Item

impl<C, F> Cursor for Filter<C, F> where
    C: Cursor,
    F: FnMut(&C::Item) -> bool
[src]

type Item = C::Item

Loading content...