pub struct Reader { /* private fields */ }Expand description
Reader is designed to read data from given path in an asynchronous manner.
§Usage
Reader provides multiple ways to read data from given reader.
Reader implements Clone so you can clone it and store in place where ever you want.
§Direct
Reader provides public API including Reader::read. You can use those APIs directly without extra copy.
use opendal::Operator;
use opendal::Result;
async fn test(op: Operator) -> Result<()> {
let r = op.reader("path/to/file").await?;
let bs = r.read(0..1024).await?;
Ok(())
}§Read like Stream
use anyhow::Result;
use bytes::Bytes;
use futures::TryStreamExt;
use opendal::Operator;
async fn test(op: Operator) -> Result<()> {
let s = op
.reader("path/to/file")
.await?
.into_bytes_stream(1024..2048)
.await?;
let bs: Vec<Bytes> = s.try_collect().await?;
Ok(())
}§Read like AsyncRead and AsyncBufRead
use anyhow::Result;
use bytes::Bytes;
use futures::AsyncReadExt;
use opendal::Operator;
async fn test(op: Operator) -> Result<()> {
let mut r = op
.reader("path/to/file")
.await?
.into_futures_async_read(1024..2048)
.await?;
let mut bs = vec![];
let n = r.read_to_end(&mut bs).await?;
Ok(())
}Implementations§
Source§impl Reader
impl Reader
Sourcepub async fn read(&self, range: impl RangeBounds<u64>) -> Result<Buffer>
pub async fn read(&self, range: impl RangeBounds<u64>) -> Result<Buffer>
Read give range from reader into Buffer.
This operation is zero-copy, which means it keeps the [bytes::Bytes] returned by underlying
storage services without any extra copy or intensive memory allocations.
Sourcepub async fn read_into(
&self,
buf: &mut impl BufMut,
range: impl RangeBounds<u64>,
) -> Result<usize>
pub async fn read_into( &self, buf: &mut impl BufMut, range: impl RangeBounds<u64>, ) -> Result<usize>
Read all data from reader into given [BufMut].
This operation will copy and write bytes into given [BufMut]. Allocation happens while
[BufMut] doesn’t have enough space.
Sourcepub async fn fetch(&self, ranges: Vec<Range<u64>>) -> Result<Vec<Buffer>>
pub async fn fetch(&self, ranges: Vec<Range<u64>>) -> Result<Vec<Buffer>>
Fetch specific ranges from reader.
This operation try to merge given ranges into a list of
non-overlapping ranges. Users may also specify a gap to merge
close ranges.
The returning Buffer may share the same underlying memory without
any extra copy.
Sourcepub async fn into_stream(
self,
range: impl RangeBounds<u64>,
) -> Result<BufferStream>
pub async fn into_stream( self, range: impl RangeBounds<u64>, ) -> Result<BufferStream>
Create a buffer stream to read specific range from given reader.
§Notes
BufferStream is a zero-cost abstraction. It doesn’t involve extra copy of data.
It will return underlying Buffer directly.
The Buffer this stream yields can be seen as an iterator of [Bytes].
§Inputs
range: The range of data to read. range like..it will read all data from reader.
§Examples
§Basic Usage
use std::io;
use bytes::Bytes;
use futures::TryStreamExt;
use opendal::Buffer;
use opendal::Operator;
use opendal::Result;
async fn test(op: Operator) -> io::Result<()> {
let mut s = op
.reader("hello.txt")
.await?
.into_stream(1024..2048)
.await?;
let bs: Vec<Buffer> = s.try_collect().await?;
// We can use those buffer as bytes if we want.
let bytes_vec: Vec<Bytes> = bs.clone().into_iter().flatten().collect();
// Or we can merge them into a single [`Buffer`] and later use it as [`bytes::Buf`].
let new_buffer: Buffer = bs.into_iter().flatten().collect::<Buffer>();
Ok(())
}§Concurrent Read
The following example reads data in 256B chunks with 8 concurrent.
use std::io;
use bytes::Bytes;
use futures::TryStreamExt;
use opendal::Buffer;
use opendal::Operator;
use opendal::Result;
async fn test(op: Operator) -> io::Result<()> {
let s = op
.reader_with("hello.txt")
.concurrent(8)
.chunk(256)
.await?
.into_stream(1024..2048)
.await?;
// Every buffer except the last one in the stream will be 256B.
let bs: Vec<Buffer> = s.try_collect().await?;
// We can use those buffer as bytes if we want.
let bytes_vec: Vec<Bytes> = bs.clone().into_iter().flatten().collect();
// Or we can merge them into a single [`Buffer`] and later use it as [`bytes::Buf`].
let new_buffer: Buffer = bs.into_iter().flatten().collect::<Buffer>();
Ok(())
}Sourcepub async fn into_futures_async_read(
self,
range: impl RangeBounds<u64>,
) -> Result<FuturesAsyncReader>
pub async fn into_futures_async_read( self, range: impl RangeBounds<u64>, ) -> Result<FuturesAsyncReader>
Convert reader into FuturesAsyncReader which implements [futures::AsyncRead],
[futures::AsyncSeek] and [futures::AsyncBufRead].
§Notes
FuturesAsyncReader is not a zero-cost abstraction. The underlying reader
returns an owned Buffer, which involves an extra copy operation.
§Inputs
range: The range of data to read. range like..it will read all data from reader.
§Examples
§Basic Usage
use std::io;
use futures::io::AsyncReadExt;
use opendal::Operator;
use opendal::Result;
async fn test(op: Operator) -> io::Result<()> {
let mut r = op
.reader("hello.txt")
.await?
.into_futures_async_read(1024..2048)
.await?;
let mut bs = Vec::new();
r.read_to_end(&mut bs).await?;
Ok(())
}§Concurrent Read
The following example reads data in 256B chunks with 8 concurrent.
use std::io;
use futures::io::AsyncReadExt;
use opendal::Operator;
use opendal::Result;
async fn test(op: Operator) -> io::Result<()> {
let mut r = op
.reader_with("hello.txt")
.concurrent(8)
.chunk(256)
.await?
.into_futures_async_read(1024..2048)
.await?;
let mut bs = Vec::new();
r.read_to_end(&mut bs).await?;
Ok(())
}Sourcepub async fn into_bytes_stream(
self,
range: impl RangeBounds<u64>,
) -> Result<FuturesBytesStream>
pub async fn into_bytes_stream( self, range: impl RangeBounds<u64>, ) -> Result<FuturesBytesStream>
Convert reader into FuturesBytesStream which implements [futures::Stream].
§Inputs
range: The range of data to read. range like..it will read all data from reader.
§Examples
§Basic Usage
use std::io;
use bytes::Bytes;
use futures::TryStreamExt;
use opendal::Operator;
use opendal::Result;
async fn test(op: Operator) -> io::Result<()> {
let mut s = op
.reader("hello.txt")
.await?
.into_bytes_stream(1024..2048)
.await?;
let bs: Vec<Bytes> = s.try_collect().await?;
Ok(())
}§Concurrent Read
The following example reads data in 256B chunks with 8 concurrent.
use std::io;
use bytes::Bytes;
use futures::TryStreamExt;
use opendal::Operator;
use opendal::Result;
async fn test(op: Operator) -> io::Result<()> {
let mut s = op
.reader_with("hello.txt")
.concurrent(8)
.chunk(256)
.await?
.into_bytes_stream(1024..2048)
.await?;
let bs: Vec<Bytes> = s.try_collect().await?;
Ok(())
}Trait Implementations§
Auto Trait Implementations§
impl Freeze for Reader
impl !RefUnwindSafe for Reader
impl Send for Reader
impl Sync for Reader
impl Unpin for Reader
impl !UnwindSafe for Reader
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<T> Conv for T
impl<T> Conv for T
§impl<T> FmtForward for T
impl<T> FmtForward for T
§fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
self to use its Binary implementation when Debug-formatted.§fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
self to use its Display implementation when
Debug-formatted.§fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
self to use its LowerExp implementation when
Debug-formatted.§fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
self to use its LowerHex implementation when
Debug-formatted.§fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
self to use its Octal implementation when Debug-formatted.§fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
self to use its Pointer implementation when
Debug-formatted.§fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
self to use its UpperExp implementation when
Debug-formatted.§fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
self to use its UpperHex implementation when
Debug-formatted.§fn fmt_list(self) -> FmtList<Self>where
&'a Self: for<'a> IntoIterator,
fn fmt_list(self) -> FmtList<Self>where
&'a Self: for<'a> IntoIterator,
§impl<T> FutureExt for T
impl<T> FutureExt for T
§fn with_context(self, otel_cx: Context) -> WithContext<Self>
fn with_context(self, otel_cx: Context) -> WithContext<Self>
§fn with_current_context(self) -> WithContext<Self>
fn with_current_context(self) -> WithContext<Self>
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::RequestSource§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::Request§impl<L> LayerExt<L> for L
impl<L> LayerExt<L> for L
§fn named_layer<S>(&self, service: S) -> Layered<<L as Layer<S>>::Service, S>where
L: Layer<S>,
fn named_layer<S>(&self, service: S) -> Layered<<L as Layer<S>>::Service, S>where
L: Layer<S>,
Layered].§impl<T> Pipe for Twhere
T: ?Sized,
impl<T> Pipe for Twhere
T: ?Sized,
§fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
§fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
self and passes that borrow into the pipe function. Read more§fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
self and passes that borrow into the pipe function. Read more§fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
§fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R,
) -> R
fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R, ) -> R
§fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
self, then passes self.as_ref() into the pipe function.§fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
self, then passes self.as_mut() into the pipe
function.§fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
self, then passes self.deref() into the pipe function.§impl<T> Pointable for T
impl<T> Pointable for T
§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
§impl<T> Tap for T
impl<T> Tap for T
§fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
Borrow<B> of a value. Read more§fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
BorrowMut<B> of a value. Read more§fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
AsRef<R> view of a value. Read more§fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
AsMut<R> view of a value. Read more§fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
Deref::Target of a value. Read more§fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
Deref::Target of a value. Read more§fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
.tap() only in debug builds, and is erased in release builds.§fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
.tap_mut() only in debug builds, and is erased in release
builds.§fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
.tap_borrow() only in debug builds, and is erased in release
builds.§fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
.tap_borrow_mut() only in debug builds, and is erased in release
builds.§fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
.tap_ref() only in debug builds, and is erased in release
builds.§fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
.tap_ref_mut() only in debug builds, and is erased in release
builds.§fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
.tap_deref() only in debug builds, and is erased in release
builds.