Skip to main content

PrettyStringSink

Struct PrettyStringSink 

Source
pub struct PrettyStringSink<'a> { /* private fields */ }
Expand description

JsonWrite sink that emits indented, multi-line JSON.

Wraps a String and tracks container depth + a one-byte lookahead (pending_open). When an opener ([ / {) is followed immediately by the matching closer (no contents), the sink emits the compact form [] / {}. Otherwise it inserts a newline plus the current indent before each element and before the closing bracket.

Indent unit defaults to two spaces; configure via Self::with_indent.

Implementations§

Source§

impl<'a> PrettyStringSink<'a>

Source

pub const fn new(out: &'a mut String) -> Self

Build a pretty sink writing to out with the default 2-space indent.

Source

pub const fn with_indent(out: &'a mut String, indent: &'static str) -> Self

Build a pretty sink with a custom indent string. Pass "\t" for tabs, " " for four spaces, etc. The indent must be pure whitespace — JSON doesn’t validate it on the wire, but emitting non-whitespace would corrupt the output.

Trait Implementations§

Source§

impl<'a> Debug for PrettyStringSink<'a>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl JsonWrite for PrettyStringSink<'_>

Available on crate feature alloc only.
Source§

type Error = Error

Sink-specific error type. 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 write_byte(&mut self, b: u8) -> Result<(), Self::Error>

Append a single ASCII byte. Used for structural punctuation ({, }, [, ], ,, :, ").
Source§

fn write_str_raw(&mut self, s: &str) -> Result<(), Self::Error>

Append a &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_escaped_str(&mut self, s: &str) -> Result<(), Self::Error>

Append a JSON-quoted, escaped string (including the surrounding " 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_float_f64(&mut self, f: f64) -> Result<(), Self::Error>

Write a finite f64 as a JSON number. Non-finite inputs (inf, -inf, NaN) have no JSON representation; sinks reject them via their Self::Error type. Read more
Source§

fn reserve_hint(&mut self, _additional: usize)

Hint that at least additional more bytes will be written. Sinks backed by a growable buffer (like ByteSink) can amortize capacity growth across a known-size sequence; sinks without a reservation concept treat this as a no-op. Read more
Source§

unsafe fn write_byte_unchecked(&mut self, b: u8) -> Result<(), Self::Error>

Append a single ASCII byte WITHOUT a per-write capacity check. Read more
Source§

fn write_raw_bytes(&mut self, b: &[u8]) -> Result<(), Self::Error>

Append a byte slice whose contents are known-valid UTF-8. Read more
Source§

fn write_int_i64(&mut self, n: i64) -> Result<(), Self::Error>

Write a signed 64-bit integer as a JSON number.
Source§

fn write_int_u64(&mut self, n: u64) -> Result<(), Self::Error>

Write an unsigned 64-bit integer as a JSON number.
Source§

fn write_int_i128(&mut self, n: i128) -> Result<(), Self::Error>

Write a signed 128-bit integer as a JSON number.
Source§

fn write_int_u128(&mut self, n: u128) -> Result<(), Self::Error>

Write an unsigned 128-bit integer as a JSON number.
Source§

unsafe fn write_float_f64_unchecked( &mut self, f: f64, ) -> Result<(), Self::Error>

Write a finite f64 ASSUMING the caller has reserved at least f64::MAX_SERIALIZED_LEN (32) bytes of sink capacity. Skips the per-call cap check in Vec::extend_from_slice — the second-largest mispredict source after the comma Vec::push. Read more
Source§

unsafe fn write_float_f64_unchecked_finite( &mut self, f: f64, ) -> Result<(), Self::Error>

Like write_float_f64_unchecked, but additionally requires the caller to have validated that f is finite. The per-element finiteness branch in the bench’s hot loop was the dominant remaining mispredict source after the cap-check elimination; hoisting validation to a one-shot pre-scan over the whole slice removes that branch from the inner loop entirely. Read more
Source§

unsafe fn write_float_f64_taint(&mut self, f: f64) -> Result<(), Self::Error>

Per-element float write that branchlessly tolerates non-finite input. Tainted bytes (junk ASCII for non-finite values) land in the sink; the caller is expected to query the sink’s take_nonfinite_taint() once at the end of the slice and turn a non-zero result into a typed error. Read more
Source§

fn take_nonfinite_taint(&mut self) -> u64

Read and clear the accumulated non-finite taint from the sink. Returns 0 for sinks without taint tracking. The array writer queries this once after a batch of write_float_f64_taint calls; a non-zero result means at least one input was inf/NaN and the call must return Err(NonFiniteFloat).

Auto Trait Implementations§

§

impl<'a> !UnwindSafe for PrettyStringSink<'a>

§

impl<'a> Freeze for PrettyStringSink<'a>

§

impl<'a> RefUnwindSafe for PrettyStringSink<'a>

§

impl<'a> Send for PrettyStringSink<'a>

§

impl<'a> Sync for PrettyStringSink<'a>

§

impl<'a> Unpin for PrettyStringSink<'a>

§

impl<'a> UnsafeUnpin for PrettyStringSink<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.