Struct chronicle::queue::BytesWriter
#[repr(C)]pub struct BytesWriter { /* private fields */ }
Expand description
A BytesWriter
enables a writer to directly access mutable bytes within a reserved section with explicit size at the current end of a Queue.
In some cases this can result in reduced write latency by eliminating a copy.
It also enables Queue to be directly integrated with components which write directly to memory.
A BytesWriter
internally wraps a write DocumentContext
, and so holds the Queue write lock for the duration of its scope.
As with DocumentContext
it is recommended to use the scoped form of BytesWriter
which automatically handles closure when dropped.
The size of the section of queue reserved by a BytesWriter
can be adjusted using resize
, and similarly to DocumentContext
write attempts can be abandoned using rollback_on_close
without changing the state of the Queue.
Data written using a BytesWriter
is normally read using a complementary BytesReader.
Example
let mut appender = queue.acquire_appender().unwrap();
{
let mut writer = appender.scoped_bytes_writer(MSGSIZE);
let slice = writer.as_mut().as_slice();
...
// directly write to the slice (which writes directly to the Queue)
...
// write lock released and write atomically committed as the writer goes out of scope
}
Implementations§
§impl BytesWriter
impl BytesWriter
§impl BytesWriter
impl BytesWriter
pub fn resize(self: Pin<&mut Self>, size: u64)
pub fn resize(self: Pin<&mut Self>, size: u64)
Resize the reserved section of the Queue wrapped by this BytesWriter
The reserved size can be extended as well as shrunk. The actual size of data written to the Queue
will be the original size requested when creating the BytesWriter
, or whichever size was passed
to the most recent resize
call.
§impl BytesWriter
impl BytesWriter
pub fn rollback_on_close(self: Pin<&mut Self>)
pub fn rollback_on_close(self: Pin<&mut Self>)
Abandon a write. Any changes will be discarded when this BytesWriter
goes out of scope, and the state of the
Queue will be as it was before the BytesWriter
was created.