Bitbite

Trait Bitbite 

Source
pub trait Bitbite: DerefMut<Target = Self::Unit> {
    type Unit: PrimInt;

    // Provided methods
    fn get_flag(&self, flag: &Flag<Self::Unit>) -> Self::Unit { ... }
    fn set_flag(&mut self, value: Self::Unit, flag: &Flag<Self::Unit>) { ... }
    fn set_on(&mut self, value: Self::Unit, flag: &Flag<Self::Unit>) { ... }
    fn reset_flag(&mut self, flag: &Flag<Self::Unit>) { ... }
    fn set_off(&mut self, value: Self::Unit, flag: &Flag<Self::Unit>) { ... }
    fn bit_or(&self, value: Self::Unit, flag: &Flag<Self::Unit>) -> Self::Unit { ... }
    fn bit_or_assign(&mut self, value: Self::Unit, flag: &Flag<Self::Unit>) { ... }
    fn bit_and(&self, value: Self::Unit, flag: &Flag<Self::Unit>) -> Self::Unit { ... }
    fn bit_and_assign(&mut self, value: Self::Unit, flag: &Flag<Self::Unit>) { ... }
    fn bit_xor(&self, value: Self::Unit, flag: &Flag<Self::Unit>) -> Self::Unit { ... }
    fn bit_xor_assign(&mut self, value: Self::Unit, flag: &Flag<Self::Unit>) { ... }
}
Expand description

Bitbite Bitbite is a simple trait that would help you interact bytes with flags easily

Required Associated Types§

Provided Methods§

Source

fn get_flag(&self, flag: &Flag<Self::Unit>) -> Self::Unit

get_flag will return the value of the flag shifted

§Example
 use bitbite::{Flag, Bitbite};
 struct NesCartridgeF6(pub u8);
 impl NesCartridgeF6 {
   pub const LOWER_MAPPER: Flag<u8> = Flag::<u8>::new(0b1111_0000);
 }

 impl Bitbite for NesCartridgeF6 {
    type Unit = u8;
 }
  
 let mut t = NesCartridgeF6(0b0110_0000);
 let lower_mapper = t.get_flag(&NesCartridgeF6::LOWER_MAPPER);
 assert_eq!(lower_mapper, 0b0110);
Source

fn set_flag(&mut self, value: Self::Unit, flag: &Flag<Self::Unit>)

set_flag will set the flag to the given value, overriding the previous value

§Example
 use bitbite::{Flag, Bitbite};

 struct NesCartridgeF6(pub u8);
 impl NesCartridgeF6 {
   pub const LOWER_MAPPER: Flag<u8> = Flag::<u8>::new(0b1111_0000);
 }
  
 impl Bitbite for NesCartridgeF6 {
     type Unit = u8;
 }
  
 let mut t = NesCartridgeF6(0b0100_0000);
 t.set_flag(0b0010, &NesCartridgeF6::LOWER_MAPPER);
 assert_eq!(t.get_flag(&NesCartridgeF6::LOWER_MAPPER), 0b0010);
Source

fn set_on(&mut self, value: Self::Unit, flag: &Flag<Self::Unit>)

👎Deprecated since 0.2.0: Please use bit_or_assign instead.

set_on will set the given bits in the flag on without changing it’s previous state

§Example
 use bitbite::{Flag, Bitbite};

 struct NesCartridgeF6(pub u8);
 impl NesCartridgeF6 {
   pub const LOWER_MAPPER: Flag<u8> = Flag::<u8>::new(0b1111_0000);
 }

 impl Bitbite for NesCartridgeF6 {
     type Unit = u8;
 }
  
 let mut t = NesCartridgeF6(0b0100_0000);
 t.set_on(0b0010, &NesCartridgeF6::LOWER_MAPPER);
 assert_eq!(t.get_flag(&NesCartridgeF6::LOWER_MAPPER), 0b0110);
Source

fn reset_flag(&mut self, flag: &Flag<Self::Unit>)

reset_flag will reset all the bits of the specified flag to 0

§Example
 use bitbite::{Flag, Bitbite};

 struct NesCartridgeF6(pub u8);
 impl NesCartridgeF6 {
   pub const LOWER_MAPPER: Flag<u8> = Flag::<u8>::new(0b1111_0000);
 }

 impl Bitbite for NesCartridgeF6 {
     type Unit = u8;
 }
  
 let mut t = NesCartridgeF6(0b0110_0000);
 t.reset_flag(&NesCartridgeF6::LOWER_MAPPER);
 assert_eq!(t.get_flag(&NesCartridgeF6::LOWER_MAPPER), 0b0000);
Source

fn set_off(&mut self, value: Self::Unit, flag: &Flag<Self::Unit>)

👎Deprecated since 0.2.0: Please use bit_and_assign instead.

set_off will turn off specific bits of the flag
You pass in the bits you want to set off
For example we have this flag 0b1110, and we want to set off the second bit from the right we would have to pass
set_off(0b0100) - this will turn off only the second bit and all other bits will remain the same
Output - 0b1011

§Usage:
 use bitbite::{Flag, Bitbite};

 struct NesCartridgeF6(pub u8);
 impl NesCartridgeF6 {
   pub const LOWER_MAPPER: Flag<u8> = Flag::<u8>::new(0b1111_0000);
 }

 impl Bitbite for NesCartridgeF6 {
     type Unit = u8;
 }
  
 let mut t = NesCartridgeF6(0b1110_0000);
 t.set_off(0b0100, &NesCartridgeF6::LOWER_MAPPER);
 assert_eq!(t.get_flag(&NesCartridgeF6::LOWER_MAPPER), 0b1010);
