diff --git a/core/src/layers/complete.rs b/core/src/layers/complete.rs index 40c09568904..12ef4be30ce 100644 --- a/core/src/layers/complete.rs +++ b/core/src/layers/complete.rs @@ -33,7 +33,7 @@ use crate::raw::oio::LazyReader; use crate::raw::oio::PrefixLister; use crate::raw::oio::RangeReader; use crate::raw::oio::StreamableReader; -use crate::raw::oio::TwoWaysReader; +use crate::raw::TwoWays; use crate::raw::*; use crate::*; @@ -471,10 +471,8 @@ impl LayeredAccessor for CompleteAccessor { type Inner = A; type Reader = CompleteReader; type BlockingReader = CompleteReader; - type Writer = oio::TwoWaysWriter< - CompleteWriter, - oio::ExactBufWriter>, - >; + type Writer = + TwoWays, oio::ExactBufWriter>>; type BlockingWriter = CompleteWriter; type Lister = CompleteLister; type BlockingLister = CompleteLister; @@ -540,8 +538,8 @@ impl LayeredAccessor for CompleteAccessor { let w = CompleteWriter::new(w); let w = match buffer_size { - None => oio::TwoWaysWriter::One(w), - Some(size) => oio::TwoWaysWriter::Two(oio::ExactBufWriter::new(w, size)), + None => TwoWays::One(w), + Some(size) => TwoWays::Two(oio::ExactBufWriter::new(w, size)), }; Ok((rp, w)) @@ -676,7 +674,7 @@ impl LayeredAccessor for CompleteAccessor { } pub type CompleteReader = - TwoWaysReader, BufferReader>>; + TwoWays, BufferReader>>; type InnerCompleteReader = FourWaysReader< LazyReader, diff --git a/core/src/raw/enum_utils.rs b/core/src/raw/enum_utils.rs new file mode 100644 index 00000000000..8728ea65be2 --- /dev/null +++ b/core/src/raw/enum_utils.rs @@ -0,0 +1,101 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +use std::io::SeekFrom; +use std::task::{Context, Poll}; + +use crate::raw::*; +use crate::*; + +/// TwoWays is used to implement [`Read`] or [`Writer`] based on two ways. +/// +/// Users can wrap two different readers/writers together. +pub enum TwoWays { + /// The first type for the [`TwoWays`]. + One(ONE), + /// The second type for the [`TwoWays`]. + Two(TWO), +} + +impl oio::Read for TwoWays { + fn poll_read(&mut self, cx: &mut Context<'_>, buf: &mut [u8]) -> Poll> { + match self { + Self::One(one) => one.poll_read(cx, buf), + Self::Two(two) => two.poll_read(cx, buf), + } + } + + fn poll_seek(&mut self, cx: &mut Context<'_>, pos: SeekFrom) -> Poll> { + match self { + Self::One(one) => one.poll_seek(cx, pos), + Self::Two(two) => two.poll_seek(cx, pos), + } + } + + fn poll_next(&mut self, cx: &mut Context<'_>) -> Poll>> { + match self { + Self::One(one) => one.poll_next(cx), + Self::Two(two) => two.poll_next(cx), + } + } +} + +impl oio::BlockingRead for TwoWays { + fn read(&mut self, buf: &mut [u8]) -> Result { + match self { + Self::One(one) => one.read(buf), + Self::Two(two) => two.read(buf), + } + } + + fn seek(&mut self, pos: SeekFrom) -> Result { + match self { + Self::One(one) => one.seek(pos), + Self::Two(two) => two.seek(pos), + } + } + + fn next(&mut self) -> Option> { + match self { + Self::One(one) => one.next(), + Self::Two(two) => two.next(), + } + } +} + +impl oio::Write for TwoWays { + fn poll_write(&mut self, cx: &mut Context<'_>, bs: &dyn oio::WriteBuf) -> Poll> { + match self { + Self::One(one) => one.poll_write(cx, bs), + Self::Two(two) => two.poll_write(cx, bs), + } + } + + fn poll_close(&mut self, cx: &mut Context<'_>) -> Poll> { + match self { + Self::One(one) => one.poll_close(cx), + Self::Two(two) => two.poll_close(cx), + } + } + + fn poll_abort(&mut self, cx: &mut Context<'_>) -> Poll> { + match self { + Self::One(one) => one.poll_abort(cx), + Self::Two(two) => two.poll_abort(cx), + } + } +} diff --git a/core/src/raw/mod.rs b/core/src/raw/mod.rs index 0d4b1d6d36d..99a3d1d4f97 100644 --- a/core/src/raw/mod.rs +++ b/core/src/raw/mod.rs @@ -71,3 +71,6 @@ pub mod adapters; pub mod oio; #[cfg(feature = "tests")] pub mod tests; + +mod enum_utils; +pub use enum_utils::TwoWays; diff --git a/core/src/raw/oio/read/compose_read.rs b/core/src/raw/oio/read/compose_read.rs index a81fb57982b..ccf350f27a0 100644 --- a/core/src/raw/oio/read/compose_read.rs +++ b/core/src/raw/oio/read/compose_read.rs @@ -15,87 +15,30 @@ // specific language governing permissions and limitations // under the License. -use std::{ - io::SeekFrom, - task::{Context, Poll}, -}; +use std::io::SeekFrom; +use std::task::{Context, Poll}; use crate::raw::*; - -/// TwoWaysReader is used to implement [`Read`] based on two ways. -/// -/// Users can wrap two different readers together. -pub enum TwoWaysReader { - /// The first type for the [`TwoWaysReader`]. - One(ONE), - /// The second type for the [`TwoWaysReader`]. - Two(TWO), -} - -impl oio::Read for TwoWaysReader { - fn poll_read(&mut self, cx: &mut Context<'_>, buf: &mut [u8]) -> Poll> { - match self { - Self::One(one) => one.poll_read(cx, buf), - Self::Two(two) => two.poll_read(cx, buf), - } - } - - fn poll_seek(&mut self, cx: &mut Context<'_>, pos: SeekFrom) -> Poll> { - match self { - Self::One(one) => one.poll_seek(cx, pos), - Self::Two(two) => two.poll_seek(cx, pos), - } - } - - fn poll_next(&mut self, cx: &mut Context<'_>) -> Poll>> { - match self { - Self::One(one) => one.poll_next(cx), - Self::Two(two) => two.poll_next(cx), - } - } -} - -impl oio::BlockingRead for TwoWaysReader { - fn read(&mut self, buf: &mut [u8]) -> crate::Result { - match self { - Self::One(one) => one.read(buf), - Self::Two(two) => two.read(buf), - } - } - - fn seek(&mut self, pos: SeekFrom) -> crate::Result { - match self { - Self::One(one) => one.seek(pos), - Self::Two(two) => two.seek(pos), - } - } - - fn next(&mut self) -> Option> { - match self { - Self::One(one) => one.next(), - Self::Two(two) => two.next(), - } - } -} +use crate::*; /// FourWaysReader is used to implement [`Read`] based on four ways. /// /// Users can wrap four different readers together. pub enum FourWaysReader { - /// The first type for the [`TwoWaysReader`]. + /// The first type for the [`FourWaysReader`]. One(ONE), - /// The second type for the [`TwoWaysReader`]. + /// The second type for the [`FourWaysReader`]. Two(TWO), - /// The third type for the [`TwoWaysReader`]. + /// The third type for the [`FourWaysReader`]. Three(THREE), - /// The fourth type for the [`TwoWaysReader`]. + /// The fourth type for the [`FourWaysReader`]. Four(FOUR), } impl oio::Read for FourWaysReader { - fn poll_read(&mut self, cx: &mut Context<'_>, buf: &mut [u8]) -> Poll> { + fn poll_read(&mut self, cx: &mut Context<'_>, buf: &mut [u8]) -> Poll> { match self { Self::One(one) => one.poll_read(cx, buf), Self::Two(two) => two.poll_read(cx, buf), @@ -104,7 +47,7 @@ impl oio::Rea } } - fn poll_seek(&mut self, cx: &mut Context<'_>, pos: SeekFrom) -> Poll> { + fn poll_seek(&mut self, cx: &mut Context<'_>, pos: SeekFrom) -> Poll> { match self { Self::One(one) => one.poll_seek(cx, pos), Self::Two(two) => two.poll_seek(cx, pos), @@ -113,7 +56,7 @@ impl oio::Rea } } - fn poll_next(&mut self, cx: &mut Context<'_>) -> Poll>> { + fn poll_next(&mut self, cx: &mut Context<'_>) -> Poll>> { match self { Self::One(one) => one.poll_next(cx), Self::Two(two) => two.poll_next(cx), @@ -130,7 +73,7 @@ impl< FOUR: oio::BlockingRead, > oio::BlockingRead for FourWaysReader { - fn read(&mut self, buf: &mut [u8]) -> crate::Result { + fn read(&mut self, buf: &mut [u8]) -> Result { match self { Self::One(one) => one.read(buf), Self::Two(two) => two.read(buf), @@ -139,7 +82,7 @@ impl< } } - fn seek(&mut self, pos: SeekFrom) -> crate::Result { + fn seek(&mut self, pos: SeekFrom) -> Result { match self { Self::One(one) => one.seek(pos), Self::Two(two) => two.seek(pos), @@ -148,7 +91,7 @@ impl< } } - fn next(&mut self) -> Option> { + fn next(&mut self) -> Option> { match self { Self::One(one) => one.next(), Self::Two(two) => two.next(), diff --git a/core/src/raw/oio/read/mod.rs b/core/src/raw/oio/read/mod.rs index c7ac45abe4c..2efe14a6d01 100644 --- a/core/src/raw/oio/read/mod.rs +++ b/core/src/raw/oio/read/mod.rs @@ -54,4 +54,3 @@ pub use buffer_reader::BufferReader; mod compose_read; pub use compose_read::FourWaysReader; -pub use compose_read::TwoWaysReader; diff --git a/core/src/raw/oio/write/compose_write.rs b/core/src/raw/oio/write/compose_write.rs index 6572eecf8e9..78b8198951c 100644 --- a/core/src/raw/oio/write/compose_write.rs +++ b/core/src/raw/oio/write/compose_write.rs @@ -28,7 +28,7 @@ //! //! ```txt //! impl Accessor for OssBackend { -//! type Writer = oio::TwoWaysWriter< +//! type Writer = raw::TwoWays< //! oio::MultipartUploadWriter, //! oio::AppendObjectWriter, //! >; @@ -44,39 +44,6 @@ use std::task::Poll; use crate::raw::*; use crate::*; -/// TwoWaysWrite is used to implement [`Write`] based on two ways. -/// -/// Users can wrap two different writers together. -pub enum TwoWaysWriter { - /// The first type for the [`TwoWaysWriter`]. - One(ONE), - /// The second type for the [`TwoWaysWriter`]. - Two(TWO), -} - -impl oio::Write for TwoWaysWriter { - fn poll_write(&mut self, cx: &mut Context<'_>, bs: &dyn oio::WriteBuf) -> Poll> { - match self { - Self::One(one) => one.poll_write(cx, bs), - Self::Two(two) => two.poll_write(cx, bs), - } - } - - fn poll_close(&mut self, cx: &mut Context<'_>) -> Poll> { - match self { - Self::One(one) => one.poll_close(cx), - Self::Two(two) => two.poll_close(cx), - } - } - - fn poll_abort(&mut self, cx: &mut Context<'_>) -> Poll> { - match self { - Self::One(one) => one.poll_abort(cx), - Self::Two(two) => two.poll_abort(cx), - } - } -} - /// ThreeWaysWriter is used to implement [`Write`] based on three ways. /// /// Users can wrap three different writers together. diff --git a/core/src/raw/oio/write/mod.rs b/core/src/raw/oio/write/mod.rs index df0b83b2819..58026a1edd7 100644 --- a/core/src/raw/oio/write/mod.rs +++ b/core/src/raw/oio/write/mod.rs @@ -25,7 +25,6 @@ pub use api::Writer; mod compose_write; pub use compose_write::ThreeWaysWriter; -pub use compose_write::TwoWaysWriter; mod multipart_upload_write; pub use multipart_upload_write::MultipartUploadPart; diff --git a/core/src/services/azblob/writer.rs b/core/src/services/azblob/writer.rs index 989684288b4..895bce1bf5c 100644 --- a/core/src/services/azblob/writer.rs +++ b/core/src/services/azblob/writer.rs @@ -28,7 +28,7 @@ use crate::*; const X_MS_BLOB_TYPE: &str = "x-ms-blob-type"; pub type AzblobWriters = - oio::TwoWaysWriter, oio::AppendObjectWriter>; + TwoWays, oio::AppendObjectWriter>; pub struct AzblobWriter { core: Arc, diff --git a/core/src/services/azdls/writer.rs b/core/src/services/azdls/writer.rs index e9e57cd973d..e17cd1c28af 100644 --- a/core/src/services/azdls/writer.rs +++ b/core/src/services/azdls/writer.rs @@ -27,7 +27,7 @@ use crate::raw::*; use crate::*; pub type AzdlsWriters = - oio::TwoWaysWriter, oio::AppendObjectWriter>; + TwoWays, oio::AppendObjectWriter>; pub struct AzdlsWriter { core: Arc, diff --git a/core/src/services/azfile/writer.rs b/core/src/services/azfile/writer.rs index e6e199997f6..fbd8daff10c 100644 --- a/core/src/services/azfile/writer.rs +++ b/core/src/services/azfile/writer.rs @@ -26,7 +26,7 @@ use crate::raw::*; use crate::*; pub type AzfileWriters = - oio::TwoWaysWriter, oio::AppendObjectWriter>; + TwoWays, oio::AppendObjectWriter>; pub struct AzfileWriter { core: Arc, diff --git a/core/src/services/cos/writer.rs b/core/src/services/cos/writer.rs index 0363a647162..b1d1dbac505 100644 --- a/core/src/services/cos/writer.rs +++ b/core/src/services/cos/writer.rs @@ -26,7 +26,7 @@ use crate::raw::*; use crate::*; pub type CosWriters = - oio::TwoWaysWriter, oio::AppendObjectWriter>; + TwoWays, oio::AppendObjectWriter>; pub struct CosWriter { core: Arc, diff --git a/core/src/services/obs/writer.rs b/core/src/services/obs/writer.rs index 82daefee592..1dff95eda12 100644 --- a/core/src/services/obs/writer.rs +++ b/core/src/services/obs/writer.rs @@ -27,7 +27,7 @@ use crate::raw::*; use crate::*; pub type ObsWriters = - oio::TwoWaysWriter, oio::AppendObjectWriter>; + TwoWays, oio::AppendObjectWriter>; pub struct ObsWriter { core: Arc, diff --git a/core/src/services/oss/writer.rs b/core/src/services/oss/writer.rs index 8405f78e779..1770ffeb64d 100644 --- a/core/src/services/oss/writer.rs +++ b/core/src/services/oss/writer.rs @@ -26,7 +26,7 @@ use crate::raw::*; use crate::*; pub type OssWriters = - oio::TwoWaysWriter, oio::AppendObjectWriter>; + TwoWays, oio::AppendObjectWriter>; pub struct OssWriter { core: Arc,