From 22cc4fc5d69171f161d4cead8337f27b4c229e77 Mon Sep 17 00:00:00 2001 From: Andreas Fuchs Date: Tue, 25 Feb 2025 08:02:27 -0500 Subject: [PATCH] Update call sites for rand 0.9 This release changes some parts of the API incompatibly, so this adjusts our code correspondingly: * Use `rng` instead of `thread_rng` * Correctly implement `UniformSampler`, using the `new` associated function that now returns a Result. * Handle that result with an expect --- governor/src/jitter.rs | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/governor/src/jitter.rs b/governor/src/jitter.rs index 2439c69..b06dfdb 100644 --- a/governor/src/jitter.rs +++ b/governor/src/jitter.rs @@ -2,11 +2,11 @@ use std::prelude::v1::*; use crate::nanos::Nanos; #[cfg(feature = "jitter")] -use rand::distributions::uniform::{SampleBorrow, SampleUniform, UniformInt, UniformSampler}; +use rand::distr::uniform::{SampleBorrow, SampleUniform, UniformInt, UniformSampler}; #[cfg(feature = "jitter")] -use rand::distributions::{Distribution, Uniform}; +use rand::distr::{Distribution, Uniform}; #[cfg(feature = "jitter")] -use rand::{thread_rng, Rng}; +use rand::{rng, Rng}; use std::ops::Add; use std::time::Duration; @@ -107,8 +107,9 @@ impl Jitter { if self.min == self.max { return self.min; } - let uniform = Uniform::new(self.min, self.max); - uniform.sample(&mut thread_rng()) + let uniform = + Uniform::new(self.min, self.max).expect("range is large enough for a distribution"); + uniform.sample(&mut rng()) } /// Returns a random amount of jitter within the configured interval. @@ -127,26 +128,26 @@ pub struct UniformJitter(UniformInt); impl UniformSampler for UniformJitter { type X = Nanos; - fn new(low: B1, high: B2) -> Self + fn new(low: B1, high: B2) -> Result where B1: SampleBorrow + Sized, B2: SampleBorrow + Sized, { - UniformJitter(UniformInt::new( + Ok(UniformJitter(UniformInt::new( low.borrow().as_u64(), high.borrow().as_u64(), - )) + )?)) } - fn new_inclusive(low: B1, high: B2) -> Self + fn new_inclusive(low: B1, high: B2) -> Result where B1: SampleBorrow + Sized, B2: SampleBorrow + Sized, { - UniformJitter(UniformInt::new_inclusive( + Ok(UniformJitter(UniformInt::new( low.borrow().as_u64(), high.borrow().as_u64(), - )) + )?)) } fn sample(&self, rng: &mut R) -> Self::X {