Skip to content

Commit

Permalink
Update rand to 0.9.0
Browse files Browse the repository at this point in the history
See: https://rust-random.github.io/book/update-0.9.html

The upgrade has quite a few breaking changes. Most notable are:
- Drop support for `usize` and `isize` from Uniform
- Uniform::sample returns result (used to panic)
- Deprecate `gen` like function names (replace with `random`
  • Loading branch information
Thomas Duindam committed Feb 3, 2025
1 parent 475d1d3 commit 230bd1e
Show file tree
Hide file tree
Showing 31 changed files with 203 additions and 84 deletions.
2 changes: 1 addition & 1 deletion dummy_derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ pub fn derive_dummy(input: TokenStream) -> TokenStream {
impl #impl_generics #crate_name::Dummy<#crate_name::Faker> for #receiver_name #ty_generics #where_clause {
fn dummy_with_rng<R: #crate_name::Rng + ?Sized>(_: &#crate_name::Faker, rng: &mut R) -> Self {
let options = [#(#variant_opts),*];
match #crate_name::rand::seq::SliceRandom::choose(
match #crate_name::rand::seq::IndexedRandom::choose(
<_ as ::std::convert::AsRef<[usize]>>::as_ref(&options),
rng,
)
Expand Down
6 changes: 3 additions & 3 deletions fake/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ rustdoc-args = ["--cfg", "docsrs"]

[dependencies]
dummy = { version = "0.9", path = "../dummy_derive", optional = true }
rand = "0.8"
rand = "0.9"
random_color = { version = "1", optional = true }
deunicode = "1.6"
chrono = { version = "0.4", features = [
Expand All @@ -36,7 +36,7 @@ num-traits = { version = "0.2", optional = true }
rust_decimal = { version = "1", default-features = false, optional = true }
bigdecimal-rs = { version = "0.4", package = "bigdecimal", default-features = false, optional = true }
zerocopy = { version = "0.8", optional = true }
rand_core = { version = "0.6", optional = true }
rand_core = { version = "0.9.0", optional = true }
glam = { version = "0.29", optional = true }
url-escape = { version = "0.1", optional = true }
bson = { version = "2", optional = true }
Expand All @@ -49,7 +49,7 @@ base64 = { version = "0.22.1", optional = true }
chrono = { version = "0.4", features = ["clock"], default-features = false }
fake = { path = ".", features = ["derive"] }
proptest = { version = "1.0.0", features = ["std"], default-features = false }
rand_chacha = "0.3"
rand_chacha = "0.9.0"

[features]
# Provide derive(Dummy) macros.
Expand Down
4 changes: 2 additions & 2 deletions fake/examples/collections.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use fake::{Fake, Faker};
use rand::distributions;
use rand::distr;
use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};

fn main() {
Expand Down Expand Up @@ -28,7 +28,7 @@ fn main() {
println!("BTreeSet {:?}", btree_set);

// generate fixed length nested vec [[[u8;2];3];4] with value using sampler
let sampler = distributions::Uniform::new_inclusive(1, 10);
let sampler = distr::Uniform::new_inclusive(1, 10).expect("Can sample uniform");
let v3 = fake::vec![u8 as sampler; 4, 3, 2];
println!("random nested vec {:?}", v3);

Expand Down
4 changes: 2 additions & 2 deletions fake/examples/primitives.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use fake::{Fake, Faker};
use rand::distributions;
use rand::distr;
use rand::rngs::StdRng;
use rand::SeedableRng;

Expand All @@ -16,7 +16,7 @@ fn main() {
println!("u8 ({}) in [MIN, MAX]", (..).fake::<u8>());

// to reuse sampler `Uniform` for value generation
let sampler = distributions::Uniform::new_inclusive(1, 10);
let sampler = distr::Uniform::new_inclusive(1, 10).expect("Can");
for _ in 0..5 {
let v: usize = sampler.fake();
println!("sample value {}", v);
Expand Down
2 changes: 1 addition & 1 deletion fake/src/faker/impls/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use crate::faker::name::raw::{FirstName, LastName, Name};
use crate::faker::numerify_sym;
use crate::locales::Data;
use crate::{Dummy, Fake, Faker};
use rand::seq::SliceRandom;
use rand::Rng;
use rand::seq::IndexedRandom;

impl<L: Data> Dummy<CityPrefix<L>> for String {
fn dummy_with_rng<R: Rng + ?Sized>(_: &CityPrefix<L>, rng: &mut R) -> Self {
Expand Down
2 changes: 1 addition & 1 deletion fake/src/faker/impls/administrative.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::faker::administrative::raw::*;
use crate::locales::FR_FR;
use crate::{Dummy, Fake};
use rand::seq::SliceRandom;
use rand::seq::IndexedRandom;
use rand::Rng;

// ref https://fr.wikipedia.org/wiki/Num%C3%A9rotation_des_d%C3%A9partements_fran%C3%A7ais
Expand Down
3 changes: 1 addition & 2 deletions fake/src/faker/impls/automotive.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use crate::faker::automotive::raw::*;
use crate::locales::FR_FR;
use crate::{Dummy, Fake};
use rand::prelude::SliceRandom;
use rand::seq::IndexedRandom;
use rand::Rng;
use std::char;

/* ABC without I, O and U
As with the SIV system, The letters I and O were never used because they could be confused with other characters, like 1 and 0.
ref https://en.wikipedia.org/wiki/Vehicle_registration_plates_of_France
Expand Down
2 changes: 1 addition & 1 deletion fake/src/faker/impls/barcode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use crate::faker::boolean::raw::Boolean;
use crate::faker::numerify_sym;
use crate::locales::Data;
use crate::{Dummy, Fake};
use rand::prelude::SliceRandom;
use rand::Rng;
use rand::seq::IndexedRandom;

const ISBN_MAX_LENGTH: usize = 13;

Expand Down
2 changes: 1 addition & 1 deletion fake/src/faker/impls/company.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use crate::faker::company::raw::*;
use crate::faker::name::raw::LastName;
use crate::locales::Data;
use crate::{Dummy, Fake};
use rand::seq::SliceRandom;
use rand::Rng;
use rand::seq::IndexedRandom;

impl<L: Data> Dummy<CompanySuffix<L>> for String {
fn dummy_with_rng<R: Rng + ?Sized>(_: &CompanySuffix<L>, rng: &mut R) -> Self {
Expand Down
3 changes: 2 additions & 1 deletion fake/src/faker/impls/creditcard.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use crate::faker::creditcard::raw::CreditCardNumber;
use crate::locales::Data;
use crate::Dummy;
use rand::{prelude::IteratorRandom, seq::SliceRandom, Rng};
use rand::{ Rng};
use rand::seq::{IndexedRandom, IteratorRandom};

type PrefixCreditcard<'a> = (u8, Option<&'a [u8]>, &'a [usize]);

Expand Down
2 changes: 1 addition & 1 deletion fake/src/faker/impls/currency.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::faker::currency::raw::*;
use crate::locales::Data;
use crate::Dummy;
use rand::seq::SliceRandom;
use rand::seq::IndexedRandom;
use rand::Rng;

impl<L: Data> Dummy<CurrencyCode<L>> for String {
Expand Down
2 changes: 1 addition & 1 deletion fake/src/faker/impls/filesystem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ use crate::faker::filesystem::raw::*;
use crate::impls::std::path::PathFaker;
use crate::locales::{Data, EN};
use crate::{Dummy, Fake};
use rand::seq::SliceRandom;
use rand::Rng;
use std::path::PathBuf;
use rand::seq::IndexedRandom;

const MIME_TYPES: &[&str] = &[
"application/1d-interleaved-parityfec",
Expand Down
8 changes: 4 additions & 4 deletions fake/src/faker/impls/finance.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::faker::finance::raw::*;
use crate::locales::Data;
use crate::{Dummy, Fake};
use rand::seq::SliceRandom;
use rand::Rng;
use rand::seq::IndexedRandom;

const ALPHABET: &[char; 26] = &[
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S',
Expand Down Expand Up @@ -51,9 +51,9 @@ impl<L: Data> Dummy<Bic<L>> for String {
);
} else if prob < 90 {
(
rng.gen_range('0'..='9'),
rng.gen_range('0'..='9'),
rng.gen_range('0'..='9'),
rng.random_range('0'..='9'),
rng.random_range('0'..='9'),
rng.random_range('0'..='9'),
)
} else {
(
Expand Down
10 changes: 5 additions & 5 deletions fake/src/faker/impls/internet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ use crate::faker::name::raw::FirstName;
use crate::locales::Data;
use crate::{Dummy, Fake, Faker};
use deunicode::AsciiChars;
use rand::distributions::{Distribution, Uniform};
use rand::seq::SliceRandom;
use rand::distr::{Distribution, Uniform};
use rand::Rng;
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
use rand::seq::IndexedRandom;

impl<L: Data> Dummy<FreeEmailProvider<L>> for String {
fn dummy_with_rng<R: Rng + ?Sized>(_: &FreeEmailProvider<L>, rng: &mut R) -> Self {
Expand Down Expand Up @@ -86,7 +86,7 @@ impl<L: Data> Dummy<Password<L>> for String {

impl<L: Data> Dummy<IPv4<L>> for String {
fn dummy_with_rng<R: Rng + ?Sized>(_: &IPv4<L>, rng: &mut R) -> Self {
let u = Uniform::new_inclusive(u8::MIN, u8::MAX);
let u = Uniform::new_inclusive(u8::MIN, u8::MAX).expect("u8::MIN <= u8::MAX");
format!(
"{}.{}.{}.{}",
u.sample(rng),
Expand All @@ -106,7 +106,7 @@ impl<L: Data> Dummy<IPv4<L>> for Ipv4Addr {

impl<L: Data> Dummy<IPv6<L>> for String {
fn dummy_with_rng<R: Rng + ?Sized>(_: &IPv6<L>, rng: &mut R) -> Self {
let u = Uniform::new_inclusive(u16::MIN, u16::MAX);
let u = Uniform::new_inclusive(u16::MIN, u16::MAX).expect("u16::MIN <= u16::MAX");
format!(
"{:X}:{:X}:{:X}:{:X}:{:X}:{:X}:{:X}:{:X}",
u.sample(rng),
Expand Down Expand Up @@ -143,7 +143,7 @@ impl<L: Data> Dummy<IP<L>> for IpAddr {

impl<L: Data> Dummy<MACAddress<L>> for String {
fn dummy_with_rng<R: Rng + ?Sized>(_: &MACAddress<L>, rng: &mut R) -> Self {
let u = Uniform::new_inclusive(u8::MIN, u8::MAX);
let u = Uniform::new_inclusive(u8::MIN, u8::MAX).expect("u8::MIN <= u8::MAX");
format!(
"{:02X}:{:02X}:{:02X}:{:02X}:{:02X}:{:02X}",
u.sample(rng),
Expand Down
2 changes: 1 addition & 1 deletion fake/src/faker/impls/job.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::faker::job::raw::*;
use crate::locales::Data;
use crate::{Dummy, Fake};
use rand::seq::SliceRandom;
use rand::seq::IndexedRandom;
use rand::Rng;

impl<L: Data> Dummy<Seniority<L>> for String {
Expand Down
2 changes: 1 addition & 1 deletion fake/src/faker/impls/lorem.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::faker::lorem::raw::*;
use crate::locales::Data;
use crate::{Dummy, Fake};
use rand::seq::SliceRandom;
use rand::Rng;
use rand::seq::IndexedRandom;

impl<L: Data> Dummy<Word<L>> for String {
fn dummy_with_rng<R: Rng + ?Sized>(_: &Word<L>, rng: &mut R) -> Self {
Expand Down
2 changes: 1 addition & 1 deletion fake/src/faker/impls/name.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::faker::name::raw::*;
use crate::locales::Data;
use crate::{Dummy, Fake};
use rand::seq::SliceRandom;
use rand::seq::IndexedRandom;
use rand::Rng;

impl<L: Data> Dummy<FirstName<L>> for String {
Expand Down
2 changes: 1 addition & 1 deletion fake/src/faker/impls/number.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::faker::number::raw::*;
use crate::faker::numerify_sym;
use crate::locales::Data;
use crate::Dummy;
use rand::seq::SliceRandom;
use rand::seq::IndexedRandom;
use rand::Rng;

impl<L: Data> Dummy<Digit<L>> for String {
Expand Down
2 changes: 1 addition & 1 deletion fake/src/faker/impls/phone_number.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::faker::numerify_sym;
use crate::faker::phone_number::raw::*;
use crate::locales::Data;
use crate::Dummy;
use rand::seq::SliceRandom;
use rand::seq::IndexedRandom;
use rand::Rng;

impl<L: Data> Dummy<PhoneNumber<L>> for String {
Expand Down
8 changes: 4 additions & 4 deletions fake/src/impls/glam/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ mod tests {
#[test]
fn fake_vec2() {
let rng = &mut StdRng::from_seed(SEED);
let expected = vec2(rng.gen(), rng.gen());
let expected = vec2(rng.random(), rng.random());
let rng = &mut StdRng::from_seed(SEED);
let fake = Faker.fake_with_rng::<Vec2, _>(rng);
assert_eq!(expected, fake);
Expand All @@ -70,7 +70,7 @@ mod tests {
#[test]
fn fake_vec3() {
let rng = &mut StdRng::from_seed(SEED);
let expected = vec3(rng.gen(), rng.gen(), rng.gen());
let expected = vec3(rng.random(), rng.random(), rng.random());
let rng = &mut StdRng::from_seed(SEED);
let fake = Faker.fake_with_rng::<Vec3, _>(rng);
assert_eq!(expected, fake);
Expand All @@ -79,7 +79,7 @@ mod tests {
#[test]
fn fake_vec4() {
let rng = &mut StdRng::from_seed(SEED);
let expected = vec4(rng.gen(), rng.gen(), rng.gen(), rng.gen());
let expected = vec4(rng.random(), rng.random(), rng.random(), rng.random());
let rng = &mut StdRng::from_seed(SEED);
let fake = Faker.fake_with_rng::<Vec4, _>(rng);
assert_eq!(expected, fake);
Expand All @@ -88,7 +88,7 @@ mod tests {
#[test]
fn fake_mat4() {
let rng = &mut StdRng::from_seed(SEED);
let expected: Vec<f32> = (0..16).map(|_| rng.gen()).collect();
let expected: Vec<f32> = (0..16).map(|_| rng.random()).collect();
let rng = &mut StdRng::from_seed(SEED);
let fake = Faker.fake_with_rng::<Mat4, _>(rng);
assert_eq!(expected[0..16], fake.to_cols_array());
Expand Down
6 changes: 3 additions & 3 deletions fake/src/impls/std/net.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
use crate::{Dummy, Fake, Faker};
use rand::distributions::{Distribution, Uniform};
use rand::distr::{Distribution, Uniform};
use rand::Rng;
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddrV4, SocketAddrV6};

impl Dummy<Faker> for Ipv4Addr {
fn dummy_with_rng<R: Rng + ?Sized>(_: &Faker, rng: &mut R) -> Self {
let u = Uniform::new_inclusive(u8::MIN, u8::MAX);
let u = Uniform::new_inclusive(u8::MIN, u8::MAX).expect("u8::MIN <= u8::MAX");
Ipv4Addr::new(u.sample(rng), u.sample(rng), u.sample(rng), u.sample(rng))
}
}

impl Dummy<Faker> for Ipv6Addr {
fn dummy_with_rng<R: Rng + ?Sized>(_: &Faker, rng: &mut R) -> Self {
let u = Uniform::new_inclusive(u16::MIN, u16::MAX);
let u = Uniform::new_inclusive(u16::MIN, u16::MAX).expect("u16::MIN <= u16::MAX");
Ipv6Addr::new(
u.sample(rng),
u.sample(rng),
Expand Down
15 changes: 7 additions & 8 deletions fake/src/impls/std/num.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
use crate::{Dummy, Faker};
use rand::distributions::{Distribution, Uniform};
use rand::distr::{Distribution, Uniform};
use rand::Rng;
use std::num::{
NonZeroI128, NonZeroI16, NonZeroI32, NonZeroI64, NonZeroI8, NonZeroIsize, NonZeroU128,
NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU8, NonZeroUsize,
NonZeroI128, NonZeroI16, NonZeroI32, NonZeroI64, NonZeroI8, NonZeroU128, NonZeroU16,
NonZeroU32, NonZeroU64, NonZeroU8, NonZeroUsize,
};

macro_rules! signed_faker_impl {
($nz_typ: ty, $typ:ty) => {
impl Dummy<Faker> for $nz_typ {
fn dummy_with_rng<R: Rng + ?Sized>(_: &Faker, rng: &mut R) -> Self {
if rng.gen_bool(0.5) {
let u = Uniform::new_inclusive(<$typ>::MIN, -1);
if rng.random_bool(0.5) {
let u = Uniform::new_inclusive(<$typ>::MIN, -1).expect("Can sample uniform");
<$nz_typ>::new(u.sample(rng)).unwrap()
} else {
let u = Uniform::new_inclusive(1, <$typ>::MAX);
let u = Uniform::new_inclusive(1, <$typ>::MAX).expect("Can sample uniform");
<$nz_typ>::new(u.sample(rng)).unwrap()
}
}
Expand All @@ -26,7 +26,7 @@ macro_rules! unsigned_faker_impl {
($nz_typ: ty, $typ:ty) => {
impl Dummy<Faker> for $nz_typ {
fn dummy_with_rng<R: Rng + ?Sized>(_: &Faker, rng: &mut R) -> Self {
let u = Uniform::new_inclusive(1, <$typ>::MAX);
let u = Uniform::new_inclusive(1, <$typ>::MAX).expect("Can sample uniform");
<$nz_typ>::new(u.sample(rng)).unwrap()
}
}
Expand All @@ -39,7 +39,6 @@ signed_faker_impl!(NonZeroI32, i32);
signed_faker_impl!(NonZeroI64, i64);
#[cfg(not(target_os = "emscripten"))]
signed_faker_impl!(NonZeroI128, i128);
signed_faker_impl!(NonZeroIsize, isize);

unsigned_faker_impl!(NonZeroU8, u8);
unsigned_faker_impl!(NonZeroU16, u16);
Expand Down
2 changes: 1 addition & 1 deletion fake/src/impls/std/path.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::locales::{Data, EN};
use crate::{Dummy, Fake, Faker};
use rand::seq::SliceRandom;
use rand::seq::IndexedRandom;
use rand::Rng;
use std::path::PathBuf;

Expand Down
Loading

0 comments on commit 230bd1e

Please sign in to comment.