mirror of
https://github.com/juspay/hyperswitch.git
synced 2025-11-02 04:04:43 +08:00
fix: fixes to surface level mandate issues (#180)
This commit is contained in:
@ -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"
|
||||
|
||||
@ -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,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user