Merge pull request #58 from emberian/bitvec-0.22
Upgrade to bitvec 0.22 with const generics
This commit is contained in:
commit
3966c797e2
3 changed files with 19 additions and 28 deletions
|
@ -13,7 +13,7 @@ tokio = ["tokio_1", "futures-core"]
|
|||
|
||||
[dependencies]
|
||||
libc = "0.2.89"
|
||||
bitvec = "0.21"
|
||||
bitvec = "0.22"
|
||||
nix = "0.23"
|
||||
|
||||
tokio_1 = { package = "tokio", version = "1.0", features = ["net"], optional = true }
|
||||
|
|
|
@ -146,34 +146,38 @@ macro_rules! evdev_enum {
|
|||
evdev_enum!(
|
||||
$t,
|
||||
Array: bitvec::BitArr!(for <$t>::COUNT, in u8),
|
||||
|x| x,
|
||||
|x| x,
|
||||
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,)*
|
||||
);
|
||||
};
|
||||
(
|
||||
$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,)*
|
||||
) => {
|
||||
impl $crate::attribute_set::ArrayedEvdevEnum for $t {
|
||||
type Array = $Array;
|
||||
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;
|
||||
f(arr)
|
||||
arr
|
||||
}
|
||||
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;
|
||||
f(arr)
|
||||
arr
|
||||
}
|
||||
fn array_as_buf(arr: &mut Self::Array) -> &mut [u8] {
|
||||
let f: fn(&mut Self::Array) -> &mut [u8] = $arr_as_buf;
|
||||
f(arr)
|
||||
$arr_as_buf(arr)
|
||||
}
|
||||
fn zeroed_array() -> Self::Array {
|
||||
$zero()
|
||||
$zero
|
||||
}
|
||||
}
|
||||
evdev_enum!($t, $($(#[$attr])* $c = $val,)*);
|
||||
|
|
|
@ -19,22 +19,9 @@ impl Key {
|
|||
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!(
|
||||
Key,
|
||||
Array: Box<[u8; KEY_ARRAY_LEN]>,
|
||||
|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),
|
||||
box Array,
|
||||
KEY_RESERVED = 0,
|
||||
KEY_ESC = 1,
|
||||
KEY_1 = 2,
|
||||
|
|
Loading…
Reference in a new issue