use crate::bitman::checked::index::IndexU8;
use crate::bitman::checked::indices::IndexRangeU8;
use core::ops::BitAnd;
use core::ops::Shl;
use core::ops::Shr;
pub trait ReadBitSafe {
type Type;
fn read_bit_safe(&self, index: impl IndexU8) -> bool;
}
impl ReadBitSafe for u8 {
type Type = Self;
fn read_bit_safe(&self, index: impl IndexU8) -> bool {
let temporary_1 = self.shr(index.value());
let temporary_2 = 0b1.bitand(temporary_1);
temporary_2 == 1
}
}
pub trait ReadBitsSafe {
type Type;
fn read_bits_safe(&self, range: impl IndexRangeU8 + core::marker::Copy) -> u8;
}
impl ReadBitsSafe for u8 {
type Type = Self;
fn read_bits_safe(&self, range: impl IndexRangeU8 + core::marker::Copy) -> u8 {
let bits = Self::BITS as Self;
let start = range.start();
let end = range.end();
let temporary_1 = self.shr(start);
let temporary_2 = temporary_1.shl(start);
let amount = bits - end - 1;
let temporary_3 = temporary_2.shl(amount);
let temporary_4 = temporary_3.shr(amount);
let temporary_5 = temporary_4.shr(start);
temporary_5
}
}