[][src]Macro spirit::simple_fragment

macro_rules! simple_fragment {
    (impl Fragment for $ty: ty {
        type Resource = $resource: ty;
        type Installer = $installer: ty;
        fn create(&$self: tt, $name: tt: &'static str) -> $result: ty $block: block
    }) => { ... };
    (impl Fragment for $ty: ty {
        type Driver = $driver: ty;
        type Resource = $resource: ty;
        type Installer = $installer: ty;
        fn create(&$self: tt, $name: tt: &'static str) -> $result: ty $block: block
    }) => { ... };
}

A helper macro to implement a simple Fragment.

The full implementation of a Fragment requires a lot of work that is not usually needed.

In case the Fragment should not implement the two-stage creation, this can be used to cut down on the boilerplate a bit.

Examples

use spirit::AnyError;
use spirit::simple_fragment;
use spirit::fragment::Installer;

// The message as a resource
struct Message(String);

// An installer of a resource
#[derive(Default)]
struct MessageInstaller;

impl<O, C> Installer<Message, O, C> for MessageInstaller {
    type UninstallHandle = ();
    fn install(&mut self, message: Message, _name: &str) {
        println!("{}", message.0);
    }
}

// Configuration of a message from the config file
struct MessageCfg {
    msg: String,
}

simple_fragment! {
    impl Fragment for MessageCfg {
        type Resource = Message;
        type Installer = MessageInstaller;
        fn create(&self, _name: &'static str) -> Result<Message, AnyError> {
            Ok(Message(self.msg.clone()))
        }
    }
}

If the Driver is not provided (as in the above example), the trivial driver is going to be used.