| // Copyright 2026 The Fuchsia Authors. All rights reserved. | |
| // Use of this source code is governed by a BSD-style license that can be | |
| // found in the LICENSE file. | |
| //! Zero-cost formatting trait re-exports and fixed-capacity [`StrBuf`] (`vx_core::fmt`). | |
| use ::core::mem::MaybeUninit; | |
| use ::core::ops::Deref; | |
| pub use ::core::fmt::{Debug, Display, Error, Formatter, Result, Write}; | |
| /// Error returned when an append would exceed a [`StrBuf`]'s fixed capacity. | |
| /// | |
| /// Zero-sized so `Result<(), BufOverflow>` fits in a single register (`1 B`). | |
| #[derive(Copy, Clone, PartialEq, Eq)] | |
| pub struct BufOverflow; | |
| const _: () = assert!(::core::mem::size_of::<BufOverflow>() == 0); | |
| const _: () = assert!(::core::mem::size_of::<::core::result::Result<(), BufOverflow>>() == 1); | |
| // Implemented manually with `#[inline(always)]` rather than `#[derive(Debug)]` | |
| // so `vx_core.o` does not emit an out-of-line `Debug::fmt` referencing | |
| // unlinked `core::fmt::Formatter::write_str`. | |
| impl ::core::fmt::Debug for BufOverflow { | |
| #[inline(always)] | |
| fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result { | |
| f.write_str("BufOverflow") | |
| } | |
| } | |
| /// Non-generic verified byte-append core backing [`StrBuf::push_str`]. | |
| /// | |
| /// Copies `src` into `buf[*len..*len + src.len()]` and advances `*len`, or | |
| /// returns `Err(BufOverflow)` without modifying `buf` or `*len`. | |
| #[curry::verified] | |
| pub fn str_buf_push_bytes( | |
| buf: &mut [MaybeUninit<u8>], | |
| len: &mut usize, | |
| src: &[u8], | |
| ) -> ::core::result::Result<(), BufOverflow> { | |
| let start = *len; | |
| if start > buf.len() || src.len() > buf.len() - start { | |
| return Err(BufOverflow); | |
| } | |
| let mut i: usize = 0; | |
| while i < src.len() { | |
| let Some(&b) = src.get(i) else { | |
| return Err(BufOverflow); | |
| }; | |
| let Some(slot) = buf.get_mut(start + i) else { | |
| return Err(BufOverflow); | |
| }; | |
| slot.write(b); | |
| i += 1; | |
| } | |
| *len = start + src.len(); | |
| Ok(()) | |
| } | |
| /// Fixed-capacity `#![no_std]` UTF-8 string buffer (zero heap, zero panics, | |
| /// zero `memset`/`memcpy`). | |
| /// | |
| /// All mutating byte-copy work delegates to the non-generic verified core | |
| /// [`str_buf_push_bytes`] behind `#[inline(always)]` wrappers so callers stay | |
| /// ergonomic while compiled objects contain zero monomorphized generic loops. | |
| // Note: intentionally `#[repr(Rust)]` with explicit layout assertions below for | |
| // `StrBuf<64>` (`buf@+0, len@+64`) and `StrBuf<255>` (`len@+0, buf@+8`). | |
| pub struct StrBuf<const N: usize> { | |
| buf: [MaybeUninit<u8>; N], | |
| len: usize, | |
| } | |
| const _: () = assert!(::core::mem::size_of::<StrBuf<64>>() == 72); | |
| const _: () = assert!(::core::mem::align_of::<StrBuf<64>>() == 8); | |
| const _: () = assert!(::core::mem::offset_of!(StrBuf<64>, buf) == 0); | |
| const _: () = assert!(::core::mem::offset_of!(StrBuf<64>, len) == 64); | |
| const _: () = assert!(::core::mem::size_of::<StrBuf<255>>() == 264); | |
| const _: () = assert!(::core::mem::align_of::<StrBuf<255>>() == 8); | |
| const _: () = assert!(::core::mem::offset_of!(StrBuf<255>, len) == 0); | |
| const _: () = assert!(::core::mem::offset_of!(StrBuf<255>, buf) == 8); | |
| impl<const N: usize> StrBuf<N> { | |
| /// Construct an empty `StrBuf` without zeroing the backing array. | |
| #[inline(always)] | |
| pub const fn new() -> Self { | |
| Self { buf: [MaybeUninit::uninit(); N], len: 0 } | |
| } | |
| /// Current byte length of the accumulated UTF-8 string. | |
| #[inline(always)] | |
| pub const fn len(&self) -> usize { | |
| self.len | |
| } | |
| /// Maximum byte capacity `N` of this buffer. | |
| #[inline(always)] | |
| pub const fn capacity(&self) -> usize { | |
| N | |
| } | |
| /// Remaining free byte capacity (`N - self.len()`). | |
| #[inline(always)] | |
| pub const fn remaining(&self) -> usize { | |
| N - self.len | |
| } | |
| /// Return `true` iff no bytes have been appended. | |
| #[inline(always)] | |
| pub const fn is_empty(&self) -> bool { | |
| self.len == 0 | |
| } | |
| /// Reset the accumulated length to `0`. | |
| #[inline(always)] | |
| pub fn clear(&mut self) { | |
| self.len = 0; | |
| } | |
| /// Append valid UTF-8 slice `s`, returning `Err(BufOverflow)` without | |
| /// mutating `self` if `self.len() + s.len() > N`. | |
| #[inline(always)] | |
| pub fn push_str(&mut self, s: &str) -> ::core::result::Result<(), BufOverflow> { | |
| str_buf_push_bytes(&mut self.buf, &mut self.len, s.as_bytes()) | |
| } | |
| /// View the accumulated UTF-8 bytes as `&[u8]`. | |
| #[inline(always)] | |
| pub fn as_bytes(&self) -> &[u8] { | |
| // SAFETY: `self.buf[0..self.len]` is strictly initialized by `push_str` | |
| // (via `str_buf_push_bytes`), and `self.len <= N` is preserved by every method. | |
| unsafe { ::core::slice::from_raw_parts(self.buf.as_ptr() as *const u8, self.len) } | |
| } | |
| /// View the accumulated bytes as a UTF-8 `&str`. | |
| #[inline(always)] | |
| pub fn as_str(&self) -> &str { | |
| let bytes = self.as_bytes(); | |
| // SAFETY: `self.buf[0..self.len]` is formed strictly by concatenating | |
| // valid UTF-8 `&str` slices via `push_str`, preserving UTF-8. | |
| unsafe { ::core::str::from_utf8_unchecked(bytes) } | |
| } | |
| } | |
| impl<const N: usize> Default for StrBuf<N> { | |
| #[inline(always)] | |
| fn default() -> Self { | |
| Self::new() | |
| } | |
| } | |
| impl<const N: usize> Deref for StrBuf<N> { | |
| type Target = str; | |
| #[inline(always)] | |
| fn deref(&self) -> &str { | |
| self.as_str() | |
| } | |
| } | |
| impl<const N: usize> AsRef<str> for StrBuf<N> { | |
| #[inline(always)] | |
| fn as_ref(&self) -> &str { | |
| self.as_str() | |
| } | |
| } | |
| impl<const N: usize> AsRef<[u8]> for StrBuf<N> { | |
| #[inline(always)] | |
| fn as_ref(&self) -> &[u8] { | |
| self.as_bytes() | |
| } | |
| } | |
| impl<const N: usize> Write for StrBuf<N> { | |
| #[inline(always)] | |
| fn write_str(&mut self, s: &str) -> Result { | |
| self.push_str(s).map_err(|_| Error) | |
| } | |
| } |