This is an automated email from the ASF dual-hosted git repository. xuanwo pushed a commit to branch try-into-future in repository https://gitbox.apache.org/repos/asf/opendal.git
commit 4479a0de4010d16a6adab678fe8f2dee02889c01 Author: Xuanwo <[email protected]> AuthorDate: Fri Nov 24 09:30:35 2023 +0800 Save work Signed-off-by: Xuanwo <[email protected]> --- core/src/types/operator/operator.rs | 21 ++++------ core/src/types/operator/operator_futures.rs | 65 +++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+), 12 deletions(-) diff --git a/core/src/types/operator/operator.rs b/core/src/types/operator/operator.rs index c0f08d515f..b209ed506a 100644 --- a/core/src/types/operator/operator.rs +++ b/core/src/types/operator/operator.rs @@ -15,6 +15,7 @@ // specific language governing permissions and limitations // under the License. +use std::future::Future; use std::time::Duration; use bytes::Buf; @@ -280,22 +281,18 @@ impl Operator { /// # Ok(()) /// # } /// ``` - pub fn stat_with(&self, path: &str) -> FutureStat { + pub fn stat_with(&self, path: &str) -> FutureStatX<impl Future<Output = Result<Metadata>>> { let path = normalize_path(path); - let fut = FutureStat(OperatorFuture::new( - self.inner().clone(), + let fut = FutureStatX(OperatorFutureX { + acc: self.inner().clone(), path, - OpStat::default(), - |inner, path, args| { - let fut = async move { - let rp = inner.stat(&path, args).await?; - Ok(rp.into_metadata()) - }; - - Box::pin(fut) + args: OpStat::default(), + f: |inner, path, args| async move { + let rp = inner.stat(&path, args).await?; + Ok(rp.into_metadata()) }, - )); + }); fut } diff --git a/core/src/types/operator/operator_futures.rs b/core/src/types/operator/operator_futures.rs index 58dd9e74cb..d4e496711c 100644 --- a/core/src/types/operator/operator_futures.rs +++ b/core/src/types/operator/operator_futures.rs @@ -19,6 +19,7 @@ //! //! By using futures, users can add more options for operation. +use std::future::IntoFuture; use std::mem; use std::ops::RangeBounds; use std::pin::Pin; @@ -140,6 +141,70 @@ impl FutureStat { } } +pub(crate) struct OperatorFutureX<T, F> { + /// The accessor to the underlying object storage + pub acc: FusedAccessor, + /// The path of string + pub path: String, + /// The input args + pub args: T, + /// The function which will move all the args and return a static future + pub f: fn(FusedAccessor, String, T) -> F, +} + +impl<T, F> OperatorFutureX<T, F> { + fn map_args(mut self, f: impl FnOnce(T) -> T) -> Self { + self.args = (f)(self.args); + self + } +} + +impl<T, F> IntoFuture for OperatorFutureX<T, F> +where + F: Future, +{ + type Output = F::Output; + type IntoFuture = F; + + fn into_future(self) -> Self::IntoFuture { + (self.f)(self.acc, self.path, self.args) + } +} + +pub struct FutureStatX<F>(pub(crate) OperatorFutureX<OpStat, F>); + +impl<F> IntoFuture for FutureStatX<F> +where + F: Future<Output = Result<Metadata>>, +{ + type Output = F::Output; + type IntoFuture = F; + + fn into_future(self) -> Self::IntoFuture { + self.0.into_future() + } +} + +impl<F> FutureStatX<F> { + /// Set the If-Match for this operation. + pub fn if_match(mut self, v: &str) -> Self { + self.0 = self.0.map_args(|args| args.with_if_match(v)); + self + } + + /// Set the If-None-Match for this operation. + pub fn if_none_match(mut self, v: &str) -> Self { + self.0 = self.0.map_args(|args| args.with_if_none_match(v)); + self + } + + /// Set the version for this operation. + pub fn version(mut self, v: &str) -> Self { + self.0 = self.0.map_args(|args| args.with_version(v)); + self + } +} + impl Future for FutureStat { type Output = Result<Metadata>;
