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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
use crate::{c::c_char, Ustr, Ustring};
use std::{
ffi::{CStr, OsStr},
fmt,
fmt::{Debug, Display, Formatter},
ops::{Deref, DerefMut},
os::unix::ffi::OsStrExt,
path::Path,
str::Utf8Error,
};
#[repr(transparent)]
#[derive(Eq, Ord, PartialOrd, PartialEq, Hash)]
pub struct Bstr {
bytes: [u8],
}
impl Bstr {
pub fn empty() -> &'static Self {
static E: [u8; 0] = [];
Self::from_bytes(&E[..])
}
pub fn from_bytes(s: &[u8]) -> &Self {
unsafe { &*(s as *const _ as *const _) }
}
pub fn from_bytes_mut(s: &mut [u8]) -> &mut Self {
unsafe { &mut *(s as *mut _ as *mut _) }
}
#[allow(clippy::should_implement_trait)]
pub fn from_str(s: &str) -> &Self {
Self::from_bytes(s.as_bytes())
}
pub fn from_os_str(s: &OsStr) -> &Self {
Self::from_bytes(s.as_bytes())
}
pub fn from_path(s: &Path) -> &Self {
Self::from_os_str(s.as_os_str())
}
pub fn as_bytes(&self) -> &[u8] {
&self.bytes
}
pub fn as_bytes_mut(&mut self) -> &mut [u8] {
&mut self.bytes
}
pub fn as_str(&self) -> Result<&str, Utf8Error> {
std::str::from_utf8(self.as_bytes())
}
pub fn as_os_str(&self) -> &OsStr {
OsStr::from_bytes(self.as_bytes())
}
pub fn as_path(&self) -> &Path {
Path::new(self.as_os_str())
}
pub fn as_ptr(&self) -> *const c_char {
self.as_bytes().as_ptr() as *const c_char
}
pub fn as_mut_ptr(&mut self) -> *mut c_char {
self.as_bytes_mut().as_mut_ptr() as *mut c_char
}
pub fn len(&self) -> usize {
self.bytes.len()
}
pub fn is_empty(&self) -> bool {
self.len() == 0
}
pub fn display(&self) -> impl Display + '_ {
self.as_path().display()
}
pub fn to_ustring(&self) -> Ustring {
let mut vec = Vec::with_capacity(self.len() + 1);
vec.extend_from_slice(self.as_bytes());
vec.push(0);
unsafe { Ustring::from_vec_with_nul_unchecked(vec) }
}
}
impl ToOwned for Bstr {
type Owned = Ustring;
fn to_owned(&self) -> Self::Owned {
self.to_ustring()
}
}
impl Deref for Bstr {
type Target = [u8];
fn deref(&self) -> &Self::Target {
self.as_bytes()
}
}
impl DerefMut for Bstr {
fn deref_mut(&mut self) -> &mut Self::Target {
self.as_bytes_mut()
}
}
impl AsRef<[u8]> for Bstr {
fn as_ref(&self) -> &[u8] {
self.as_bytes()
}
}
impl AsMut<[u8]> for Bstr {
fn as_mut(&mut self) -> &mut [u8] {
self.as_bytes_mut()
}
}
impl AsRef<Bstr> for CStr {
fn as_ref(&self) -> &Bstr {
Ustr::from_c_str(self).as_bstr()
}
}
impl AsRef<OsStr> for Bstr {
fn as_ref(&self) -> &OsStr {
self.as_os_str()
}
}
impl AsRef<Path> for Bstr {
fn as_ref(&self) -> &Path {
self.as_path()
}
}
impl Debug for Bstr {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
Debug::fmt(self.as_os_str(), f)
}
}