pub struct ByteBase2 { /* private fields */ }Expand description
A binary representation of a byte.
Implementations§
Source§impl ByteBase2
impl ByteBase2
Sourcepub fn ones(&self) -> usize
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);Sourcepub fn iter(&self) -> impl Iterator<Item = bool>
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));Sourcepub fn from_string(pattern: impl Into<String>) -> Result<Self, InvalidPattern>
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));Trait Implementations§
Source§impl Index<usize> for ByteBase2
Access the bits in this byte.
impl Index<usize> for ByteBase2
Access the bits in this byte.
Index 0 access the least significative bit.
Source§impl IndexMut<usize> for ByteBase2
Mutably access the bits in this byte.
impl IndexMut<usize> for ByteBase2
Mutably access the bits in this byte.
Index 0 access the least significative bit.
impl StructuralPartialEq for ByteBase2
Auto Trait Implementations§
impl Freeze for ByteBase2
impl RefUnwindSafe for ByteBase2
impl Send for ByteBase2
impl Sync for ByteBase2
impl Unpin for ByteBase2
impl UnwindSafe for ByteBase2
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
Mutably borrows from an owned value. Read more