You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Implement Arbitrary for AsMut<[T: Arbitrary]> #291
Because of #265, it's no longer possible to do something like
#[derive(Clone,Debug)]#[cfg_attr(test, derive(PartialEq,Eq))]structFoo{foo:ChaChaPolyNonce,// has a BorrowMut<[u8]> impl}#[cfg(test)]impl quickcheck::ArbitraryforFoo{fnarbitrary(g:&mut quickcheck::Gen) -> Self{letmut foo = ChaChaPolyNonce::default();
g.fill_bytes(&mut foo);// No more RngCore!Self{ foo }}}
But it seems as though it would be reasonable to simply have an Arbitrary implementation for &mut [T: Arbitrary] that does fill_bytes internally.
Having BorrowMut<[T: Arbitrary]> + Default types (or maybe some proxy/wrapper) implement Arbitrary is certainly possible. But how would you implement Arbitrary for & mut [T: Arbitrary] based on fill_bytes in the general case?
Sorry, I was trying to generalise the original issue I had (being unable to use fill_bytes with the new quickcheck release) into being able to handle arbitrary &mut[T: Arbitrary] without adjusting how you would actually solve it.
I also forgot that arbitrary doesn't take &mut self or anything so you'd need to implement it as a Gen method instead, so something more like:
implGen{/// Fill a mutable slice of any Arbitrary-compatible type with Arbitrary/// values.pubfnfill_slice<S,T>(&mutself,mutslice:S)whereT:Arbitrary,S:AsMut<[T]>,{
slice.as_mut().fill_with(|| T::arbitrary(self))}}
I might just send a PR with this because the above works for me.
Activity
neithernut commentedon May 22, 2021
Having
BorrowMut<[T: Arbitrary]> + Defaulttypes (or maybe some proxy/wrapper) implementArbitraryis certainly possible. But how would you implementArbitraryfor& mut [T: Arbitrary]based onfill_bytesin the general case?cyphar commentedon May 23, 2021
Sorry, I was trying to generalise the original issue I had (being unable to use
fill_byteswith the new quickcheck release) into being able to handle arbitrary&mut[T: Arbitrary]without adjusting how you would actually solve it.I also forgot that arbitrary doesn't take
&mut selfor anything so you'd need to implement it as aGenmethod instead, so something more like:I might just send a PR with this because the above works for me.
[-]Implement Arbitrary for &mut [T: Arbitrary][/-][+]Implement Arbitrary for AsMut<[T: Arbitrary]>[/+]