pub struct TypedBuffer { /* private fields */ }Expand description
Safe wrapper around ZL_TypedBuffer for typed decompression output.
TypedBuffer owns its internal buffer and frees it on Drop.
Implementations§
Source§impl TypedBuffer
impl TypedBuffer
Sourcepub fn data_type(&self) -> ZL_Type
pub fn data_type(&self) -> ZL_Type
Get the data type of this buffer
Examples found in repository?
examples/typed_compression.rs (line 21)
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}Sourcepub fn num_elts(&self) -> usize
pub fn num_elts(&self) -> usize
Get the number of elements
Examples found in repository?
examples/typed_compression.rs (line 43)
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}Sourcepub fn elt_width(&self) -> usize
pub fn elt_width(&self) -> usize
Get the element width in bytes (for struct/numeric types)
Examples found in repository?
examples/typed_compression.rs (line 42)
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}Sourcepub fn as_bytes(&self) -> &[u8] ⓘ
pub fn as_bytes(&self) -> &[u8] ⓘ
Get read-only access to the buffer as bytes
Examples found in repository?
examples/typed_compression.rs (line 23)
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}Sourcepub fn as_numeric<T: Copy>(&self) -> Option<&[T]>
pub fn as_numeric<T: Copy>(&self) -> Option<&[T]>
Get read-only access to numeric data as a typed slice.
Returns None if:
- The data type is not numeric
- The element width doesn’t match T’s size
- The buffer is not properly aligned for T
Examples found in repository?
examples/typed_compression.rs (line 45)
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}Sourcepub fn string_lens(&self) -> Option<&[u32]>
pub fn string_lens(&self) -> Option<&[u32]>
Get the string lengths array (for string type)
Returns None if the data type is not string
Trait Implementations§
Source§impl Default for TypedBuffer
impl Default for TypedBuffer
Auto Trait Implementations§
impl Freeze for TypedBuffer
impl RefUnwindSafe for TypedBuffer
impl !Send for TypedBuffer
impl !Sync for TypedBuffer
impl Unpin for TypedBuffer
impl UnwindSafe for TypedBuffer
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
Mutably borrows from an owned value. Read more