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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
use ::core::{fmt, ops::Deref};
use ::safer_ffi::prelude::char_p;

/// A `String` with exactly one null byte, in terminating position.
///
/// This, thus, allows it to offer a `char_p::Ref<'_>` zero-cost getter.
#[derive(Debug, Clone, Hash, Eq, PartialEq, Ord, PartialOrd)]
pub struct ZString(String);

impl From<char_p::Box> for ZString {
    #[inline]
    fn from(s: char_p::Box) -> ZString {
        let mut s: String = s.into_string();
        // `.into_string()` strips the terminating `\0` for convenience.
        s.push('\0');
        ZString(s)
    }
}

impl From<ZString> for char_p::Box {
    #[inline]
    fn from(s: ZString) -> char_p::Box {
        char_p::new(s.0)
    }
}

impl TryFrom<String> for ZString {
    type Error = ::safer_ffi::char_p::InvalidNulTerminator<String>;

    #[inline]
    fn try_from(value: String) -> Result<ZString, Self::Error> {
        char_p::Box::try_from(value).map(ZString::from)
    }
}

#[allow(nonstandard_style)]
#[derive(Debug, Hash, Eq, PartialEq, Ord, PartialOrd, ::bytemuck::TransparentWrapper)]
#[repr(transparent)]
/// Same as [`ZString`], but for [`str`].
pub struct zstr(str);

impl<'r> From<char_p::Ref<'r>> for &'r zstr {
    #[inline]
    fn from(s: char_p::Ref<'r>) -> &'r zstr {
        ::bytemuck::TransparentWrapper::wrap_ref(s.to_str_with_null())
    }
}

impl<'r> From<&'r zstr> for char_p::Ref<'r> {
    #[inline]
    fn from(s: &'r zstr) -> char_p::Ref<'r> {
        char_p::Ref::try_from(&s.0).unwrap()
    }
}

impl Deref for ZString {
    type Target = zstr;

    #[inline]
    fn deref(self: &ZString) -> &zstr {
        ::bytemuck::TransparentWrapper::wrap_ref(&self.0[..])
    }
}

impl Deref for zstr {
    type Target = str;

    #[inline]
    fn deref(self: &zstr) -> &str {
        // Skips the null terminator.
        &self.0[..self.0.len() - 1]
    }
}

// Extra conversions.
// Note: anything involving `str::method()` goes through `zstr::deref()`,
// which in turn *skips* the terminating null byte.

impl fmt::Display for ZString {
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        str::fmt(self, f)
    }
}

impl ZString {
    /// More efficient than `Display`'s `.to_string()`.
    #[allow(clippy::inherent_to_string_shadow_display)]
    #[inline]
    pub fn to_string(&self) -> String {
        str::to_string(self)
    }
}

impl fmt::Display for zstr {
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        str::fmt(self, f)
    }
}

impl zstr {
    /// More efficient than `Display`'s `.to_string()`.
    #[allow(clippy::inherent_to_string_shadow_display)]
    #[inline]
    pub fn to_string(&self) -> String {
        str::to_string(self)
    }
}

impl ToOwned for zstr {
    type Owned = ZString;

    #[inline]
    fn to_owned(self: &zstr) -> ZString {
        ZString(self.0.to_owned())
    }
}

impl ::core::borrow::Borrow<zstr> for ZString {
    #[inline]
    fn borrow(self: &ZString) -> &zstr {
        self
    }
}