pub trait Write {
// Required methods
fn write(&mut self, src: &[u8]) -> Result<usize, Error>;
fn flush(&mut self) -> Result<(), Error>;
// Provided methods
fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> Result<usize, Error> { ... }
fn write_all(&mut self, buf: &[u8]) -> Result<(), Error> { ... }
fn write_fmt(&mut self, fmt: Arguments<'_>) -> Result<(), Error> { ... }
fn by_ref(&mut self) -> &mut Self
where Self: Sized { ... }
}
Expand description
A trait for objects which are byte-oriented sinks.
Implementors of the Write
trait are sometimes called ‘writers’.
Writers are defined by two required methods, write
and flush
:
-
The
write
method will attempt to write some data into the object, returning how many bytes were successfully written. -
The
flush
method is useful for adapters and explicit buffers themselves for ensuring that all buffered data has been pushed out to the ‘true sink’.
Writers are intended to be composable with one another. Many implementors
throughout take and provide types which implement the Write
trait.
Examples
use acid_io::{Cursor, Write as _};
let data = b"some bytes";
let mut arr = [0u8; 10];
let mut buffer = Cursor::new(&mut arr[..]);
buffer.write(data)?;
assert_eq!(buffer.get_ref(), data);
The trait also provides convenience methods like write_all
, which calls
write
in a loop until its entire input has been written.
Required Methods§
sourcefn write(&mut self, src: &[u8]) -> Result<usize, Error>
fn write(&mut self, src: &[u8]) -> Result<usize, Error>
Write a buffer into this writer, returning how many bytes were written.
This function will attempt to write the entire contents of buf
, but
the entire write might not succeed, or the write may also generate an
error. A call to write
represents at most one attempt to write to
any wrapped object.
Calls to write
are not guaranteed to block waiting for data to be
written, and a write which would otherwise block can be indicated through
an [Err
] variant.
If the return value is Ok(n)
then it must be guaranteed that
n <= buf.len()
. A return value of 0
typically means that the
underlying object is no longer able to accept bytes and will likely not
be able to in the future as well, or that the buffer provided is empty.
Errors
Each call to write
may generate an I/O error indicating that the
operation could not be completed. If an error is returned then no bytes
in the buffer were written to this writer.
It is not considered an error if the entire buffer could not be written to this writer.
An error of the ErrorKind::Interrupted
kind is non-fatal and the
write operation should be retried if there is nothing else to do.
Examples
use acid_io::prelude::*;
let mut dst = [0u8; 16];
dst.as_mut_slice().write(b"some bytes")?;
assert_eq!(&dst[..10], b"some bytes");
sourcefn flush(&mut self) -> Result<(), Error>
fn flush(&mut self) -> Result<(), Error>
Flush this output stream, ensuring that all intermediately buffered contents reach their destination.
Errors
It is considered an error if not all bytes could be written due to I/O errors or EOF being reached.
Examples
#[cfg(not(feature = "alloc"))]
#[cfg(feature = "alloc")]
use acid_io::prelude::*;
use acid_io::BufWriter;
let mut dst = [0u8; 16];
let mut buffer = BufWriter::new(dst.as_mut_slice());
buffer.write_all(b"some bytes")?;
buffer.flush()?;
drop(buffer);
assert_eq!(&dst[..10], b"some bytes");
Provided Methods§
sourcefn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> Result<usize, Error>
fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> Result<usize, Error>
Like write
, except that it writes from a slice of buffers.
Data is copied from each buffer in order, with the final buffer
read from possibly being only partially consumed. This method must
behave as a call to write
with the buffers concatenated would.
The default implementation calls write
with either the first nonempty
buffer provided, or an empty one if none exists.
Examples
use acid_io::prelude::*;
use acid_io::{Cursor, IoSlice};
let mut data1 = [1; 8];
let mut data2 = [15; 8];
let io_slice1 = IoSlice::new(&mut data1);
let io_slice2 = IoSlice::new(&mut data2);
let mut dst = [0u8; 16];
dst.as_mut_slice().write_vectored(&[io_slice1, io_slice2])?;
assert_eq!(&dst[..8], &data1);
assert_eq!(&dst[8..], &data2);
sourcefn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
Attempts to write an entire buffer into this writer.
This method will continuously call write
until there is no more data
to be written or an error of non-ErrorKind::Interrupted
kind is
returned. This method will not return until the entire buffer has been
successfully written or such an error occurs. The first error that is
not of ErrorKind::Interrupted
kind generated from this method will be
returned.
If the buffer contains no data, this will never call write
.
Errors
This function will return the first error of
non-ErrorKind::Interrupted
kind that write
returns.
use acid_io::prelude::*;
let mut src = b"some bytes";
let mut large = [0u8; 16];
large.as_mut_slice().write_all(src)?;
// write_all to a buffer that's not large enough
let mut small = [0u8; 4];
let res = small.as_mut_slice().write_all(src);
assert!(res.is_err());
sourcefn write_fmt(&mut self, fmt: Arguments<'_>) -> Result<(), Error>
fn write_fmt(&mut self, fmt: Arguments<'_>) -> Result<(), Error>
Writes a formatted string into this writer, returning any error encountered.
This method is primarily used to interface with the
[format_args!()
] macro, and it is rare that this should
explicitly be called. The [write!()
] macro should be favored to
invoke this method instead.
This function internally uses the write_all
method on
this trait and hence will continuously write data so long as no errors
are received. This also means that partial writes are not indicated in
this signature.
Errors
This function will return any I/O error reported while formatting.
Examples
use acid_io::prelude::*;
let mut buffer = [0u8; 32];
// this call
write!(buffer.as_mut_slice(), "{:.*}", 2, 1.234567)?;
// turns into this:
buffer.as_mut_slice().write_fmt(format_args!("{:.*}", 2, 1.234567))?;
sourcefn by_ref(&mut self) -> &mut Selfwhere
Self: Sized,
fn by_ref(&mut self) -> &mut Selfwhere Self: Sized,
Creates a “by reference” adapter for this instance of Write
.
The returned adapter also implements Write
and will simply borrow this
current writer.
Examples
use acid_io::Write;
let mut buffer = [0u8; 16];
let mut w = buffer.as_mut_slice();
let reference = w.by_ref();
// we can use reference just like our original buffer
reference.write_all(b"some bytes")?;