Expand description
§Zero-Copy Slices
This module provides FixedVecSlice
, a zero-copy view into a portion of a
FixedVec
. Slices can be created from both immutable and mutable vectors
and provide a way to operate on a sub-region of the data without copying.
§Examples
§Creating and using an immutable slice
use compressed_intvec::fixed::{FixedVec, UFixedVec};
let data: Vec<u32> = (0..10).collect();
let vec: UFixedVec<u32> = FixedVec::builder().build(&data).unwrap();
// Create a slice of the elements from index 2 to 6.
let slice = vec.slice(2, 5).unwrap();
assert_eq!(slice.len(), 5);
assert_eq!(slice.get(0), Some(2));
assert_eq!(slice.get(4), Some(6));
// The slice can be iterated over.
let sum: u32 = slice.iter().sum();
assert_eq!(sum, 2 + 3 + 4 + 5 + 6);
§Creating and using a mutable slice
use compressed_intvec::fixed::{FixedVec, UFixedVec, BitWidth};
let data: Vec<u32> = (0..10).collect();
let mut vec: UFixedVec<u32> = FixedVec::builder().bit_width(BitWidth::Explicit(7)).build(&data).unwrap();
// `split_at_mut` is one way to get mutable slices.
let (_, mut slice2) = vec.split_at_mut(5);
// Mutate an element within the second slice.
if let Some(mut proxy) = slice2.at_mut(0) { // index 0 of slice2 is index 5 of vec
*proxy = 99;
}
assert_eq!(vec.get(5), Some(99));
Structs§
- Fixed
VecSlice - A zero-copy view into a contiguous portion of a
FixedVec
.