diff --git a/core/src/raw/futures_util.rs b/core/src/raw/futures_util.rs index d7a1168b6340..b933b00546ea 100644 --- a/core/src/raw/futures_util.rs +++ b/core/src/raw/futures_util.rs @@ -25,6 +25,20 @@ use futures::stream::FuturesOrdered; use futures::FutureExt; use futures::StreamExt; +/// MaybeSendFuture is a wrapper trait allow us to use `Send` or not depends on the target. +#[cfg(not(target_arch = "wasm32"))] +pub trait MaybeSendFuture: Future + Send {} + +#[cfg(not(target_arch = "wasm32"))] +impl MaybeSendFuture for F where F: Future + Send {} + +/// MaybeSendFuture is a wrapper trait allow us to use `Send` or not depends on the target. +#[cfg(target_arch = "wasm32")] +pub trait MaybeSendFuture: Future {} + +#[cfg(target_arch = "wasm32")] +impl MaybeSendFuture for F where F: Future {} + /// BoxedFuture is the type alias of [`futures::future::BoxFuture`]. /// /// We will switch to [`futures::future::LocalBoxFuture`] on wasm32 target. diff --git a/core/src/raw/mod.rs b/core/src/raw/mod.rs index ee8feac2608d..1a86c1d807bf 100644 --- a/core/src/raw/mod.rs +++ b/core/src/raw/mod.rs @@ -65,6 +65,7 @@ pub use std_io_util::*; mod futures_util; pub use futures_util::BoxedFuture; pub use futures_util::ConcurrentFutures; +pub use futures_util::MaybeSendFuture; mod enum_utils; pub use enum_utils::*; diff --git a/core/src/raw/oio/write/multipart_upload_write.rs b/core/src/raw/oio/write/multipart_upload_write.rs index 66105b4d4a4f..3790fa6579c0 100644 --- a/core/src/raw/oio/write/multipart_upload_write.rs +++ b/core/src/raw/oio/write/multipart_upload_write.rs @@ -21,7 +21,6 @@ use std::task::ready; use std::task::Context; use std::task::Poll; -use async_trait::async_trait; use futures::Future; use futures::FutureExt; use futures::StreamExt; @@ -51,15 +50,13 @@ use crate::*; /// ``` /// /// We will use `write_once` instead of starting a new multipart upload. -#[cfg_attr(not(target_arch = "wasm32"), async_trait)] -#[cfg_attr(target_arch = "wasm32", async_trait(?Send))] pub trait MultipartUploadWrite: Send + Sync + Unpin + 'static { /// write_once is used to write the data to underlying storage at once. /// /// MultipartUploadWriter will call this API when: /// /// - All the data has been written to the buffer and we can perform the upload at once. - async fn write_once(&self, size: u64, body: AsyncBody) -> Result<()>; + fn write_once(&self, size: u64, body: AsyncBody) -> impl MaybeSendFuture>; /// initiate_part will call start a multipart upload and return the upload id. /// @@ -68,7 +65,7 @@ pub trait MultipartUploadWrite: Send + Sync + Unpin + 'static { /// - the total size of data is unknown. /// - the total size of data is known, but the size of current write /// is less then the total size. - async fn initiate_part(&self) -> Result; + fn initiate_part(&self) -> impl MaybeSendFuture>; /// write_part will write a part of the data and returns the result /// [`MultipartUploadPart`]. @@ -77,20 +74,24 @@ pub trait MultipartUploadWrite: Send + Sync + Unpin + 'static { /// order. /// /// - part_number is the index of the part, starting from 0. - async fn write_part( + fn write_part( &self, upload_id: &str, part_number: usize, size: u64, body: AsyncBody, - ) -> Result; + ) -> impl MaybeSendFuture>; /// complete_part will complete the multipart upload to build the final /// file. - async fn complete_part(&self, upload_id: &str, parts: &[MultipartUploadPart]) -> Result<()>; + fn complete_part( + &self, + upload_id: &str, + parts: &[MultipartUploadPart], + ) -> impl MaybeSendFuture>; /// abort_part will cancel the multipart upload and purge all data. - async fn abort_part(&self, upload_id: &str) -> Result<()>; + fn abort_part(&self, upload_id: &str) -> impl MaybeSendFuture>; } /// The result of [`MultipartUploadWrite::write_part`]. diff --git a/core/src/services/b2/writer.rs b/core/src/services/b2/writer.rs index 59f4367fb59f..6dbca976074b 100644 --- a/core/src/services/b2/writer.rs +++ b/core/src/services/b2/writer.rs @@ -17,7 +17,6 @@ use std::sync::Arc; -use async_trait::async_trait; use http::StatusCode; use super::core::B2Core; @@ -46,7 +45,6 @@ impl B2Writer { } } -#[async_trait] impl oio::MultipartUploadWrite for B2Writer { async fn write_once(&self, size: u64, body: AsyncBody) -> Result<()> { let resp = self diff --git a/core/src/services/cos/writer.rs b/core/src/services/cos/writer.rs index b1d1dbac505a..c236e12d0139 100644 --- a/core/src/services/cos/writer.rs +++ b/core/src/services/cos/writer.rs @@ -45,7 +45,6 @@ impl CosWriter { } } -#[async_trait] impl oio::MultipartUploadWrite for CosWriter { async fn write_once(&self, size: u64, body: AsyncBody) -> Result<()> { let mut req = self diff --git a/core/src/services/obs/writer.rs b/core/src/services/obs/writer.rs index 1dff95eda12d..900e66d98f1a 100644 --- a/core/src/services/obs/writer.rs +++ b/core/src/services/obs/writer.rs @@ -46,7 +46,6 @@ impl ObsWriter { } } -#[async_trait] impl oio::MultipartUploadWrite for ObsWriter { async fn write_once(&self, size: u64, body: AsyncBody) -> Result<()> { let mut req = self diff --git a/core/src/services/oss/writer.rs b/core/src/services/oss/writer.rs index 1770ffeb64d2..45254a454ced 100644 --- a/core/src/services/oss/writer.rs +++ b/core/src/services/oss/writer.rs @@ -45,7 +45,6 @@ impl OssWriter { } } -#[async_trait] impl oio::MultipartUploadWrite for OssWriter { async fn write_once(&self, size: u64, body: AsyncBody) -> Result<()> { let mut req = diff --git a/core/src/services/s3/writer.rs b/core/src/services/s3/writer.rs index 22ecc041abdd..c116f574c2f9 100644 --- a/core/src/services/s3/writer.rs +++ b/core/src/services/s3/writer.rs @@ -17,7 +17,6 @@ use std::sync::Arc; -use async_trait::async_trait; use http::StatusCode; use super::core::*; @@ -44,8 +43,6 @@ impl S3Writer { } } -#[cfg_attr(not(target_arch = "wasm32"), async_trait)] -#[cfg_attr(target_arch = "wasm32", async_trait(?Send))] impl oio::MultipartUploadWrite for S3Writer { async fn write_once(&self, size: u64, body: AsyncBody) -> Result<()> { let mut req = self diff --git a/core/src/services/upyun/writer.rs b/core/src/services/upyun/writer.rs index b4f508212202..10793b58e5cf 100644 --- a/core/src/services/upyun/writer.rs +++ b/core/src/services/upyun/writer.rs @@ -17,7 +17,6 @@ use std::sync::Arc; -use async_trait::async_trait; use http::StatusCode; use super::core::constants::X_UPYUN_MULTI_UUID; @@ -40,7 +39,6 @@ impl UpyunWriter { } } -#[async_trait] impl oio::MultipartUploadWrite for UpyunWriter { async fn write_once(&self, size: u64, body: AsyncBody) -> Result<()> { let req = self