Skip to content

Commit

Permalink
Merge pull request #197 from lumeohq/fix-rand-in-macro
Browse files Browse the repository at this point in the history
Fix direct use of rand in derive macro
  • Loading branch information
cksac authored Oct 17, 2024
2 parents 5c083eb + d0963f0 commit 23a753a
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 12 deletions.
4 changes: 2 additions & 2 deletions dummy_derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ pub fn derive_dummy(input: TokenStream) -> TokenStream {
impl #impl_generics ::fake::Dummy<::fake::Faker> for #receiver_name #ty_generics #where_clause {
fn dummy_with_rng<R: ::fake::Rng + ?Sized>(_: &::fake::Faker, rng: &mut R) -> Self {
let options = [#(#variant_opts),*];
match rand::seq::SliceRandom::choose(options.as_ref(), rng).unwrap() {
match ::fake::rand::seq::SliceRandom::choose(options.as_ref(), rng).unwrap() {
#(#match_statements)*
_ => {
unreachable!()
Expand Down Expand Up @@ -234,7 +234,7 @@ fn expose_field(f: &DummyField) -> proc_macro2::TokenStream {
if let Some(ref from) = f.from {
let from_ty = syn::parse_str::<syn::Type>(from).unwrap();
quote! {
std::convert::Into::<#field_ty>::into(::fake::Fake::fake_with_rng::<#from_ty, _>(&(#faker), rng))
::std::convert::Into::<#field_ty>::into(::fake::Fake::fake_with_rng::<#from_ty, _>(&(#faker), rng))
}
} else if let Some(ref wrapper) = f.wrapper {
let wrapper_ty = syn::parse_str::<syn::Type>(wrapper).unwrap();
Expand Down
6 changes: 3 additions & 3 deletions fake/src/faker/impls/internet.rs
Original file line number Diff line number Diff line change
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_value(), u8::max_value());
let u = Uniform::new_inclusive(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_value(), u16::max_value());
let u = Uniform::new_inclusive(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_value(), u8::max_value());
let u = Uniform::new_inclusive(u8::MIN, u8::MAX);
format!(
"{:02X}:{:02X}:{:02X}:{:02X}:{:02X}:{:02X}",
u.sample(rng),
Expand Down
4 changes: 2 additions & 2 deletions fake/src/impls/std/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ 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_value(), u8::max_value());
let u = Uniform::new_inclusive(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_value(), u16::max_value());
let u = Uniform::new_inclusive(u16::MIN, u16::MAX);
Ipv6Addr::new(
u.sample(rng),
u.sample(rng),
Expand Down
8 changes: 4 additions & 4 deletions fake/src/impls/std/primitives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ macro_rules! range_impl {

impl Dummy<ops::RangeFrom<Self>> for $typ {
fn dummy_with_rng<R: Rng + ?Sized>(range: &ops::RangeFrom<Self>, rng: &mut R) -> Self {
let u = Uniform::new_inclusive(range.start, std::$typ::MAX);
let u = Uniform::new_inclusive(range.start, $typ::MAX);
u.sample(rng)
}
}

impl Dummy<ops::RangeFull> for $typ {
fn dummy_with_rng<R: Rng + ?Sized>(_: &ops::RangeFull, rng: &mut R) -> Self {
let u = Uniform::new_inclusive(std::$typ::MIN, std::$typ::MAX);
let u = Uniform::new_inclusive($typ::MIN, $typ::MAX);
u.sample(rng)
}
}
Expand All @@ -57,7 +57,7 @@ macro_rules! range_impl {

impl Dummy<ops::RangeTo<Self>> for $typ {
fn dummy_with_rng<R: Rng + ?Sized>(range: &ops::RangeTo<Self>, rng: &mut R) -> Self {
rng.gen_range(std::$typ::MIN..range.end)
rng.gen_range($typ::MIN..range.end)
}
}

Expand All @@ -66,7 +66,7 @@ macro_rules! range_impl {
range: &ops::RangeToInclusive<Self>,
rng: &mut R,
) -> Self {
let u = Uniform::new_inclusive(std::$typ::MIN, range.end);
let u = Uniform::new_inclusive($typ::MIN, range.end);
u.sample(rng)
}
}
Expand Down
2 changes: 2 additions & 0 deletions fake/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@
//! # }
//! ```
#[doc(hidden)]
pub use rand;
#[doc(hidden)]
pub use rand::Rng;

/// Generate default fake value for given type using [`Fake`].
Expand Down
2 changes: 1 addition & 1 deletion fake/src/locales/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub struct RegistrantRule {
pub registrant_len: i8,
}

#[cfg_attr(tool_attributes, rustfmt_skip)]
#[rustfmt::skip]
pub trait Data {
const LOREM_WORD: &'static [&'static str] = &["alias", "consequatur", "aut", "perferendis", "sit", "voluptatem", "accusantium", "doloremque", "aperiam", "eaque", "ipsa", "quae", "ab", "illo", "inventore", "veritatis", "et", "quasi", "architecto", "beatae", "vitae", "dicta", "sunt", "explicabo", "aspernatur", "aut", "odit", "aut", "fugit", "sed", "quia", "consequuntur", "magni", "dolores", "eos", "qui", "ratione", "voluptatem", "sequi", "nesciunt", "neque", "dolorem", "ipsum", "quia", "dolor", "sit", "amet", "consectetur", "adipisci", "velit", "sed", "quia", "non", "numquam", "eius", "modi", "tempora", "incidunt", "ut", "labore", "et", "dolore", "magnam", "aliquam", "quaerat", "voluptatem", "ut", "enim", "ad", "minima", "veniam", "quis", "nostrum", "exercitationem", "ullam", "corporis", "nemo", "enim", "ipsam", "voluptatem", "quia", "voluptas", "sit", "suscipit", "laboriosam", "nisi", "ut", "aliquid", "ex", "ea", "commodi", "consequatur", "quis", "autem", "vel", "eum", "iure", "reprehenderit", "qui", "in", "ea", "voluptate", "velit", "esse", "quam", "nihil", "molestiae", "et", "iusto", "odio", "dignissimos", "ducimus", "qui", "blanditiis", "praesentium", "laudantium", "totam", "rem", "voluptatum", "deleniti", "atque", "corrupti", "quos", "dolores", "et", "quas", "molestias", "excepturi", "sint", "occaecati", "cupiditate", "non", "provident", "sed", "ut", "perspiciatis", "unde", "omnis", "iste", "natus", "error", "similique", "sunt", "in", "culpa", "qui", "officia", "deserunt", "mollitia", "animi", "id", "est", "laborum", "et", "dolorum", "fuga", "et", "harum", "quidem", "rerum", "facilis", "est", "et", "expedita", "distinctio", "nam", "libero", "tempore", "cum", "soluta", "nobis", "est", "eligendi", "optio", "cumque", "nihil", "impedit", "quo", "porro", "quisquam", "est", "qui", "minus", "id", "quod", "maxime", "placeat", "facere", "possimus", "omnis", "voluptas", "assumenda", "est", "omnis", "dolor", "repellendus", "temporibus", "autem", "quibusdam", "et", "aut", "consequatur", "vel", "illum", "qui", "dolorem", "eum", "fugiat", "quo", "voluptas", "nulla", "pariatur", "at", "vero", "eos", "et", "accusamus", "officiis", "debitis", "aut", "rerum", "necessitatibus", "saepe", "eveniet", "ut", "et", "voluptates", "repudiandae", "sint", "et", "molestiae", "non", "recusandae", "itaque", "earum", "rerum", "hic", "tenetur", "a", "sapiente", "delectus", "ut", "aut", "reiciendis", "voluptatibus", "maiores", "doloribus", "asperiores", "repellat"];

Expand Down

0 comments on commit 23a753a

Please sign in to comment.