This commit is contained in:
chemicstry 2023-02-27 02:03:50 +02:00
parent e960bca5fd
commit f0247d305c
4 changed files with 0 additions and 164 deletions

View File

@ -1,132 +0,0 @@
use core::marker::PhantomData;
pub trait Packet {
fn data(&self) -> &[u8];
fn data_mut(&mut self) -> &mut [u8];
#[inline]
fn get<F: AsPacketField>(&self, field: &F) -> F::T {
field.get(self.data())
}
#[inline]
fn set<F: AsPacketField>(&mut self, field: &F, val: F::T) {
field.set(self.data_mut(), val)
}
}
pub trait AsPacketField {
type T;
fn get(&self, data: &[u8]) -> Self::T;
fn set(&self, data: &mut [u8], val: Self::T);
}
pub struct BitField {
pub byte: usize,
pub bit: u8,
pub mask: u8,
}
impl BitField {
pub const fn new(byte: usize, bit: u8, size: u8) -> Self {
let mask = (0xFF >> size) << bit;
Self { byte, bit, mask }
}
}
impl AsPacketField for BitField {
type T = u8;
#[inline]
fn get(&self, data: &[u8]) -> Self::T {
(data[self.byte] & self.mask) >> self.bit
}
#[inline]
fn set(&self, data: &mut [u8], val: Self::T) {
data[self.byte] = (val << self.bit) | (data[self.byte] & !self.mask)
}
}
pub struct BoolField {
pub byte: usize,
pub bit: u8,
}
impl BoolField {
pub const fn new(byte: usize, bit: u8) -> Self {
Self { byte, bit }
}
}
impl AsPacketField for BoolField {
type T = bool;
#[inline]
fn get(&self, data: &[u8]) -> Self::T {
data[self.byte] & (1 << self.bit) != 0
}
#[inline]
fn set(&self, data: &mut [u8], val: Self::T) {
data[self.byte] = ((val as u8) << self.bit) | (data[self.byte] & !(1 << self.bit))
}
}
pub struct Field<T> {
pub byte: usize,
_phantom: PhantomData<T>,
}
impl<T> Field<T> {
pub const fn new(byte: usize) -> Self {
Self {
byte,
_phantom: PhantomData,
}
}
}
impl<T> AsPacketField for Field<T> {
type T = T;
#[inline]
fn get(&self, data: &[u8]) -> Self::T {
unsafe { core::ptr::read(data.as_ptr().offset(self.byte as _) as *const T) }
}
#[inline]
fn set(&self, data: &mut [u8], val: Self::T) {
unsafe { core::ptr::write(data.as_mut_ptr().offset(self.byte as _) as *mut T, val) }
}
}
#[cfg(test)]
mod tests {
use super::{AsPacketField, BitField, BoolField};
#[test]
fn bitfield() {
let field = BitField::new(0, 4, 3);
let mut data = [0b1111_1111];
assert_eq!(field.get(&data), 0b111);
field.set(&mut data, 0b000);
assert_eq!(field.get(&data), 0b000);
assert_eq!(data, [0b1000_1111]);
}
#[test]
fn boolfield() {
let field = BoolField::new(0, 5);
let mut data = [0b1111_1111];
assert_eq!(field.get(&data), true);
field.set(&mut data, false);
assert_eq!(field.get(&data), false);
assert_eq!(data, [0b1101_1111]);
}
}

View File

@ -1,14 +1,3 @@
// #[bitfield(bytes = 1)]
// #[derive(BitfieldSpecifier)]
// pub struct Control {
// pub vendor_specific: B2,
// #[skip]
// __: B3,
// pub naca: B1,
// #[skip]
// __: B2,
// }
use crate::packed_struct;
packed_struct! {

View File

@ -1,21 +1,3 @@
// use super::control::Control;
// #[bitfield(bytes = 6)]
// pub struct InquiryCommand {
// /// Always 0x12
// pub op_code: B8,
// #[skip]
// __: B7,
// /// If set, return vital data related to the page_code field
// pub enable_vital_product_data: B1,
// /// What kind of vital data to return
// pub page_code: B8,
// /// Amount of bytes allocation for data-in transfer
// pub allocation_length: B16,
// /// Control byte
// pub control: Control,
// }
use super::Control;
use crate::class::msc::subclass::scsi::enums::{
PeripheralDeviceType, PeripheralQualifier, ResponseDataFormat, SpcVersion, TargetPortGroupSupport,

View File

@ -1,6 +1,3 @@
// `bytes` in `#[bitfield(bytes = 6)]` causes a warning
#![allow(redundant_semicolons)]
mod control;
pub use control::*;