Expand description
This crate aims at reducing the coloring of rust functions, by simplifying the process of
creating functions which functions both as async and as blocking functions. This is performed
with the Desaturated
trait, which implements IntoFuture
and Blocking
, depending on
which feature flags are set.
The idea is that a desaturated function can be created by combining the async
color with the
blocking color, as such:
use desaturate::{Desaturated, IntoDesaturated};
fn do_something() -> impl Desaturated<()> {
async {
// Do something here
}.desaturate(|| {
// Do the same thing here
})
}
Now, this doesn’t reduce code duplication, per se, but it can enable it, especially when used
with the desaturate
macro. This allows us to create functions like this:
use desaturate::{desaturate, Blocking};
#[desaturate]
async fn do_the_thing(arg: i32) -> i32 {
arg * 2
}
#[desaturate]
async fn do_something(arg1: i32, arg2: i32) -> i32 {
do_the_thing(arg1).await + do_the_thing(arg2).await
}
fn main() {
let result = do_something(5, 10).call();
println!("I got to play with the async functions, and got {result}");
}
This also takes care of lifetimes, so you can make functions which track (or ignore) lifetimes.
use desaturate::{desaturate, Blocking};
#[desaturate]
async fn add_1(i: &mut i32) -> i32 {
let old = *i;
*i += 1;
old
}
fn main() {
let mut value = 5;
println!("Counting: {}", add_1(&mut value).call());
println!("Counting: {}", add_1(&mut value).call());
assert_eq!(value, 7);
}
It can also be used on member functions, as such:
use desaturate::{desaturate, Blocking};
struct MyType<'a> {
inner: i32,
pointer: &'a i32,
}
impl<'a> MyType<'a> {
#[desaturate(lifetime = "'a")]
async fn get_inner_mut(&mut self) -> &mut i32 {
&mut self.inner
}
#[desaturate]
async fn get_pointer(&self) -> &'a i32 {
self.pointer
}
}
Modules§
- boxed
std
- Recursion
Traits§
- Blocking
- Desaturated
- This trait is dynamic based on feature flags. For this reason, it’s not allowed to be directly implemented, as this would break the additive features guideline.
- Into
Desaturated - Into
Desaturated With
Attribute Macros§
- desaturate
macros
- This macro will try to automatic add the trait
Desaturated
to a function signature.