/// A buffer for construct a string while avoiding heap allocation. /// /// The only requirement is that the buffer is large enough to hold the formatted string. pubstruct WriteBuffer<const SIZE: usize> {
buf: [MaybeUninit<u8>; SIZE],
len: usize,
}
impl<const SIZE: usize> core::ops::Deref for WriteBuffer<SIZE> { type Target = str;
fn deref(&self) -> &Self::Target { // SAFETY: `buf` is only written to by the `fmt::Write::write_str` implementation which // writes a valid UTF-8 string to `buf` and correctly sets `len`. unsafe { let s = maybe_uninit_slice_assume_init_ref(&self.buf[..self.len]);
str::from_utf8_unchecked(s)
}
}
}
/// Equivalent of [`MaybeUninit::uninit_array`] that compiles on stable. #[must_use] #[inline(always)] constfn maybe_uninit_uninit_array<T, const N: usize>() -> [MaybeUninit<T>; N] { // SAFETY: An uninitialized `[MaybeUninit<_>; LEN]` is valid. unsafe { MaybeUninit::<[MaybeUninit<T>; N]>::uninit().assume_init() }
}
/// Equivalent of [`MaybeUninit::write_slice`] that compiles on stable. fn maybe_uninit_write_slice<'a, T>(this: &'a mut [MaybeUninit<T>], src: &[T]) -> &e='color:blue'>'a mut [T] where
T: Copy,
{ #[allow(trivial_casts)] // SAFETY: T and MaybeUninit<T> have the same layout let uninit_src = unsafe { &*(src as *const [T] as *const [MaybeUninit<T>]) };
this.copy_from_slice(uninit_src);
// SAFETY: Valid elements have just been copied into `this` so it is initialized unsafe { maybe_uninit_slice_assume_init_mut(this) }
}
/// Equivalent of [`MaybeUninit::slice_assume_init_mut`] that compiles on stable. /// /// # Safety /// /// See [`MaybeUninit::slice_assume_init_mut`](https://doc.rust-lang.org/stable/std/mem/union.MaybeUninit.html#method.slice_assume_init_mut). #[inline(always)] unsafefn maybe_uninit_slice_assume_init_mut<T, U>(slice: &mut [MaybeUninit<T>]) -> &mut [U] { #[allow(trivial_casts)] // SAFETY: similar to safety notes for `slice_get_ref`, but we have a mutable reference which is // also guaranteed to be valid for writes. unsafe {
&mut *(slice as *mut [MaybeUninit<T>] as *mut [U])
}
}
/// Equivalent of [`MaybeUninit::slice_assume_init_ref`] that compiles on stable. /// /// # Safety /// /// See [`MaybeUninit::slice_assume_init_ref`](https://doc.rust-lang.org/stable/std/mem/union.MaybeUninit.html#method.slice_assume_init_ref). #[inline(always)] constunsafefn maybe_uninit_slice_assume_init_ref<T>(slice: &[MaybeUninit<T>]) -> &[T] { #[allow(trivial_casts)] // SAFETY: casting `slice` to a `*const [T]` is safe since the caller guarantees that `slice` is // initialized, and `MaybeUninit` is guaranteed to have the same layout as `T`. The pointer // obtained is valid since it refers to memory owned by `slice` which is a reference and thus // guaranteed to be valid for reads. unsafe {
&*(slice as *const [MaybeUninit<T>] as *const [T])
}
}
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.12 Sekunden
(vorverarbeitet am 2026-06-19)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.