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

Error handling: Result vs panic #8

Closed
dhardy opened this issue Oct 18, 2017 · 6 comments
Closed

Error handling: Result vs panic #8

dhardy opened this issue Oct 18, 2017 · 6 comments

Comments

@dhardy
Copy link
Owner

dhardy commented Oct 18, 2017

The consensus on this seems to be that Result is better in general, but there is little point for the simple next_u* functions, resulting in the following members of Rng or similar traits

    fn next_u32(&mut self) -> u32;
    // other next_u* ...

    fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error>;

    // possibly also a panicing fill function:
    fn fill_bytes(&mut self, dest: &mut [u8]);
@dhardy dhardy added this to the rand-core RFC milestone Oct 18, 2017
@newpavlov
Copy link

newpavlov commented Oct 23, 2017

A bit of bike-shedding: I am not sure about fill_bytes name, we have sort of convention which originates from the standard library: if we have try_foo method we'll have panicking foo counterpart as well, not foobar or something else. Plus fill will be 6 symbols shorter. :)

I understand the desire to keep names from the previous rand, but I don't think it warrants such inconsistency.

@dhardy
Copy link
Owner Author

dhardy commented Oct 23, 2017

Good point. I'm happy to rename to fill. Any objections?

@dhardy
Copy link
Owner Author

dhardy commented Oct 30, 2017

On second thoughts, fill_bytes is an existing function, so to avoid breakage it would be better to use fill_bytes and try_fill_bytes.

@burdges
Copy link

burdges commented Oct 30, 2017

I'd say fill_bytes and/or try_fill_bytes to reserve fill/try_fill for a function that handles &mut [T] where T: Rand.

@dhardy
Copy link
Owner Author

dhardy commented Oct 30, 2017

@burdges I'm pretty sure that will never be part of the same trait (if my RFCs get accepted): Rng will be in a separate crate from the distribution code (which handles generation of values of other types).

@burdges
Copy link

burdges commented Oct 30, 2017

I see. In fact, gen should already work fine for [T; N], while collections have rng.iter().collect() and replacing a &mut [T] sounds rare, so an explicit for (x,y) in slice.iter_mut().zip(rng.iter()) { *x = y } sounds fine.

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

No branches or pull requests

3 participants