Skip to content

Commit 2c6d751

Browse files
authored
bugfix: Fix macro for defining the extension
With previous macro definition, it was not allowed to use `self` in the method body because it had :block fragment specifier and rustc complained that "`self` value is a keyword only available in methods with a `self` parameter". This changes the macro to be like a "delegation" to a function with the actual implementation. Using :path fragment specifier so that we can refer to functions in modules.
1 parent 38a702c commit 2c6d751

File tree

1 file changed

+23
-11
lines changed

1 file changed

+23
-11
lines changed

src/lib.rs

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ impl BaseRng for GlobalRng {
4040
macro_rules! define_ext {
4141
($(
4242
$(#[$meta:meta])*
43-
fn $name:ident(&mut self, $($argname:ident:$argty:ty),*) -> $ret:ty $bl:block
43+
fn $name:ident(&mut self, $($argname:ident:$argty:ty),*) -> $ret:ty => $imp:path;
4444
)*) => {
4545
/// Extra methods for [`fastrand::Rng`].
4646
pub trait RngExt {
@@ -53,7 +53,9 @@ macro_rules! define_ext {
5353
impl RngExt for Rng {
5454
$(
5555
$(#[$meta])*
56-
fn $name(&mut self, $($argname: $argty),*) -> $ret $bl
56+
fn $name(&mut self, $($argname: $argty),*) -> $ret {
57+
$imp(self, $($argname),*)
58+
}
5759
)*
5860
}
5961

@@ -63,7 +65,9 @@ macro_rules! define_ext {
6365
pub fn $name($($argname:$argty),*) -> $ret {
6466
impl GlobalRng {
6567
$(#[$meta])*
66-
fn $name(&mut self, $($argname:$argty),*) -> $ret $bl
68+
fn $name(&mut self, $($argname:$argty),*) -> $ret {
69+
$imp(self, $($argname),*)
70+
}
6771
}
6872

6973
GlobalRng::$name(&mut GlobalRng, $($argname),*)
@@ -74,20 +78,28 @@ macro_rules! define_ext {
7478

7579
define_ext! {
7680
/// Generate a 32-bit floating point number in the specified range.
77-
fn f32_range(&mut self, range: impl RangeBounds<f32>) -> f32 {
78-
let _ = range;
79-
todo!()
80-
}
81+
fn f32_range(&mut self, range: impl RangeBounds<f32>) -> f32 => f32_range_impl;
8182

8283
/// Generate a 64-bit floating point number in the specified range.
83-
fn f64_range(&mut self, range: impl RangeBounds<f64>) -> f64 {
84-
let _ = range;
85-
todo!()
86-
}
84+
fn f64_range(&mut self, range: impl RangeBounds<f64>) -> f64 => f64_range_impl;
8785
}
8886

8987
mod __private {
9088
#[doc(hidden)]
9189
pub trait Sealed {}
9290
impl Sealed for fastrand::Rng {}
9391
}
92+
93+
#[inline]
94+
fn f32_range_impl(rng: &mut impl BaseRng, range: impl RangeBounds<f32>) -> f32 {
95+
let _ = rng;
96+
let _ = range;
97+
todo!()
98+
}
99+
100+
#[inline]
101+
fn f64_range_impl(rng: &mut impl BaseRng, range: impl RangeBounds<f64>) -> f64 {
102+
let _ = rng;
103+
let _ = range;
104+
todo!()
105+
}

0 commit comments

Comments
 (0)