spinel_pack/
eui.rs
1use std::fmt::{Debug, Display, Error, Formatter};
6use zerocopy::{FromBytes, Immutable, IntoBytes, KnownLayout, Ref, Unaligned};
7
8#[derive(Debug, Eq, PartialEq, Hash, thiserror::Error)]
10pub struct WrongSize;
11
12impl Display for WrongSize {
13 fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
14 <Self as Debug>::fmt(self, f)
15 }
16}
17
18#[derive(
20 Debug, Eq, PartialEq, Hash, Copy, Clone, KnownLayout, FromBytes, IntoBytes, Immutable, Unaligned,
21)]
22#[repr(C)]
23#[derive(Default)]
24pub struct EUI64(pub [u8; 8]);
25
26impl<'a> std::convert::From<&'a EUI64> for &'a [u8] {
28 fn from(val: &'a EUI64) -> Self {
29 &val.0
30 }
31}
32
33impl<'a> std::convert::TryInto<&'a EUI64> for &'a [u8] {
36 type Error = WrongSize;
37
38 fn try_into(self) -> Result<&'a EUI64, Self::Error> {
39 Ref::<_, EUI64>::from_bytes(self)
40 .map_err(Into::into)
41 .map_err(|_: zerocopy::SizeError<_, _>| WrongSize)
42 .map(Ref::into_ref)
43 }
44}
45
46#[derive(
48 Debug, Eq, PartialEq, Hash, Copy, Clone, KnownLayout, FromBytes, IntoBytes, Immutable, Unaligned,
49)]
50#[repr(C)]
51#[derive(Default)]
52pub struct EUI48(pub [u8; 6]);
53
54impl<'a> std::convert::From<&'a EUI48> for &'a [u8] {
56 fn from(val: &'a EUI48) -> Self {
57 &val.0
58 }
59}
60
61impl<'a> std::convert::TryInto<&'a EUI48> for &'a [u8] {
64 type Error = WrongSize;
65
66 fn try_into(self) -> Result<&'a EUI48, Self::Error> {
67 Ref::<_, EUI48>::from_bytes(self)
68 .map_err(Into::into)
69 .map_err(|_: zerocopy::SizeError<_, _>| WrongSize)
70 .map(Ref::into_ref)
71 }
72}