pub struct BatchGuard<'a, const N: usize> { /* private fields */ }Expand description
Guard for an active batch on a historical bitmap.
Provides mutable access to an active batch, allowing operations that modify the bitmap through a diff layer. Changes are not applied to the underlying bitmap until commit is called.
§Lifecycle
The guard must be either:
- Committed: Call commit(commit_number) to apply changes and store a historical snapshot.
- Dropped: Drop without committing to discard all changes.
§Examples
let mut bitmap: BitMap<4> = BitMap::new();
// Create a batch guard and make changes
let mut batch = bitmap.start_batch();
batch.push(true);
batch.push(false);
batch.commit(1).unwrap();
// Bitmap is now modified
assert_eq!(bitmap.len(), 2);§Discarding Changes
let mut bitmap: BitMap<4> = BitMap::new();
{
let mut batch = bitmap.start_batch();
batch.push(true);
// batch dropped here without commit
}
// Bitmap is unchanged
assert_eq!(bitmap.len(), 0);Implementations§
Source§impl<'a, const N: usize> BatchGuard<'a, N>
impl<'a, const N: usize> BatchGuard<'a, N>
Sourcepub fn len(&self) -> u64
pub fn len(&self) -> u64
Get the length of the bitmap as it would be after committing this batch.
Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true if the bitmap would be empty after committing this batch.
Sourcepub fn pruned_chunks(&self) -> usize
pub fn pruned_chunks(&self) -> usize
Get the number of pruned chunks after this batch.
Sourcepub fn get_bit(&self, bit: u64) -> bool
pub fn get_bit(&self, bit: u64) -> bool
Get a bit value with read-through semantics.
Returns the bit’s value as it would be after committing this batch. Priority: appended bits > modified bits > original bitmap.
§Panics
Panics if the bit offset is out of bounds or if the bit has been pruned.
Sourcepub fn get_chunk(&self, bit: u64) -> [u8; N]
pub fn get_chunk(&self, bit: u64) -> [u8; N]
Get a chunk value with read-through semantics.
Reconstructs the chunk if it has modifications, otherwise returns from current.
§Panics
Panics if the bit offset is out of bounds or if the chunk has been pruned.
Sourcepub fn set_bit(&mut self, bit: u64, value: bool) -> &mut Self
pub fn set_bit(&mut self, bit: u64, value: bool) -> &mut Self
Set a bit value in the batch.
§Panics
Panics if the bit offset is out of bounds or if the bit has been pruned.
Sourcepub fn push_chunk(&mut self, chunk: &[u8; N]) -> &mut Self
pub fn push_chunk(&mut self, chunk: &[u8; N]) -> &mut Self
Push a full chunk to the end of the bitmap.
Sourcepub fn pop(&mut self) -> bool
pub fn pop(&mut self) -> bool
Pop the last bit from the bitmap.
Returns the value of the popped bit, accounting for any modifications in this batch.
§Panics
Panics if the bitmap is empty.
Sourcepub fn prune_to_bit(&mut self, bit: u64) -> &mut Self
pub fn prune_to_bit(&mut self, bit: u64) -> &mut Self
Prune chunks up to the chunk containing the given bit offset.
Note: bit can equal projected_len when pruning at a chunk boundary.
§Panics
Panics if bit is > the projected length of the batch.
Sourcepub fn commit(self, commit_number: u64) -> Result<(), Error>
pub fn commit(self, commit_number: u64) -> Result<(), Error>
Commit the batch, applying its changes and storing a historical snapshot.
§Errors
Returns Error::NonMonotonicCommit if the commit number is not greater than the previous commit.
Returns Error::ReservedCommitNumber if the commit number is u64::MAX.