pub struct ChaosWriter<W: Write> { /* private fields */ }Expand description
Wraps a Write and injects failures per a FailureSchedule.
On PartialWrite failures, the wrapper writes one byte (when the
caller asked for at least one) before returning the error, so the
caller observes a partial-flush state.
On all other failure modes, the wrapper returns the error without writing any bytes.
§Example
use dev_chaos::{io::ChaosWriter, FailureMode, FailureSchedule};
use std::io::Write;
let mut sink: Vec<u8> = Vec::new();
let schedule = FailureSchedule::on_attempts(&[2], FailureMode::IoError);
let mut writer = ChaosWriter::new(&mut sink, schedule);
writer.write_all(b"a").unwrap(); // attempt 1: ok
assert!(writer.write_all(b"b").is_err()); // attempt 2: fails
drop(writer);
assert_eq!(sink, b"a");Implementations§
Source§impl<W: Write> ChaosWriter<W>
impl<W: Write> ChaosWriter<W>
Sourcepub fn new(inner: W, schedule: FailureSchedule) -> Self
pub fn new(inner: W, schedule: FailureSchedule) -> Self
Wrap inner with the given schedule.
Sourcepub fn attempt_count(&self) -> usize
pub fn attempt_count(&self) -> usize
Number of write attempts (successful or failed) so far.
Sourcepub fn into_inner(self) -> W
pub fn into_inner(self) -> W
Consume the wrapper and return the underlying writer.
Trait Implementations§
Source§impl<W: Write> Write for ChaosWriter<W>
impl<W: Write> Write for ChaosWriter<W>
Source§fn write(&mut self, buf: &[u8]) -> Result<usize>
fn write(&mut self, buf: &[u8]) -> Result<usize>
Writes a buffer into this writer, returning how many bytes were written. Read more
Source§fn flush(&mut self) -> Result<()>
fn flush(&mut self) -> Result<()>
Flushes this output stream, ensuring that all intermediately buffered
contents reach their destination. Read more
Source§fn is_write_vectored(&self) -> bool
fn is_write_vectored(&self) -> bool
🔬This is a nightly-only experimental API. (
can_vector)1.0.0 · Source§fn 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. Read more
Source§fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>
fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>
🔬This is a nightly-only experimental API. (
write_all_vectored)Attempts to write multiple buffers into this writer. Read more
Auto Trait Implementations§
impl<W> !Freeze for ChaosWriter<W>
impl<W> RefUnwindSafe for ChaosWriter<W>where
W: RefUnwindSafe,
impl<W> Send for ChaosWriter<W>where
W: Send,
impl<W> Sync for ChaosWriter<W>where
W: Sync,
impl<W> Unpin for ChaosWriter<W>where
W: Unpin,
impl<W> UnsafeUnpin for ChaosWriter<W>where
W: UnsafeUnpin,
impl<W> UnwindSafe for ChaosWriter<W>where
W: UnwindSafe,
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
Mutably borrows from an owned value. Read more