Attribute Macro module

Source
#[module]
Expand description

Declare a module to export internal types.

use rioc::{injectable, provider};

mod sub {
    use rioc::{injectable, module};
    use std::rc::Rc;

    #[injectable]
    struct InternalType(#[inject(123)] i32); // Not visible outside of module.

    #[injectable]
    pub struct Facade<'a> {
        hidden: &'a InternalType,
        public: Rc<i32>,
    }

    #[injectable]
    // The absolute public path to access the module.
    // If no path is given, the struct name will be used and must be unique across all modules.
    // Keywords like `crate` and `Self` will be substituted accordingly.
    #[module(crate::sub::Self)]
    // Public type exports must be made on the struct (not the fields).
    // To prevent name collisions, use absolute paths in types.
    #[export(std::rc::Rc<i32>, self.public.clone())]
    pub struct Module {
        #[export] // Fields exports are for internal types.
        hidden: InternalType,
        #[inject(Rc::new(456))]
        public: Rc<i32>,
    }
}

#[injectable]
#[provider]
struct Provider {
    #[import]
    // To import module public exports, use the absolute path given in its definition.
    sub_mod: crate::sub::Module,
}

#[provider]
struct InitProvider;

fn main() {
    let provider = InitProvider.provide::<Provider>();
    let facade = provider.provide::<sub::Facade>();
}