This is an automated email from the ASF dual-hosted git repository. xuanwo pushed a commit to branch lazy-reader in repository https://gitbox.apache.org/repos/asf/incubator-opendal.git
commit 0ce3b2eb92093eb4c537e913b2b47e1d99da6293 Author: Xuanwo <[email protected]> AuthorDate: Wed Oct 25 23:21:32 2023 +0800 Imple,emt std_read Signed-off-by: Xuanwo <[email protected]> --- core/src/raw/oio/read/api.rs | 2 +- core/src/raw/oio/read/mod.rs | 3 ++ core/src/raw/oio/read/std_read.rs | 62 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 66 insertions(+), 1 deletion(-) diff --git a/core/src/raw/oio/read/api.rs b/core/src/raw/oio/read/api.rs index da3225930..79fd77c33 100644 --- a/core/src/raw/oio/read/api.rs +++ b/core/src/raw/oio/read/api.rs @@ -269,7 +269,7 @@ pub type BlockingReader = Box<dyn BlockingRead>; /// /// `Read` is required to be implemented, `Seek` and `Iterator` /// is optional. We use `Read` to make users life easier. -pub trait BlockingRead: Send + Sync + 'static { +pub trait BlockingRead: Send + Sync { /// Read synchronously. fn read(&mut self, buf: &mut [u8]) -> Result<usize>; diff --git a/core/src/raw/oio/read/mod.rs b/core/src/raw/oio/read/mod.rs index e5f66a538..16510636a 100644 --- a/core/src/raw/oio/read/mod.rs +++ b/core/src/raw/oio/read/mod.rs @@ -42,3 +42,6 @@ pub use futures_read::FuturesReader; mod tokio_read; pub use tokio_read::TokioReader; + +mod std_read; +pub use std_read::StdReader; diff --git a/core/src/raw/oio/read/std_read.rs b/core/src/raw/oio/read/std_read.rs new file mode 100644 index 000000000..926b9abb3 --- /dev/null +++ b/core/src/raw/oio/read/std_read.rs @@ -0,0 +1,62 @@ +// 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 crate::raw::*; +use crate::*; +use bytes::Bytes; +use std::io::Seek; +use std::io::{Read, SeekFrom}; + +/// FuturesReader implements [`oio::BlockingRead`] via [`Read`] + [`Seek`]. +pub struct StdReader<R: Read + Seek> { + inner: R, +} + +impl<R: Read + Seek> StdReader<R> { + /// Create a new std reader. + pub fn new(inner: R) -> Self { + Self { inner } + } +} + +impl<R> oio::BlockingRead for StdReader<R> +where + R: Read + Seek + Send + Sync, +{ + fn read(&mut self, buf: &mut [u8]) -> Result<usize> { + self.inner.read(buf).map_err(|err| { + new_std_io_error(err) + .with_operation(oio::ReadOperation::BlockingRead) + .with_context("source", "StdReader") + }) + } + + fn seek(&mut self, pos: SeekFrom) -> Result<u64> { + self.inner.seek(pos).map_err(|err| { + new_std_io_error(err) + .with_operation(oio::ReadOperation::BlockingSeek) + .with_context("source", "StdReader") + }) + } + + fn next(&mut self) -> Option<Result<Bytes>> { + Some(Err(Error::new( + ErrorKind::Unsupported, + "StdReader doesn't support poll_next", + ))) + } +}
