function-compose
function-compose is a function composition library that allows composition of async and sync functions
There are two macros required for the fn-composer function composition to work.
Proc attribute Macro - composeable
Declarative Macro - compose!
Proc attribute macro composeable is add to any function that can be composed. e.g below is simple function to add 10 to a given number.
Composeable macro documentation
Below snippet is an async example. The async function should return BoxFuture.
Below is example of async multiplication and add_100 to a number asynchronously
Below is example of async addition of 3 values
Below is example of async addition of 3 values with last parameter accepting reference
It is possible to compose single arg sync function with two arg async function and vice versa.
Below are the examples of how to compose async functions
let result = compose!;
let result = compose!;
Below are examples of composing async and sync functions
let result = compose!.await;
assert_eq!;
let result = compose!.await;
assert_eq!;
It is possible to inject the seconds args of async functions using .provide
method.
This could be useful for injecting database connection or other external service interaction.
See the example below
// Check the '.provide' method below of injecting second args to add_async function
let result = compose!.await;
assert_eq!;
Example of multiple injection to async function
let result = compose!.await;
assert_eq!;
Example of multiple injection to async function in the second position
let result = compose!.await;
assert_eq!;
Example of multiple injection to shared reference to async function
let one = &1;
let result = compose!.await;
assert_eq!;
Retry in Fn Composer
Composeable macro supports retrying a function at specified interval in case of Error returned by the function. This could be useful when trying make a database call or connect to network endpoint. Make sure to install https://docs.rs/retry/latest/retry/ before proceeding with retry feature.
Retry mechanism is implemented as part of composeable procedureal macro. Below is example of add_10 function configured to be retried 2 times after initial failure.
use *;
Retry can be applied to both sync and async functions.
for async functions, all arguments to the function must be either shared reference or exclusive reference.
Below is example of async function with retry.
Apart from fixed duration retries, it is possible to configure with exponential delay. Refer to retry documentation for all available delay options https://docs.rs/retry/latest/retry/all.html