[][src]Trait mongod::AsUpdate

pub trait AsUpdate<U: Update> {
    fn update() -> U;
fn into_update(self) -> U; }

Used to tie a type implementing Collection to its companion Update type.

Examples

Tying User to its Update.

use mongod::bson::Document;
use mongod::{AsUpdate, Error, Update};

#[derive(Bson, Mongo)]
#[mongo(collection="users")]
pub struct User {
    pub name: String,
}

#[derive(Default)]
pub struct UserUpdate {
    pub name: Option<String>,
}

impl Update for UserUpdate {
    fn new() -> Self {
       UserUpdate::default()
    }
    fn into_document(self) -> Result<Document, Error> {
        let mut doc = Document::new();
        if let Some(value) = self.name {
            doc.insert("name", value);
        }
        Ok(doc)
    }
}

impl mongod::AsUpdate<UserUpdate> for User {
    fn update() -> UserUpdate {
        UserUpdate::default()
    }
    fn into_update(self) -> UserUpdate {
        UserUpdate {
            name: Some(self.name),
        }
    }
}

Required methods

fn update() -> U[src]

Returns the Collections update.

fn into_update(self) -> U[src]

Converts the Collection instance into its update.

Loading content...

Implementors

Loading content...