2 releases
0.1.1 | Feb 5, 2024 |
---|---|
0.1.0 | Feb 4, 2024 |
#310 in Procedural macros
31 downloads per month
13KB
232 lines
Functional trait
Description
A simple macro that inspired by java's functional interface.
the macro impls a trait for Fn
, FnMut
or FnOnce
when the trait:
-
contains one and only one method
-
the method has a receiver, and the receiver is
&self
,&mut self
orself
-
has no generic types in the trait or the method (may change in the future versions)
-
has no generic lifetime in the trait (may change in the future versions)
-
has no super trait (may change in the future versions)
-
is not unsafe
-
have no unsafe method (may change in the future versions)
Example
use functional_trait::functional_trait;
#[functional_trait]
trait A {
fn a(&self, i: i32, j: i32) -> i32;
}
#[functional_trait]
trait B {
fn b(&mut self, i: i32, j: i32) -> i32;
}
#[functional_trait]
trait C {
fn c(self, i: i32, j: i32) -> i32;
}
#[functional_trait]
trait D {
fn d<'c>(&self, b: &'c i32) -> &'c i32;
}
fn main() {
let f = |a, b| a + b + 10;
dbg!(f.a(1, 2));
let mut i = 0;
let mut f = |a, b| {
i += 1;
a + b + i
};
dbg!(f.b(1, 2));
let s = String::new();
let f = |a, b| {
drop(s);
a + b + i
};
dbg!(f.c(1, 2));
let f = {
fn f(a: &i32) -> &i32 {
a
}
f
};
f.d(&1);
}
Dependencies
~340–790KB
~19K SLoC