pub struct Rewriter<'a, W: ?Sized + 'a> { /* private fields */ }
Expand description
A lightweight adapter from std::io::Write
to core::fmt::Write
.
Since Rust encodes strings and string slices in UTF-8, forwarding
core::fmt::Write::write_str
to std::io::Write::write_all
is
straight-forward. The challenge is that std::io::Error
covers many
different error conditions, whereas core::fmt::Error
is a unit-like
struct. The primary benefit of this adapter is that it tracks the most
recent I/O error. Hence, if the rewriter fails with a format error, code
using this struct can recover the underlying I/O error.
§Example
The match below illustrates how to do just that:
let mut cursor = Cursor::new(vec![0; 10]);
let mut writer = Rewriter::new(&mut cursor);
match writer.write_str("Hello!") {
Ok(()) => (),
Err(_) => return Err(writer.into_err()),
}
assert_eq!(&cursor.get_ref()[0..5], b"Hello");
Implementations§
Trait Implementations§
Auto Trait Implementations§
impl<'a, W> Freeze for Rewriter<'a, W>where
W: ?Sized,
impl<'a, W> !RefUnwindSafe for Rewriter<'a, W>
impl<'a, W> Send for Rewriter<'a, W>
impl<'a, W> Sync for Rewriter<'a, W>
impl<'a, W> Unpin for Rewriter<'a, W>where
W: ?Sized,
impl<'a, W> !UnwindSafe for Rewriter<'a, W>
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