Trait boulder::GeneratorWithPersianRug
source · [−]pub trait GeneratorWithPersianRug<C> where
C: Context, {
type Output;
fn generate<'b, B>(&mut self, context: B) -> (Self::Output, B)
where
B: 'b + Mutator<Context = C>;
}
persian-rug
only.Expand description
Something which can generate a sequence of objects of some
persian_rug::Contextual
type.
The only required function in this trait is
‘generate’ which creates a
new object, mutating the generator as a byproduct, using a
Context
. Most generators will allow
customisation of the sequence of produced objects in some way.
An object implementing this trait will be automatically created
for you as part of the GeneratableWithPersianRug
derive
macro. That generator will have a method for each field of the
result type, to allow you to set a generator for the field. It
will produce a default sequence (as configured by the attributes
placed on the type) for every field that is not customised.
Note that the generator produced by this macro changes type as its default sequences are altered; this is mostly transparent. This is required because this trait is not object safe, and therefore there is no overarching type that can represent any generator for a given field.
Required Associated Types
Required Methods
Make a new object.
Example
use boulder::GeneratorWithPersianRug;
use persian_rug::{contextual, persian_rug, Context, Mutator};
#[contextual(State)]
struct Foo {
a: i32
}
#[persian_rug]
struct State (
#[table] Foo,
);
struct FooGenerator {
next: i32
};
impl GeneratorWithPersianRug<State> for FooGenerator {
type Output = Foo;
fn generate<'b, B>(&mut self, context: B) -> (Self::Output, B)
where
B: 'b + Mutator<Context = State>
{
let result = self.next;
self.next = self.next + 1;
(Foo { a: result }, context)
}
}
let mut s = State(Default::default());
let mut g = FooGenerator { next: 6 };
let (f1, _) = g.generate(&mut s);
assert_eq!(f1.a, 6);
let (f2, _) = g.generate(&mut s);
assert_eq!(f2.a, 7);