openthread/ot/types/
network_key.rsuse crate::prelude_internal::*;
#[derive(Default, Copy, Clone)]
#[repr(transparent)]
pub struct NetworkKey(pub otNetworkKey);
impl_ot_castable!(NetworkKey, otNetworkKey);
impl std::fmt::Debug for NetworkKey {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "NetworkKey({})", hex::encode(self.as_slice()))
}
}
impl NetworkKey {
pub fn as_slice(&self) -> &[u8] {
&self.0.m8
}
pub fn to_vec(&self) -> Vec<u8> {
self.as_slice().to_vec()
}
}
impl From<[u8; OT_NETWORK_KEY_SIZE as usize]> for NetworkKey {
fn from(key: [u8; OT_NETWORK_KEY_SIZE as usize]) -> Self {
Self(otNetworkKey { m8: key })
}
}
impl From<NetworkKey> for [u8; OT_NETWORK_KEY_SIZE as usize] {
fn from(key: NetworkKey) -> Self {
key.0.m8
}
}
impl NetworkKey {
pub fn try_ref_from_slice(slice: &[u8]) -> Result<&NetworkKey, ot::WrongSize> {
if slice.len() == OT_NETWORK_KEY_SIZE as usize {
Ok(unsafe { Self::ref_from_ot_ptr((slice as *const [u8]) as *const otNetworkKey) }
.unwrap())
} else {
Err(ot::WrongSize)
}
}
}
#[derive(Default, Copy, Clone)]
#[repr(transparent)]
pub struct Pskc(pub otPskc);
impl_ot_castable!(Pskc, otPskc);
impl std::fmt::Debug for Pskc {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "Pskc({:?})", hex::encode(self.as_slice()))
}
}
impl Pskc {
pub fn as_slice(&self) -> &[u8] {
&self.0.m8
}
pub fn to_vec(&self) -> Vec<u8> {
self.as_slice().to_vec()
}
}
impl From<[u8; OT_PSKC_MAX_SIZE as usize]> for Pskc {
fn from(key: [u8; OT_PSKC_MAX_SIZE as usize]) -> Self {
Self(otPskc { m8: key })
}
}
impl From<Pskc> for [u8; OT_PSKC_MAX_SIZE as usize] {
fn from(key: Pskc) -> Self {
key.0.m8
}
}
impl Pskc {
pub fn try_ref_from_slice(slice: &[u8]) -> Result<&Pskc, ot::WrongSize> {
if slice.len() == OT_PSKC_MAX_SIZE as usize {
Ok(unsafe { Self::ref_from_ot_ptr((slice as *const [u8]) as *const otPskc) }.unwrap())
} else {
Err(ot::WrongSize)
}
}
}