Skip to main content

Generator

Trait Generator 

Source
pub trait Generator<T>: Send + Sync {
    // Provided methods
    fn map<U, F>(self, f: F) -> Mapped<T, U, F, Self>
       where Self: Sized,
             F: Fn(T) -> U + Send + Sync { ... }
    fn flat_map<U, G, F>(self, f: F) -> FlatMapped<T, U, G, F, Self>
       where Self: Sized,
             G: Generator<U>,
             F: Fn(T) -> G + Send + Sync { ... }
    fn filter<F>(self, predicate: F) -> Filtered<T, F, Self>
       where Self: Sized,
             F: Fn(&T) -> bool + Send + Sync { ... }
    fn boxed<'a>(self) -> BoxedGenerator<'a, T>
       where Self: Sized + Send + Sync + 'a { ... }
}
Expand description

The core trait for all generators.

Generators produce values of type T and optionally provide a [BasicGenerator] for server-based generation via as_basic().

Implementors override one or both of as_basic and do_draw:

  • “Always basic” leaf generators override only as_basic. The default do_draw delegates to it.
  • Generators with a client-side fallback (filter, flat_map, lists with non-basic elements, …) override do_draw. They may also override as_basic to opt into schema generation when their inputs allow it.

Provided Methods§

Source

fn map<U, F>(self, f: F) -> Mapped<T, U, F, Self>
where Self: Sized, F: Fn(T) -> U + Send + Sync,

Transform generated values using a function.

When the source generator has a schema (i.e. as_basic() returns Some), the schema is preserved and the function is composed into the parse step.

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

// Generate even integers by doubling
let evens = gs::integers::<i32>().map(|n| n * 2);
Source

fn flat_map<U, G, F>(self, f: F) -> FlatMapped<T, U, G, F, Self>
where Self: Sized, G: Generator<U>, F: Fn(T) -> G + Send + Sync,

Generate a value, then use it to choose or configure another generator.

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

// Generate a length, then a vec of exactly that length
let generator = gs::integers::<usize>()
    .min_value(1)
    .max_value(10)
    .flat_map(|len| gs::vecs(gs::integers::<i32>())
        .min_size(len)
        .max_size(len));
Source

fn filter<F>(self, predicate: F) -> Filtered<T, F, Self>
where Self: Sized, F: Fn(&T) -> bool + Send + Sync,

Only keep generated values that satisfy the predicate.

Retries up to 3 times, then calls assume(false) to reject the test case.

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

// Generate integers, then filter out the even ones
let odds = gs::integers::<i32>().filter(|n| n % 2 != 0);
Source

fn boxed<'a>(self) -> BoxedGenerator<'a, T>
where Self: Sized + Send + Sync + 'a,

Convert this generator into a type-erased boxed generator.

This is needed when you have generators of different concrete types but the same output type and need to store them together, e.g. in a Vec or when passing to one_of().

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

// Different generator types producing the same output type —
// boxing lets them be stored in a vec and passed to one_of
let generator = vec![
    gs::integers::<i32>().min_value(0).max_value(10).boxed(),
    gs::integers::<i32>().map(|n| n * 100).boxed(),
    gs::sampled_from(vec![1, 2, 3]).boxed(),
];

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl<T, G: Generator<T>> Generator<T> for &G

Implementors§

Source§

impl Generator<Box<RawValue>> for RawValueGenerator

Available on crate features serde_json_raw_value and serde_json only.
Source§

impl Generator<Date> for hegel::extras::jiff::DateGenerator

Available on crate feature jiff only.
Source§

impl Generator<DateTime> for hegel::extras::jiff::DateTimeGenerator

Available on crate feature jiff only.
Source§

impl Generator<Duration> for DurationGenerator

Source§

impl Generator<FixedOffset> for FixedOffsetGenerator

Available on crate feature chrono only.
Source§

impl Generator<HegelRandom> for RandomsGenerator

Available on crate feature rand only.
Source§

impl Generator<NaiveDate> for NaiveDateGenerator

Available on crate feature chrono only.
Source§

impl Generator<NaiveDateTime> for NaiveDateTimeGenerator

Available on crate feature chrono only.
Source§

impl Generator<NaiveTime> for NaiveTimeGenerator

Available on crate feature chrono only.
Source§

