Trait metafactory::MetaFactoryUnstable [-]  [+] [src]

pub trait MetaFactory {
    fn get_type(&self) -> TypeDef;
    fn get_arg_types(&self) -> Vec<TypeDef>;
    fn new(&self, arg_getters: Vec<Box<Any>>) -> Result<Box<Any>, FactoryErrorKind>;
    fn new_aggregate(&self) -> Aggregate<'static>;
}

Implements reflection and initiation of any abstract object constructor.

Information about constructor

The first goal of this trait is being able to inspect the construction mechanism at the runtime.

MetaFactory contains information about the constructor source: the return type and argument types. It also contains a method new that can create a function to invoke this source and get the values.

Factory initiation

The second goal is to avoid donwcasting construction arguments on every Factory invocation.

There are separate traits for MetaFactory and "real" Factory. MetaFactory is used to build a real Factory by passing all the required constructor arguments as factories under Vec<Box<Any>>.

Internaly, all parent Factorys will be downcasted to correct types and stored inside returned Factory's scope, so that all of them can be invoked with a simple take() call.

Simple value as constructor

This library allows to use many sources as "constructors", the simplest of which is a cloneable value. In this example we will use such value to create a new constructor metadata, check if argument and return types are correct, and then create an actual getter for the value:

use metafactory::{ metafactory, AsFactoryExt };

let metafactory = metafactory(5i);
assert!(metafactory.get_type().is::<int>());
assert!(metafactory.get_arg_types().len() == 0); // clonable int has no arguments

let factory = metafactory
    .new(
        Vec::new() // No arguments in this case.
    )
    .ok().unwrap()
    .as_factory_of::<int>()
    .unwrap();

assert_eq!(factory.take(), 5i);

Required Methods

fn get_type(&self) -> TypeDef

fn get_arg_types(&self) -> Vec<TypeDef>

fn new(&self, arg_getters: Vec<Box<Any>>) -> Result<Box<Any>, FactoryErrorKind>

fn new_aggregate(&self) -> Aggregate<'static>

Implementors