fn-decorator

The crate contains macros for implementing wrapper functions around member or static functions.

Decorator function examples

Decorating a function without parameters

```rust use fndecorator::usedecorator;

fn decorator(f: fn() -> i64) -> i64 { f() + 1 }

[use_decorator(decorator())]

fn get_1() -> i64 { 1 }

[test]

fn fnwithoutparamsdecorator() { let result = get1(); assert_eq!(result, 2); } ```

Decorating a function with parameters

```rust use fndecorator::usedecorator;

fn decorator(f: fn(x: i64) -> i64, x: i64) -> i64 { f(x) * 2 }

[use_decorator(decorator())]

fn double(x: i64) -> i64 { x * 2 }

[test]

fn decoratorwithoutparams() { let result = double(2); assert_eq!(result, 8); } ```

Using a decorator function that has a parameter

```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) }

[usedecorator(decorator("middle".tostring()))]

fn concat(left: String, right: String) -> String { left + &right }

[test]

fn decoratorwithparam() { let result = concat("left".into(), "right".into()); asserteq!(result, "leftmiddle_right"); } ```

Decorating a member function

```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 } }

[test]

fn implmemberdecorator() { let obj = MyStruct { x: 1 }; let result = obj.add(1); assert_eq!(result, 3); } ```

Decorating a static member function

```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 } }

[test]

fn implstaticmemberdecorator() { let result = MyStruct::double(2); asserteq!(result, 5); } ```

Decorating an async function

```rust use std::future::Future;

use fndecorator::usedecorator;

async fn decorator>(f: fn(x: i64) -> FutureType, x: i64) -> i64 { f(x).await * 2 }

[use_decorator(decorator())]

async fn double(x: i64) -> i64 { x * 2 }

[tokio::test]

async fn asyncdecorator() { let result = double(2).await; asserteq!(result, 8); } ```

Decorating an async member function

```rust use std::future::Future;

use fndecorator::useimpl_decorator;

async fn decorator<'a, FutureType: Future>( f: fn(&'a MyStruct, y: i64) -> FutureType, receiver: &'a MyStruct, y: i64, ) -> i64 { f(receiver, y).await + 1 }

struct MyStruct { x: i64, }

impl MyStruct { #[useimpldecorator(decorator())] async fn add(&self, y: i64) -> i64 { self.x + y } }

[tokio::test]

async fn asyncimplmemberdecorator() { let obj = MyStruct { x: 1 }; let result = obj.add(1).await; asserteq!(result, 3); } ```

Decorating an async static member function

```rust use std::future::Future;

use fndecorator::useimpl_decorator;

async fn decorator>(f: fn(y: i64) -> FutureType, x: i64) -> i64 { f(x).await + 1 }

struct MyStruct;

impl MyStruct { #[useimpldecorator(decorator())] async fn double(x: i64) -> i64 { x * 2 } }

[tokio::test]

async fn asyncimplmemberdecorator() { let result = MyStruct::double(2).await; asserteq!(result, 5); } ```