Crate function_compose
source ·Expand description
Crate function-compose
provides utilities for composing functions and way to inject arguments to functions
The composeable functions should return rust Result type with FnError as Err type
§Usage
use function_compose::composeable;
#[composeable()]
pub fn add_10(a: i32) -> Result<i32, FnError> {
Ok(a + 10)
}
§The async function should return BoxFuture and the error type should be FnError.
use function_compose::composeable;
use futures::{future::BoxFuture, FutureExt};
#[composeable()]
pub fn add_async(a: i32, b: i32) -> BoxFuture<'static, Result<i32, FnError>> {
async move {
let r = a + b;
Ok(r)
}.boxed()
}
§Composing async and sync functions usage
ⓘ
use function_compose::compose;
use fn_macros::composeable;
use futures::{future::BoxFuture, FutureExt};
#[composeable()]
pub fn add_async(a: i32, b: i32) -> BoxFuture<'static, Result<i32, FnError>> {
async move {
let r = a + b;
Ok(r)
}.boxed()
}
#[composeable()]
pub fn add_10(a: i32) -> Result<i32, FnError> {
Ok(a + 10)
}
async fn test(){
let result = compose!(add_async.provide(10) -> add_100 -> with_args(10)).await;
assert_eq!(210, result.unwrap());
}
§Function argument injection usage
use function_compose::composeable;
use futures::{future::BoxFuture, FutureExt};
#[composeable()]
pub fn add_3_arg_async(a: i32,b: i32, c:i32) -> BoxFuture<'static, Result<i32, FnError>>{
async move{
let r = a + b + c;
Ok(r)
}.boxed()
}
§Example of multiple injection to async function
ⓘ
use crate::compose;
let result = compose!(add_3_arg_async.provide(100).provide(200) -> add_10 -> with_args(10)).await;
assert_eq!(220, result.unwrap());
Modules§
Macros§
- This macros makes it possible to concatenate identifiers at compile time and use them as normal. It’s an extension/replacement of
std::concat_idents
, since in comprassion to the std-solution, the idents here can be used everywhere.
Structs§
Enums§
Traits§
- trait Then allows you to compose functions. Type param A represents the function arg of Self
Functions§
- Function to box FnOnce sync function with 1 aguments and coerce it to BoxedAsyncFn1
- Function to box FnOnce sync function with 2 aguments and coerce it to BoxedAsyncFn2
- Function to box FnOnce sync function with 3 aguments and coerce it to BoxedAsyncFn3
- Function to box FnOnce sync function with 4 aguments and coerce it to BoxedAsyncFn4
- Function to box FnOnce sync function with 5 aguments and coerce it to BoxedAsyncFn5
- Function to box FnOnce sync function with 6 aguments and coerce it to BoxedAsyncFn6
- Function to box FnOnce sync function with 7 aguments and coerce it to BoxedAsyncFn7
- Function to box FnOnce sync function with 8 aguments and coerce it to BoxedAsyncFn8
- Function to box FnOnce sync function with 1 aguments and coerce it to BoxedFn1
- Function to box FnOnce sync function with 2 aguments and coerce it to BoxedFn2
- Function to box FnOnce sync function with 3 aguments and coerce it to BoxedFn3
- Function to box FnOnce sync function with 4 aguments and coerce it to BoxedFn4
- Function to box FnOnce sync function with 5 aguments and coerce it to BoxedFn5
- Function to box FnOnce sync function with 6 aguments and coerce it to BoxedFn6
- Function to box FnOnce sync function with 7 aguments and coerce it to BoxedFn7
- Function to box FnOnce sync function with 8 aguments and coerce it to BoxedFn8
- dependency injection function provider_async_f2 for injecting the last argument of a given async function
- dependency injection function provider_async_f3 for injecting the last argument of a given async function
- dependency injection function provider_async_f4 for injecting the last argument of a given async function
- dependency injection function provider_async_f5 for injecting the last argument of a given async function
- dependency injection function provider_async_f6 for injecting the last argument of a given async function
- dependency injection function provider_async_f7 for injecting the last argument of a given async function
- dependency injection function provider_async_f8 for injecting the last argument of a given async function
- dependency injection function provide_f2 for injecting the last argument of a given sync function
- dependency injection function provide_f3 for injecting the last argument of a given sync function
- dependency injection function provide_f4 for injecting the last argument of a given sync function
- dependency injection function provide_f5 for injecting the last argument of a given sync function
- dependency injection function provide_f6 for injecting the last argument of a given sync function
- dependency injection function provide_f7 for injecting the last argument of a given sync function
- dependency injection function provide_f8 for injecting the last argument of a given sync function
Type Aliases§
- Type alias BoxedAsyncFn1 for Boxed FnOnce async function1 arguments
- Type alias BoxedAsyncFn2 for Boxed FnOnce async function2 arguments
- Type alias BoxedAsyncFn3 for Boxed FnOnce async function3 arguments
- Type alias BoxedAsyncFn4 for Boxed FnOnce async function4 arguments
- Type alias BoxedAsyncFn5 for Boxed FnOnce async function5 arguments
- Type alias BoxedAsyncFn6 for Boxed FnOnce async function6 arguments
- Type alias BoxedAsyncFn7 for Boxed FnOnce async function7 arguments
- Type alias BoxedAsyncFn8 for Boxed FnOnce async function8 arguments
- Type alias BoxedFn1 for Boxed FnOnce sync function with 1 arguments
- Type alias BoxedFn2 for Boxed FnOnce sync function with 2 arguments
- Type alias BoxedFn3 for Boxed FnOnce sync function with 3 arguments
- Type alias BoxedFn4 for Boxed FnOnce sync function with 4 arguments
- Type alias BoxedFn5 for Boxed FnOnce sync function with 5 arguments
- Type alias BoxedFn6 for Boxed FnOnce sync function with 6 arguments
- Type alias BoxedFn7 for Boxed FnOnce sync function with 7 arguments
- Type alias BoxedFn8 for Boxed FnOnce sync function with 8 arguments