| Crates.io | fnaop |
| lib.rs | fnaop |
| version | 0.1.0 |
| created_at | 2024-07-06 18:52:07.380882+00 |
| updated_at | 2024-07-06 18:52:07.380882+00 |
| description | fnaop is a lightweight and flexible Rust library designed to bring Aspect-Oriented Programming (AOP) to your Rust functions. By using fnaop, you can easily add pre and post function logic without modifying the core functionality of your functions, enabling cleaner and more maintainable code. |
| homepage | https://github.com/photowey/fnaop |
| repository | https://github.com/photowey/fnaop |
| max_upload_size | |
| id | 1294290 |
| size | 31,288 |
fnaopfnaop is a lightweight and flexible Rust library designed to bring Aspect-Oriented Programming (AOP) to your Rust
functions. By using fnaop, you can easily add pre and post function logic without modifying the core functionality
of your functions, enabling cleaner and more maintainable code.
UsageAdd this to your Cargo.toml:
[dependencies]
fnaop = "0.1"
APIsAspect:
An attribute macro for applying Aspect-Oriented Programming (AOP) to functions. The Aspect macro allows you to
specify before and after functions that will be called before and after the target function respectively. This is
useful for encapsulating cross-cutting concerns such as logging, metrics, or other side effects.
NoarmalOrdinary function, adapted to 0 or more ordinary parameters.
#[Aspect(before = "before_fn")]
pub fn say_hello(x: i64) {
println!("Hello:say_hello, {}", x);
}
#[Aspect(after = "after_fn")]
pub fn say_hello(x: i64) {
println!("Hello:say_hello, {}", x);
}
#[Aspect(before = "before_fn", after = "after_fn")]
pub fn say_hello(x: i64) {
println!("Hello:say_hello, {}", x);
}
// ----------------------------------------------------------------
#[Aspect(before = "before_fn_empty", after = "before_fn_empty")]
pub fn say_hello_empty() {
println!("Hello:say_hello_empty");
}
LifetimeFunctions with lifetime parameters.
#[Aspect(before = "before::struct_before_fn_lifetime", after = "after::struct_after_fn_lifetime")]
pub fn say_hello_struct_lifetime<'a>(ctx: LifetimeHelloContext<'a>) {
println!("struct::Hello, {} {}", ctx.x, ctx.y);
}
Return valueFunctions with return values.
#[Aspect(before = "before::struct_before_fn_lifetime", after = "after::struct_after_fn_lifetime")]
pub fn say_hello_struct_lifetime_with_return<'a>(ctx: LifetimeHelloContext<'a>) -> i64 {
println!("struct::Hello, {} {}", ctx.x, ctx.y);
*ctx.x
}
Others…
// @see integration_tests.rs