1 unstable release
0.0.1 | Oct 29, 2024 |
---|
#10 in #ewe-platform
Used in ewe_routing
6KB
AsyncUtils
A crates for the ewe_platform package.
Notice
This is a work in progress crate, please do not depend on for now
lib.rs
:
Async TryFrom
/TryInto
traits.
Why
In async-std we created async versions of FromStream
, IntoStream
, and
Iterator::collect
. These traits represent conversions from one type to
another. But the canonical way of performing this conversion is through the
TryFrom
and TryInto
traits.
For example when deserializing some MyBody
from a Request
, you will want
to declare a TryFrom<Request> for MyBody
which consumes the bytes in the
request and tries to create the body. This operation is fallible, and when
writing async code also needs to be async.
This crate provides traits for that, through the
async_trait
crate. This is an experiment, but we'll likely want to extend async-std
with this at some point too.
Examples
use ewe_async_utils::{async_trait, TryFrom};
struct GreaterThanZero(i32);
#[async_trait]
impl TryFrom<i32> for GreaterThanZero {
type Error = &'static str;
async fn try_from(value: i32) -> Result<Self, Self::Error> {
// pretend we're actually doing async IO here instead.
if value <= 0 {
Err("GreaterThanZero only accepts value superior than zero!")
} else {
Ok(GreaterThanZero(value))
}
}
}
Dependencies
~235–680KB
~16K SLoC