Trait boulder::BuilderWithPersianRug
source · [−]pub trait BuilderWithPersianRug<C>: Sized where
C: Context, {
type Result;
fn build<'b, B>(self, context: B) -> (Self::Result, B)
where
B: 'b + Mutator<Context = C>;
}
persian-rug
only.Expand description
Something which can create a default object of some
persian_rug::Contextual
type.
persian-rug is a crate that provides arena based
management for objects of different types. The natural reason to
derive this trait over Builder
is when your
target type belongs to a persian_rug::Context
.
The only required function in this trait is
build
which creates an object,
using a Context
, consuming the
builder. Most builders will allow customisation of the produced
object in some way.
An object implementing this trait will be automatically created
for you as part of the BuildableWithPersianRug
derive
macro. That builder will have a method for each field of the
result type, to customise its value, and will produce a default
value for every field which is not customised.
Required Associated Types
Required Methods
Create the final object. The context
argument is a
persian_rug::Mutator
. It is not a mutable reference to a
mutator, so it cannot be reborrowed, instead it must be
returned for re-use, without being consumed.
Example
use boulder::BuilderWithPersianRug;
use persian_rug::{contextual, persian_rug, Mutator};
#[contextual(State)]
struct Foo {
a: i32
}
#[persian_rug]
struct State (
#[table] Foo,
);
struct FooBuilder {
a: i32
}
impl BuilderWithPersianRug<State> for FooBuilder {
type Result = Foo;
fn build<'b, B>(self, mutator: B) -> (Self::Result, B)
where
B: 'b + Mutator<Context = State>
{
(Foo { a: self.a }, mutator)
}
}
let mut s = State(Default::default());
let b = FooBuilder { a: 3 };
let (f, _) = b.build(&mut s);
assert_eq!(f.a, 3);