pub struct Compressed { /* private fields */ }
Expand description
A compressed binary object with integrity verification.
Compressed
provides a way to efficiently store and transmit binary data
using the DEFLATE compression algorithm. It includes built-in integrity
verification through a CRC32 checksum and optional cryptographic digest.
The compression is implemented using the raw DEFLATE format as described in IETF RFC 1951 with the following configuration equivalent to:
deflateInit2(zstream, 5, Z_DEFLATED, -15, 8, Z_DEFAULT_STRATEGY)
Features:
- Automatic compression with configurable compression level
- Integrity verification via CRC32 checksum
- Optional cryptographic digest for content identification
- Smart behavior for small data (stores uncompressed if compression would increase size)
- CBOR serialization/deserialization support
Implementations§
Source§impl Compressed
impl Compressed
Sourcepub fn new(
checksum: u32,
uncompressed_size: usize,
compressed_data: Vec<u8>,
digest: Option<Digest>,
) -> Result<Self>
pub fn new( checksum: u32, uncompressed_size: usize, compressed_data: Vec<u8>, digest: Option<Digest>, ) -> Result<Self>
Creates a new Compressed
object with the specified parameters.
This is a low-level constructor that allows direct creation of a
Compressed
object without performing compression. It’s primarily
intended for deserialization or when working with pre-compressed
data.
§Parameters
checksum
- CRC32 checksum of the uncompressed datauncompressed_size
- Size of the original uncompressed data in bytescompressed_data
- The compressed data bytesdigest
- Optional cryptographic digest of the content
§Returns
A Result
containing the new Compressed
object if successful,
or an error if the parameters are invalid.
§Errors
Returns an error if the compressed data is larger than the uncompressed size, which would indicate a logical inconsistency.
§Example
use bc_components::Compressed;
use bc_crypto::hash::crc32;
let data = b"hello world";
let checksum = crc32(data);
let uncompressed_size = data.len();
// In a real scenario, this would be actually compressed data
let compressed_data = data.to_vec();
let compressed =
Compressed::new(checksum, uncompressed_size, compressed_data, None)
.unwrap();
Sourcepub fn from_uncompressed_data(
uncompressed_data: impl AsRef<[u8]>,
digest: Option<Digest>,
) -> Self
pub fn from_uncompressed_data( uncompressed_data: impl AsRef<[u8]>, digest: Option<Digest>, ) -> Self
Creates a new Compressed
object by compressing the provided data.
This is the primary method for creating compressed data. It automatically handles compression using the DEFLATE algorithm with a compression level of 6.
If the compressed data would be larger than the original data (which can happen with small or already compressed inputs), the original data is stored instead.
§Parameters
uncompressed_data
- The original data to compressdigest
- Optional cryptographic digest of the content
§Returns
A new Compressed
object containing the compressed (or original) data.
§Example
use bc_components::Compressed;
// Compress a string
let data = "This is a longer string that should compress well with repeated patterns. \
This is a longer string that should compress well with repeated patterns.";
let compressed = Compressed::from_uncompressed_data(data.as_bytes(), None);
// The compressed size should be smaller than the original
assert!(compressed.compressed_size() < data.len());
// We can recover the original data
let uncompressed = compressed.uncompress().unwrap();
assert_eq!(uncompressed, data.as_bytes());
Sourcepub fn uncompress(&self) -> Result<Vec<u8>>
pub fn uncompress(&self) -> Result<Vec<u8>>
Decompresses and returns the original uncompressed data.
This method performs the reverse of the compression process, restoring the original data. It also verifies the integrity of the data using the stored checksum.
§Returns
A Result
containing the uncompressed data if successful,
or an error if decompression fails or the checksum doesn’t match.
§Errors
Returns an error if:
- The compressed data is corrupt and cannot be decompressed
- The checksum of the decompressed data doesn’t match the stored checksum
§Example
use bc_components::Compressed;
// Original data
let original = b"This is some example data to compress";
// Compress it
let compressed = Compressed::from_uncompressed_data(original, None);
// Uncompress to get the original data back
let uncompressed = compressed.uncompress().unwrap();
assert_eq!(uncompressed, original);
Sourcepub fn compressed_size(&self) -> usize
pub fn compressed_size(&self) -> usize
Returns the size of the compressed data in bytes.
§Returns
The size of the compressed data in bytes.
§Example
use bc_components::Compressed;
let data = b"Hello world!";
let compressed = Compressed::from_uncompressed_data(data, None);
// For small inputs like this, compression might not be effective
// so the compressed_size might equal the original size
println!("Compressed size: {}", compressed.compressed_size());
Sourcepub fn compression_ratio(&self) -> f64
pub fn compression_ratio(&self) -> f64
Returns the compression ratio of the data.
The compression ratio is calculated as (compressed size) / (uncompressed size), so lower values indicate better compression.
§Returns
A floating-point value representing the compression ratio.
- Values less than 1.0 indicate effective compression
- Values equal to 1.0 indicate no compression was applied
- Values of NaN can occur if the uncompressed size is zero
§Example
use bc_components::Compressed;
// A string with a lot of repetition should compress well
let data = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
let compressed = Compressed::from_uncompressed_data(data.as_bytes(), None);
// Should have a good compression ratio (much less than 1.0)
let ratio = compressed.compression_ratio();
assert!(ratio < 0.5);
Sourcepub fn digest_ref_opt(&self) -> Option<&Digest>
pub fn digest_ref_opt(&self) -> Option<&Digest>
Returns a reference to the digest of the compressed data, if available.
§Returns
An optional reference to the Digest
associated with this compressed
data.
§Example
use bc_components::{Compressed, Digest};
let data = b"Hello world!";
let digest = Digest::from_image(data);
let compressed =
Compressed::from_uncompressed_data(data, Some(digest.clone()));
// We can retrieve the digest we associated with the compressed data
assert_eq!(compressed.digest_ref_opt(), Some(&digest));
Sourcepub fn has_digest(&self) -> bool
pub fn has_digest(&self) -> bool
Returns whether this compressed data has an associated digest.
§Returns
true
if this compressed data has a digest, false
otherwise.
§Example
use bc_components::{Compressed, Digest};
// Create compressed data without a digest
let compressed1 = Compressed::from_uncompressed_data(b"Hello", None);
assert!(!compressed1.has_digest());
// Create compressed data with a digest
let digest = Digest::from_image(b"Hello");
let compressed2 =
Compressed::from_uncompressed_data(b"Hello", Some(digest));
assert!(compressed2.has_digest());
Trait Implementations§
Source§impl AsRef<Compressed> for Compressed
Implementation of AsRef<Compressed>
for Compressed
.
impl AsRef<Compressed> for Compressed
Implementation of AsRef<Compressed>
for Compressed
.
This allows passing a Compressed
instance to functions that take
AsRef<Compressed>
parameters.
Source§fn as_ref(&self) -> &Compressed
fn as_ref(&self) -> &Compressed
Source§impl CBORTagged for Compressed
Implementation of the CBORTagged
trait for Compressed
.
impl CBORTagged for Compressed
Implementation of the CBORTagged
trait for Compressed
.
Defines the CBOR tag(s) used when serializing a Compressed
object.
Source§impl CBORTaggedDecodable for Compressed
Implementation of CBOR decoding for Compressed
.
impl CBORTaggedDecodable for Compressed
Implementation of CBOR decoding for Compressed
.
Defines how to create a Compressed
object from untagged CBOR.
Source§fn from_untagged_cbor(cbor: CBOR) -> Result<Self>
fn from_untagged_cbor(cbor: CBOR) -> Result<Self>
Source§fn from_tagged_cbor(cbor: CBOR) -> Result<Self, Error>where
Self: Sized,
fn from_tagged_cbor(cbor: CBOR) -> Result<Self, Error>where
Self: Sized,
Source§impl CBORTaggedEncodable for Compressed
Implementation of CBOR encoding for Compressed
.
impl CBORTaggedEncodable for Compressed
Implementation of CBOR encoding for Compressed
.
Defines how a Compressed
object is serialized to untagged CBOR.
The format is:
[
checksum: uint,
uncompressed_size: uint,
compressed_data: bytes,
digest?: Digest // Optional
]
Source§fn untagged_cbor(&self) -> CBOR
fn untagged_cbor(&self) -> CBOR
Source§fn tagged_cbor(&self) -> CBOR
fn tagged_cbor(&self) -> CBOR
Source§impl Clone for Compressed
impl Clone for Compressed
Source§fn clone(&self) -> Compressed
fn clone(&self) -> Compressed
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moreSource§impl Debug for Compressed
Implementation of the Debug
trait for Compressed
.
impl Debug for Compressed
Implementation of the Debug
trait for Compressed
.
Provides a human-readable debug representation of a Compressed
object
showing its key properties: checksum, sizes, compression ratio, and digest.
Source§impl DigestProvider for Compressed
Implementation of the DigestProvider
trait for Compressed
.
impl DigestProvider for Compressed
Implementation of the DigestProvider
trait for Compressed
.
Allows Compressed
objects with digests to be used with APIs that accept
DigestProvider
implementations.
Source§impl From<Compressed> for CBOR
Conversion from Compressed
to CBOR for serialization.
impl From<Compressed> for CBOR
Conversion from Compressed
to CBOR for serialization.
Source§fn from(value: Compressed) -> Self
fn from(value: Compressed) -> Self
Source§impl PartialEq for Compressed
impl PartialEq for Compressed
Source§impl TryFrom<CBOR> for Compressed
Conversion from CBOR to Compressed
for deserialization.
impl TryFrom<CBOR> for Compressed
Conversion from CBOR to Compressed
for deserialization.