1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
use alloc::vec::Vec;

pub struct AttValue {
  value: Vec<u8>,
}

impl AttValue {
  pub(super) const fn new() -> Self {
    Self { value: Vec::new() }
  }

  #[inline]
  pub fn value(&self) -> &[u8] {
    &self.value
  }

  #[inline]
  pub fn as_ref<T: Sized>(&self) -> Option<&T> {
    if self.len() == core::mem::size_of::<T>() {
      unsafe { Some(&*(self.value.as_ptr() as *const T)) }
    } else {
      None
    }
  }

  #[inline]
  pub fn as_mut<T: Sized>(&mut self) -> Option<&mut T> {
    if self.len() == core::mem::size_of::<T>() {
      unsafe { Some(&mut *(self.value.as_mut_ptr() as *mut T)) }
    } else {
      None
    }
  }

  #[inline]
  pub fn is_empty(&self) -> bool {
    self.value.is_empty()
  }

  #[inline]
  pub fn len(&self) -> usize {
    self.value.len()
  }

  #[inline]
  pub fn clear(&mut self) {
    self.value.clear();
  }

  #[inline]
  pub fn set_value(&mut self, value: &[u8]) {
    self.value.clear();
    self.value.extend_from_slice(value);
  }

  #[inline]
  pub fn set_from<T: Sized>(&mut self, p: &T) {
    let slice = unsafe {
      ::core::slice::from_raw_parts((p as *const T) as *const u8, ::core::mem::size_of::<T>())
    };
    self.set_value(slice);
  }

  #[inline]
  pub fn extend(&mut self, value: &[u8]) {
    self.value.extend_from_slice(value);
  }
}