Struct dodo::collection::Collection[][src]

pub struct Collection<T, S, R> { /* fields omitted */ }

Collection of entities, backed by provided storage.

See this crate root documentation for more information.

Example

use dodo::prelude::*;

type PersonCollection = Collection<Person, Directory, JsonSerializer>;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let directory = Directory::new(&path)?;
    let mut collection = PersonCollection::new(directory);

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

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

    println!("{:?}", entities);

    Ok(())
}

Implementations

impl<T, S, R> Collection<T, S, R> where
    T: Entity,
    S: Storage,
    R: Serializer
[src]

pub fn new(storage: S) -> Self[src]

Create a new collection, using provided storage.

Examples

use dodo::prelude::*;

type PersonCollection = Collection<Person, Directory, JsonSerializer>;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let directory = Directory::new(&path)?;
    let collection = PersonCollection::new(directory);
    Ok(())
}

pub fn find(&self, id: Uuid) -> Result<T>[src]

Find an specific entity, if it exists.

Returns an error if not found.

Examples

let person = collection.find(id);

match person {
    Ok(person) => println!("Found!"),
    Err(e) if e.is_not_found() => println!("Not found!"),
    Err(_) => println!("Other error!")
}

pub fn find_all(&self) -> Result<CollectionCursor<'_, T, S, S::Iterator, R>>[src]

Provide a cursor iterating through the entities.

Examples

let persons = collection.find_all()?.collect();

println!("{:#?}", persons);

pub fn insert(&mut self, entity: &mut T) -> Result<()>[src]

Insert entity into this collection.

The collection always assigns a new id to the entity, even if it already has one. Thus, the entity need to be mutable when inserted.

Examples

let mut person = Person::new();  //Required to be mutable
collection.insert(&mut person)?; //Assign new id to the entity.

pub fn update(&mut self, entity: &T) -> Result<()>[src]

Update entity in this collection to a new version.

The entity must already exist in the collection and have an id.

Examples

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

person.age = 1337;
collection.update(&person)?;

pub fn upsert(&mut self, entity: &T) -> Result<()>[src]

Update or insert entity into this collection.

The entity is created if it doesn’t exists, using the id currently assigned to it. You will receive an error if the entity doesn’t have an id.

Examples

let mut person = Person::new();
person.id = Some(Uuid::parse_str("78190929-3d84-4735-9e40-80e3cd5530e9").unwrap());
collection.upsert(&mut person)?; //Doesn't exist ? No problem here!

let mut person = Person::new();
collection.insert(&mut person)?;
person.age = 1337;
collection.upsert(&mut person)?; //Already exist ? No problem too!

pub fn delete(&mut self, id: Uuid) -> Result<bool>[src]

Delete entity with provided id.

This does not fail if the entity doesn’t exist. Instead, this returns a boolean : if true, the entity was deleted, and if false, the entity was not found.

Examples

let mut person = Person::new();
collection.insert(&mut person)?; //Inserted here.
assert!(collection.delete(person.id.unwrap())?); //Deleted here.

let id : Uuid = Uuid::parse_str("78190929-3d84-4735-9e40-80e3cd5530e9").unwrap();
assert!(!collection.delete(id)?); //Doesn't exist ? No error!

pub fn clear(&mut self) -> Result<()>[src]

Delete every entity in this collection.

Everything in this collection will be deleted. Use at your own risks.

Examples

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

collection.clear()?;

Trait Implementations

impl<T: Clone, S: Clone, R: Clone> Clone for Collection<T, S, R>[src]

impl<T: Debug, S: Debug, R: Debug> Debug for Collection<T, S, R>[src]

Auto Trait Implementations

impl<T, S, R> RefUnwindSafe for Collection<T, S, R> where
    R: RefUnwindSafe,
    S: RefUnwindSafe,
    T: RefUnwindSafe

impl<T, S, R> Send for Collection<T, S, R> where
    R: Send,
    S: Send,
    T: Send

impl<T, S, R> Sync for Collection<T, S, R> where
    R: Sync,
    S: Sync,
    T: Sync

impl<T, S, R> Unpin for Collection<T, S, R> where
    R: Unpin,
    S: Unpin,
    T: Unpin

impl<T, S, R> UnwindSafe for Collection<T, S, R> where
    R: UnwindSafe,
    S: UnwindSafe,
    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> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

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.

impl<V, T> VZip<V> for T where
    V: MultiLane<T>,