pub(crate) struct SliceWriter<T> {
data: T,
offset: usize,
}
Expand description
An object that supports writing into a byte-slice of fixed size.
Since the writer API does not allow all write_*
functions to report errors,
this type defers any truncated-data errors until you try to retrieve the
inner data.
Fields§
§data: T
The object we’re writing into. Must have fewer than usize::LEN bytes.
offset: usize
Our current write position within that object.
Implementations§
Source§impl<T> SliceWriter<T>
impl<T> SliceWriter<T>
Source§impl<T> SliceWriter<T>
impl<T> SliceWriter<T>
Sourcepub(crate) fn new(data: T) -> Self
pub(crate) fn new(data: T) -> Self
Construct a new SliceWriter
Typically, you would want to use this on a type that implements
AsMut<[u8]>
, or else it won’t be very useful.
Preexisting bytes in the data
object will be unchanged, unless you use
the [Writer
] API to write to them.
Sourcepub(crate) fn try_unwrap(self) -> Result<(T, usize), SliceWriterError>
pub(crate) fn try_unwrap(self) -> Result<(T, usize), SliceWriterError>
Try to extract the data from this SliceWriter
.
On success (if we did not write “off the end” of the underlying object), return the object and the number of bytes we wrote into it. (Bytes after that position are unchanged.)
On failure (if we tried to write too much), return an error.
Sourcepub(crate) fn offset(&self) -> Result<usize, SliceWriterError>
pub(crate) fn offset(&self) -> Result<usize, SliceWriterError>
Return the number of bytes written into this SliceWriter
so far,
or an error if it has overflowed.
Sourcepub(crate) fn assert_offset_is(&self, n: usize)
pub(crate) fn assert_offset_is(&self, n: usize)
debug_assert
that this writer is at position n
.
§Panics
Panics if debugging assertions are enabled and the write has overflowed,
or is at a position other than n
.
Trait Implementations§
Source§impl<T> Writer for SliceWriter<T>
impl<T> Writer for SliceWriter<T>
§fn write_u128(&mut self, x: u128)
fn write_u128(&mut self, x: u128)
§fn write_zeros(&mut self, n: usize)
fn write_zeros(&mut self, n: usize)
§fn write<E>(&mut self, e: &E) -> Result<(), EncodeError>where
E: Writeable + ?Sized,
fn write<E>(&mut self, e: &E) -> Result<(), EncodeError>where
E: Writeable + ?Sized,
§fn write_and_consume<E>(&mut self, e: E) -> Result<(), EncodeError>where
E: WriteableOnce,
fn write_and_consume<E>(&mut self, e: E) -> Result<(), EncodeError>where
E: WriteableOnce,
§fn write_nested_u8len(&mut self) -> NestedWriter<'_, Self, u8>
fn write_nested_u8len(&mut self) -> NestedWriter<'_, Self, u8>
§fn write_nested_u16len(&mut self) -> NestedWriter<'_, Self, u16>
fn write_nested_u16len(&mut self) -> NestedWriter<'_, Self, u16>
§fn write_nested_u32len(&mut self) -> NestedWriter<'_, Self, u32>
fn write_nested_u32len(&mut self) -> NestedWriter<'_, Self, u32>
Auto Trait Implementations§
impl<T> Freeze for SliceWriter<T>where
T: Freeze,
impl<T> RefUnwindSafe for SliceWriter<T>where
T: RefUnwindSafe,
impl<T> Send for SliceWriter<T>where
T: Send,
impl<T> Sync for SliceWriter<T>where
T: Sync,
impl<T> Unpin for SliceWriter<T>where
T: Unpin,
impl<T> UnwindSafe for SliceWriter<T>where
T: UnwindSafe,
Blanket Implementations§
§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
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
§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait>
(where Trait: Downcast
) to Box<dyn Any>
, which can then be
downcast
into Box<dyn ConcreteType>
where ConcreteType
implements Trait
.§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait>
(where Trait: Downcast
) to Rc<Any>
, which can then be further
downcast
into Rc<ConcreteType>
where ConcreteType
implements Trait
.§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &Any
’s vtable from &Trait
’s.§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &mut Any
’s vtable from &mut Trait
’s.§impl<T> DowncastSend for T
impl<T> DowncastSend for T
§impl<T> DowncastSync for T
impl<T> DowncastSync for T
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more