Skip to content

Implement Read for some wrapper of Rng in terms of fill_bytes or try_fill_bytes #254

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

Closed
sdleffler opened this issue Feb 6, 2018 · 5 comments
Labels
E-easy Participation: easy job F-new-int Functionality: new, within Rand P-low Priority: Low

Comments

@sdleffler
Copy link

Hey folks. I wanted to be able to take an Rng and shove it into a function that expects R: Read, i.e. treat it as an endless bytestream. There doesn't seem to be a nice convenient way out there to, say, turn the Rng into an iterator and then that into a Read, anyhow it makes sense to me to give Rng the capabilities of Read (since Read just says, "fill up this buffer with random bytes, please!") which one might note, Rng already supports. Why not implement Read for some wrapper around Rng in terms of try_fill_bytes? Perhaps struct ReadableRng<R: Rng>(R);.

@dhardy
Copy link
Member

dhardy commented Feb 6, 2018

That shouldn't be hard to do. Is there good reason for including it within the rand lib though? You can create and implement this type externally from rand.

@sdleffler
Copy link
Author

That's absolutely true. I would argue that in the same way, it's possible to implement ReadRng outside of rand as well, but it happens to be there nonetheless; this makes me feel like there's a reasonable argument for types involving stdlib Read living inside rand. Also, I'm left wondering about whether or not it might even be reasonable to give Read to Rng as a parent trait, perhaps as an alternative to fill_bytes or try_fill_bytes (although I strongly suspect that ship has sailed.)

@dhardy
Copy link
Member

dhardy commented Feb 9, 2018

ReadRng is in rand because it was used by OsRng, however that now has its own version so ReadRng could be removed. I've not seen any actual proposal to do that however, and would consider a PR adding a wrapper implementing Read.

Making Rng itself extend Read was proposed by @huonw a while back IIRC; unfortunately it is problematic for no-std support because Read and io::Result are not available outside of std.

@dhardy dhardy added X-enhancement F-new-int Functionality: new, within Rand P-low Priority: Low labels Mar 14, 2018
@pitdicker pitdicker added the E-easy Participation: easy job label Apr 5, 2018
@pitdicker
Copy link
Contributor

This could live in read.rs.

@pitdicker
Copy link
Contributor

Now io::Read is implemented for all RNGs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
E-easy Participation: easy job F-new-int Functionality: new, within Rand P-low Priority: Low
Projects
None yet
Development

No branches or pull requests

3 participants