TypedRef

Struct TypedRef 

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

Safe wrapper around ZL_TypedRef for typed input data.

IMPORTANT: TypedRef borrows the input data. The borrowed data must remain valid for the lifetime of the TypedRef and through any compression call that uses it.

Implementations§

Source§

impl<'a> TypedRef<'a>

Source

pub fn serial(data: &'a [u8]) -> Self

Create a TypedRef for serial (untyped byte array) data

Examples found in repository?
examples/typed_compression.rs (line 14)
8fn main() -> Result<(), Box<dyn std::error::Error>> {
9    println!("=== TypedRef Compression Examples ===\n");
10
11    // Example 1: Serial (untyped) data
12    println!("Example 1: Serial TypedRef");
13    let text = b"Hello from TypedRef! This is untyped byte data.";
14    let tref_serial = TypedRef::serial(text);
15
16    let compressed = compress_typed_ref(&tref_serial)?;
17    let tbuf = decompress_typed_buffer(&compressed)?;
18
19    println!("  Original size: {} bytes", text.len());
20    println!("  Compressed size: {} bytes", compressed.len());
21    println!("  Data type: {:?}", tbuf.data_type());
22
23    assert_eq!(text.as_slice(), tbuf.as_bytes());
24    println!("  ✓ Round-trip successful!\n");
25
26    // Example 2: Numeric u32 data
27    println!("Example 2: Numeric TypedRef (u32)");
28    let numbers: Vec<u32> = (0..1000).collect();
29    let tref_numeric = TypedRef::numeric(&numbers)?;
30
31    let compressed = compress_typed_ref(&tref_numeric)?;
32    let tbuf = decompress_typed_buffer(&compressed)?;
33
34    let original_size = numbers.len() * std::mem::size_of::<u32>();
35    println!("  Original size: {} bytes ({} elements)", original_size, numbers.len());
36    println!("  Compressed size: {} bytes", compressed.len());
37    println!(
38        "  Compression ratio: {:.2}%",
39        (compressed.len() as f64 / original_size as f64) * 100.0
40    );
41    println!("  Data type: {:?}", tbuf.data_type());
42    println!("  Element width: {} bytes", tbuf.elt_width());
43    println!("  Element count: {}", tbuf.num_elts());
44
45    let decompressed = tbuf.as_numeric::<u32>().expect("Failed to extract u32 data");
46    assert_eq!(numbers.as_slice(), decompressed);
47    println!("  ✓ Round-trip successful!\n");
48
49    // Example 3: Numeric u64 data
50    println!("Example 3: Numeric TypedRef (u64)");
51    let large_numbers: Vec<u64> = (0..500).map(|i| i * 1000000).collect();
52    let tref_numeric = TypedRef::numeric(&large_numbers)?;
53
54    let compressed = compress_typed_ref(&tref_numeric)?;
55    let tbuf = decompress_typed_buffer(&compressed)?;
56
57    let original_size = large_numbers.len() * std::mem::size_of::<u64>();
58    println!("  Original size: {} bytes ({} elements)", original_size, large_numbers.len());
59    println!("  Compressed size: {} bytes", compressed.len());
60    println!(
61        "  Compression ratio: {:.2}%",
62        (compressed.len() as f64 / original_size as f64) * 100.0
63    );
64
65    let decompressed = tbuf.as_numeric::<u64>().expect("Failed to extract u64 data");
66    assert_eq!(large_numbers.as_slice(), decompressed);
67    println!("  ✓ Round-trip successful!\n");
68
69    // Example 4: Struct data
70    println!("Example 4: Struct TypedRef");
71
72    // Simulate a simple struct as raw bytes: [id: u32, value: u32]
73    let struct_width = 8; // 2 * sizeof(u32)
74    let struct_count = 100;
75    let mut struct_data = Vec::with_capacity(struct_width * struct_count);
76
77    for i in 0..struct_count {
78        // Write id (u32)
79        struct_data.extend_from_slice(&(i as u32).to_le_bytes());
80        // Write value (u32)
81        struct_data.extend_from_slice(&((i * 10) as u32).to_le_bytes());
82    }
83
84    let tref_struct = TypedRef::structs(&struct_data, struct_width, struct_count)?;
85
86    let compressed = compress_typed_ref(&tref_struct)?;
87    let tbuf = decompress_typed_buffer(&compressed)?;
88
89    println!("  Original size: {} bytes ({} structs of {} bytes)",
90             struct_data.len(), struct_count, struct_width);
91    println!("  Compressed size: {} bytes", compressed.len());
92    println!(
93        "  Compression ratio: {:.2}%",
94        (compressed.len() as f64 / struct_data.len() as f64) * 100.0
95    );
96
97    assert_eq!(struct_data.as_slice(), tbuf.as_bytes());
98    println!("  ✓ Round-trip successful!\n");
99
100    println!("All TypedRef compression examples completed successfully!");
101    println!("\nNote: TypedRef provides type information to OpenZL, enabling");
102    println!("type-specific compression optimizations not available with serial compression.");
103
104    Ok(())
105}
Source

pub fn numeric<T: Copy>(data: &'a [T]) -> Result<Self, Error>

Create a TypedRef for numeric data.

T must have size 1, 2, 4, or 8 bytes (u8, u16, u32, u64, i8, i16, i32, i64, f32, f64)

Examples found in repository?
examples/typed_compression.rs (line 29)
8fn main() -> Result<(), Box<dyn std::error::Error>> {
9    println!("=== TypedRef Compression Examples ===\n");
10
11    // Example 1: Serial (untyped) data
12    println!("Example 1: Serial TypedRef");
13    let text = b"Hello from TypedRef! This is untyped byte data.";
14    let tref_serial = TypedRef::serial(text);
15
16    let compressed = compress_typed_ref(&tref_serial)?;
17    let tbuf = decompress_typed_buffer(&compressed)?;
18
19    println!("  Original size: {} bytes", text.len());
20    println!("  Compressed size: {} bytes", compressed.len());
21    println!("  Data type: {:?}", tbuf.data_type());
22
23    assert_eq!(text.as_slice(), tbuf.as_bytes());
24    println!("  ✓ Round-trip successful!\n");
25
26    // Example 2: Numeric u32 data
27    println!("Example 2: Numeric TypedRef (u32)");
28    let numbers: Vec<u32> = (0..1000).collect();
29    let tref_numeric = TypedRef::numeric(&numbers)?;
30
31    let compressed = compress_typed_ref(&tref_numeric)?;
32    let tbuf = decompress_typed_buffer(&compressed)?;
33
34    let original_size = numbers.len() * std::mem::size_of::<u32>();
35    println!("  Original size: {} bytes ({} elements)", original_size, numbers.len());
36    println!("  Compressed size: {} bytes", compressed.len());
37    println!(
38        "  Compression ratio: {:.2}%",
39        (compressed.len() as f64 / original_size as f64) * 100.0
40    );
41    println!("  Data type: {:?}", tbuf.data_type());
42    println!("  Element width: {} bytes", tbuf.elt_width());
43    println!("  Element count: {}", tbuf.num_elts());
44
45    let decompressed = tbuf.as_numeric::<u32>().expect("Failed to extract u32 data");
46    assert_eq!(numbers.as_slice(), decompressed);
47    println!("  ✓ Round-trip successful!\n");
48
49    // Example 3: Numeric u64 data
50    println!("Example 3: Numeric TypedRef (u64)");
51    let large_numbers: Vec<u64> = (0..500).map(|i| i * 1000000).collect();
52    let tref_numeric = TypedRef::numeric(&large_numbers)?;
53
54    let compressed = compress_typed_ref(&tref_numeric)?;
55    let tbuf = decompress_typed_buffer(&compressed)?;
56
57    let original_size = large_numbers.len() * std::mem::size_of::<u64>();
58    println!("  Original size: {} bytes ({} elements)", original_size, large_numbers.len());
59    println!("  Compressed size: {} bytes", compressed.len());
60    println!(
61        "  Compression ratio: {:.2}%",
62        (compressed.len() as f64 / original_size as f64) * 100.0
63    );
64
65    let decompressed = tbuf.as_numeric::<u64>().expect("Failed to extract u64 data");
66    assert_eq!(large_numbers.as_slice(), decompressed);
67    println!("  ✓ Round-trip successful!\n");
68
69    // Example 4: Struct data
70    println!("Example 4: Struct TypedRef");
71
72    // Simulate a simple struct as raw bytes: [id: u32, value: u32]
73    let struct_width = 8; // 2 * sizeof(u32)
74    let struct_count = 100;
75    let mut struct_data = Vec::with_capacity(struct_width * struct_count);
76
77    for i in 0..struct_count {
78        // Write id (u32)
79        struct_data.extend_from_slice(&(i as u32).to_le_bytes());
80        // Write value (u32)
81        struct_data.extend_from_slice(&((i * 10) as u32).to_le_bytes());
82    }
83
84    let tref_struct = TypedRef::structs(&struct_data, struct_width, struct_count)?;
85
86    let compressed = compress_typed_ref(&tref_struct)?;
87    let tbuf = decompress_typed_buffer(&compressed)?;
88
89    println!("  Original size: {} bytes ({} structs of {} bytes)",
90             struct_data.len(), struct_count, struct_width);
91    println!("  Compressed size: {} bytes", compressed.len());
92    println!(
93        "  Compression ratio: {:.2}%",
94        (compressed.len() as f64 / struct_data.len() as f64) * 100.0
95    );
96
97    assert_eq!(struct_data.as_slice(), tbuf.as_bytes());
98    println!("  ✓ Round-trip successful!\n");
99
100    println!("All TypedRef compression examples completed successfully!");
101    println!("\nNote: TypedRef provides type information to OpenZL, enabling");
102    println!("type-specific compression optimizations not available with serial compression.");
103
104    Ok(())
105}
Source

pub fn strings(flat: &'a [u8], lens: &'a [u32]) -> Self

Create a TypedRef for string data (flat format with lengths array)

  • flat: concatenated string bytes
  • lens: array of string lengths (u32)
Source

pub fn structs( bytes: &'a [u8], width: usize, count: usize, ) -> Result<Self, Error>

Create a TypedRef for struct data (concatenated fields)

  • bytes: flattened struct data
  • width: size of each struct element in bytes
  • count: number of struct elements
Examples found in repository?
examples/typed_compression.rs (line 84)
8fn main() -> Result<(), Box<dyn std::error::Error>> {
9    println!("=== TypedRef Compression Examples ===\n");
10
11    // Example 1: Serial (untyped) data
12    println!("Example 1: Serial TypedRef");
13    let text = b"Hello from TypedRef! This is untyped byte data.";
14    let tref_serial = TypedRef::serial(text);
15
16    let compressed = compress_typed_ref(&tref_serial)?;
17    let tbuf = decompress_typed_buffer(&compressed)?;
18
19    println!("  Original size: {} bytes", text.len());
20    println!("  Compressed size: {} bytes", compressed.len());
21    println!("  Data type: {:?}", tbuf.data_type());
22
23    assert_eq!(text.as_slice(), tbuf.as_bytes());
24    println!("  ✓ Round-trip successful!\n");
25
26    // Example 2: Numeric u32 data
27    println!("Example 2: Numeric TypedRef (u32)");
28    let numbers: Vec<u32> = (0..1000).collect();
29    let tref_numeric = TypedRef::numeric(&numbers)?;
30
31    let compressed = compress_typed_ref(&tref_numeric)?;
32    let tbuf = decompress_typed_buffer(&compressed)?;
33
34    let original_size = numbers.len() * std::mem::size_of::<u32>();
35    println!("  Original size: {} bytes ({} elements)", original_size, numbers.len());
36    println!("  Compressed size: {} bytes", compressed.len());
37    println!(
38        "  Compression ratio: {:.2}%",
39        (compressed.len() as f64 / original_size as f64) * 100.0
40    );
41    println!("  Data type: {:?}", tbuf.data_type());
42    println!("  Element width: {} bytes", tbuf.elt_width());
43    println!("  Element count: {}", tbuf.num_elts());
44
45    let decompressed = tbuf.as_numeric::<u32>().expect("Failed to extract u32 data");
46    assert_eq!(numbers.as_slice(), decompressed);
47    println!("  ✓ Round-trip successful!\n");
48
49    // Example 3: Numeric u64 data
50    println!("Example 3: Numeric TypedRef (u64)");
51    let large_numbers: Vec<u64> = (0..500).map(|i| i * 1000000).collect();
52    let tref_numeric = TypedRef::numeric(&large_numbers)?;
53
54    let compressed = compress_typed_ref(&tref_numeric)?;
55    let tbuf = decompress_typed_buffer(&compressed)?;
56
57    let original_size = large_numbers.len() * std::mem::size_of::<u64>();
58    println!("  Original size: {} bytes ({} elements)", original_size, large_numbers.len());
59    println!("  Compressed size: {} bytes", compressed.len());
60    println!(
61        "  Compression ratio: {:.2}%",
62        (compressed.len() as f64 / original_size as f64) * 100.0
63    );
64
65    let decompressed = tbuf.as_numeric::<u64>().expect("Failed to extract u64 data");
66    assert_eq!(large_numbers.as_slice(), decompressed);
67    println!("  ✓ Round-trip successful!\n");
68
69    // Example 4: Struct data
70    println!("Example 4: Struct TypedRef");
71
72    // Simulate a simple struct as raw bytes: [id: u32, value: u32]
73    let struct_width = 8; // 2 * sizeof(u32)
74    let struct_count = 100;
75    let mut struct_data = Vec::with_capacity(struct_width * struct_count);
76
77    for i in 0..struct_count {
78        // Write id (u32)
79        struct_data.extend_from_slice(&(i as u32).to_le_bytes());
80        // Write value (u32)
81        struct_data.extend_from_slice(&((i * 10) as u32).to_le_bytes());
82    }
83
84    let tref_struct = TypedRef::structs(&struct_data, struct_width, struct_count)?;
85
86    let compressed = compress_typed_ref(&tref_struct)?;
87    let tbuf = decompress_typed_buffer(&compressed)?;
88
89    println!("  Original size: {} bytes ({} structs of {} bytes)",
90             struct_data.len(), struct_count, struct_width);
91    println!("  Compressed size: {} bytes", compressed.len());
92    println!(
93        "  Compression ratio: {:.2}%",
94        (compressed.len() as f64 / struct_data.len() as f64) * 100.0
95    );
96
97    assert_eq!(struct_data.as_slice(), tbuf.as_bytes());
98    println!("  ✓ Round-trip successful!\n");
99
100    println!("All TypedRef compression examples completed successfully!");
101    println!("\nNote: TypedRef provides type information to OpenZL, enabling");
102    println!("type-specific compression optimizations not available with serial compression.");
103
104    Ok(())
105}

Trait Implementations§

Source§

impl Drop for TypedRef<'_>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl<'a> Freeze for TypedRef<'a>

§

impl<'a> RefUnwindSafe for TypedRef<'a>

§

impl<'a> !Send for TypedRef<'a>

§

impl<'a> !Sync for TypedRef<'a>

§

impl<'a> Unpin for TypedRef<'a>

§

impl<'a> UnwindSafe for TypedRef<'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.