pub struct Buffer<const N: usize, const PREFIX: bool = false> { /* private fields */ }Expand description
A correctly sized stack allocation for the formatted bytes to be written into.
N is the amount of bytes of the input, while PREFIX specifies whether
the “0x” prefix is prepended to the output.
Note that this buffer will contain only the prefix, if specified, and null (‘\0’) bytes before any formatting is done.
§Examples
let mut buffer = const_hex::Buffer::<4>::new();
let printed = buffer.format(b"1234");
assert_eq!(printed, "31323334");Implementations§
Source§impl<const N: usize, const PREFIX: bool> Buffer<N, PREFIX>
impl<const N: usize, const PREFIX: bool> Buffer<N, PREFIX>
Sourcepub const fn new() -> Buffer<N, PREFIX>
pub const fn new() -> Buffer<N, PREFIX>
This is a cheap operation; you don’t need to worry about reusing buffers for efficiency.
Sourcepub const fn const_format(self, array: &[u8; N]) -> Buffer<N, PREFIX>
pub const fn const_format(self, array: &[u8; N]) -> Buffer<N, PREFIX>
Print an array of bytes into this buffer.
Sourcepub const fn const_format_upper(self, array: &[u8; N]) -> Buffer<N, PREFIX>
pub const fn const_format_upper(self, array: &[u8; N]) -> Buffer<N, PREFIX>
Print an array of bytes into this buffer.
Sourcepub fn format(&mut self, array: &[u8; N]) -> &mut str
pub fn format(&mut self, array: &[u8; N]) -> &mut str
Print an array of bytes into this buffer and return a reference to its lower hex string representation within the buffer.
Sourcepub fn format_upper(&mut self, array: &[u8; N]) -> &mut str
pub fn format_upper(&mut self, array: &[u8; N]) -> &mut str
Print an array of bytes into this buffer and return a reference to its upper hex string representation within the buffer.
Sourcepub fn format_slice<T>(&mut self, slice: T) -> &mut str
pub fn format_slice<T>(&mut self, slice: T) -> &mut str
Print a slice of bytes into this buffer and return a reference to its lower hex string representation within the buffer.
§Panics
If the slice is not exactly N bytes long.
Sourcepub fn format_slice_upper<T>(&mut self, slice: T) -> &mut str
pub fn format_slice_upper<T>(&mut self, slice: T) -> &mut str
Print a slice of bytes into this buffer and return a reference to its upper hex string representation within the buffer.
§Panics
If the slice is not exactly N bytes long.
Sourcepub const fn as_str(&self) -> &str
pub const fn as_str(&self) -> &str
Returns a reference to the underlying bytes casted to a string slice.
Sourcepub fn as_mut_str(&mut self) -> &mut str
pub fn as_mut_str(&mut self) -> &mut str
Returns a mutable reference to the underlying bytes casted to a string slice.
Sourcepub const fn as_byte_array<const LEN: usize>(&self) -> &[u8; LEN]
pub const fn as_byte_array<const LEN: usize>(&self) -> &[u8; LEN]
Sourcepub fn as_mut_byte_array<const LEN: usize>(&mut self) -> &mut [u8; LEN]
pub fn as_mut_byte_array<const LEN: usize>(&mut self) -> &mut [u8; LEN]
Returns a mutable reference the underlying stack-allocated byte array.
§Panics
If LEN does not equal Self::LEN.
See as_byte_array for more information.
Sourcepub unsafe fn as_mut_bytes(&mut self) -> &mut [u8] ⓘ
pub unsafe fn as_mut_bytes(&mut self) -> &mut [u8] ⓘ
Returns a mutable reference to the underlying bytes.
§Safety
The caller must ensure that the content of the slice is valid UTF-8
before the borrow ends and the underlying str is used.
Use of a str whose contents are not valid UTF-8 is undefined behavior.
Sourcepub unsafe fn buffer(&mut self) -> &mut [u8] ⓘ
pub unsafe fn buffer(&mut self) -> &mut [u8] ⓘ
Returns a mutable reference to the underlying buffer, excluding the prefix.
§Safety
See as_mut_bytes.
Sourcepub const fn as_ptr(&self) -> *const u8
pub const fn as_ptr(&self) -> *const u8
Returns a raw pointer to the buffer.
The caller must ensure that the buffer outlives the pointer this function returns, or else it will end up pointing to garbage.
Sourcepub fn as_mut_ptr(&mut self) -> *mut u8
pub fn as_mut_ptr(&mut self) -> *mut u8
Returns an unsafe mutable pointer to the slice’s buffer.
The caller must ensure that the slice outlives the pointer this function returns, or else it will end up pointing to garbage.
Trait Implementations§
Auto Trait Implementations§
impl<const N: usize, const PREFIX: bool> Freeze for Buffer<N, PREFIX>
impl<const N: usize, const PREFIX: bool> RefUnwindSafe for Buffer<N, PREFIX>
impl<const N: usize, const PREFIX: bool> Send for Buffer<N, PREFIX>
impl<const N: usize, const PREFIX: bool> Sync for Buffer<N, PREFIX>
impl<const N: usize, const PREFIX: bool> Unpin for Buffer<N, PREFIX>
impl<const N: usize, const PREFIX: bool> UnsafeUnpin for Buffer<N, PREFIX>
impl<const N: usize, const PREFIX: bool> UnwindSafe for Buffer<N, PREFIX>
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§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.Source§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.Source§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.Source§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.Source§impl<T> DowncastSend for T
impl<T> DowncastSend for T
Source§impl<T> DowncastSync for T
impl<T> DowncastSync for T
Source§impl<T> FmtForward for T
impl<T> FmtForward for T
Source§fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
self to use its Binary implementation when Debug-formatted.Source§fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
self to use its Display implementation when
Debug-formatted.Source§fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
self to use its LowerExp implementation when
Debug-formatted.Source§fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
self to use its LowerHex implementation when
Debug-formatted.Source§fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
self to use its Octal implementation when Debug-formatted.Source§fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
self to use its Pointer implementation when
Debug-formatted.Source§fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
self to use its UpperExp implementation when
Debug-formatted.Source§fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
self to use its UpperHex implementation when
Debug-formatted.Source§impl<T> Pipe for Twhere
T: ?Sized,
impl<T> Pipe for Twhere
T: ?Sized,
Source§fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
Source§fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
self and passes that borrow into the pipe function. Read moreSource§fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
self and passes that borrow into the pipe function. Read moreSource§fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
Source§fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R,
) -> R
fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R, ) -> R
Source§fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
self, then passes self.as_ref() into the pipe function.Source§fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
self, then passes self.as_mut() into the pipe
function.Source§fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
self, then passes self.deref() into the pipe function.Source§impl<T> Tap for T
impl<T> Tap for T
Source§fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
Borrow<B> of a value. Read moreSource§fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
BorrowMut<B> of a value. Read moreSource§fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
AsRef<R> view of a value. Read moreSource§fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
AsMut<R> view of a value. Read moreSource§fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
Deref::Target of a value. Read moreSource§fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
Deref::Target of a value. Read moreSource§fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
.tap() only in debug builds, and is erased in release builds.Source§fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
.tap_mut() only in debug builds, and is erased in release
builds.Source§fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
.tap_borrow() only in debug builds, and is erased in release
builds.Source§fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
.tap_borrow_mut() only in debug builds, and is erased in release
builds.Source§fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
.tap_ref() only in debug builds, and is erased in release
builds.Source§fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
.tap_ref_mut() only in debug builds, and is erased in release
builds.Source§fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
.tap_deref() only in debug builds, and is erased in release
builds.