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 defaultdo_drawdelegates to it. - Generators with a client-side fallback (filter, flat_map, lists with
non-basic elements, …) override
do_draw. They may also overrideas_basicto opt into schema generation when their inputs allow it.
Provided Methods§
Sourcefn map<U, F>(self, f: F) -> Mapped<T, U, F, Self>
fn map<U, F>(self, f: F) -> Mapped<T, U, F, Self>
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);Sourcefn flat_map<U, G, F>(self, f: F) -> FlatMapped<T, U, G, F, Self>
fn flat_map<U, G, F>(self, f: F) -> FlatMapped<T, U, G, F, Self>
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));Sourcefn filter<F>(self, predicate: F) -> Filtered<T, F, Self>
fn filter<F>(self, predicate: F) -> Filtered<T, F, Self>
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);Sourcefn boxed<'a>(self) -> BoxedGenerator<'a, T>
fn boxed<'a>(self) -> BoxedGenerator<'a, T>
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§
impl<T, G: Generator<T>> Generator<T> for &G
Implementors§
impl Generator<Box<RawValue>> for RawValueGenerator
serde_json_raw_value and serde_json only.impl Generator<Date> for hegel::extras::jiff::DateGenerator
jiff only.impl Generator<DateTime> for hegel::extras::jiff::DateTimeGenerator
jiff only.impl Generator<Duration> for DurationGenerator
impl Generator<FixedOffset> for FixedOffsetGenerator
chrono only.impl Generator<HegelRandom> for RandomsGenerator
rand only.impl Generator<NaiveDate> for NaiveDateGenerator
chrono only.impl Generator<NaiveDateTime> for NaiveDateTimeGenerator
chrono only.impl Generator<NaiveTime> for NaiveTimeGenerator
chrono only.impl Generator<Number> for NumberGenerator
serde_json only.impl Generator<Offset> for OffsetGenerator
jiff only.impl Generator<SignedDuration> for SignedDurationGenerator
jiff only.impl Generator<Span> for SpanGenerator
jiff only.impl Generator<String> for hegel::generators::DateGenerator
impl Generator<String> for hegel::generators::DateTimeGenerator
impl Generator<String> for DomainGenerator
impl Generator<String> for EmailGenerator
impl Generator<String> for IpAddressGenerator
impl Generator<String> for RegexGenerator
impl Generator<String> for TextGenerator
impl Generator<String> for hegel::generators::TimeGenerator
impl Generator<String> for UrlGenerator
impl Generator<String> for UuidsGenerator
impl Generator<Time> for hegel::extras::jiff::TimeGenerator
jiff only.impl Generator<TimeDelta> for TimeDeltaGenerator
chrono only.impl Generator<Timestamp> for TimestampGenerator
jiff only.impl Generator<Value> for FixedDictGenerator<'_>
impl Generator<Value> for ValueGenerator
serde_json only.impl Generator<Vec<u8>> for BinaryGenerator
impl Generator<WeekdaySet> for WeekdaySetGenerator
chrono only.impl Generator<bool> for BoolGenerator
impl Generator<char> for CharactersGenerator
impl<'a, T: Clone + Send + Sync + 'a> Generator<T> for SampledFromGenerator<'a, T>
impl<G, Tz> Generator<DateTime<Tz>> for hegel::extras::chrono::DateTimeGenerator<G, Tz>
chrono only.impl<G: Generator<T> + Send + Sync, T, const N: usize> Generator<[T; N]> for ArrayGenerator<G, T, N>
impl<K, V, KT, VT> Generator<HashMap<KT, VT>> for HashMapGenerator<K, V, KT, VT>
impl<S: Generator<Weekday>> Generator<NaiveWeek> for NaiveWeekGenerator<S>
chrono only.impl<T, F, G> Generator<T> for Filtered<T, F, G>
impl<T, G> Generator<HashSet<T>> for HashSetGenerator<G, T>
impl<T, G> Generator<Option<T>> for OptionalGenerator<G, T>where
G: Generator<T>,
impl<T, G> Generator<Vec<T>> for VecGenerator<G, T>where
G: Generator<T>,
impl<T, U, F, G> Generator<U> for Mapped<T, U, F, G>
impl<T, U, G2, F, G1> Generator<U> for FlatMapped<T, U, G2, F, G1>
impl<T: Clone + Send + Sync> Generator<T> for JustGenerator<T>
impl<T: Float + DeserializeOwned + Serialize + Send + Sync + 'static> Generator<T> for FloatGenerator<T>
impl<T: Integer + DeserializeOwned + Serialize + Send + Sync + 'static> Generator<T> for IntegerGenerator<T>
impl<T> Generator<T> for BoxedGenerator<'_, T>
impl<T> Generator<T> for OneOfGenerator<'_, T>
impl<TS, TZ> Generator<Zoned> for ZonedGenerator<TS, TZ>
jiff only.