| 1 |
use crate::builder::Str; |
| 2 |
#[cfg(feature = "string")] |
| 3 |
use std::borrow::Cow; |
| 4 |
|
| 5 |
/// A UTF-8-encoded fixed string |
| 6 |
/// |
| 7 |
/// <div class="warning"> |
| 8 |
/// |
| 9 |
/// **NOTE:** To support dynamic values (i.e. `OsString`), enable the `string` |
| 10 |
/// feature |
| 11 |
/// |
| 12 |
/// </div> |
| 13 |
#[derive(Default, Clone, Eq, PartialEq, PartialOrd, Ord, Hash)] |
| 14 |
pub struct OsStr { |
| 15 |
name: Inner, |
| 16 |
} |
| 17 |
|
| 18 |
impl OsStr { |
| 19 |
#[cfg(feature = "string")] |
| 20 |
pub(crate) fn from_string(name: std::ffi::OsString) -> Self { |
| 21 |
Self { |
| 22 |
name: Inner::from_string(name), |
| 23 |
} |
| 24 |
} |
| 25 |
|
| 26 |
#[cfg(feature = "string")] |
| 27 |
pub(crate) fn from_ref(name: &std::ffi::OsStr) -> Self { |
| 28 |
Self { |
| 29 |
name: Inner::from_ref(name), |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
pub(crate) fn from_static_ref(name: &'static std::ffi::OsStr) -> Self { |
| 34 |
Self { |
| 35 |
name: Inner::from_static_ref(name), |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
/// Get the raw string as an `std::ffi::OsStr` |
| 40 |
pub fn as_os_str(&self) -> &std::ffi::OsStr { |
| 41 |
self.name.as_os_str() |
| 42 |
} |
| 43 |
|
| 44 |
/// Get the raw string as an `OsString` |
| 45 |
pub fn to_os_string(&self) -> std::ffi::OsString { |
| 46 |
self.as_os_str().to_owned() |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
impl From<&'_ Self> for OsStr { |
| 51 |
fn from(id: &'_ Self) -> Self { |
| 52 |
id.clone() |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
#[cfg(feature = "string")] |
| 57 |
impl From<Str> for OsStr { |
| 58 |
fn from(id: Str) -> Self { |
| 59 |
match id.into_inner() { |
| 60 |
crate::builder::StrInner::Static(s) => Self::from_static_ref(std::ffi::OsStr::new(s)), |
| 61 |
crate::builder::StrInner::Owned(s) => Self::from_ref(std::ffi::OsStr::new(s.as_ref())), |
| 62 |
} |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
#[cfg(not(feature = "string"))] |
| 67 |
impl From<Str> for OsStr { |
| 68 |
fn from(id: Str) -> Self { |
| 69 |
Self::from_static_ref(std::ffi::OsStr::new(id.into_inner().0)) |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
impl From<&'_ Str> for OsStr { |
| 74 |
fn from(id: &'_ Str) -> Self { |
| 75 |
id.clone().into() |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
#[cfg(feature = "string")] |
| 80 |
impl From<std::ffi::OsString> for OsStr { |
| 81 |
fn from(name: std::ffi::OsString) -> Self { |
| 82 |
Self::from_string(name) |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
#[cfg(feature = "string")] |
| 87 |
impl From<&'_ std::ffi::OsString> for OsStr { |
| 88 |
fn from(name: &'_ std::ffi::OsString) -> Self { |
| 89 |
Self::from_ref(name.as_os_str()) |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
#[cfg(feature = "string")] |
| 94 |
impl From<String> for OsStr { |
| 95 |
fn from(name: String) -> Self { |
| 96 |
Self::from_string(name.into()) |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
#[cfg(feature = "string")] |
| 101 |
impl From<&'_ String> for OsStr { |
| 102 |
fn from(name: &'_ String) -> Self { |
| 103 |
Self::from_ref(name.as_str().as_ref()) |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
impl From<&'static std::ffi::OsStr> for OsStr { |
| 108 |
fn from(name: &'static std::ffi::OsStr) -> Self { |
| 109 |
Self::from_static_ref(name) |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
impl From<&'_ &'static std::ffi::OsStr> for OsStr { |
| 114 |
fn from(name: &'_ &'static std::ffi::OsStr) -> Self { |
| 115 |
Self::from_static_ref(name) |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
impl From<&'static str> for OsStr { |
| 120 |
fn from(name: &'static str) -> Self { |
| 121 |
Self::from_static_ref(name.as_ref()) |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
impl From<&'_ &'static str> for OsStr { |
| 126 |
fn from(name: &'_ &'static str) -> Self { |
| 127 |
Self::from_static_ref((*name).as_ref()) |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
#[cfg(feature = "string")] |
| 132 |
impl From<Cow<'static, str>> for OsStr { |
| 133 |
fn from(cow: Cow<'static, str>) -> Self { |
| 134 |
match cow { |
| 135 |
Cow::Borrowed(s) => Self::from(s), |
| 136 |
Cow::Owned(s) => Self::from(s), |
| 137 |
} |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
impl From<OsStr> for std::ffi::OsString { |
| 142 |
fn from(name: OsStr) -> Self { |
| 143 |
name.name.into_os_string() |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
impl From<OsStr> for std::path::PathBuf { |
| 148 |
fn from(name: OsStr) -> Self { |
| 149 |
std::ffi::OsString::from(name).into() |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
impl std::fmt::Debug for OsStr { |
| 154 |
#[inline] |
| 155 |
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 156 |
std::fmt::Debug::fmt(self.as_os_str(), f) |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
impl std::ops::Deref for OsStr { |
| 161 |
type Target = std::ffi::OsStr; |
| 162 |
|
| 163 |
#[inline] |
| 164 |
fn deref(&self) -> &std::ffi::OsStr { |
| 165 |
self.as_os_str() |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
impl AsRef<std::ffi::OsStr> for OsStr { |
| 170 |
#[inline] |
| 171 |
fn as_ref(&self) -> &std::ffi::OsStr { |
| 172 |
self.as_os_str() |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
impl AsRef<std::path::Path> for OsStr { |
| 177 |
#[inline] |
| 178 |
fn as_ref(&self) -> &std::path::Path { |
| 179 |
std::path::Path::new(self) |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
impl std::borrow::Borrow<std::ffi::OsStr> for OsStr { |
| 184 |
#[inline] |
| 185 |
fn borrow(&self) -> &std::ffi::OsStr { |
| 186 |
self.as_os_str() |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
impl PartialEq<str> for OsStr { |
| 191 |
#[inline] |
| 192 |
fn eq(&self, other: &str) -> bool { |
| 193 |
PartialEq::eq(self.as_os_str(), other) |
| 194 |
} |
| 195 |
} |
| 196 |
impl PartialEq<OsStr> for str { |
| 197 |
#[inline] |
| 198 |
fn eq(&self, other: &OsStr) -> bool { |
| 199 |
PartialEq::eq(self, other.as_os_str()) |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
impl PartialEq<&'_ str> for OsStr { |
| 204 |
#[inline] |
| 205 |
fn eq(&self, other: &&str) -> bool { |
| 206 |
PartialEq::eq(self.as_os_str(), *other) |
| 207 |
} |
| 208 |
} |
| 209 |
impl PartialEq<OsStr> for &'_ str { |
| 210 |
#[inline] |
| 211 |
fn eq(&self, other: &OsStr) -> bool { |
| 212 |
PartialEq::eq(*self, other.as_os_str()) |
| 213 |
} |
| 214 |
} |
| 215 |
|
| 216 |
impl PartialEq<&'_ std::ffi::OsStr> for OsStr { |
| 217 |
#[inline] |
| 218 |
fn eq(&self, other: &&std::ffi::OsStr) -> bool { |
| 219 |
PartialEq::eq(self.as_os_str(), *other) |
| 220 |
} |
| 221 |
} |
| 222 |
impl PartialEq<OsStr> for &'_ std::ffi::OsStr { |
| 223 |
#[inline] |
| 224 |
fn eq(&self, other: &OsStr) -> bool { |
| 225 |
PartialEq::eq(*self, other.as_os_str()) |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
impl PartialEq<String> for OsStr { |
| 230 |
#[inline] |
| 231 |
fn eq(&self, other: &String) -> bool { |
| 232 |
PartialEq::eq(self.as_os_str(), other.as_str()) |
| 233 |
} |
| 234 |
} |
| 235 |
impl PartialEq<OsStr> for String { |
| 236 |
#[inline] |
| 237 |
fn eq(&self, other: &OsStr) -> bool { |
| 238 |
PartialEq::eq(self.as_str(), other.as_os_str()) |
| 239 |
} |
| 240 |
} |
| 241 |
|
| 242 |
impl PartialEq<std::ffi::OsString> for OsStr { |
| 243 |
#[inline] |
| 244 |
fn eq(&self, other: &std::ffi::OsString) -> bool { |
| 245 |
PartialEq::eq(self.as_os_str(), other.as_os_str()) |
| 246 |
} |
| 247 |
} |
| 248 |
impl PartialEq<OsStr> for std::ffi::OsString { |
| 249 |
#[inline] |
| 250 |
fn eq(&self, other: &OsStr) -> bool { |
| 251 |
PartialEq::eq(self.as_os_str(), other.as_os_str()) |
| 252 |
} |
| 253 |
} |
| 254 |
|
| 255 |
#[cfg(feature = "string")] |
| 256 |
pub(crate) mod inner { |
| 257 |
#[derive(Clone)] |
| 258 |
pub(crate) enum Inner { |
| 259 |
Static(&'static std::ffi::OsStr), |
| 260 |
Owned(Box<std::ffi::OsStr>), |
| 261 |
} |
| 262 |
|
| 263 |
impl Inner { |
| 264 |
pub(crate) fn from_string(name: std::ffi::OsString) -> Self { |
| 265 |
Self::Owned(name.into_boxed_os_str()) |
| 266 |
} |
| 267 |
|
| 268 |
pub(crate) fn from_ref(name: &std::ffi::OsStr) -> Self { |
| 269 |
Self::Owned(Box::from(name)) |
| 270 |
} |
| 271 |
|
| 272 |
pub(crate) fn from_static_ref(name: &'static std::ffi::OsStr) -> Self { |
| 273 |
Self::Static(name) |
| 274 |
} |
| 275 |
|
| 276 |
pub(crate) fn as_os_str(&self) -> &std::ffi::OsStr { |
| 277 |
match self { |
| 278 |
Self::Static(s) => s, |
| 279 |
Self::Owned(s) => s.as_ref(), |
| 280 |
} |
| 281 |
} |
| 282 |
|
| 283 |
pub(crate) fn into_os_string(self) -> std::ffi::OsString { |
| 284 |
self.as_os_str().to_owned() |
| 285 |
} |
| 286 |
} |
| 287 |
} |
| 288 |
|
| 289 |
#[cfg(not(feature = "string"))] |
| 290 |
pub(crate) mod inner { |
| 291 |
#[derive(Clone)] |
| 292 |
pub(crate) struct Inner(&'static std::ffi::OsStr); |
| 293 |
|
| 294 |
impl Inner { |
| 295 |
pub(crate) fn from_static_ref(name: &'static std::ffi::OsStr) -> Self { |
| 296 |
Self(name) |
| 297 |
} |
| 298 |
|
| 299 |
pub(crate) fn as_os_str(&self) -> &std::ffi::OsStr { |
| 300 |
self.0 |
| 301 |
} |
| 302 |
|
| 303 |
pub(crate) fn into_os_string(self) -> std::ffi::OsString { |
| 304 |
self.as_os_str().to_owned() |
| 305 |
} |
| 306 |
} |
| 307 |
} |
| 308 |
|
| 309 |
pub(crate) use inner::Inner; |
| 310 |
|
| 311 |
impl Default for Inner { |
| 312 |
fn default() -> Self { |
| 313 |
Self::from_static_ref(std::ffi::OsStr::new("")) |
| 314 |
} |
| 315 |
} |
| 316 |
|
| 317 |
impl PartialEq for Inner { |
| 318 |
fn eq(&self, other: &Self) -> bool { |
| 319 |
self.as_os_str() == other.as_os_str() |
| 320 |
} |
| 321 |
} |
| 322 |
|
| 323 |
impl PartialOrd for Inner { |
| 324 |
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> { |
| 325 |
Some(self.cmp(other)) |
| 326 |
} |
| 327 |
} |
| 328 |
|
| 329 |
impl Ord for Inner { |
| 330 |
fn cmp(&self, other: &Self) -> std::cmp::Ordering { |
| 331 |
self.as_os_str().cmp(other.as_os_str()) |
| 332 |
} |
| 333 |
} |
| 334 |
|
| 335 |
impl Eq for Inner {} |
| 336 |
|
| 337 |
impl std::hash::Hash for Inner { |
| 338 |
#[inline] |
| 339 |
fn hash<H: std::hash::Hasher>(&self, state: &mut H) { |
| 340 |
self.as_os_str().hash(state); |
| 341 |
} |
| 342 |
} |
| 343 |
|
| 344 |
#[cfg(test)] |
| 345 |
#[cfg(feature = "string")] |
| 346 |
mod tests { |
| 347 |
use super::*; |
| 348 |
|
| 349 |
#[test] |
| 350 |
#[cfg(feature = "string")] |
| 351 |
fn from_cow_borrowed() { |
| 352 |
let cow = Cow::Borrowed("hello"); |
| 353 |
let osstr = OsStr::from(cow); |
| 354 |
assert_eq!(osstr, OsStr::from("hello")); |
| 355 |
} |
| 356 |
|
| 357 |
#[test] |
| 358 |
#[cfg(feature = "string")] |
| 359 |
fn from_cow_owned() { |
| 360 |
let cow = Cow::Owned("world".to_owned()); |
| 361 |
let osstr = OsStr::from(cow); |
| 362 |
assert_eq!(osstr, OsStr::from("world")); |
| 363 |
} |
| 364 |
} |