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
impl ZstdDictionary
Sourcepub fn from_bytes(bytes: Vec<u8>) -> Result<Self>
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:
bytesis emptybytes.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());Sourcepub fn is_empty(&self) -> bool
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
impl Clone for ZstdDictionary
Source§fn clone(&self) -> ZstdDictionary
fn clone(&self) -> ZstdDictionary
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for ZstdDictionary
impl Debug for ZstdDictionary
Source§impl PartialEq for ZstdDictionary
impl PartialEq for ZstdDictionary
impl Eq for ZstdDictionary
impl StructuralPartialEq for ZstdDictionary
Auto Trait Implementations§
impl Freeze for ZstdDictionary
impl RefUnwindSafe for ZstdDictionary
impl Send for ZstdDictionary
impl Sync for ZstdDictionary
impl Unpin for ZstdDictionary
impl UnsafeUnpin for ZstdDictionary
impl UnwindSafe for ZstdDictionary
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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