The crate contains macros for implementing wrapper functions around member or static functions.
use_decorator
Decorator function
rust
fn decorator(f: fn() -> i64) -> i64 {
f() + 1
}
Decorating a function ```rust
fn get_1() -> i64 { 1 } ```
When get_1()
is called, then decorator(get_1)
is executed instead. The decorator function can decide whether it calls the received function or not.
There is also a use_impl_decorator
macro that works in impl
blocks.
Both macros can have the same parameters:
* Decorator function call that should be executed. This can contain parameters. See examples for exact usage!
* hide_parameters = [...]
: if the decorator function signature does not match the decorated, then this list can be used to hide some parameters from the decorator function.
* debug
: when this parameter is given, then the code will generate a compile error with the generated source code. This is useful for debugging purposes.
```rust use fndecorator::usedecorator;
fn decorator(f: fn() -> i64) -> i64 { f() + 1 }
fn get_1() -> i64 { 1 }
fn fnwithoutparamsdecorator() { let result = get1(); assert_eq!(result, 2); } ```
```rust use fndecorator::usedecorator;
fn decorator(f: fn(x: i64) -> i64, x: i64) -> i64 { f(x) * 2 }
fn double(x: i64) -> i64 { x * 2 }
fn decoratorwithoutparams() { let result = double(2); assert_eq!(result, 8); } ```
```rust use fndecorator::usedecorator;
fn decorator( middle: String, f: fn(String, String) -> String, left: String, right: String, ) -> String { let left = left + &middle; f(left, right) }
fn concat(left: String, right: String) -> String { left + &right }
fn decoratorwithparam() { let result = concat("left".into(), "right".into()); asserteq!(result, "leftmiddle_right"); } ```
Please be aware that the tests does not contain this code, because it produces a compile time error.
```rust use fndecorator::usedecorator;
fn decorator(f: fn() -> i64) -> i64 { f() + 1 }
fn get_1() -> i64 { 1 }
fn fnwithoutparamsdecorator() { let result = get1(); assert_eq!(result, 2); } ```
```rust use fndecorator::useimpl_decorator;
fn decorator(f: fn(&MyStruct, y: i64) -> i64, receiver: &MyStruct, y: i64) -> i64 { f(receiver, y) + 1 }
struct MyStruct { x: i64, }
impl MyStruct { #[useimpldecorator(decorator())] fn add(&self, y: i64) -> i64 { self.x + y } }
fn implmemberdecorator() { let obj = MyStruct { x: 1 }; let result = obj.add(1); assert_eq!(result, 3); } ```
```rust use fndecorator::useimpl_decorator;
fn decorator(f: fn(x: i64) -> i64, x: i64) -> i64 { f(x) + 1 }
struct MyStruct;
impl MyStruct { #[useimpldecorator(decorator())] fn double(x: i64) -> i64 { x * 2 } }
fn implstaticmemberdecorator() { let result = MyStruct::double(2); asserteq!(result, 5); } ```
```rust use std::future::Future;
use fndecorator::usedecorator;
async fn decorator
async fn double(x: i64) -> i64 { x * 2 }
async fn asyncdecorator() { let result = double(2).await; asserteq!(result, 8); } ```
```rust use std::future::Future;
use fndecorator::usedecorator;
async fn decorator
async fn concat(left: String, right: String) -> String { left + &right }
async fn hidingparamsofasyncfndecorator() { let result = concat("left".into(), "right".into()).await; asserteq!(result, "leftmiddleright"); } ```
```rust use std::future::Future;
use fndecorator::useimpl_decorator;
async fn decorator<'a, FutureType: Future
struct MyStruct { left: String, }
impl MyStruct { #[useimpldecorator(decorator("middle".tostring()), hideparameters = [right])] async fn concat(&mut self, right: String) -> &String { self.left.push_str(&right); &self.left } }
async fn hidingparamsofasyncimplmemberdecorator() { let mut obj = MyStruct { left: "left".into(), }; let result = obj.concat("right".into()).await; asserteq!(result, "leftmiddle_right"); } ```
```rust use fndecorator::usedecorator;
fn decorator(middle: String, f: impl FnOnce(String) -> String, right: String) -> String { let right = middle + &right; f(right) }
fn concat(left: String, right: String) -> String { left + &right }
fn hidingparamsoffndecorator() { let result = concat("left".into(), "right".into()); asserteq!(result, "leftmiddle_right"); } ```
```rust use fndecorator::useimpl_decorator;
fn decorator(middle: String, f: impl FnOnce(&MyStruct) -> String, receiver: &MyStruct) -> String { let newstruct = MyStruct { left: receiver.left.clone() + &middle, }; f(&newstruct) }
struct MyStruct { left: String, }
impl MyStruct { #[useimpldecorator(decorator("middle".tostring()), hideparameters = [right])] fn concat(&self, right: String) -> String { self.left.clone() + &right } }
fn hidingparamsofimplmemberdecorator() { let obj = MyStruct { left: "left".into(), }; let result = obj.concat("right".into()); asserteq!(result, "leftmiddleright"); } ```
```rust use fndecorator::useimpl_decorator;
fn decorator(middle: String, f: impl FnOnce(String) -> String, right: String) -> String { let right = middle + &right; f(right) }
struct MyStruct { left: String, }
impl MyStruct { #[useimpldecorator(decorator("middle".tostring()), hideparameters = [self])] fn concat(&self, right: String) -> String { self.left.clone() + &right } }
fn hidingselfparaminimplmemberdecorator() { let obj = MyStruct { left: "left".into(), }; let result = obj.concat("right".into()); asserteq!(result, "leftmiddle_right"); } ```
```rust use std::future::Future;
use fndecorator::useimpl_decorator;
async fn decorator<'a, FutureType: Future
struct MyStruct { x: i64, }
impl MyStruct { #[useimpldecorator(decorator())] async fn add(&self, y: i64) -> i64 { self.x + y } }
async fn asyncimplmemberdecorator() { let obj = MyStruct { x: 1 }; let result = obj.add(1).await; asserteq!(result, 3); } ```
```rust use std::future::Future;
use fndecorator::useimpl_decorator;
async fn decorator
struct MyStruct;
impl MyStruct { #[useimpldecorator(decorator())] async fn double(x: i64) -> i64 { x * 2 } }
async fn asyncimplmemberdecorator() { let result = MyStruct::double(2).await; asserteq!(result, 5); } ```