pub struct Writer { /* private fields */ }
Expand description
Writer is designed to write data into given path in an asynchronous manner.
Notes
Please make sure either close
or abort
has been called before
dropping the writer otherwise the data could be lost.
Notes
Writer can be used in two ways:
- Sized: write data with a known size by specify the content length.
- Unsized: write data with an unknown size, also known as streaming.
All services will support sized
writer and provide special optimization if
the given data size is the same as the content length, allowing them to
be written in one request.
Some services also supports unsized
writer. They MAY buffer part of the data
and flush them into storage at needs. And finally, the file will be available
after close
has been called.
Implementations§
source§impl Writer
impl Writer
sourcepub async fn sink<S, T>(&mut self, size: u64, sink_from: S) -> Result<()>where
S: Stream<Item = Result<T>> + Send + Sync + Unpin + 'static,
T: Into<Bytes>,
pub async fn sink<S, T>(&mut self, size: u64, sink_from: S) -> Result<()>where S: Stream<Item = Result<T>> + Send + Sync + Unpin + 'static, T: Into<Bytes>,
Sink into writer.
sink will read data from given streamer and write them into writer directly without extra in-memory buffer.
Notes
- Sink doesn’t support to be used with write concurrently.
- Sink doesn’t support to be used without content length now.
Examples
use std::io::Result;
use bytes::Bytes;
use futures::stream;
use futures::StreamExt;
use opendal::Operator;
#[tokio::main]
async fn sink_example(op: Operator) -> Result<()> {
let mut w = op
.writer_with("path/to/file")
.content_length(2 * 4096)
.await?;
let stream = stream::iter(vec![vec![0; 4096], vec![1; 4096]]).map(Ok);
w.sink(2 * 4096, stream).await?;
w.close().await?;
Ok(())
}
sourcepub async fn copy<R>(&mut self, size: u64, read_from: R) -> Result<()>where
R: AsyncRead + Send + Sync + Unpin + 'static,
pub async fn copy<R>(&mut self, size: u64, read_from: R) -> Result<()>where R: AsyncRead + Send + Sync + Unpin + 'static,
Copy into writer.
copy will read data from given reader and write them into writer directly with only one constant in-memory buffer.
Notes
- Copy doesn’t support to be used with write concurrently.
- Copy doesn’t support to be used without content length now.
Examples
use std::io::Result;
use bytes::Bytes;
use futures::stream;
use futures::StreamExt;
use opendal::Operator;
use futures::io::Cursor;
#[tokio::main]
async fn copy_example(op: Operator) -> Result<()> {
let mut w = op
.writer_with("path/to/file")
.content_length(4096)
.await?;
let reader = Cursor::new(vec![0;4096]);
w.copy(4096, reader).await?;
w.close().await?;
Ok(())
}
Trait Implementations§
source§impl AsyncWrite for Writer
impl AsyncWrite for Writer
source§fn poll_flush(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<Result<()>>
fn poll_flush(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<Result<()>>
Writer makes sure that every write is flushed.
source§fn poll_write(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &[u8]
) -> Poll<Result<usize>>
fn poll_write( self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &[u8] ) -> Poll<Result<usize>>
buf
into the object. Read moresource§impl AsyncWrite for Writer
impl AsyncWrite for Writer
source§fn poll_write(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &[u8]
) -> Poll<Result<usize>>
fn poll_write( self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &[u8] ) -> Poll<Result<usize>>
buf
into the object. Read moresource§fn poll_flush(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<Result<()>>
fn poll_flush(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<Result<()>>
source§fn poll_shutdown(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<()>>
fn poll_shutdown(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<()>>
source§fn poll_write_vectored(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
bufs: &[IoSlice<'_>]
) -> Poll<Result<usize, Error>>
fn poll_write_vectored( self: Pin<&mut Self>, cx: &mut Context<'_>, bufs: &[IoSlice<'_>] ) -> Poll<Result<usize, Error>>
poll_write
, except that it writes from a slice of buffers. Read moresource§fn is_write_vectored(&self) -> bool
fn is_write_vectored(&self) -> bool
poll_write_vectored
implementation. Read moreimpl Sync for Writer
Safety
Writer will only be accessed by &mut Self
Auto Trait Implementations§
impl !RefUnwindSafe for Writer
impl Send for Writer
impl Unpin for Writer
impl !UnwindSafe for Writer
Blanket Implementations§
§impl<W> AsyncWriteExt for Wwhere
W: AsyncWrite + ?Sized,
impl<W> AsyncWriteExt for Wwhere W: AsyncWrite + ?Sized,
§fn flush(&mut self) -> Flush<'_, Self>where
Self: Unpin,
fn flush(&mut self) -> Flush<'_, Self>where Self: Unpin,
AsyncWrite
. Read more§fn close(&mut self) -> Close<'_, Self>where
Self: Unpin,
fn close(&mut self) -> Close<'_, Self>where Self: Unpin,
AsyncWrite
.§fn write<'a>(&'a mut self, buf: &'a [u8]) -> Write<'a, Self>where
Self: Unpin,
fn write<'a>(&'a mut self, buf: &'a [u8]) -> Write<'a, Self>where Self: Unpin,
buf
into the object. Read more§fn write_vectored<'a>(
&'a mut self,
bufs: &'a [IoSlice<'a>]
) -> WriteVectored<'a, Self>where
Self: Unpin,
fn write_vectored<'a>( &'a mut self, bufs: &'a [IoSlice<'a>] ) -> WriteVectored<'a, Self>where Self: Unpin,
bufs
into the object using vectored
IO operations. Read more