os_id/
data.rs

1use core::{hash, cmp, fmt};
2
3#[derive(Copy, Clone)]
4///Thread name, limited to 16 characters which is common limit for unix systems.
5///
6///Note that actual limit is 15 characters as 16 includes is needed for terminating character.
7///
8///Commonly it is valid utf-8 string, but due to potential encoding differences, it is possible
9///that name cannot be interpreted as utf-8 string.
10///In this case  user is encouraged to use `as_bytes` method and perform conversion by himself.
11///
12///
13///## fmt
14///
15///- `Debug` trait outputs content as 16 bytes.
16///
17///- `Display` trait outputs content as string, if possible, otherwise fallbacks to byte slice.
18pub struct ThreadName {
19    name: [u8; 16]
20}
21
22impl ThreadName {
23    #[inline(always)]
24    ///Creates new empty string name
25    pub const fn new() -> Self {
26        Self {
27            name: [0; 16]
28        }
29    }
30
31    #[inline(always)]
32    ///Initializes name from buffer.
33    pub const fn name(name: [u8; 16]) -> Self {
34        Self {
35            name
36        }
37    }
38
39    #[inline(always)]
40    ///Retrieves current thread's name
41    pub fn current() -> Self {
42        crate::thread::get_current_thread_name()
43    }
44
45    #[inline]
46    ///Returns name as byte slice
47    pub fn as_bytes(&self) -> &[u8] {
48        let mut idx = 0;
49        while idx < self.name.len() {
50            if self.name[idx] == 0 {
51                return &self.name[..idx]
52            }
53            idx += 1;
54        }
55
56        self.name.as_slice()
57    }
58
59    #[inline]
60    ///Returns name as string, checking whether it is valid utf-8 before.
61    ///
62    ///In case of underlying name not to be valid string, returns byte slice with content.
63    ///
64    ///On windows it never fails.
65    pub fn as_str(&self) -> Result<&str, &[u8]> {
66        #[cold]
67        #[inline(never)]
68        fn fail<T>(res: T) -> T {
69            res
70        }
71
72        let bytes = self.as_bytes();
73        match core::str::from_utf8(bytes) {
74            Ok(res) => Ok(res),
75            Err(_) => fail(Err(bytes)),
76        }
77    }
78}
79
80impl AsRef<[u8]> for ThreadName {
81    #[inline(always)]
82    fn as_ref(&self) -> &[u8] {
83        self.as_bytes()
84    }
85}
86
87impl core::borrow::Borrow<[u8]> for ThreadName {
88    #[inline(always)]
89    fn borrow(&self) -> &[u8] {
90        self.as_bytes()
91    }
92}
93
94impl Eq for ThreadName {}
95
96impl PartialEq<ThreadName> for ThreadName {
97    #[inline(always)]
98    fn eq(&self, other: &Self) -> bool {
99        self.as_bytes() == other.as_bytes()
100    }
101}
102
103impl PartialEq<ThreadName> for &str {
104    #[inline(always)]
105    fn eq(&self, other: &ThreadName) -> bool {
106        self.as_bytes() == other.as_bytes()
107    }
108}
109
110impl PartialEq<ThreadName> for str {
111    #[inline(always)]
112    fn eq(&self, other: &ThreadName) -> bool {
113        self.as_bytes() == other.as_bytes()
114    }
115}
116
117impl PartialEq<ThreadName> for &[u8] {
118    #[inline(always)]
119    fn eq(&self, other: &ThreadName) -> bool {
120        *self == other.as_bytes()
121    }
122}
123
124impl PartialEq<ThreadName> for [u8] {
125    #[inline(always)]
126    fn eq(&self, other: &ThreadName) -> bool {
127        self == other.as_bytes()
128    }
129}
130
131impl PartialEq<str> for ThreadName {
132    #[inline(always)]
133    fn eq(&self, other: &str) -> bool {
134        self.as_bytes() == other.as_bytes()
135    }
136}
137
138impl PartialEq<&str> for ThreadName {
139    #[inline(always)]
140    fn eq(&self, other: &&str) -> bool {
141        self.as_bytes() == other.as_bytes()
142    }
143}
144
145impl PartialEq<[u8]> for ThreadName {
146    #[inline(always)]
147    fn eq(&self, other: &[u8]) -> bool {
148        self.as_bytes() == other
149    }
150}
151
152impl PartialEq<&[u8]> for ThreadName {
153    #[inline(always)]
154    fn eq(&self, other: &&[u8]) -> bool {
155        self.as_bytes() == *other
156    }
157}
158
159impl cmp::Ord for ThreadName {
160    fn cmp(&self, other: &Self) -> cmp::Ordering {
161        self.as_bytes().cmp(other.as_bytes())
162    }
163}
164
165impl PartialOrd for ThreadName {
166    fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
167        Some(self.cmp(other))
168    }
169}
170
171impl hash::Hash for ThreadName {
172    fn hash<H: hash::Hasher>(&self, hasher: &mut H) {
173        self.as_bytes().hash(hasher)
174    }
175}
176
177impl fmt::Debug for ThreadName {
178    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
179        fmt::Debug::fmt(&self.name, fmt)
180    }
181}
182
183impl fmt::Display for ThreadName {
184    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
185        match self.as_str() {
186            Ok(name) => fmt.write_str(name),
187            Err(bytes) => fmt::Debug::fmt(bytes, fmt)
188        }
189    }
190}