Skip to main content

DeferredGeneratorDefinition

Struct DeferredGeneratorDefinition 

Source
pub struct DeferredGeneratorDefinition<T> { /* private fields */ }
Expand description

A deferred generator definition that can produce generator handles before its implementation is known.

Created by deferred(). Call generator() to get handles that can be passed to other generators, then call set() to provide the actual implementation. set consumes the definition, ensuring it can only be called once.

§Panics

Drawing from a generator handle before set() has been called will panic.

§Example

use hegel::generators::{self as gs, Generator};

enum Tree {
    Leaf(i32),
    Branch(Box<Tree>, Box<Tree>),
}

let tree = gs::deferred::<Tree>();
let leaf = gs::integers::<i32>().map(Tree::Leaf);
let branch = hegel::tuples!(tree.generator(), tree.generator())
    .map(|(l, r)| Tree::Branch(Box::new(l), Box::new(r)));
tree.set(hegel::one_of!(leaf, branch));

Implementations§

Source§

impl<T: Send + Sync + 'static> DeferredGeneratorDefinition<T>

Source

pub fn generator(&self) -> BoxedGenerator<'static, T>

Return a generator handle that will delegate to whatever is eventually passed to set().

Can be called multiple times to produce independent handles that all share the same underlying definition.

Source

pub fn set(self, generator: impl Generator<T> + 'static)

Set the implementation for this deferred generator.

All handles previously returned by generator() will delegate to the provided generator. Consumes the definition, so it can only be called once.

§Panics

Drawing from a handle before set is called will panic.

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.