From 30586d8ae5c73f7978501176c1e7ff9a6b59ef1d Mon Sep 17 00:00:00 2001 From: Kevin Glasson Date: Tue, 3 Jan 2023 16:41:35 +0800 Subject: [PATCH] feat: remove 'static lifetime from NumberWithFormat Expand the def_fakers macro to optionally capture the lifetime of the function so that NumberWithFormat could take an argument with a shorter-than-'static lifetime. --- README.md | 2 +- fake/README.md | 2 +- fake/examples/fakers.rs | 6 ++++++ fake/src/faker/impls/number.rs | 2 +- fake/src/faker/mod.rs | 18 +++++++++--------- 5 files changed, 18 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 37e31ea..99c7ea0 100644 --- a/README.md +++ b/README.md @@ -135,7 +135,7 @@ NameWithTitle(); ```rust Digit(); -NumberWithFormat(fmt: &'static str); +NumberWithFormat<'a>(fmt: &'a str); ``` ## Boolean diff --git a/fake/README.md b/fake/README.md index bac8335..3bafa49 100644 --- a/fake/README.md +++ b/fake/README.md @@ -135,7 +135,7 @@ NameWithTitle(); ```rust Digit(); -NumberWithFormat(fmt: &'static str); +NumberWithFormat<'a>(fmt: &'a str); ``` ## Boolean diff --git a/fake/examples/fakers.rs b/fake/examples/fakers.rs index da106f8..a9e5c4c 100644 --- a/fake/examples/fakers.rs +++ b/fake/examples/fakers.rs @@ -1,3 +1,4 @@ +use fake::faker::address::en::StreetName; use fake::locales::{EN, FR_FR, ZH_CN, ZH_TW}; use fake::Fake; @@ -267,6 +268,11 @@ fn number_faker() { let val: String = NumberWithFormat(EN, "FLAT 0# ^#/F").fake(); println!("{:?}", val); + + // non-'static string + let fmt = String::from("FLAT 0# ^#/F"); + let val: String = NumberWithFormat(EN, &fmt).fake(); + println!("{:?}", val); } fn phone_number_faker() { diff --git a/fake/src/faker/impls/number.rs b/fake/src/faker/impls/number.rs index 20ce1df..30d4249 100644 --- a/fake/src/faker/impls/number.rs +++ b/fake/src/faker/impls/number.rs @@ -18,7 +18,7 @@ impl Dummy> for &str { } } -impl Dummy> for String { +impl Dummy> for String { fn dummy_with_rng(c: &NumberWithFormat, rng: &mut R) -> Self { numerify_sym(c.1, rng) } diff --git a/fake/src/faker/mod.rs b/fake/src/faker/mod.rs index 8c38d7d..ca24c15 100644 --- a/fake/src/faker/mod.rs +++ b/fake/src/faker/mod.rs @@ -15,30 +15,30 @@ fn numerify_sym(string: &str, rng: &mut R) -> String { } macro_rules! def_fakers { - (@m $locale_m:ident=>$locale_s:ident { $($name:ident($($arg:ident : $typ:ty),*);)+}) => { + (@m $locale_m:ident=>$locale_s:ident { $($name:ident$(< $($lts:lifetime),* >)?($($arg:ident : $typ:ty),*);)+}) => { pub mod $locale_m { use super::raw; use crate::locales::$locale_s; $( #[inline] #[allow(non_snake_case)] - pub fn $name($($arg:$typ),*) -> raw::$name<$locale_s> { + pub fn $name$(< $($lts),* >)?($($arg:$typ),*) -> raw::$name<$locale_s> { raw::$name($locale_s, $($arg),*) } )+ } }; - ($($name:ident($($arg:ident : $typ:ty),*);)+) => { + ($($name:ident$(< $($lts:lifetime),* >)?($($arg:ident : $typ:ty),*);)+) => { pub mod raw { $( - pub struct $name(pub L, $(pub $typ),*); + pub struct $name<$( $($lts),* , )?L>(pub L, $(pub $typ),*); )+ } - def_fakers!(@m en=>EN {$($name($($arg:$typ),*);)+}); - def_fakers!(@m fr_fr=>FR_FR {$($name($($arg:$typ),*);)+}); - def_fakers!(@m zh_tw=>ZH_TW {$($name($($arg:$typ),*);)+}); - def_fakers!(@m zh_cn=>ZH_CN {$($name($($arg:$typ),*);)+}); + def_fakers!(@m en=>EN {$($name$(< $($lts),* >)?($($arg:$typ),*);)+}); + def_fakers!(@m fr_fr=>FR_FR {$($name$(< $($lts),* >)?($($arg:$typ),*);)+}); + def_fakers!(@m zh_tw=>ZH_TW {$($name$(< $($lts),* >)?($($arg:$typ),*);)+}); + def_fakers!(@m zh_cn=>ZH_CN {$($name$(< $($lts),* >)?($($arg:$typ),*);)+}); }; } @@ -200,7 +200,7 @@ pub mod name { pub mod number { def_fakers! { Digit(); - NumberWithFormat(fmt: &'static str); + NumberWithFormat<'a>(fmt: &'a str); } }