pub struct Bitfield { /* private fields */ }Expand description
A memory-efficient bitfield using 1 bit per element.
§Memory Efficiency
For a torrent with 10,000 pieces:
Vec<bool>: 10,000 bytesBitfield: 1,250 bytes (8x reduction)
§Examples
use aria2_protocol::bittorrent::piece::bitfield::Bitfield;
let mut bf = Bitfield::new(100);
bf.set(5).unwrap();
assert!(bf.test(5));
assert!(!bf.test(6));
assert_eq!(bf.count_set(), 1);Implementations§
Source§impl Bitfield
impl Bitfield
Sourcepub fn all_set(num_bits: usize) -> Bitfield
pub fn all_set(num_bits: usize) -> Bitfield
Create a bitfield with all bits set (true).
§Examples
use aria2_protocol::bittorrent::piece::bitfield::Bitfield;
let bf = Bitfield::all_set(50);
assert!(bf.is_all_set());
assert_eq!(bf.count_set(), 50);Sourcepub fn from_bytes(data: &[u8], num_bits: usize) -> Bitfield
pub fn from_bytes(data: &[u8], num_bits: usize) -> Bitfield
Create a bitfield from raw bytes (e.g., from a peer’s bitfield message).
§Arguments
data- Raw byte datanum_bits- Number of valid bits
§Examples
use aria2_protocol::bittorrent::piece::bitfield::Bitfield;
// Bitfield with bits 0 and 7 set: 0b10000001 = 0x81
let bf = Bitfield::from_bytes(&[0x81], 8);
assert!(bf.test(0));
assert!(bf.test(7));Sourcepub fn as_bytes(&self) -> &[u8] ⓘ
pub fn as_bytes(&self) -> &[u8] ⓘ
Get the raw byte representation.
This is useful for sending bitfield messages to peers.
Sourcepub fn test(&self, index: usize) -> bool
pub fn test(&self, index: usize) -> bool
Test if a bit is set.
Returns false for out-of-bounds indices.
§Examples
use aria2_protocol::bittorrent::piece::bitfield::Bitfield;
let mut bf = Bitfield::new(10);
bf.set(3).unwrap();
assert!(bf.test(3));
assert!(!bf.test(4));
assert!(!bf.test(100)); // Out of bounds returns falseSourcepub fn count_clear(&self) -> usize
pub fn count_clear(&self) -> usize
Count the number of clear bits.
Sourcepub fn is_all_set(&self) -> bool
pub fn is_all_set(&self) -> bool
Check if all bits are set.
Sourcepub fn is_all_clear(&self) -> bool
pub fn is_all_clear(&self) -> bool
Check if all bits are clear.
Sourcepub fn find_first_set(&self) -> Option<usize>
pub fn find_first_set(&self) -> Option<usize>
Find the first set bit.
Returns None if no bits are set.
Sourcepub fn find_first_clear(&self) -> Option<usize>
pub fn find_first_clear(&self) -> Option<usize>
Find the first clear bit.
Returns None if all bits are set.
Sourcepub fn find_next_set(&self, after: usize) -> Option<usize>
pub fn find_next_set(&self, after: usize) -> Option<usize>
Find the next set bit after the given index.
Returns None if no more set bits exist.
Sourcepub fn iter_set(&self) -> SetBitIter<'_>
pub fn iter_set(&self) -> SetBitIter<'_>
Get an iterator over all set bit indices.
Sourcepub fn iter_clear(&self) -> ClearBitIter<'_>
pub fn iter_clear(&self) -> ClearBitIter<'_>
Get an iterator over all clear bit indices.
Sourcepub fn memory_usage(&self) -> usize
pub fn memory_usage(&self) -> usize
Calculate memory usage in bytes.
This is the actual storage size, not including struct overhead.
Sourcepub fn vec_bool_memory_usage(&self) -> usize
pub fn vec_bool_memory_usage(&self) -> usize
Calculate memory usage if using Vec<bool> for the same number of bits.
This is useful for comparing memory savings.
Sourcepub fn memory_savings_ratio(&self) -> f64
pub fn memory_savings_ratio(&self) -> f64
Calculate memory savings ratio compared to Vec<bool>.
Returns the ratio of Vec<bool> memory to Bitfield memory.
A value of 8.0 means Bitfield uses 8x less memory.
Sourcepub fn bitand_assign(&mut self, other: &Bitfield)
pub fn bitand_assign(&mut self, other: &Bitfield)
Perform a bitwise AND with another bitfield.
Both bitfields must have the same length.
Sourcepub fn bitor_assign(&mut self, other: &Bitfield)
pub fn bitor_assign(&mut self, other: &Bitfield)
Perform a bitwise OR with another bitfield.
Both bitfields must have the same length.
Sourcepub fn bitxor_assign(&mut self, other: &Bitfield)
pub fn bitxor_assign(&mut self, other: &Bitfield)
Perform a bitwise XOR with another bitfield.
Both bitfields must have the same length.
Trait Implementations§
impl Eq for Bitfield
impl StructuralPartialEq for Bitfield
Auto Trait Implementations§
impl Freeze for Bitfield
impl RefUnwindSafe for Bitfield
impl Send for Bitfield
impl Sync for Bitfield
impl Unpin for Bitfield
impl UnsafeUnpin for Bitfield
impl UnwindSafe for Bitfield
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.