-
Notifications
You must be signed in to change notification settings - Fork 175
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implementing Arbitrary, what to name Strategy? #98
Comments
If you use impl Arbitrary for Polarization {
type Parameters = PolarizationKind;
type Strategy = BoxedStrategy<Self>;
fn arbitrary_with(args: Self::Parameters) -> Self::Strategy {
match args {
// ...
}.boxed() // Erase the full type to just BoxedStrategy<Value>
}
} Right now, that's generally the best choice on stable Rust (in some cases it could be desirable to avoid boxing for performance, e.g. if your test needs to generate tens of millions of values), and here you don't have a choice since the lambdas in If you're using unstable, I think named extential types would be another option here, but I'm not familiar enough with the current state of the feature to provide an example off hand. |
So using If you want to avoid boxing and write out the full type in
As for the current state of the Meanwhile, @AltSysrq's suggestion to use
nit: not exactly, these closures don't capture any state from outside so they can be coerced to function pointers. |
Good catch, there's that too. The simplest way to make my example work then is to put match args {
PolarizationKind::Linear => any_linear_polarization().boxed(),
PolarizationKind::Circular => any_circular_polarization().boxed(),
PolarizationKind::Elliptical => any_elliptical_polarization().boxed(),
PolarizationKind::Any => {
prop_oneof![
any_linear_polarization(),
any_circular_polarization(),
any_elliptical_polarization(),
].boxed()
}
} |
Closing due to inactivity. |
I have an enum that I want to select an arbitrary variant of during my tests:
where
Angle
andHandedness
areand
Complex
comes fromnum_complex
.I also want to be able to select arbitrary values of particular variants i.e.
Polarization::Linear(Angle)
with an arbitraryAngle
. To do that, I just defined a new enum with the variants:I started implementing
Arbitrary
forPolarization
, but I'm kind of stuck on thetype Strategy = ???
piece. I'm not exactly sure what the type of myStrategy
is. Here is my implementation so far:The text was updated successfully, but these errors were encountered: