Skip to main content

ZstdDictionary

Struct ZstdDictionary 

Source
pub struct ZstdDictionary(/* private fields */);
Expand description

A validated, size-bounded zstd dictionary blob.

Type invariant: self.len() <= MAX_DICT_SIZE (112 KiB) and the first four bytes are the zstd dictionary magic 0xEC30A437. All public constructors funnel through the private new_checked gate; callers outside this module cannot construct an invalid value.

Sharing is performed once at the enum level via Arc<ZstdDictionary> in crate::compression::secure::ByteCodec::ZstdDict. The inner Vec<u8> is intentionally not wrapped in a second Arc — that would create double indirection with no benefit.

§Examples

use pjson_rs::compression::zstd::{ZstdDictCompressor, ZstdDictionary, N_TRAIN};

// Build enough samples for training (at least 8 needed by libzstd; N_TRAIN = 32).
let item = b"{\"id\":1,\"name\":\"test\",\"value\":42,\"active\":true}";
let samples: Vec<Vec<u8>> = (0..N_TRAIN).map(|i| {
    format!("{{\"id\":{i},\"name\":\"item\",\"value\":{},\"active\":true}}", i * 10)
        .into_bytes()
}).collect();

let dict = ZstdDictCompressor::train(&samples, 65536).expect("training should succeed");
assert!(dict.len() <= 65536);
assert!(!dict.is_empty());

Implementations§

Source§

impl ZstdDictionary

Source

pub fn from_bytes(bytes: Vec<u8>) -> Result<Self>

Construct a ZstdDictionary from a raw byte blob produced by libzstd.

Validates the magic header and the 112 KiB size cap.

§Errors

Returns Error::CompressionError if:

  • bytes is empty
  • bytes.len() > MAX_DICT_SIZE
  • the first four bytes are not the zstd dictionary magic 0xEC30A437
§Examples
use pjson_rs::compression::zstd::ZstdDictionary;

// Empty bytes are rejected.
assert!(ZstdDictionary::from_bytes(vec![]).is_err());

// Bytes without the correct magic are rejected.
assert!(ZstdDictionary::from_bytes(vec![0x00, 0x01, 0x02, 0x03]).is_err());

// A blob larger than MAX_DICT_SIZE is rejected.
use pjson_rs::compression::zstd::MAX_DICT_SIZE;
let oversized = vec![0x37u8, 0xA4, 0x30, 0xEC]
    .into_iter()
    .chain(std::iter::repeat(0u8).take(MAX_DICT_SIZE))
    .collect::<Vec<_>>();
assert!(ZstdDictionary::from_bytes(oversized).is_err());
Source

pub fn as_bytes(&self) -> &[u8]

Returns the raw dictionary bytes.

Source

pub fn len(&self) -> usize

Returns the dictionary size in bytes (always <= MAX_DICT_SIZE).

Source

pub fn is_empty(&self) -> bool

Returns true if the dictionary has no bytes.

This can never be true for a successfully constructed ZstdDictionary because new_checked rejects empty inputs. The method exists to satisfy Clippy’s len_without_is_empty requirement.

Trait Implementations§

Source§

impl Clone for ZstdDictionary

Source§

fn clone(&self) -> ZstdDictionary

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for ZstdDictionary

Source§

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

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

impl PartialEq for ZstdDictionary

Source§

fn eq(&self, other: &ZstdDictionary) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Eq for ZstdDictionary

Source§

impl StructuralPartialEq for ZstdDictionary

Auto Trait Implementations§

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> FromRef<T> for T
where T: Clone,

Source§

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

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

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more