Skip to main content

Crate zcstring

Crate zcstring 

Source
Expand description

§ZCString

ZCString is a wrapper around arcstr::Substr designed for zero-copy parsing using a thread-local context. It allows you to track a “source” string and derive substrings from it without unnecessary allocations. serde_json is currently supported by default.

§Main Functionality

  • Context-aware creation: Uses a thread-local SOURCE to check if a new string is actually a sub-slice of an existing managed string.
  • RAII Guards: Provides a SourceGuard to safely manage the lifecycle of the thread-local source.
  • Serde Integration: Optional (defaults to on) support for efficient zero-copy deserialization via the serde feature flag.

§Crate Features

  • default By default, serde and std are enabled.
  • serde (Optional): Enables serialization and deserialization support via serde and serde_json.
  • std (Optional): Enables From<String> implementations.

§serde_json example

use arcstr::literal;
use serde::Deserialize;
use std::error::Error;
use zcstring::{ZCString, serde_json_from_zcstring};

#[derive(Debug, Deserialize)]
struct Animal {
   animal: ZCString,
   color: ZCString,
}

fn main() -> Result<(), Box<dyn Error>> {
    let json = literal!(r#"{"animal":"cat", "color": "red"}"#);
    // ZCString::from below is zero-copy from the ArcStr json
    // when done struct members will point to memory within
    // the json string.
    let animal = serde_json_from_zcstring::<Animal>(json.into())?;
    println!("animal: {:?}", animal);
    Ok(())
}

Structs§

SourceGuard
An RAII guard used to manage the lifecycle of the thread-local string source.
ZCString
ZCString wrapper struct
ZCStringIterWrapper
str iterator wrapper automatically converts &str to ZCString maintaining source references.

Enums§

ReaderErrorstd

Functions§

serde_json_from_zcstringserde_json
Parses a JSON string into type T while using the provided ZCString as the context for any zero-copy deserialization.