[][src]Trait spirit::extension::Extension

pub trait Extension<B> {
    fn apply(self, builder: B) -> Result<B, AnyError>;
}

The basic extension trait.

It allows being plugged into a Builder or Spirit and modifying it in an arbitrary way.

It is more common to apply the helper by the with or with_singleton method than directly.

There's an implementation of Extension for FnOnce(Extensible) -> Result<Extensible, AnyError>, so extensions can be either custom types or just closures (which are often more convenient than defining an empty type and the implementation).

It is better to define the extension in a generic way (eg. accepting any type that is Extensible) instead of eg. a Builder directly. Note that sometimes it is needed to restrict the acceptable Extensibles to the base ones, with Ok = E, like in the example below. They'll still be possible to apply to Results.

Examples

use spirit::{AnyError ,Empty, Spirit};
use spirit::extension::{Extension, Extensible};
use spirit::prelude::*;

struct CfgPrint;

impl<E: Extensible<Ok = E>> Extension<E> for CfgPrint {
    fn apply(self, ext: E) -> Result<E, AnyError> {
        Ok(ext.on_config(|_opts, _config| println!("Config changed")))
    }
}

Spirit::<Empty, Empty>::new()
    .with(CfgPrint)
    .run(|_spirit| {
        println!("Running...");
        Ok(())
    })
use spirit::{Empty, Extensible, Spirit};
use spirit::prelude::*;

fn cfg_print<E: Extensible<Ok = E>>(ext: E) -> E {
    ext.on_config(|_opts, _config| println!("Config changed"))
}

Spirit::<Empty, Empty>::new()
    .with(cfg_print)
    .run(|_spirit| {
        println!("Running...");
        Ok(())
    })

Required methods

fn apply(self, builder: B) -> Result<B, AnyError>

Perform the transformation on the given extensible.

And yes, it is possible to do multiple primitive transformations inside one extension (this is what makes extensions useful for 3rd party crates, they can integrate with just one call of with).

Loading content...

Implementors

impl<B, F, R> Extension<B> for F where
    F: FnOnce(B) -> R,
    R: IntoResult<B>, 
[src]

impl<F, B, E, D, T> Extension<B> for Pipeline<F, E, D, T, (B::Opts, B::Config)> where
    B::Config: DeserializeOwned + Send + Sync + 'static,
    B::Opts: StructOpt + Send + Sync + 'static,
    B: Extensible<Ok = B>,
    CompiledPipeline<B::Opts, B::Config, T, T::OutputInstaller, D, E, T::OutputResource, <T::OutputInstaller as Installer<T::OutputResource, B::Opts, B::Config>>::UninstallHandle>: for<'a> BoundedCompiledPipeline<'a, B::Opts, B::Config> + Send + 'static,
    D: Driver<F> + Send + 'static,
    F: Fragment,
    T: Transformation<<D::SubFragment as Fragment>::Resource, <D::SubFragment as Fragment>::Installer, D::SubFragment>,
    T::OutputInstaller: Installer<T::OutputResource, B::Opts, B::Config> + 'static, 
[src]

Loading content...