impl Generator<Number> for NumberGenerator

Available on crate feature serde_json only.
Source§

impl Generator<Offset> for OffsetGenerator

Available on crate feature jiff only.
Source§

impl Generator<SignedDuration> for SignedDurationGenerator

Available on crate feature jiff only.
Source§

impl Generator<Span> for SpanGenerator

Available on crate feature jiff only.
Source§

impl Generator<String> for hegel::generators::DateGenerator

Source§

impl Generator<String> for hegel::generators::DateTimeGenerator

Source§

impl Generator<String> for DomainGenerator

Source§

impl Generator<String> for EmailGenerator

Source§

impl Generator<String> for IpAddressGenerator

Source§

impl Generator<String> for RegexGenerator

Source§

impl Generator<String> for TextGenerator

Source§

impl Generator<String> for hegel::generators::TimeGenerator

Source§

impl Generator<String> for UrlGenerator

Source§

impl Generator<String> for UuidsGenerator

Source§

impl Generator<Time> for hegel::extras::jiff::TimeGenerator

Available on crate feature jiff only.
Source§

impl Generator<TimeDelta> for TimeDeltaGenerator

Available on crate feature chrono only.
Source§

impl Generator<Timestamp> for TimestampGenerator

Available on crate feature jiff only.
Source§

impl Generator<Value> for FixedDictGenerator<'_>

Source§

impl Generator<Value> for ValueGenerator

Available on crate feature serde_json only.
Source§

impl Generator<Vec<u8>> for BinaryGenerator

Source§

impl Generator<WeekdaySet> for WeekdaySetGenerator

Available on crate feature chrono only.
Source§

impl Generator<bool> for BoolGenerator

Source§

impl Generator<char> for CharactersGenerator

Source§

impl<'a, T: Clone + Send + Sync + 'a> Generator<T> for SampledFromGenerator<'a, T>

Source§

impl<G, Tz> Generator<DateTime<Tz>> for hegel::extras::chrono::DateTimeGenerator<G, Tz>
where G: Generator<Tz>, Tz: TimeZone + Send + Sync + 'static,

Available on crate feature chrono only.
Source§

impl<G: Generator<T> + Send + Sync, T, const N: usize> Generator<[T; N]> for ArrayGenerator<G, T, N>

Source§

impl<K, V, KT, VT> Generator<HashMap<KT, VT>> for HashMapGenerator<K, V, KT, VT>
where K: Generator<KT>, V: Generator<VT>, KT: Eq + Hash,

Source§

impl<S: Generator<Weekday>> Generator<NaiveWeek> for NaiveWeekGenerator<S>

Available on crate feature chrono only.
Source§

impl<T, F, G> Generator<T> for Filtered<T, F, G>
where T: Clone + Send + Sync, G: Generator<T>, F: Fn(&T) -> bool + Send + Sync,

Source§

impl<T, G> Generator<HashSet<T>> for HashSetGenerator<G, T>
where G: Generator<T>, T: Eq + Hash,

Source§

impl<T, G> Generator<Option<T>> for OptionalGenerator<G, T>
where G: Generator<T>,

Source§

impl<T, G> Generator<Vec<T>> for VecGenerator<G, T>
where G: Generator<T>,

Source§

impl<T, U, F, G> Generator<U> for Mapped<T, U, F, G>
where G: Generator<T>, F: Fn(T) -> U + Send + Sync,

Source§

impl<T, U, G2, F, G1> Generator<U> for FlatMapped<T, U, G2, F, G1>
where G1: Generator<T>, G2: Generator<U>, F: Fn(T) -> G2 + Send + Sync,

Source§

impl<T: Clone + Send + Sync> Generator<T> for JustGenerator<T>

Source§

impl<T: Float + DeserializeOwned + Serialize + Send + Sync + 'static> Generator<T> for FloatGenerator<T>

Source§

impl<T: Integer + DeserializeOwned + Serialize + Send + Sync + 'static> Generator<T> for IntegerGenerator<T>

Source§

impl<T> Generator<T> for BoxedGenerator<'_, T>

Source§

impl<T> Generator<T> for OneOfGenerator<'_, T>

Source§

impl<TS, TZ> Generator<Zoned> for ZonedGenerator<TS, TZ>

Available on crate feature jiff only.