pub struct ByteSink<'a> { /* private fields */ }Expand description
JsonWrite sink that appends to a Vec<u8>.
Bypasses String’s UTF-8 invariant maintenance — every byte
written is known-valid by construction, so the caller can convert
to String via from_utf8_unchecked after serialization completes.
Implementations§
Trait Implementations§
Source§impl JsonWrite for ByteSink<'_>
Available on crate feature alloc only.
impl JsonWrite for ByteSink<'_>
alloc only.Source§unsafe fn write_byte_unchecked(&mut self, b: u8) -> Result<(), Self::Error>
unsafe fn write_byte_unchecked(&mut self, b: u8) -> Result<(), Self::Error>
Branchless byte append. Skips Vec::push’s capacity check; relies
on the caller having reserved enough via reserve_hint.
§Safety
self.out.len() < self.out.capacity() must hold. The caller’s
reserve_hint is what makes this safe in write_array for
primitive slices.
Source§unsafe fn write_float_f64_unchecked(
&mut self,
f: f64,
) -> Result<(), Self::Error>
unsafe fn write_float_f64_unchecked( &mut self, f: f64, ) -> Result<(), Self::Error>
SAFETY-relying override: caller must have reserved ≥ 32 bytes via
reserve_hint. Skips Vec::extend_from_slice’s per-call cap
check that the per-element float loop otherwise pays.
The non-finite arm is outlined via #[cold] so the branch
predictor gets a strong static hint that the error path is rare.
On perf record -e branch-misses the inlined Err::new(...)
path inflated the finiteness jg’s mispredict rate; outlining
it pushed the cold-arm PCs out of the hot loop’s history.
Source§unsafe fn write_float_f64_unchecked_finite(
&mut self,
f: f64,
) -> Result<(), Self::Error>
unsafe fn write_float_f64_unchecked_finite( &mut self, f: f64, ) -> Result<(), Self::Error>
Finite-known override: skip both the cap check AND the finiteness
check. The slice writer guarantees both preconditions via a
one-shot pre-scan + reserve_hint. This removes the last
data-dependent branch from the bench’s per-element loop.
§Safety
cap - len ≥ 32 AND f.is_finite().
Source§unsafe fn write_float_f64_taint(&mut self, f: f64) -> Result<(), Self::Error>
unsafe fn write_float_f64_taint(&mut self, f: f64) -> Result<(), Self::Error>
Taint-tracking float write: accumulates non-finite-ness into the
sink’s nonfinite_taint field for end-of-slice query. No
per-element branch on finiteness.
Source§type Error = Error
type Error = Error
StringSink uses core::convert::Infallible
for the byte/string writes; the typed-level to_string entry point
widens to crate::Error so non-finite floats can surface.Source§fn reserve_hint(&mut self, additional: usize)
fn reserve_hint(&mut self, additional: usize)
Source§fn write_byte(&mut self, b: u8) -> Result<(), Self::Error>
fn write_byte(&mut self, b: u8) -> Result<(), Self::Error>
{, }, [, ], ,, :, ").Source§fn write_str_raw(&mut self, s: &str) -> Result<(), Self::Error>
fn write_str_raw(&mut self, s: &str) -> Result<(), Self::Error>
&str verbatim. Caller is responsible for any escaping
— this is the structural / pre-escaped path. For user string
payloads, call Self::write_escaped_str instead.Source§fn write_raw_bytes(&mut self, b: &[u8]) -> Result<(), Self::Error>
fn write_raw_bytes(&mut self, b: &[u8]) -> Result<(), Self::Error>
Source§fn write_escaped_str(&mut self, s: &str) -> Result<(), Self::Error>
fn write_escaped_str(&mut self, s: &str) -> Result<(), Self::Error>
" characters). The default impl escapes one byte at a time
through write_byte; sinks with bulk-write capability (like
StringSink) override with a literal-run fast path.Source§fn write_int_i64(&mut self, n: i64) -> Result<(), Self::Error>
fn write_int_i64(&mut self, n: i64) -> Result<(), Self::Error>
Source§fn write_int_u64(&mut self, n: u64) -> Result<(), Self::Error>
fn write_int_u64(&mut self, n: u64) -> Result<(), Self::Error>
Source§fn write_int_i128(&mut self, n: i128) -> Result<(), Self::Error>
fn write_int_i128(&mut self, n: i128) -> Result<(), Self::Error>
Source§fn write_int_u128(&mut self, n: u128) -> Result<(), Self::Error>
fn write_int_u128(&mut self, n: u128) -> Result<(), Self::Error>
Source§fn write_float_f64(&mut self, f: f64) -> Result<(), Self::Error>
fn write_float_f64(&mut self, f: f64) -> Result<(), Self::Error>
f64 as a JSON number. Non-finite inputs (inf,
-inf, NaN) have no JSON representation; sinks reject them
via their Self::Error type. Read moreSource§fn take_nonfinite_taint(&mut self) -> u64
fn take_nonfinite_taint(&mut self) -> u64
write_float_f64_taint
calls; a non-zero result means at least one input was inf/NaN
and the call must return Err(NonFiniteFloat).