pub struct FmtWriter<'a> { /* private fields */ }Expand description
๐น A specialized formatter with a fixed byte buffer and truncation detection.
๐ text/fmt
It should be faster at runtime than the default formatter.
Implementationsยง
Sourceยงimpl<'a> FmtWriter<'a>
impl<'a> FmtWriter<'a>
Sourcepub const fn is_truncated(&self) -> bool
pub const fn is_truncated(&self) -> bool
Returns true if truncation occurred.
Sourcepub const fn written_len(&self) -> usize
pub const fn written_len(&self) -> usize
Returns the number of bytes written.
Sourcepub fn format(
buf: &'a mut [u8],
args: FmtArguments<'_>,
) -> Result<&'a str, &'a str> โ
pub fn format( buf: &'a mut [u8], args: FmtArguments<'_>, ) -> Result<&'a str, &'a str> โ
Writes formatted output into the given byte buffer, returning a string slice.
Returns:
Ok(&str)if all the formatted data fits intobuf.Err(&str)containing the valid partial result if truncation occurred.
ยงExamples
let mut buf = [0u8; 32]; // Big enough to fit everything
let s = FmtWriter::format(&mut buf, format_args!["Test: {} {}", "foo", 42]);
assert_eq!(Ok("Test: foo 42"), s);
let mut buf = [0u8; 9]; // Can't fit everything
let s = FmtWriter::format(&mut buf, format_args!["Test: {} {}", "foo", 42]);
assert_eq!(Err("Test: foo"), s);Sourcepub fn format_len(
buf: &'a mut [u8],
args: FmtArguments<'_>,
) -> Result<usize, usize> โ
pub fn format_len( buf: &'a mut [u8], args: FmtArguments<'_>, ) -> Result<usize, usize> โ
Writes formatted output into the given byte buffer, returning the number of written bytes.
Returns:
Ok(usize)if all the formatted data fits intobuf.Err(usize)containing the number of valid UTF-8 written bytes if truncation ocurred,
ยงExamples
let mut buf = [0u8; 32];
assert_eq![Ok(12), FmtWriter::format_len(&mut buf, format_args!["Test: {} {}", "foo", 42])];
let mut buf = [0u8; 9];
assert_eq![Err(9), FmtWriter::format_len(&mut buf, format_args!["Test: {} {}", "foo", 42])];Sourcepub fn format_len_unchecked(buf: &'a mut [u8], args: FmtArguments<'_>) -> usize
pub fn format_len_unchecked(buf: &'a mut [u8], args: FmtArguments<'_>) -> usize
Writes formatted output into the given byte buffer, returning the number of written bytes.
Sourcepub const fn write_str_truncate(&mut self, s: &str) -> usize
pub const fn write_str_truncate(&mut self, s: &str) -> usize
Writes a string slice to the buffer, returning the number of bytes written.
It copies as much of the string as will fit into the remaining buffer space.
ยงExamples
const fn create_message(buf: &mut [u8]) -> &str {
let mut writer = FmtWriter::new(buf);
writer.write_str_truncate("Hello");
writer.write_str_truncate(" world");
writer.into_str_const()
}Sourcepub const fn write_str_truncate_checked(
&mut self,
s: &str,
) -> Result<&str, &str> โ
pub const fn write_str_truncate_checked( &mut self, s: &str, ) -> Result<&str, &str> โ
Writes a string slice, returning the actually written part.
Returns Err if truncation occurred, with the truncated result.
Sourcepub const fn write_str_truncate_checked_len(
&mut self,
s: &str,
) -> Result<usize, usize> โ
pub const fn write_str_truncate_checked_len( &mut self, s: &str, ) -> Result<usize, usize> โ
Writes a string slice, returning the number of the bytes actually written.
Sourcepub fn as_str(&self) -> &str โ
pub fn as_str(&self) -> &str โ
Returns the written content as a valid UTFโ8 string.
If the final write ended in the middle of a multiโbyte codepoint only the valid prefix is returned.
ยงFeatures
Makes use of the dep_simd_utf8 feature to accelerate validation,
and of the unsafe_str feature to avoid double validation.
Sourcepub fn into_str(self) -> &'a str โ
pub fn into_str(self) -> &'a str โ
Returns the written content as a valid UTFโ8 string.
If the final write ended in the middle of a multiโbyte codepoint only the valid prefix is returned.
ยงFeatures
Makes use of the dep_simd_utf8 feature to accelerate validation,
and of the unsafe_str feature to avoid double validation.
Sourcepub const fn as_str_const(&'a self) -> &'a str โ
pub const fn as_str_const(&'a self) -> &'a str โ
Returns the written content as a valid UTFโ8 string.
If the final write ended in the middle of a multiโbyte codepoint only the valid prefix is returned. Compile-time friendly version using basic validation.
ยงFeatures
Makes use of the unsafe_str feature to avoid double validation.
Sourcepub const fn into_str_const(self) -> &'a str โ
pub const fn into_str_const(self) -> &'a str โ
Returns the written content as a valid UTFโ8 string.
If the final write ended in the middle of a multiโbyte codepoint only the valid prefix is returned. Compile-time friendly version using basic validation.
ยงFeatures
Makes use of the unsafe_str feature to avoid double validation.
Trait Implementationsยง
Auto Trait Implementationsยง
impl<'a> !UnwindSafe for FmtWriter<'a>
impl<'a> Freeze for FmtWriter<'a>
impl<'a> RefUnwindSafe for FmtWriter<'a>
impl<'a> Send for FmtWriter<'a>
impl<'a> Sync for FmtWriter<'a>
impl<'a> Unpin for FmtWriter<'a>
impl<'a> UnsafeUnpin for FmtWriter<'a>
Blanket Implementationsยง
Sourceยงimpl<T> AnyExt for T
impl<T> AnyExt for T
Sourceยงfn type_hash_with<H: Hasher>(&self, hasher: H) -> u64
fn type_hash_with<H: Hasher>(&self, hasher: H) -> u64
TypeId of Self using a custom hasher.Sourceยงfn as_any_mut(&mut self) -> &mut dyn Anywhere
Self: Sized,
fn as_any_mut(&mut self) -> &mut dyn Anywhere
Self: Sized,
Sourceยงfn as_any_box(self: Box<Self>) -> Box<dyn Any>where
Self: Sized,
fn as_any_box(self: Box<Self>) -> Box<dyn Any>where
Self: Sized,
alloc only.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> ByteSized for T
impl<T> ByteSized for T
Sourceยงconst BYTE_ALIGN: usize = _
const BYTE_ALIGN: usize = _
Sourceยงfn byte_align(&self) -> usize
fn byte_align(&self) -> usize
Sourceยงfn ptr_size_ratio(&self) -> [usize; 2]
fn ptr_size_ratio(&self) -> [usize; 2]
Sourceยงimpl<T> MemExt for Twhere
T: ?Sized,
impl<T> MemExt for Twhere
T: ?Sized,
Sourceยงconst NEEDS_DROP: bool = _
const NEEDS_DROP: bool = _
Sourceยงfn mem_align_of<T>() -> usize
fn mem_align_of<T>() -> usize
Sourceยงfn mem_align_of_val(&self) -> usize
fn mem_align_of_val(&self) -> usize
Sourceยงfn mem_size_of<T>() -> usize
fn mem_size_of<T>() -> usize
Sourceยงfn mem_size_of_val(&self) -> usize
fn mem_size_of_val(&self) -> usize
Sourceยงfn mem_needs_drop(&self) -> bool
fn mem_needs_drop(&self) -> bool
true if dropping values of this type matters. Read moreSourceยงfn mem_forget(self)where
Self: Sized,
fn mem_forget(self)where
Self: Sized,
self without running its destructor. Read moreSourceยงfn mem_replace(&mut self, other: Self) -> Selfwhere
Self: Sized,
fn mem_replace(&mut self, other: Self) -> Selfwhere
Self: Sized,
Sourceยงunsafe fn mem_zeroed<T>() -> T
unsafe fn mem_zeroed<T>() -> T
unsafe_layout only.T represented by the all-zero byte-pattern. Read moreSourceยงunsafe fn mem_transmute_copy<Src, Dst>(src: &Src) -> Dst
unsafe fn mem_transmute_copy<Src, Dst>(src: &Src) -> Dst
unsafe_layout only.T represented by the all-zero byte-pattern. Read moreSourceยงfn mem_as_bytes(&self) -> &[u8] โ
fn mem_as_bytes(&self) -> &[u8] โ
unsafe_slice only.