Skip to content
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

Backport Arbitrary support to 0.7.x and 0.6.x versions #641

Closed
greyblake opened this issue Oct 11, 2022 · 2 comments
Closed

Backport Arbitrary support to 0.7.x and 0.6.x versions #641

greyblake opened this issue Oct 11, 2022 · 2 comments

Comments

@greyblake
Copy link

Motivation
We're using Diesel 1.x ,which depends on uuid 0.7.x. This forces our project to use uuid 0.7.x too.
We'd really love to use Arbitrary to simplify testing in the project, but uuid started supporting Arbitrary only in 1.0 (which we cannot use at the moment due to the Diesel).

Solution
Backport support of Arbitrary to 0.7.x and 0.8.x version of UUID.

Alternatives

Is it blocking?
I would not say that is blocking. Rather nice to have.

Anything else?

We're fine to contribute and open pull requests if the maintainer gives us a positive signal.

Thank you!

@greyblake greyblake changed the title Backport Arbitrary support to 0.7.x version Backport Arbitrary support to 0.7.x and 0.6.x versions Oct 18, 2022
@KodrAus
Copy link
Member

KodrAus commented May 6, 2023

Sorry for never replying on this one @greyblake, but it looks like rust-fuzz/arbitrary#33 did get resolved, so I'll go ahead and close this one. If you do still find yourself needing to backport this support though I would be happy to accept a PR for it.

@KodrAus KodrAus closed this as completed May 6, 2023
@greyblake
Copy link
Author

greyblake commented May 6, 2023

@KodrAus No worries, I know what it is to maintain an open source project.
Right, so I've added that ability to Arbitrary and luckily it was merged.

No we can workaround it with the following:

use uuid:uuid;
use arbitrary::Arbitrary;

#[derive(Arbitrary)]
struct User {
    #[arbitrary(with = arbitrary_uuid)]
    id: Uuid,
}

pub fn arbitrary_uuid(u: &mut Unstructured<'_>) -> arbitrary::Result<Uuid> {
    let bytes: [u8; 16] = u
        .bytes(16)?
        .try_into()
        .map_err(|_| arbitrary::Error::NotEnoughData)?;
    Uuid::from_bytes(&bytes).map_err(|_| arbitrary::Error::IncorrectFormat)
}

For the case if, let's say, the uuid is within a container (Option, Vec, HashMap, etc), one can use arbitrary_ext that helps to deal with that.

For example:

use uuid:uuid;
use arbitrary::Arbitrary;
use arbitrary_ext::{arbitrary_vec, arbitrary_option};

#[derive(Arbitrary)]
struct Group {
    #[arbitrary(with = arbitrary_option(arbitrary_uuid))]
    admin_id: Option<Uuid>,

    #[arbitrary(with = arbitrary_vec(arbitrary_uuid))]
    members_ids: Vec<Uuid>,
}

Of course, this can be helpful not only for old versions of UUID crate, but for other crates, which do not support arbitrary as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants