openthread/ot/types/
network_name.rs1use crate::prelude_internal::*;
6
7use core::cmp::Ordering;
8use core::fmt::{Debug, Formatter};
9
10#[derive(Default, Copy, Clone)]
13#[repr(transparent)]
14pub struct NetworkName(pub otNetworkName);
15
16impl_ot_castable!(NetworkName, otNetworkName);
17
18impl Debug for NetworkName {
19 fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
20 match self.try_as_str() {
21 Ok(s) => s.fmt(f),
22 Err(_) => write!(f, "[{:?}]", hex::encode(self.as_slice())),
23 }
24 }
25}
26
27impl PartialEq for NetworkName {
28 fn eq(&self, other: &Self) -> bool {
29 self.as_slice() == other.as_slice()
30 }
31}
32
33impl Eq for NetworkName {}
34
35impl PartialOrd for NetworkName {
36 fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
37 Some(self.cmp(other))
38 }
39}
40
41impl Ord for NetworkName {
42 fn cmp(&self, other: &Self) -> Ordering {
43 self.as_slice().cmp(other.as_slice())
44 }
45}
46
47impl NetworkName {
48 pub fn try_from_slice(slice: &[u8]) -> Result<Self, ot::WrongSize> {
50 let len = slice.len();
51 if len > OT_NETWORK_NAME_MAX_SIZE as usize {
52 return Err(ot::WrongSize);
53 }
54
55 sa::assert_eq_size!(u8, ::std::os::raw::c_char);
56 let slice = zerocopy::Ref::into_ref(
57 zerocopy::Ref::<_, [::std::os::raw::c_char]>::from_bytes(slice).unwrap(),
58 );
59
60 let mut ret = NetworkName::default();
61 ret.0.m8[0..len].clone_from_slice(slice);
62
63 Ok(ret)
64 }
65
66 #[allow(clippy::len_without_is_empty)]
68 pub fn len(&self) -> usize {
69 self.0.m8.iter().position(|&x| x == 0).unwrap_or(OT_NETWORK_NAME_MAX_SIZE as usize)
70 }
71
72 pub fn as_slice(&self) -> &[u8] {
74 use zerocopy::IntoBytes as _;
75
76 sa::assert_eq_size!(u8, ::std::os::raw::c_char);
77 self.0.m8[0..self.len()].as_bytes()
78 }
79
80 pub fn to_vec(&self) -> Vec<u8> {
82 self.as_slice().to_vec()
83 }
84
85 pub fn try_as_str(&self) -> Result<&str, std::str::Utf8Error> {
87 std::str::from_utf8(self.as_slice())
88 }
89
90 pub fn as_c_str(&self) -> *const ::std::os::raw::c_char {
92 self.0.m8.as_ptr()
93 }
94}
95
96impl<'a> TryFrom<&'a str> for NetworkName {
97 type Error = ot::WrongSize;
98
99 fn try_from(value: &'a str) -> Result<Self, Self::Error> {
100 NetworkName::try_from_slice(value.as_bytes())
101 }
102}
103
104impl<'a> TryFrom<&'a [u8]> for NetworkName {
105 type Error = ot::WrongSize;
106
107 fn try_from(value: &'a [u8]) -> Result<Self, Self::Error> {
108 NetworkName::try_from_slice(value)
109 }
110}
111
112impl TryFrom<Vec<u8>> for NetworkName {
113 type Error = ot::WrongSize;
114
115 fn try_from(value: Vec<u8>) -> Result<Self, Self::Error> {
116 NetworkName::try_from_slice(&value)
117 }
118}
119
120impl From<&NetworkName> for otNetworkName {
121 fn from(x: &NetworkName) -> Self {
122 *x.as_ot_ref()
123 }
124}