stable_osstring_encoding/
lib.rs1#![warn(
2 missing_docs,
3 clippy::all,
4 clippy::nursery,
5 clippy::perf,
6 clippy::pedantic,
7 unreachable_pub
8)]
9#![allow(clippy::doc_markdown)] #![doc = include_str!("../readme.md")]
11
12
13
14#[cfg(not(any(unix, windows)))]
23compile_error!(
24 "This crate currently only supports Windows and Unix (Linux and Macos). Adding support for your platform is likely very easy, please consider opening an issue for it in \"stable-osstring-encoding\"'s issue tracker."
25);
26
27
28
29use std::{
30 borrow::Cow,
31 ffi::{OsStr, OsString},
32 path::{Path, PathBuf},
33};
34
35
36
37#[cfg(any(unix, target_os = "wasi"))]
39pub mod impl_unix_wasi;
40#[cfg(windows)]
42pub mod impl_windows;
43
44pub mod stable_os_string;
46pub use stable_os_string::*;
47
48pub mod traits;
50pub use traits::*;
51
52
53
54#[cfg(unix)]
56pub type EncodingWidth = u8;
57#[cfg(windows)]
59pub type EncodingWidth = u16;
60
61
62
63impl<T: ToRawStableEncoding + ?Sized> ToStableEncoding for T {
66 fn to_stable_encoding(&self) -> StableOsString {
67 unsafe { StableOsString::from_vec(self.to_raw_stable_encoding()) }
68 }
69}
70
71impl<T: IntoRawStableEncoding> IntoStableEncoding for T {
72 fn into_stable_encoding(self) -> StableOsString {
73 unsafe { StableOsString::from_vec(self.into_raw_stable_encoding()) }
74 }
75}
76
77
78
79impl ToRawStableEncoding for OsString {
82 fn to_raw_stable_encoding(&self) -> Vec<EncodingWidth> {
83 self.as_os_str().to_raw_stable_encoding()
84 }
85}
86
87impl ToRawStableEncoding for Path {
88 fn to_raw_stable_encoding(&self) -> Vec<EncodingWidth> {
89 self.as_os_str().to_raw_stable_encoding()
90 }
91}
92
93impl ToRawStableEncoding for PathBuf {
94 fn to_raw_stable_encoding(&self) -> Vec<EncodingWidth> {
95 self.as_os_str().to_raw_stable_encoding()
96 }
97}
98
99impl IntoRawStableEncoding for PathBuf {
100 fn into_raw_stable_encoding(self) -> Vec<EncodingWidth> {
101 self.into_os_string().into_raw_stable_encoding()
102 }
103}
104
105impl IntoRawStableEncoding for Cow<'_, OsStr> {
106 fn into_raw_stable_encoding(self) -> Vec<EncodingWidth> {
107 match self {
108 Cow::Borrowed(v) => v.to_raw_stable_encoding(),
109 Cow::Owned(v) => v.into_raw_stable_encoding(),
110 }
111 }
112}
113
114impl IntoRawStableEncoding for Cow<'_, Path> {
115 fn into_raw_stable_encoding(self) -> Vec<EncodingWidth> {
116 match self {
117 Cow::Borrowed(v) => v.to_raw_stable_encoding(),
118 Cow::Owned(v) => v.into_raw_stable_encoding(),
119 }
120 }
121}
122
123impl FromRawStableEncoding for PathBuf {
124 unsafe fn from_raw_stable_encoding<'a>(encoded: impl Into<Cow<'a, [EncodingWidth]>>) -> Self {
125 let os_string = unsafe { OsString::from_raw_stable_encoding(encoded) };
126 Self::from(os_string)
127 }
128}
129
130
131
132#[cfg(test)]
133mod test {
134 use crate::{
135 FromRawStableEncoding, IntoRawStableEncoding, IntoStableEncoding, ToRawStableEncoding,
136 ToStableEncoding,
137 };
138 use std::{
139 borrow::Cow,
140 ffi::{OsStr, OsString},
141 path::{Path, PathBuf},
142 };
143
144 #[test]
145 fn to_from_raw() {
146 let start = OsString::from("test");
147 let as_stable_1 = start.to_raw_stable_encoding();
148 let as_stable_2 = start.into_raw_stable_encoding();
149 assert_eq!(as_stable_1, as_stable_2);
150
151 let as_stable_1 = &*as_stable_1; let as_os_string_1 = unsafe { OsString::from_raw_stable_encoding(as_stable_1) };
154 let as_os_string_2 = unsafe { OsString::from_raw_stable_encoding(as_stable_2) };
155 assert_eq!(as_os_string_1, as_os_string_2);
156
157 let as_str = as_os_string_1.to_str();
158 assert_eq!(as_str, Some("test"));
159 }
160
161 #[test]
162 fn to_from() {
163 let start = OsString::from("test");
164 let as_stable_1 = start.to_stable_encoding();
165 let as_stable_2 = start.into_stable_encoding();
166 assert_eq!(as_stable_1, as_stable_2);
167
168 let as_os_string_1 = as_stable_1.into_os_string();
169 let as_str = as_os_string_1.to_str();
170 assert_eq!(as_str, Some("test"));
171 }
172
173 #[test]
174 fn to_from_path_buf_raw() {
175 let start = PathBuf::from(OsString::from("test"));
176 let as_stable_1 = start.to_raw_stable_encoding();
177 let as_stable_2 = start.clone().into_raw_stable_encoding();
178 assert_eq!(as_stable_1, as_stable_2);
179
180 let as_os_string_1 = unsafe { OsString::from_raw_stable_encoding(as_stable_1) };
181 assert_eq!(as_os_string_1, start.into_os_string());
182 }
183
184 #[test]
185 fn to_from_path_buf() {
186 let start = PathBuf::from(OsString::from("test"));
187 let as_stable_1 = start.to_stable_encoding();
188 let as_stable_2 = start.clone().into_stable_encoding();
189 assert_eq!(as_stable_1, as_stable_2);
190
191 let as_path_buf_1 = as_stable_1.into_path_buf();
192 assert_eq!(as_path_buf_1, start);
193 }
194
195 #[test]
196 fn assert_trait_impls() {
197 let _ = OsString::new().to_stable_encoding();
198 let _ = OsString::new().into_stable_encoding();
199 let _ = OsString::new().to_raw_stable_encoding();
200 let _ = OsString::new().into_raw_stable_encoding();
201 let _ = OsStr::new("").to_stable_encoding();
202 let _ = OsStr::new("").to_raw_stable_encoding();
203 let _ = PathBuf::new().to_stable_encoding();
204 let _ = PathBuf::new().into_stable_encoding();
205 let _ = PathBuf::new().to_raw_stable_encoding();
206 let _ = PathBuf::new().into_raw_stable_encoding();
207 let _ = Path::new("").to_stable_encoding();
208 let _ = Path::new("").to_raw_stable_encoding();
209 let _ = Cow::Borrowed(OsStr::new("")).to_stable_encoding();
210 let _ = Cow::Borrowed(OsStr::new("")).into_stable_encoding();
211 let _ = Cow::Borrowed(OsStr::new("")).to_raw_stable_encoding();
212 let _ = Cow::Borrowed(OsStr::new("")).into_raw_stable_encoding();
213 let _ = Cow::Owned::<OsStr>(OsString::new()).to_stable_encoding();
214 let _ = Cow::Owned::<OsStr>(OsString::new()).into_stable_encoding();
215 let _ = Cow::Owned::<OsStr>(OsString::new()).to_raw_stable_encoding();
216 let _ = Cow::Owned::<OsStr>(OsString::new()).into_raw_stable_encoding();
217 let _ = Cow::Borrowed(Path::new("")).to_stable_encoding();
218 let _ = Cow::Borrowed(Path::new("")).into_stable_encoding();
219 let _ = Cow::Borrowed(Path::new("")).to_raw_stable_encoding();
220 let _ = Cow::Borrowed(Path::new("")).into_raw_stable_encoding();
221 let _ = Cow::Owned::<Path>(PathBuf::new()).to_stable_encoding();
222 let _ = Cow::Owned::<Path>(PathBuf::new()).into_stable_encoding();
223 let _ = Cow::Owned::<Path>(PathBuf::new()).to_raw_stable_encoding();
224 let _ = Cow::Owned::<Path>(PathBuf::new()).into_raw_stable_encoding();
225 }
226}