Skip to content

Implement Arbitrary for AsMut<[T: Arbitrary]> #291

Open

Description

@cyphar

Because of #265, it's no longer possible to do something like

#[derive(Clone, Debug)]
#[cfg_attr(test, derive(PartialEq, Eq))]
struct Foo {
    foo: ChaChaPolyNonce, // has a BorrowMut<[u8]> impl
}

#[cfg(test)]
impl quickcheck::Arbitrary for Foo {
    fn arbitrary(g: &mut quickcheck::Gen) -> Self {
        let mut 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.

Activity

  1. neithernut commented on May 22, 2021

    @neithernut
    Contributor

    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?

  2. cyphar commented on May 23, 2021

    @cyphar
    Author

    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:

    impl Gen {
        /// Fill a mutable slice of any Arbitrary-compatible type with Arbitrary
        /// values.
        pub fn fill_slice<S, T>(&mut self, mut slice: S)
        where
            T: 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.

  3. changed the title [-]Implement Arbitrary for &mut [T: Arbitrary][/-] [+]Implement Arbitrary for AsMut<[T: Arbitrary]>[/+] on May 23, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions