Struct Bitfield

Source
pub struct Bitfield {
    pub pages: Pager,
    /* private fields */
}
Expand description

Bitfield instance.

Fields§

§pages: Pager

A memory-pager instance.

Implementations§

Source§

impl Bitfield

Source

pub fn new(page_size: usize) -> Self

Create a new instance.

§Panics

The page size must be a multiple of 2, and bigger than 0.

Source

pub fn from_file( file: &mut File, page_size: usize, offset: Option<usize>, ) -> Result<Self>

Create a new instance from a File.

Source

pub fn set(&mut self, index: usize, value: bool) -> Change

Set a bit to true or false. Returns a boolean indicating if the value was changed.

Source

pub fn get(&mut self, index: usize) -> bool

Get the value of a bit.

Source

pub fn get_byte(&self, index: usize) -> u8

Get a byte from our internal buffers.

Source

pub fn set_byte(&mut self, index: usize, byte: u8) -> Change

Set a byte to the right value inside our internal buffers.

Source

pub fn page_size(&self) -> usize

Get the memory page size in bytes.

Source

pub fn len(&self) -> usize

Get the amount of bits in the bitfield.

§Examples
let mut bits = Bitfield::new(1024);
assert_eq!(bits.len(), 0);
bits.set(0, true);
assert_eq!(bits.len(), 8);
bits.set(1, true);
assert_eq!(bits.len(), 8);
bits.set(9, false);
assert_eq!(bits.len(), 16);
Source

pub fn byte_len(&self) -> usize

Get the amount of bytes in the bitfield.

§Examples
let mut bits = Bitfield::new(1024);
assert_eq!(bits.byte_len(), 0);
bits.set(0, true);
assert_eq!(bits.byte_len(), 1);
bits.set(1, true);
assert_eq!(bits.byte_len(), 1);
bits.set(9, false);
assert_eq!(bits.byte_len(), 2);
Source

pub fn page_len(&self) -> usize

Get the amount of memory pages in the bitfield.

§Examples
let mut bits = Bitfield::new(1024);
assert_eq!(bits.page_len(), 0);
bits.set(0, true);
assert_eq!(bits.page_len(), 1);
bits.set(1, true);
assert_eq!(bits.page_len(), 1);
bits.set(2, false);
assert_eq!(bits.page_len(), 1);
bits.set(1024 * 8 + 1, true);
assert_eq!(bits.page_len(), 2);
Source

pub fn is_empty(&self) -> bool

Returns true if no bits are stored.

§Examples
let mut bits = Bitfield::new(1024);
assert!(bits.is_empty());
bits.set(0, true);
assert!(!bits.is_empty());
Source

pub fn iter(&mut self) -> Iter<'_>

Create an Iterator that iterates over all pages.

Source

pub fn to_bytes(&self) -> Result<Vec<u8>>

Trait Implementations§

Source§

impl Debug for Bitfield

Source§

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

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

impl Default for Bitfield

Create a new instance with a page_size of 1kb.

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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, 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.