Skip to main content

Bitfield

Struct Bitfield 

Source
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 bytes
  • Bitfield: 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

Source

pub fn new(num_bits: usize) -> Bitfield

Create a new bitfield with all bits unset (false).

§Arguments
  • num_bits - Number of bits to store
§Examples
use aria2_protocol::bittorrent::piece::bitfield::Bitfield;

let bf = Bitfield::new(100);
assert_eq!(bf.len(), 100);
assert!(bf.is_all_clear());
Source

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);
Source

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 data
  • num_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));
Source

pub fn as_bytes(&self) -> &[u8]

Get the raw byte representation.

This is useful for sending bitfield messages to peers.

Source

pub fn len(&self) -> usize

Get the total number of bits.

Source

pub fn is_empty(&self) -> bool

Check if the bitfield is empty.

Source

pub fn set(&mut self, index: usize) -> Option<()>

Set a bit to true.

§Errors

Returns None if index is out of bounds.

§Examples
use aria2_protocol::bittorrent::piece::bitfield::Bitfield;

let mut bf = Bitfield::new(10);
assert!(bf.set(5).is_some());
assert!(bf.set(100).is_none()); // Out of bounds
Source

pub fn clear(&mut self, index: usize) -> Option<()>

Clear a bit (set to false).

§Errors

Returns None if index is out of bounds.

Source

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 false
Source

pub fn count_set(&self) -> usize

Count the number of set bits.

§Performance

Uses count_ones() intrinsic for fast bit counting.

Source

pub fn count_clear(&self) -> usize

Count the number of clear bits.

Source

pub fn is_all_set(&self) -> bool

Check if all bits are set.

Source

pub fn is_all_clear(&self) -> bool

Check if all bits are clear.

Source

pub fn find_first_set(&self) -> Option<usize>

Find the first set bit.

Returns None if no bits are set.

Source

pub fn find_first_clear(&self) -> Option<usize>

Find the first clear bit.

Returns None if all bits are set.

Source

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.

Source

pub fn iter_set(&self) -> SetBitIter<'_>

Get an iterator over all set bit indices.

Source

pub fn iter_clear(&self) -> ClearBitIter<'_>

Get an iterator over all clear bit indices.

Source

pub fn memory_usage(&self) -> usize

Calculate memory usage in bytes.

This is the actual storage size, not including struct overhead.

Source

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.

Source

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.

Source

pub fn set_all(&mut self)

Set all bits to true.

Source

pub fn clear_all(&mut self)

Clear all bits.

Source

pub fn bitand_assign(&mut self, other: &Bitfield)

Perform a bitwise AND with another bitfield.

Both bitfields must have the same length.

Source

pub fn bitor_assign(&mut self, other: &Bitfield)

Perform a bitwise OR with another bitfield.

Both bitfields must have the same length.

Source

pub fn bitxor_assign(&mut self, other: &Bitfield)

Perform a bitwise XOR with another bitfield.

Both bitfields must have the same length.

Trait Implementations§

Source§

impl Clone for Bitfield

Source§

fn clone(&self) -> Bitfield

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Bitfield

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl Eq for Bitfield

Source§

impl PartialEq for Bitfield

Source§

fn eq(&self, other: &Bitfield) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for Bitfield

Auto Trait Implementations§

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more