Merge pull request #58 from emberian/bitvec-0.22

Upgrade to bitvec 0.22 with const generics
This commit is contained in:
Noa 2021-12-03 21:50:16 -06:00 committed by GitHub
commit 3966c797e2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 19 additions and 28 deletions

View file

@ -13,7 +13,7 @@ tokio = ["tokio_1", "futures-core"]
[dependencies] [dependencies]
libc = "0.2.89" libc = "0.2.89"
bitvec = "0.21" bitvec = "0.22"
nix = "0.23" nix = "0.23"
tokio_1 = { package = "tokio", version = "1.0", features = ["net"], optional = true } tokio_1 = { package = "tokio", version = "1.0", features = ["net"], optional = true }

View file

@ -145,35 +145,39 @@ macro_rules! evdev_enum {
($t:ty, Array, $($(#[$attr:meta])* $c:ident = $val:expr,)*) => { ($t:ty, Array, $($(#[$attr:meta])* $c:ident = $val:expr,)*) => {
evdev_enum!( evdev_enum!(
$t, $t,
Array:bitvec::BitArr!(for <$t>::COUNT, in u8), Array: bitvec::BitArr!(for <$t>::COUNT, in u8),
|x| x,
|x| x,
bitvec::array::BitArray::as_mut_raw_slice, bitvec::array::BitArray::as_mut_raw_slice,
bitvec::array::BitArray::zeroed, bitvec::array::BitArray::zeroed(),
$($(#[$attr])* $c = $val,)*
);
};
($t:ty, box Array, $($(#[$attr:meta])* $c:ident = $val:expr,)*) => {
evdev_enum!(
$t,
Array: Box<bitvec::BitArr!(for <$t>::COUNT, in u8)>,
bitvec::array::BitArray::as_mut_raw_slice,
Box::new(bitvec::array::BitArray::zeroed()),
$($(#[$attr])* $c = $val,)* $($(#[$attr])* $c = $val,)*
); );
}; };
( (
$t:ty, $t:ty,
Array: $Array:ty, $arr_as_slice:expr, $arr_as_slice_mut:expr, $arr_as_buf:expr, $zero:expr, Array: $Array:ty, $arr_as_buf:expr, $zero:expr,
$($(#[$attr:meta])* $c:ident = $val:expr,)* $($(#[$attr:meta])* $c:ident = $val:expr,)*
) => { ) => {
impl $crate::attribute_set::ArrayedEvdevEnum for $t { impl $crate::attribute_set::ArrayedEvdevEnum for $t {
type Array = $Array; type Array = $Array;
fn array_as_slice(arr: &Self::Array) -> &bitvec::slice::BitSlice<bitvec::order::Lsb0, u8> { fn array_as_slice(arr: &Self::Array) -> &bitvec::slice::BitSlice<bitvec::order::Lsb0, u8> {
let f: fn(&Self::Array) -> &bitvec::slice::BitSlice<bitvec::order::Lsb0, u8> = $arr_as_slice; arr
f(arr)
} }
fn array_as_slice_mut(arr: &mut Self::Array) -> &mut bitvec::slice::BitSlice<bitvec::order::Lsb0, u8> { fn array_as_slice_mut(arr: &mut Self::Array) -> &mut bitvec::slice::BitSlice<bitvec::order::Lsb0, u8> {
let f: fn(&mut Self::Array) -> &mut bitvec::slice::BitSlice<bitvec::order::Lsb0, u8> = $arr_as_slice_mut; arr
f(arr)
} }
fn array_as_buf(arr: &mut Self::Array) -> &mut [u8] { fn array_as_buf(arr: &mut Self::Array) -> &mut [u8] {
let f: fn(&mut Self::Array) -> &mut [u8] = $arr_as_buf; $arr_as_buf(arr)
f(arr)
} }
fn zeroed_array() -> Self::Array { fn zeroed_array() -> Self::Array {
$zero() $zero
} }
} }
evdev_enum!($t, $($(#[$attr])* $c = $val,)*); evdev_enum!($t, $($(#[$attr])* $c = $val,)*);
@ -185,7 +189,7 @@ macro_rules! evdev_enum {
impl std::str::FromStr for $t { impl std::str::FromStr for $t {
type Err = crate::EnumParseError; type Err = crate::EnumParseError;
fn from_str(s: &str) -> Result<Self, Self::Err> { fn from_str(s: &str) -> Result<Self, Self::Err> {
let map: &[(&'static str, $t)] = &[ let map: &[(&'static str, $t)] = &[
$((stringify!($c), Self::$c),)* $((stringify!($c), Self::$c),)*
]; ];

View file

@ -19,22 +19,9 @@ impl Key {
pub(crate) const COUNT: usize = libc::KEY_CNT; pub(crate) const COUNT: usize = libc::KEY_CNT;
} }
const fn bit_elts<T>(bits: usize) -> usize {
let width = std::mem::size_of::<T>() * 8;
bits / width + (bits % width != 0) as usize
}
// TODO: replace with BitArr!() once const generics is stable and BitView is implemented for any [T; N]
const KEY_ARRAY_LEN: usize = bit_elts::<u8>(Key::COUNT);
type KeyArray = [u8; KEY_ARRAY_LEN];
const KEY_ARRAY_INIT: KeyArray = [0; KEY_ARRAY_LEN];
evdev_enum!( evdev_enum!(
Key, Key,
Array: Box<[u8; KEY_ARRAY_LEN]>, box Array,
|x| bitvec::slice::BitSlice::from_slice(&x[..]).unwrap(),
|x| bitvec::slice::BitSlice::from_slice_mut(&mut x[..]).unwrap(),
|x| &mut x[..],
|| Box::new(KEY_ARRAY_INIT),
KEY_RESERVED = 0, KEY_RESERVED = 0,
KEY_ESC = 1, KEY_ESC = 1,
KEY_1 = 2, KEY_1 = 2,