pub trait Arbitrary: Sized + Debug {
type Parameters: Default;
type Strategy: Strategy<Value = Self>;
// Required method
fn arbitrary_with(args: Self::Parameters) -> Self::Strategy;
// Provided method
fn arbitrary() -> Self::Strategy { ... }
}
Expand description
Arbitrary determines a canonical Strategy
for the implementing type.
It provides the method arbitrary_with
which generates a Strategy
for
producing arbitrary values of the implementing type (Self
). In general,
these strategies will produce the entire set of values possible for the
type, up to some size limitation or constraints set by their parameters.
When this is not desired, strategies to produce the desired values can be
built by combining Strategy
s as described in the crate documentation.
This trait analogous to
Haskell QuickCheck’s implementation of Arbitrary
.
In this interpretation of Arbitrary
, Strategy
is the equivalent of
the Gen
monad. Unlike in QuickCheck, Arbitrary
is not a core component;
types do not need to implement Arbitrary
unless one wants to use
any
or other free functions in this module.
Arbitrary
currently only works for types which represent owned data as
opposed to borrowed data. This is a fundamental restriction of proptest
which may be lifted in the future as the generic associated types (GAT)
feature of Rust is implemented and stabilized.
If you do not have unique constraints on how to generate the data for your custom types, consider using the derive macro to implement Arbitrary
Required Associated Types§
sourcetype Parameters: Default
type Parameters: Default
The type of parameters that arbitrary_with
accepts for configuration
of the generated Strategy
. Parameters must implement Default
.
Required Methods§
sourcefn arbitrary_with(args: Self::Parameters) -> Self::Strategy
fn arbitrary_with(args: Self::Parameters) -> Self::Strategy
Provided Methods§
sourcefn arbitrary() -> Self::Strategy
fn arbitrary() -> Self::Strategy
Generates a Strategy
for producing arbitrary values
of type the implementing type (Self
).
Calling this for the type X
is the equivalent of using
X::arbitrary_with(Default::default())
.
This method is defined in the trait for optimization for the default if you want to do that. It is a logic error to not preserve the semantics when overriding.