new

Macro new 

Source
macro_rules! new {
    (Self) => { ... };
    (Self {}) => { ... };
    (Self { $($field:ident $( : $value:expr )?),* $(,)? }) => { ... };
    (@expand $field:ident : $value:expr) => { ... };
    (@expand $field:ident) => { ... };
}
Expand description

Defines the template used to create every instance in a linked object family.

You are expected to use this in the constructor of a linked object, except when you want to always express the linked object via trait objects (dyn Xyz), in which case you should use linked::new_box.

The macro body must be a struct-expression of the Self type. Any variables the macro body captures must be thread-safe (Send + Sync + 'static). The returned object itself does not need to be thread-safe.

ยงExample

use std::sync::{Arc, Mutex};

#[linked::object]
struct TokenCache {
    tokens_created: usize,
    name: String,
    master_key: Arc<Mutex<String>>,
    is_multidimensional: bool,
}

impl TokenCache {
    fn new(name: String, is_multidimensional: bool) -> Self {
        // Any shared data referenced by the macro body must be thread-safe.
        let master_key = Arc::new(Mutex::new(String::new()));

        linked::new!(Self {
            tokens_created: 0,
            name: name.clone(),
            master_key: Arc::clone(&master_key),
            is_multidimensional,
        })
    }
}

Complex expressions are supported within the Self struct-expression:

#[linked::object]
struct TokenCache {
    token_sources: Vec<linked::Box<dyn TokenSource>>,
}

impl TokenCache {
    fn new(source_families: Vec<linked::Family<linked::Box<dyn TokenSource>>>) -> Self {
        linked::new!(Self {
            token_sources: source_families
                .iter()
                .cloned()
                .map(linked::Family::into)
                .collect()
        })
    }
}

For a complete example, see examples/linked_basic.rs.