Source

fn bit_or(&self, value: Self::Unit, flag: &Flag<Self::Unit>) -> Self::Unit

bit_or will do the bitwise | operation on the given flag and will return the value without mutating the data \

§Usage:
 use bitbite::{Flag, Bitbite};

 struct NesCartridgeF6(pub u8);
 impl NesCartridgeF6 {
   pub const LOWER_MAPPER: Flag<u8> = Flag::<u8>::new(0b1111_0000);
 }

 impl Bitbite for NesCartridgeF6 {
     type Unit = u8;
 }
  
 let mut t = NesCartridgeF6(0b1010_0000);
 assert_eq!(t.bit_or(0b0100, &NesCartridgeF6::LOWER_MAPPER), 0b1110);
 assert_eq!(t.get_flag(&NesCartridgeF6::LOWER_MAPPER), 0b1010);
Source

fn bit_or_assign(&mut self, value: Self::Unit, flag: &Flag<Self::Unit>)

bit_or_assign will do the bitwise | operation on the given flag and will and will assign the result to the flag \

§Usage:
 use bitbite::{Flag, Bitbite};

 struct NesCartridgeF6(pub u8);
 impl NesCartridgeF6 {
   pub const LOWER_MAPPER: Flag<u8> = Flag::<u8>::new(0b1111_0000);
 }

 impl Bitbite for NesCartridgeF6 {
     type Unit = u8;
 }
  
 let mut t = NesCartridgeF6(0b1010_0000);
t.bit_or_assign(0b0100, &NesCartridgeF6::LOWER_MAPPER);
 assert_eq!(t.get_flag(&NesCartridgeF6::LOWER_MAPPER), 0b1110);
Source

fn bit_and(&self, value: Self::Unit, flag: &Flag<Self::Unit>) -> Self::Unit

bit_and will do the bitwise & operation on the given flag and will return the value without mutating the data \

§Usage:
 use bitbite::{Flag, Bitbite};

 struct NesCartridgeF6(pub u8);
 impl NesCartridgeF6 {
   pub const LOWER_MAPPER: Flag<u8> = Flag::<u8>::new(0b1111_0000);
 }

 impl Bitbite for NesCartridgeF6 {
     type Unit = u8;
 }
  
 let mut t = NesCartridgeF6(0b1010_0000);
  
 assert_eq!(t.bit_and(0b0110, &NesCartridgeF6::LOWER_MAPPER), 0b0010);
 assert_eq!(t.get_flag(&NesCartridgeF6::LOWER_MAPPER), 0b1010);
Source

fn bit_and_assign(&mut self, value: Self::Unit, flag: &Flag<Self::Unit>)

bit_and_assign will do the bitwise & operation on the given flag and will and will assign the result to the flag \

§Usage:
 use bitbite::{Flag, Bitbite};

 struct NesCartridgeF6(pub u8);
 impl NesCartridgeF6 {
   pub const LOWER_MAPPER: Flag<u8> = Flag::<u8>::new(0b1111_0000);
 }


 impl Bitbite for NesCartridgeF6 {
     type Unit = u8;
 }
  
 let mut t = NesCartridgeF6(0b1010_0000);

 t.bit_and_assign(0b0110, &NesCartridgeF6::LOWER_MAPPER);
 assert_eq!(t.get_flag(&NesCartridgeF6::LOWER_MAPPER), 0b0010);
Source

fn bit_xor(&self, value: Self::Unit, flag: &Flag<Self::Unit>) -> Self::Unit

bit_xor will do the bitwise ^ operation on the given flag and will return the value without mutating the data \

§Usage:
 use bitbite::{Flag, Bitbite};

 struct NesCartridgeF6(pub u8);
 impl NesCartridgeF6 {
   pub const LOWER_MAPPER: Flag<u8> = Flag::<u8>::new(0b1111_0000);
 }

 impl Bitbite for NesCartridgeF6 {
     type Unit = u8;
 }
  
 let mut t = NesCartridgeF6(0b1010_1111);
 assert_eq!(t.bit_xor(0b0110, &NesCartridgeF6::LOWER_MAPPER), 0b1100);
 assert_eq!(t.get_flag(&NesCartridgeF6::LOWER_MAPPER), 0b1010);
Source

fn bit_xor_assign(&mut self, value: Self::Unit, flag: &Flag<Self::Unit>)

bit_xor_assign will do the bitwise ^ operation on the given flag and will and will assign the result to the flag \

§Usage:
 use bitbite::{Flag, Bitbite};

 struct NesCartridgeF6(pub u8);
 impl NesCartridgeF6 {
   pub const LOWER_MAPPER: Flag<u8> = Flag::<u8>::new(0b1111_0000);
 }

 impl Bitbite for NesCartridgeF6 {
     type Unit = u8;
 }
  
 let mut t = NesCartridgeF6(0b1010_1111);
 t.bit_xor_assign(0b0110, &NesCartridgeF6::LOWER_MAPPER);
 assert_eq!(t.get_flag(&NesCartridgeF6::LOWER_MAPPER), 0b1100);

Implementors§