1use crate::sys::{Error, OsStr, OsString};
4use core::{fmt, ops::Deref};
5
6#[cfg(feature = "std")]
7use std::{
8 ffi,
9 path::{Path, PathBuf},
10};
11
12impl Clone for OsString {
13 fn clone(&self) -> Self {
14 self.to_os_str()
15 .and_then(|str| str.into_os_string())
16 .expect("Allocation error")
17 }
18}
19
20impl fmt::Debug for OsString {
21 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
22 write!(fmt, "{:?}", self.as_ref())
23 }
24}
25
26impl fmt::Display for OsString {
27 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
28 write!(fmt, "{}", self.as_ref())
29 }
30}
31
32impl Deref for OsString {
33 type Target = OsStr;
34
35 fn deref(&self) -> &OsStr {
36 self.as_ref()
37 }
38}
39
40#[derive(Debug)]
42pub enum EitherOsStr<'str> {
43 Borrowed(&'str OsStr),
45 Owned(OsString),
47}
48
49impl<'str> AsRef<OsStr> for EitherOsStr<'str> {
50 fn as_ref(&self) -> &OsStr {
51 match self {
52 Self::Borrowed(str) => str,
53 Self::Owned(string) => string.as_ref(),
54 }
55 }
56}
57
58impl<'str> Deref for EitherOsStr<'str> {
59 type Target = OsStr;
60
61 fn deref(&self) -> &OsStr {
62 self.as_ref()
63 }
64}
65
66pub trait IntoOsString {
69 fn into_os_string(self) -> Result<OsString, Error>;
71}
72
73impl IntoOsString for OsString {
74 fn into_os_string(self) -> Result<OsString, Error> {
75 Ok(self)
76 }
77}
78
79impl<'str> IntoOsString for EitherOsStr<'str> {
80 fn into_os_string(self) -> Result<OsString, Error> {
81 match self {
82 Self::Borrowed(str) => str.into_os_string(),
83 Self::Owned(string) => Ok(string),
84 }
85 }
86}
87
88#[cfg(feature = "std")]
89impl<'str> IntoOsString for &'str ffi::OsStr {
90 fn into_os_string(self) -> Result<OsString, Error> {
91 self.to_os_str()?.into_os_string()
92 }
93}
94
95#[cfg(feature = "std")]
96impl IntoOsString for PathBuf {
97 fn into_os_string(self) -> Result<OsString, Error> {
98 (*self).into_os_string()
99 }
100}
101
102#[cfg(feature = "std")]
103impl<'str> IntoOsString for &'str Path {
104 fn into_os_string(self) -> Result<OsString, Error> {
105 AsRef::<ffi::OsStr>::as_ref(self).to_os_str()?.into_os_string()
106 }
107}
108
109#[cfg(feature = "std")]
110impl IntoOsString for ffi::OsString {
111 fn into_os_string(self) -> Result<OsString, Error> {
112 (*self).into_os_string()
113 }
114}
115
116impl<'str> IntoOsString for &'str str {
117 fn into_os_string(self) -> Result<OsString, Error> {
118 self.to_os_str()?.into_os_string()
119 }
120}
121
122#[cfg(feature = "std")]
123impl IntoOsString for String {
124 fn into_os_string(self) -> Result<OsString, Error> {
125 self.to_os_str()?.into_os_string()
126 }
127}
128
129#[cfg(feature = "std")]
130impl ToOsStr for String {
131 fn to_os_str(&self) -> Result<EitherOsStr, Error> {
132 (**self).to_os_str()
133 }
134}
135
136pub trait ToOsStr {
139 fn to_os_str(&self) -> Result<EitherOsStr, Error>;
141}
142
143impl<'str> ToOsStr for EitherOsStr<'str> {
144 fn to_os_str(&self) -> Result<EitherOsStr, Error> {
145 Ok(match self {
146 EitherOsStr::Owned(string) => {
147 EitherOsStr::Owned(string.to_os_str()?.into_os_string()?)
148 },
149 EitherOsStr::Borrowed(str) => EitherOsStr::Borrowed(str),
150 })
151 }
152}
153
154impl ToOsStr for OsStr {
155 fn to_os_str(&self) -> Result<EitherOsStr, Error> {
156 Ok(EitherOsStr::Borrowed(self))
157 }
158}
159
160impl ToOsStr for OsString {
161 fn to_os_str(&self) -> Result<EitherOsStr, Error> {
162 Ok(EitherOsStr::Borrowed(self.as_ref()))
163 }
164}
165
166#[cfg(feature = "std")]
167impl ToOsStr for ffi::OsString {
168 fn to_os_str(&self) -> Result<EitherOsStr, Error> {
169 (**self).to_os_str()
170 }
171}
172
173#[cfg(feature = "std")]
174impl ToOsStr for PathBuf {
175 fn to_os_str(&self) -> Result<EitherOsStr, Error> {
176 (**self).to_os_str()
177 }
178}
179
180#[cfg(feature = "std")]
181impl ToOsStr for Path {
182 fn to_os_str(&self) -> Result<EitherOsStr, Error> {
183 AsRef::<ffi::OsStr>::as_ref(self).to_os_str()
184 }
185}