Skip to content

Commit

Permalink
Add #[track_caller] to methods which panic (#1442)
Browse files Browse the repository at this point in the history
* Add #[track_caller] to methods which panic

This makes it easier to diagnose problems when the preconidtions of
these methods are not met.

Signed-off-by: Joe Richey <[email protected]>
  • Loading branch information
josephlr authored May 5, 2024
1 parent 4f8257a commit a2375dc
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ You may also find the [Upgrade Guide](https://rust-random.github.io/book/update.
- Bump the MSRV to 1.61.0
- Rename `Rng::gen` to `Rng::random` to avoid conflict with the new `gen` keyword in Rust 2024 (#1435)
- Move all benchmarks to new `benches` crate (#1439)
- Annotate panicking methods with `#[track_caller]` (#1442)

## [0.9.0-alpha.1] - 2024-03-18
- Add the `Slice::num_choices` method to the Slice distribution (#1402)
Expand Down
1 change: 1 addition & 0 deletions rand_core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,7 @@ pub trait SeedableRng: Sized {
/// [`getrandom`]: https://docs.rs/getrandom
#[cfg(feature = "getrandom")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "getrandom")))]
#[track_caller]
fn from_entropy() -> Self {
let mut seed = Self::Seed::default();
if let Err(err) = getrandom::getrandom(seed.as_mut()) {
Expand Down
27 changes: 19 additions & 8 deletions src/rng.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@

//! [`Rng`] trait

use rand_core::{Error, RngCore};
use crate::distributions::uniform::{SampleRange, SampleUniform};
use crate::distributions::{self, Distribution, Standard};
use core::num::Wrapping;
use core::{mem, slice};
use rand_core::{Error, RngCore};

/// An automatically-implemented extension trait on [`RngCore`] providing high-level
/// generic methods for sampling values and other convenience methods.
Expand Down Expand Up @@ -124,10 +124,11 @@ pub trait Rng: RngCore {
/// ```
///
/// [`Uniform`]: distributions::uniform::Uniform
#[track_caller]
fn gen_range<T, R>(&mut self, range: R) -> T
where
T: SampleUniform,
R: SampleRange<T>
R: SampleRange<T>,
{
assert!(!range.is_empty(), "cannot sample empty range");
range.sample_single(self).unwrap()
Expand Down Expand Up @@ -236,8 +237,9 @@ pub trait Rng: RngCore {
///
/// [`fill_bytes`]: RngCore::fill_bytes
/// [`try_fill`]: Rng::try_fill
#[track_caller]
fn fill<T: Fill + ?Sized>(&mut self, dest: &mut T) {
dest.try_fill(self).unwrap_or_else(|_| panic!("Rng::fill failed"))
dest.try_fill(self).expect("Rng::fill failed")
}

/// Fill any type implementing [`Fill`] with random data
Expand Down Expand Up @@ -288,9 +290,12 @@ pub trait Rng: RngCore {
///
/// [`Bernoulli`]: distributions::Bernoulli
#[inline]
#[track_caller]
fn gen_bool(&mut self, p: f64) -> bool {
let d = distributions::Bernoulli::new(p).unwrap();
self.sample(d)
match distributions::Bernoulli::new(p) {
Ok(d) => self.sample(d),
Err(_) => panic!("p={:?} is outside range [0.0, 1.0]", p),
}
}

/// Return a bool with a probability of `numerator/denominator` of being
Expand All @@ -317,9 +322,15 @@ pub trait Rng: RngCore {
///
/// [`Bernoulli`]: distributions::Bernoulli
#[inline]
#[track_caller]
fn gen_ratio(&mut self, numerator: u32, denominator: u32) -> bool {
let d = distributions::Bernoulli::from_ratio(numerator, denominator).unwrap();
self.sample(d)
match distributions::Bernoulli::from_ratio(numerator, denominator) {
Ok(d) => self.sample(d),
Err(_) => panic!(
"p={}/{} is outside range [0.0, 1.0]",
numerator, denominator
),
}
}

/// Alias for [`Rng::random`].
Expand Down Expand Up @@ -432,8 +443,8 @@ where [T]: Fill
#[cfg(test)]
mod test {
use super::*;
use crate::test::rng;
use crate::rngs::mock::StepRng;
use crate::test::rng;
#[cfg(feature = "alloc")] use alloc::boxed::Box;

#[test]
Expand Down

0 comments on commit a2375dc

Please sign in to comment.