fix: fixes to surface level mandate issues (#180)

This commit is contained in:
Nishant Joshi
2022-12-22 12:51:52 +05:30
committed by GitHub
parent 79a4a3addf
commit dd7e093fe3
16 changed files with 202 additions and 82 deletions

View File

@ -8,8 +8,10 @@ readme = "README.md"
license = "Apache-2.0"
[dependencies]
async-trait = "0.1.59"
bytes = "1.3.0"
error-stack = "0.2.4"
futures = "0.3.25"
hex = "0.4.3"
nanoid = "0.4.0"
once_cell = "1.16.0"

View File

@ -277,3 +277,79 @@ impl<T> StringExt<T> for String {
.attach_printable_lazy(|| format!("Unable to parse {type_name} from string"))
}
}
///
/// Extending functionalities of Wrapper types for idiomatic
///
#[async_trait::async_trait]
pub trait AsyncExt<A, B> {
/// Output type of the map function
type WrappedSelf<T>;
///
/// Extending map by allowing functions which are async
///
async fn async_map<F, Fut>(self, func: F) -> Self::WrappedSelf<B>
where
F: FnOnce(A) -> Fut + Send,
Fut: futures::Future<Output = B> + Send;
///
/// Extending the `and_then` by allowing functions which are async
///
async fn async_and_then<F, Fut>(self, func: F) -> Self::WrappedSelf<B>
where
F: FnOnce(A) -> Fut + Send,
Fut: futures::Future<Output = Self::WrappedSelf<B>> + Send;
}
#[async_trait::async_trait]
impl<A: std::marker::Send, B, E: std::marker::Send> AsyncExt<A, B> for Result<A, E> {
type WrappedSelf<T> = Result<T, E>;
async fn async_and_then<F, Fut>(self, func: F) -> Self::WrappedSelf<B>
where
F: FnOnce(A) -> Fut + Send,
Fut: futures::Future<Output = Self::WrappedSelf<B>> + Send,
{
match self {
Ok(a) => func(a).await,
Err(err) => Err(err),
}
}
async fn async_map<F, Fut>(self, func: F) -> Self::WrappedSelf<B>
where
F: FnOnce(A) -> Fut + Send,
Fut: futures::Future<Output = B> + Send,
{
match self {
Ok(a) => Ok(func(a).await),
Err(err) => Err(err),
}
}
}
#[async_trait::async_trait]
impl<A: std::marker::Send, B> AsyncExt<A, B> for Option<A> {
type WrappedSelf<T> = Option<T>;
async fn async_and_then<F, Fut>(self, func: F) -> Self::WrappedSelf<B>
where
F: FnOnce(A) -> Fut + Send,
Fut: futures::Future<Output = Self::WrappedSelf<B>> + Send,
{
match self {
Some(a) => func(a).await,
None => None,
}
}
async fn async_map<F, Fut>(self, func: F) -> Self::WrappedSelf<B>
where
F: FnOnce(A) -> Fut + Send,
Fut: futures::Future<Output = B> + Send,
{
match self {
Some(a) => Some(func(a).await),
None => None,
}
}
}