pub trait Writer {
type Ok;
type Mut<'this>: Writer
where Self: 'this;
// Required methods
fn finish<C>(&mut self, cx: C) -> Result<Self::Ok, C::Error>
where C: Context;
fn borrow_mut(&mut self) -> Self::Mut<'_>;
fn extend<C>(
&mut self,
cx: C,
buffer: Vec<u8, C::Allocator>,
) -> Result<(), C::Error>
where C: Context;
fn write_bytes<C>(&mut self, cx: C, bytes: &[u8]) -> Result<(), C::Error>
where C: Context;
// Provided method
fn write_byte<C>(&mut self, cx: C, b: u8) -> Result<(), C::Error>
where C: Context { ... }
}Expand description
The trait governing how a writer works.
§Examples
use musli::{Context, Writer};
use musli::context;
// Example using Writer as a trait bound
fn write_greeting<W, C>(mut writer: W, cx: C) -> Result<W::Ok, C::Error>
where
W: Writer,
C: Context,
{
writer.write_bytes(cx, b"Hello")?;
writer.write_byte(cx, b' ')?;
writer.write_bytes(cx, b"World")?;
writer.finish(cx)
}
let mut writer = Vec::new();
let cx = context::new();
write_greeting(&mut writer, &cx)?;
assert_eq!(writer, b"Hello World");Required Associated Types§
Sourcetype Mut<'this>: Writer
where
Self: 'this
type Mut<'this>: Writer where Self: 'this
Reborrowed type.
Why oh why would we want to do this over having a simple &'this mut T?
We want to avoid recursive types, which will blow up the compiler. And
the above is a typical example of when that can go wrong. This ensures
that each call to borrow_mut dereferences the Reader at each step to
avoid constructing a large muted type, like &mut &mut &mut VecWriter.
Required Methods§
Sourcefn finish<C>(&mut self, cx: C) -> Result<Self::Ok, C::Error>where
C: Context,
fn finish<C>(&mut self, cx: C) -> Result<Self::Ok, C::Error>where
C: Context,
Finalize the writer and return the output.
Sourcefn borrow_mut(&mut self) -> Self::Mut<'_>
fn borrow_mut(&mut self) -> Self::Mut<'_>
Reborrow the current type.
§Examples
use musli::{Context, Writer};
use musli::context;
let mut writer = Vec::new();
let cx = context::new();
{
let mut borrowed = writer.borrow_mut();
borrowed.write_bytes(&cx, b"Hello")?;
}
writer.write_bytes(&cx, b" World")?;
writer.finish(&cx)?;
assert_eq!(writer, b"Hello World");Sourcefn extend<C>(
&mut self,
cx: C,
buffer: Vec<u8, C::Allocator>,
) -> Result<(), C::Error>where
C: Context,
fn extend<C>(
&mut self,
cx: C,
buffer: Vec<u8, C::Allocator>,
) -> Result<(), C::Error>where
C: Context,
Write a buffer to the current writer.
This method is used internally to write a musli Vec buffer to the writer.
Most users will use write_bytes instead.
Sourcefn write_bytes<C>(&mut self, cx: C, bytes: &[u8]) -> Result<(), C::Error>where
C: Context,
fn write_bytes<C>(&mut self, cx: C, bytes: &[u8]) -> Result<(), C::Error>where
C: Context,
Write bytes to the current writer.
§Examples
use musli::{Context, Writer};
use musli::context;
let mut writer = Vec::new();
let cx = context::new();
writer.write_bytes(&cx, b"Hello")?;
writer.write_bytes(&cx, b" ")?;
writer.write_bytes(&cx, b"World")?;
writer.finish(&cx)?;
assert_eq!(writer, b"Hello World");Provided Methods§
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.
Implementations on Foreign Types§
Source§impl Writer for Vec<u8>
Available on crate feature alloc only.
impl Writer for Vec<u8>
alloc only.