[][src]Struct binary_byte::ByteBase2

pub struct ByteBase2 { /* fields omitted */ }

A binary representation of a byte.

Implementations

impl ByteBase2[src]

pub fn ones(&self) -> usize[src]

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

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

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

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

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

pub fn from_dec(input: u8) -> Self[src]

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

pub fn as_dec(&self) -> u8[src]

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

impl Debug for ByteBase2[src]

impl Index<usize> for ByteBase2[src]

Access the bits in this byte.

Index 0 access the least significative bit.

type Output = bool

The returned type after indexing.

impl IndexMut<usize> for ByteBase2[src]

Mutably access the bits in this byte.

Index 0 access the least significative bit.

impl PartialEq<ByteBase2> for ByteBase2[src]

impl StructuralPartialEq for ByteBase2[src]

Auto Trait Implementations

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.