Struct InstanceBuilder

Source
pub struct InstanceBuilder { /* private fields */ }
Expand description

InstanceBuilder offers the creation of configured instances. Due to this pattern, you can for example use dependency injection in your tests without exposing those.

The object to be build implements the FromInstanceBuilder trait in its module.

use std::convert::Infallible;
use ::instancebuilder::{BuilderError, InstanceBuilder, FromInstanceBuilder};

struct TestImplementation {
    inner: String,
}

struct TestConfig {
    key: String,
}

impl FromInstanceBuilder for TestImplementation {
    fn try_from_builder(builder: &InstanceBuilder) -> Result<Self, BuilderError> {
        let config: &TestConfig = builder.data()?;
        Ok(Self {
            inner: config.key.clone(),
        })
    }
}

let config = TestConfig {
   key: String::from("help me!"),
};

let mut  builder = InstanceBuilder::new();
builder.insert(config);

let instance = builder.build::<TestImplementation>().unwrap();

Implementations§

Source§

impl InstanceBuilder

Source

pub fn new() -> Self

Source

pub fn insert<D: Any + Send + Sync>(&mut self, data: D)

Source

pub fn data<D: Any + Send + Sync>(&self) -> Result<&D, BuilderError>

Source

pub fn data_opt<D: Any + Send + Sync>(&self) -> Option<&D>

Source

pub fn build<T>(&self) -> Result<T, BuilderError>

Trait Implementations§

Source§

impl Default for InstanceBuilder

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.