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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
use core::fmt;
use core::ops;
use core::slice;
use core::str;

use crate::alloc;
use crate::alloc::clone::TryClone;

/// A raw static string.
///
/// We define and use this instead of relying on `&'static str` (which should
/// have a similar layout) because we want to allow static construction of the
/// `RawStr` through a C-ffi.
#[derive(Clone, Copy)]
#[repr(C)]
pub struct RawStr {
    data: *const u8,
    len: usize,
}

impl RawStr {
    /// Construct from a static string.
    pub const fn from_str(s: &'static str) -> Self {
        Self {
            data: s.as_ptr(),
            len: s.len(),
        }
    }
}

impl TryClone for RawStr {
    fn try_clone(&self) -> alloc::Result<Self> {
        Ok(*self)
    }
}

impl From<&'static str> for RawStr {
    fn from(s: &'static str) -> Self {
        Self::from_str(s)
    }
}

/// RawStr derefs into a str.
impl ops::Deref for RawStr {
    type Target = str;

    fn deref(&self) -> &Self::Target {
        unsafe { str::from_utf8_unchecked(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)
    }
}

impl PartialEq for RawStr {
    #[inline]
    fn eq(&self, other: &Self) -> bool {
        **self == **other
    }
}

impl Eq for RawStr {}

// Safety: `RawStr` references static data.
unsafe impl Send for RawStr {}
unsafe impl Sync for RawStr {}