| 1 |
use bitvec::prelude::*; |
| 2 |
use std::fmt; |
| 3 |
use std::ops::{Deref, DerefMut}; |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
#[repr(transparent)] |
| 11 |
pub struct AttributeSetRef<T> { |
| 12 |
_indexer: std::marker::PhantomData<T>, |
| 13 |
bitslice: BitSlice<u8>, |
| 14 |
} |
| 15 |
|
| 16 |
impl<T: EvdevEnum> AttributeSetRef<T> { |
| 17 |
#[inline] |
| 18 |
fn new(bitslice: &BitSlice<u8>) -> &Self { |
| 19 |
|
| 20 |
unsafe { &*(bitslice as *const BitSlice<u8> as *const Self) } |
| 21 |
} |
| 22 |
|
| 23 |
#[inline] |
| 24 |
fn new_mut(bitslice: &mut BitSlice<u8>) -> &mut Self { |
| 25 |
|
| 26 |
unsafe { &mut *(bitslice as *mut BitSlice<u8> as *mut Self) } |
| 27 |
} |
| 28 |
|
| 29 |
|
| 30 |
#[inline] |
| 31 |
pub fn contains(&self, attr: T) -> bool { |
| 32 |
self.bitslice.get(attr.to_index()).map_or(false, |b| *b) |
| 33 |
} |
| 34 |
|
| 35 |
|
| 36 |
#[inline] |
| 37 |
pub fn iter(&self) -> AttributeSetRefIter<'_, T> { |
| 38 |
self.into_iter() |
| 39 |
} |
| 40 |
|
| 41 |
pub fn insert(&mut self, attr: T) { |
| 42 |
self.set(attr, true) |
| 43 |
} |
| 44 |
|
| 45 |
pub fn remove(&mut self, attr: T) { |
| 46 |
self.set(attr, false) |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
#[inline] |
| 51 |
pub(crate) fn set(&mut self, attr: T, on: bool) { |
| 52 |
self.bitslice.set(attr.to_index(), on) |
| 53 |
} |
| 54 |
|
| 55 |
#[inline] |
| 56 |
pub(crate) fn slice_iter(&self, start: T) -> AttributeSetRefIter<'_, T> { |
| 57 |
let slice = Self::new(&self.bitslice[start.to_index()..]); |
| 58 |
|
| 59 |
AttributeSetRefIter { |
| 60 |
_indexer: std::marker::PhantomData, |
| 61 |
inner: slice.bitslice.iter_ones(), |
| 62 |
offset: start.to_index(), |
| 63 |
} |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
impl<T: EvdevEnum + fmt::Debug> fmt::Debug for AttributeSetRef<T> { |
| 68 |
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 69 |
f.debug_set().entries(self.iter()).finish() |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
impl<T: EvdevEnum> Default for &AttributeSetRef<T> { |
| 74 |
fn default() -> Self { |
| 75 |
AttributeSetRef::new(BitSlice::empty()) |
| 76 |
} |
| 77 |
} |
| 78 |
impl<T: EvdevEnum> Default for &mut AttributeSetRef<T> { |
| 79 |
fn default() -> Self { |
| 80 |
AttributeSetRef::new_mut(BitSlice::empty_mut()) |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
impl<'a, T: EvdevEnum> IntoIterator for &'a AttributeSetRef<T> { |
| 85 |
type Item = T; |
| 86 |
type IntoIter = AttributeSetRefIter<'a, T>; |
| 87 |
|
| 88 |
fn into_iter(self) -> Self::IntoIter { |
| 89 |
AttributeSetRefIter { |
| 90 |
_indexer: std::marker::PhantomData, |
| 91 |
inner: self.bitslice.iter_ones(), |
| 92 |
offset: 0, |
| 93 |
} |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
pub struct AttributeSetRefIter<'a, T> { |
| 98 |
_indexer: std::marker::PhantomData<&'a T>, |
| 99 |
inner: bitvec::slice::IterOnes<'a, u8, Lsb0>, |
| 100 |
offset: usize, |
| 101 |
} |
| 102 |
|
| 103 |
impl<T: EvdevEnum> Iterator for AttributeSetRefIter<'_, T> { |
| 104 |
type Item = T; |
| 105 |
|
| 106 |
fn next(&mut self) -> Option<Self::Item> { |
| 107 |
self.inner |
| 108 |
.next() |
| 109 |
.map(|idx| T::from_index(self.offset + idx)) |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
impl<T: EvdevEnum> ExactSizeIterator for AttributeSetRefIter<'_, T> { |
| 114 |
fn len(&self) -> usize { |
| 115 |
self.inner.len() |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
impl<T: EvdevEnum> DoubleEndedIterator for AttributeSetRefIter<'_, T> { |
| 120 |
fn next_back(&mut self) -> Option<Self::Item> { |
| 121 |
self.inner.next_back().map(T::from_index) |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
pub struct AttributeSet<T: ArrayedEvdevEnum> { |
| 126 |
container: T::Array, |
| 127 |
} |
| 128 |
|
| 129 |
impl<T: ArrayedEvdevEnum> AttributeSet<T> { |
| 130 |
pub fn new() -> Self { |
| 131 |
Self { |
| 132 |
container: T::zeroed_array(), |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
fn as_bitslice(&self) -> &BitSlice<u8> { |
| 137 |
T::array_as_slice(&self.container) |
| 138 |
} |
| 139 |
|
| 140 |
fn as_mut_bitslice(&mut self) -> &mut BitSlice<u8> { |
| 141 |
T::array_as_slice_mut(&mut self.container) |
| 142 |
} |
| 143 |
|
| 144 |
#[inline] |
| 145 |
pub(crate) fn as_mut_raw_slice(&mut self) -> &mut [u8] { |
| 146 |
T::array_as_buf(&mut self.container) |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
impl<T: ArrayedEvdevEnum> Default for AttributeSet<T> { |
| 151 |
fn default() -> Self { |
| 152 |
Self::new() |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
impl<T: ArrayedEvdevEnum> FromIterator<T> for AttributeSet<T> { |
| 157 |
fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self { |
| 158 |
let mut set = AttributeSet::default(); |
| 159 |
iter.into_iter().for_each(|el| set.insert(el)); |
| 160 |
set |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
impl<'a, T: ArrayedEvdevEnum> FromIterator<&'a T> for AttributeSet<T> { |
| 165 |
fn from_iter<I: IntoIterator<Item = &'a T>>(iter: I) -> Self { |
| 166 |
Self::from_iter(iter.into_iter().copied()) |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
impl<T: ArrayedEvdevEnum> Deref for AttributeSet<T> { |
| 171 |
type Target = AttributeSetRef<T>; |
| 172 |
fn deref(&self) -> &AttributeSetRef<T> { |
| 173 |
AttributeSetRef::new(self.as_bitslice()) |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
impl<T: ArrayedEvdevEnum> DerefMut for AttributeSet<T> { |
| 178 |
fn deref_mut(&mut self) -> &mut AttributeSetRef<T> { |
| 179 |
AttributeSetRef::new_mut(self.as_mut_bitslice()) |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
impl<'a, T: ArrayedEvdevEnum> IntoIterator for &'a AttributeSet<T> { |
| 184 |
type Item = T; |
| 185 |
type IntoIter = AttributeSetRefIter<'a, T>; |
| 186 |
|
| 187 |
fn into_iter(self) -> Self::IntoIter { |
| 188 |
(**self).into_iter() |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
impl<T: ArrayedEvdevEnum> Clone for AttributeSet<T> |
| 193 |
where |
| 194 |
T::Array: Clone, |
| 195 |
{ |
| 196 |
fn clone(&self) -> Self { |
| 197 |
Self { |
| 198 |
container: self.container.clone(), |
| 199 |
} |
| 200 |
} |
| 201 |
fn clone_from(&mut self, other: &Self) { |
| 202 |
self.container.clone_from(&other.container) |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
impl<T: ArrayedEvdevEnum + fmt::Debug> fmt::Debug for AttributeSet<T> { |
| 207 |
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 208 |
(**self).fmt(f) |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
pub trait EvdevEnum: Copy + 'static { |
| 213 |
fn from_index(i: usize) -> Self; |
| 214 |
fn to_index(self) -> usize; |
| 215 |
} |
| 216 |
|
| 217 |
pub trait ArrayedEvdevEnum: EvdevEnum { |
| 218 |
type Array; |
| 219 |
fn array_as_slice(arr: &Self::Array) -> &BitSlice<u8>; |
| 220 |
fn array_as_slice_mut(arr: &mut Self::Array) -> &mut BitSlice<u8>; |
| 221 |
fn array_as_buf(arr: &mut Self::Array) -> &mut [u8]; |
| 222 |
fn zeroed_array() -> Self::Array; |
| 223 |
} |
| 224 |
|
| 225 |
macro_rules! evdev_enum { |
| 226 |
($t:ty, Array, $($(#[$attr:meta])* $c:ident = $val:expr,)*) => { |
| 227 |
evdev_enum!( |
| 228 |
$t, |
| 229 |
Array: bitvec::BitArr!(for <$t>::COUNT, in u8), |
| 230 |
bitvec::array::BitArray::as_raw_mut_slice, |
| 231 |
bitvec::array::BitArray::ZERO, |
| 232 |
$($(#[$attr])* $c = $val,)* |
| 233 |
); |
| 234 |
}; |
| 235 |
($t:ty, box Array, $($(#[$attr:meta])* $c:ident = $val:expr,)*) => { |
| 236 |
evdev_enum!( |
| 237 |
$t, |
| 238 |
Array: Box<bitvec::BitArr!(for <$t>::COUNT, in u8)>, |
| 239 |
bitvec::array::BitArray::as_raw_mut_slice, |
| 240 |
Box::new(bitvec::array::BitArray::ZERO), |
| 241 |
$($(#[$attr])* $c = $val,)* |
| 242 |
); |
| 243 |
}; |
| 244 |
( |
| 245 |
$t:ty, |
| 246 |
Array: $Array:ty, $arr_as_buf:expr, $zero:expr, |
| 247 |
$($(#[$attr:meta])* $c:ident = $val:expr,)* |
| 248 |
) => { |
| 249 |
impl $crate::attribute_set::ArrayedEvdevEnum for $t { |
| 250 |
type Array = $Array; |
| 251 |
fn array_as_slice(arr: &Self::Array) -> &bitvec::slice::BitSlice<u8> { |
| 252 |
arr |
| 253 |
} |
| 254 |
fn array_as_slice_mut(arr: &mut Self::Array) -> &mut bitvec::slice::BitSlice<u8> { |
| 255 |
arr |
| 256 |
} |
| 257 |
fn array_as_buf(arr: &mut Self::Array) -> &mut [u8] { |
| 258 |
$arr_as_buf(arr) |
| 259 |
} |
| 260 |
fn zeroed_array() -> Self::Array { |
| 261 |
$zero |
| 262 |
} |
| 263 |
} |
| 264 |
evdev_enum!($t, $($(#[$attr])* $c = $val,)*); |
| 265 |
}; |
| 266 |
($t:ty, $($(#[$attr:meta])* $c:ident = $val:expr,)*) => { |
| 267 |
impl $t { |
| 268 |
$($(#[$attr])* pub const $c: Self = Self($val);)* |
| 269 |
|
| 270 |
const NAME_MAP: &'static [(&'static str, $t)] = &[ |
| 271 |
$((stringify!($c), Self::$c),)* |
| 272 |
]; |
| 273 |
} |
| 274 |
impl std::str::FromStr for $t { |
| 275 |
type Err = crate::EnumParseError; |
| 276 |
|
| 277 |
fn from_str(s: &str) -> Result<Self, Self::Err> { |
| 278 |
match Self::NAME_MAP.iter().find(|e| e.0 == s) { |
| 279 |
Some(e) => Ok(e.1), |
| 280 |
None => Err(crate::EnumParseError(())), |
| 281 |
} |
| 282 |
} |
| 283 |
} |
| 284 |
impl std::fmt::Debug for $t { |
| 285 |
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { |
| 286 |
#[allow(unreachable_patterns)] |
| 287 |
match *self { |
| 288 |
$(Self::$c => f.pad(stringify!($c)),)* |
| 289 |
_ => write!(f, "unknown key: {}", self.0), |
| 290 |
} |
| 291 |
} |
| 292 |
} |
| 293 |
impl $crate::attribute_set::EvdevEnum for $t { |
| 294 |
#[inline] |
| 295 |
fn from_index(i: usize) -> Self { |
| 296 |
Self(i as _) |
| 297 |
} |
| 298 |
#[inline] |
| 299 |
fn to_index(self) -> usize { |
| 300 |
self.0 as _ |
| 301 |
} |
| 302 |
} |
| 303 |
#[cfg(feature = "serde")] |
| 304 |
#[allow(unreachable_patterns)] |
| 305 |
impl serde::Serialize for $t { |
| 306 |
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> |
| 307 |
where |
| 308 |
S: serde::ser::Serializer, |
| 309 |
{ |
| 310 |
let value = match *self { |
| 311 |
$(Self::$c => stringify!($c),)* |
| 312 |
_ => unreachable!(), |
| 313 |
}; |
| 314 |
|
| 315 |
serializer.serialize_str(value) |
| 316 |
} |
| 317 |
} |
| 318 |
#[cfg(feature = "serde")] |
| 319 |
impl<'de> serde::Deserialize<'de> for $t { |
| 320 |
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
| 321 |
where |
| 322 |
D: serde::de::Deserializer<'de>, |
| 323 |
{ |
| 324 |
struct Visitor; |
| 325 |
impl<'de> serde::de::Visitor<'de> for Visitor { |
| 326 |
type Value = $t; |
| 327 |
|
| 328 |
fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result { |
| 329 |
write!(formatter, "a string with any of the constants in {}", stringify!($t)) |
| 330 |
} |
| 331 |
|
| 332 |
fn visit_str<E>(self, s: &str) -> Result<Self::Value, E> |
| 333 |
where |
| 334 |
E: serde::de::Error, |
| 335 |
{ |
| 336 |
match <$t>::NAME_MAP.iter().find(|(key, _)| s.eq_ignore_ascii_case(key)) { |
| 337 |
Some((_, v)) => Ok(*v), |
| 338 |
None => Err(serde::de::Error::invalid_value(serde::de::Unexpected::Str(s), &self)), |
| 339 |
} |
| 340 |
} |
| 341 |
} |
| 342 |
deserializer.deserialize_str(Visitor) |
| 343 |
} |
| 344 |
} |
| 345 |
} |
| 346 |
} |