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
use std::fmt;
use std::ops;
#[derive(Clone, Copy)]
#[repr(C)]
pub struct RawStr {
data: *const u8,
len: usize,
}
impl RawStr {
pub const fn from_str(s: &'static str) -> Self {
Self {
data: s.as_ptr(),
len: s.len(),
}
}
}
impl From<&'static str> for RawStr {
fn from(s: &'static str) -> Self {
Self::from_str(s)
}
}
impl ops::Deref for RawStr {
type Target = str;
fn deref(&self) -> &Self::Target {
unsafe { std::str::from_utf8_unchecked(std::slice::from_raw_parts(self.data, self.len)) }
}
}
impl fmt::Debug for RawStr {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Debug::fmt(&**self, f)
}
}
impl fmt::Display for RawStr {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(&**self, f)
}
}
unsafe impl Send for RawStr {}
unsafe impl Sync for RawStr {}