Skip to content

Working with ?Sized bounds when implementing Distribution #725

Closed

Description

@fizyk20

I'm using rand 0.6.

Say I have a struct Foo(String); and I want the Standard distribution for Foo to output values with 5-character alphanumeric strings. So I'm trying this:

impl Distribution<Foo> for Standard {
    fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> Foo {
       Foo(rng.sample_iter(&Alphanumeric).take(5).collect())
    }
}

Well... This doesn't work, since sample_iter requires R to be Sized. I also can't remove the ?Sized bound from the impl, because then the compiler complains that it is more specific than the trait.

The only workaround I managed to come up with is this:

fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> Foo {
   let mut s = String::with_capacity(5);
   for _ in 0..5 {
       s.push(Alphanumeric.sample(rng));
   }
   Foo(s)
}

It is a lot uglier, though. Is there any better way of doing this that I'm missing?

Activity

  1. dhardy commented on Feb 5, 2019

    @dhardy
    Member

    I don't think there's any reason we can't correct sample_iter to accept R: ?Sized...

    however, this method isn't really necessary anyway: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=4fb68589bbcc8715113be405c9e047ae

    impl Distribution<Foo> for Standard {
        fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> Foo {
           Foo((0..5).map(|_| Alphanumeric.sample(rng)).collect())
        }
    }

    Any thoughts on whether we should keep it? Perhaps since discoverability is better.

  2. fizyk20 commented on Feb 6, 2019

    @fizyk20
    ContributorAuthor

    It's probably a matter of personal taste, but to me rng.sample_iter(distr).take(x) looks much cleaner than (0..x).map(|_| distr.sample(rng)) - it just reads better, "I want a stream of values with such and such distribution and take x of them" rather than "transform a range of values into values from such and such distribution". And an unused closure argument in map is pretty much a code smell to me... But as I said, this might be just my personal preference.

    In any case, if the requirement for Sized can be dropped from sample_iter, it would be my preferred way forward.

  3. vks commented on Feb 6, 2019

    @vks
    Contributor

    In any case, if the requirement for Sized can be dropped from sample_iter, it would be my preferred way forward.

    Since this is not a breaking change, I agree that we should just do this. Whether or not sample_iter is needed is orthogonal to this issue.

  4. dhardy commented on Feb 15, 2019

    @dhardy
    Member
  5. added this to the 0.7 release milestone on Feb 15, 2019
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

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions