Make Uniform constructors return a result#1229
Conversation
This helps tools like `cargo geiger`.
- This is a breaking change. - The new error type had to be made public, otherwise `Uniform` could not be extended for user-defined types by implementing `UniformSampler`. - `rand_distr` was updated accordingly. - Also forbid unsafe code for crates where none is used. Fixes rust-random#1195, rust-random#1211.
|
@vks mind if I ping you on the status of this? |
|
@dhardy I'll try to have a look this Sunday. |
|
Skimming the above it looks like this should make |
|
I have a patch that is almost ready. |
dhardy
left a comment
There was a problem hiding this comment.
It's worth noting here that this PR introduces .unwrap() in several distributions where it should be statically-verifiable that the range is non-empty, but of course the language doesn't let us do that.
| /// Usually users should not call this directly but prefer to use | ||
| /// [`Uniform::new`]. | ||
| fn new<B1, B2>(low: B1, high: B2) -> Result<Self, Error> | ||
| where | ||
| B1: SampleBorrow<Self::X> + Sized, | ||
| B2: SampleBorrow<Self::X> + Sized; | ||
|
|
||
| /// Construct self, with inclusive bounds `[low, high]`. | ||
| /// | ||
| /// Usually users should not call this directly but instead use | ||
| /// `Uniform::new_inclusive`, which asserts that `low <= high` before | ||
| /// calling this. | ||
| fn new_inclusive<B1, B2>(low: B1, high: B2) -> Self | ||
| /// Usually users should not call this directly but prefer to use | ||
| /// [`Uniform::new_inclusive`]. | ||
| fn new_inclusive<B1, B2>(low: B1, high: B2) -> Result<Self, Error> |
There was a problem hiding this comment.
There's not really a reason users shouldn't call these directly now (IIRC we used to have an assert in Uniform::new), though Uniform::new is probably still the more convenient API.
|
I'll try to look into it later this week. |
|
Sure. I used the web interface to make a merge commit, but it may be incorrect. |
* Forbid unsafe code in crates without unsafe code This helps tools like `cargo geiger`. * Make `Uniform` constructors return a result - This is a breaking change. - The new error type had to be made public, otherwise `Uniform` could not be extended for user-defined types by implementing `UniformSampler`. - `rand_distr` was updated accordingly. - Also forbid unsafe code for crates where none is used. Fixes rust-random#1195, rust-random#1211. * Address review feedback * Make `sample_single` return a `Result` * Fix benchmarks * Small fixes * Update src/distributions/uniform.rs
* Forbid unsafe code in crates without unsafe code This helps tools like `cargo geiger`. * Make `Uniform` constructors return a result - This is a breaking change. - The new error type had to be made public, otherwise `Uniform` could not be extended for user-defined types by implementing `UniformSampler`. - `rand_distr` was updated accordingly. - Also forbid unsafe code for crates where none is used. Fixes rust-random#1195, rust-random#1211. * Address review feedback * Make `sample_single` return a `Result` * Fix benchmarks * Small fixes * Update src/distributions/uniform.rs
Uniformcouldnot be extended for user-defined types by implementing
UniformSampler.rand_distrwas updated accordingly.Fixes #1195, #1211.