fn-decorator

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

Decorator function examples

Decorating a function that has no 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 that has 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 parameterized decorator function

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

Hiding parameters of an async function from a decorator

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

use fndecorator::usedecorator;

async fn decorator>( middle: String, f: impl FnOnce(String) -> FutureType, right: String, ) -> String { let right = middle + &right; f(right).await }

[usedecorator(decorator("middle".tostring()), hide_parameters = [left])]

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

[tokio::test]

async fn hidingparamsofasyncfndecorator() { let result = concat("left".into(), "right".into()).await; asserteq!(result, "leftmiddleright"); } ```

Hiding parameters of an async member function from a decorator

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

use fndecorator::useimpl_decorator;

async fn decorator<'a, FutureType: Future>( middle: String, f: impl FnOnce(&'a mut MyStruct) -> FutureType, receiver: &'a mut MyStruct, ) -> &'a String { receiver.left.push_str(&middle); f(receiver).await }

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

[tokio::test]

async fn hidingparamsofasyncimplmemberdecorator() { let mut obj = MyStruct { left: "left".into(), }; let result = obj.concat("right".into()).await; asserteq!(result, "leftmiddle_right"); } ```

Hiding parameters of a function from a decorator

```rust use fndecorator::usedecorator;

fn decorator(middle: String, f: impl FnOnce(String) -> String, right: String) -> String { let right = middle + &right; f(right) }

[usedecorator(decorator("middle".tostring()), hide_parameters = [left])]

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

[test]

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

Hiding parameters of a member function from a decorator

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

[test]

fn hidingparamsofimplmemberdecorator() { let obj = MyStruct { left: "left".into(), }; let result = obj.concat("right".into()); asserteq!(result, "leftmiddleright"); } ```

Hiding self of a member function from a decorator

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

[test]

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

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