Struct ByteBase2

Source
pub struct ByteBase2 { /* private fields */ }
Expand description

A binary representation of a byte.

Implementations§

Source§

impl ByteBase2

Source

pub fn ones(&self) -> usize

Returns how many ones there is in this byte.

§Example
use binary_byte::ByteBase2;
 
let byte = ByteBase2::from_dec(15);
assert_eq!(byte.ones(), 4);
Source

pub fn iter(&self) -> impl Iterator<Item = bool>

Returns an iterator over this byte’s bits.

Yields first the least significative bit and last the most significative one.

§Example
use binary_byte::ByteBase2;
 
let byte = ByteBase2::from_string("00000011").unwrap();
let mut byte_iter = byte.iter();
assert_eq!(byte_iter.next(), Some(true));
assert_eq!(byte_iter.next(), Some(true));
assert_eq!(byte_iter.next(), Some(false));
Source

pub fn from_string(pattern: impl Into<String>) -> Result<Self, InvalidPattern>

Tries to create a ByteBase2 from a string representing an 8 bit binary number.

§Errors

Returns an Err(InvalidPattern) if the pattern length is not exactly 8 or if any of its characters is different of ‘1’ or ‘0’.

§Example
use binary_byte::{ ByteBase2, InvalidPattern };
 
assert!(ByteBase2::from_string("01111001").is_ok());
assert_eq!(ByteBase2::from_string("12001101"), Err(InvalidPattern));
assert_eq!(ByteBase2::from_string("101010100"), Err(InvalidPattern));
assert_eq!(ByteBase2::from_string("1010"), Err(InvalidPattern));
Source

pub fn from_dec(input: u8) -> Self

Creates a ByteBase2 object from an u8 value.

§Example
use binary_byte::ByteBase2;
 
let byte = ByteBase2::from_dec(15);
assert_eq!(format!("{:?}", byte), "00001111".to_string());
Source

pub fn as_dec(&self) -> u8

Converts a ByteBase2 number to its decimal representation.

§Example
use binary_byte::ByteBase2;
 
let byte = ByteBase2::from_string("00001000").unwrap();
assert_eq!(byte.as_dec(), 8);

Trait Implementations§

Source§

impl Debug for ByteBase2

Source§

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

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

impl Index<usize> for ByteBase2

Access the bits in this byte.

Index 0 access the least significative bit.

Source§

type Output = bool

The returned type after indexing.
Source§

fn index(&self, idx: usize) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
Source§

impl IndexMut<usize> for ByteBase2

Mutably access the bits in this byte.

Index 0 access the least significative bit.

Source§

fn index_mut(&mut self, idx: usize) -> &mut Self::Output

Performs the mutable indexing (container[index]) operation. Read more
Source§

impl PartialEq for ByteBase2

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl StructuralPartialEq for ByteBase2

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.