Skip to main content

stable_osstring_encoding/
lib.rs

1#![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)] // this is needed otherwise "MacOs" and "OsString" (in the crate name) creates errors
10#![doc = include_str!("../readme.md")]
11
12
13
14// How this crate works:
15// 1: `src/traits.rs` holds all the important traits which define functionality
16// 2: `src/impl_*.rs` holds the bare-bones trait impls for `OsStr::to_raw_stable_encoding()`, and `OsString::into_raw_stable_encoding()`, and `OsString::from_raw_stable_encoding()`
17// 3: `src/lib.rs` expands the impls so all useful types have all impls possible
18// 4: `src/stable_os_string.rs` builds a safe wrapper around all the other functionality
19
20
21
22#[cfg(not(any(unix, windows, target_os = "wasi")))]
23compile_error!(
24	"This crate currently only supports Windows, Unix (including both Linux and Macos), and Wasi. 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/// Contains the implementation for unix (Linux, MacOs, and more) and wasi builds, which is basically everything except for Windows
38#[cfg(any(unix, target_os = "wasi"))]
39pub mod impl_unix_wasi;
40/// Contains the implementation for windows builds
41#[cfg(windows)]
42pub mod impl_windows;
43
44/// A safe way to send an [`OsString`] (or something similar) across an abi/ffi boundary. Unlike [`OsString`] and [`OsStr`], this is guaranteed to have an encoding that will not change based on the compiler version.
45pub mod stable_os_string;
46pub use stable_os_string::*;
47
48/// All of this crate's traits
49pub mod traits;
50pub use traits::*;
51
52
53
54/// Defines the encoding width
55#[cfg(unix)]
56pub type EncodingWidth = u8;
57/// Defines the encoding width
58#[cfg(windows)]
59pub type EncodingWidth = u16;
60
61
62
63// impl ToStable for all ToRawStable
64
65impl<T: ToRawStableEncoding + ?Sized> ToStableEncoding for T {
66	#[inline]
67	fn to_stable_encoding(&self) -> StableOsString {
68		unsafe { StableOsString::from_vec(self.to_raw_stable_encoding()) }
69	}
70}
71
72impl<T: IntoRawStableEncoding> IntoStableEncoding for T {
73	#[inline]
74	fn into_stable_encoding(self) -> StableOsString {
75		unsafe { StableOsString::from_vec(self.into_raw_stable_encoding()) }
76	}
77}
78
79
80
81// additional ToRawStable impls
82
83impl ToRawStableEncoding for OsString {
84	#[inline]
85	fn to_raw_stable_encoding(&self) -> Vec<EncodingWidth> {
86		self.as_os_str().to_raw_stable_encoding()
87	}
88}
89
90impl ToRawStableEncoding for Path {
91	#[inline]
92	fn to_raw_stable_encoding(&self) -> Vec<EncodingWidth> {
93		self.as_os_str().to_raw_stable_encoding()
94	}
95}
96
97impl ToRawStableEncoding for PathBuf {
98	#[inline]
99	fn to_raw_stable_encoding(&self) -> Vec<EncodingWidth> {
100		self.as_os_str().to_raw_stable_encoding()
101	}
102}
103
104impl IntoRawStableEncoding for PathBuf {
105	#[inline]
106	fn into_raw_stable_encoding(self) -> Vec<EncodingWidth> {
107		self.into_os_string().into_raw_stable_encoding()
108	}
109}
110
111impl IntoRawStableEncoding for Cow<'_, OsStr> {
112	#[inline]
113	fn into_raw_stable_encoding(self) -> Vec<EncodingWidth> {
114		match self {
115			Cow::Borrowed(v) => v.to_raw_stable_encoding(),
116			Cow::Owned(v) => v.into_raw_stable_encoding(),
117		}
118	}
119}
120
121impl IntoRawStableEncoding for Cow<'_, Path> {
122	#[inline]
123	fn into_raw_stable_encoding(self) -> Vec<EncodingWidth> {
124		match self {
125			Cow::Borrowed(v) => v.to_raw_stable_encoding(),
126			Cow::Owned(v) => v.into_raw_stable_encoding(),
127		}
128	}
129}
130
131impl FromRawStableEncoding for PathBuf {
132	#[inline]
133	unsafe fn from_raw_stable_encoding<'a>(encoded: impl Into<Cow<'a, [EncodingWidth]>>) -> Self {
134		let os_string = unsafe { OsString::from_raw_stable_encoding(encoded) };
135		Self::from(os_string)
136	}
137}
138
139
140
141#[cfg(test)]
142mod test {
143	use crate::{
144		FromRawStableEncoding, IntoRawStableEncoding, IntoStableEncoding, ToRawStableEncoding,
145		ToStableEncoding,
146	};
147	use std::{
148		borrow::Cow,
149		ffi::{OsStr, OsString},
150		path::{Path, PathBuf},
151	};
152
153	#[test]
154	fn to_from_raw() {
155		let start = OsString::from("test");
156		let as_stable_1 = start.to_raw_stable_encoding();
157		let as_stable_2 = start.into_raw_stable_encoding();
158		assert_eq!(as_stable_1, as_stable_2);
159
160		let as_stable_1 = &*as_stable_1; // make sure &[EncodingWidth] can be given to from_raw_stable_encoding()
161
162		let as_os_string_1 = unsafe { OsString::from_raw_stable_encoding(as_stable_1) };
163		let as_os_string_2 = unsafe { OsString::from_raw_stable_encoding(as_stable_2) };
164		assert_eq!(as_os_string_1, as_os_string_2);
165
166		let as_str = as_os_string_1.to_str();
167		assert_eq!(as_str, Some("test"));
168	}
169
170	#[test]
171	fn to_from() {
172		let start = OsString::from("test");
173		let as_stable_1 = start.to_stable_encoding();
174		let as_stable_2 = start.into_stable_encoding();
175		assert_eq!(as_stable_1, as_stable_2);
176
177		let as_os_string_1 = as_stable_1.to_os_string();
178		let as_os_string_2 = as_stable_1.into_os_string();
179		assert_eq!(as_os_string_1.to_str(), Some("test"));
180		assert_eq!(as_os_string_1, as_os_string_2);
181	}
182
183	#[test]
184	fn to_from_path_buf_raw() {
185		let start = PathBuf::from(OsString::from("test"));
186		let as_stable_1 = start.to_raw_stable_encoding();
187		let as_stable_2 = start.clone().into_raw_stable_encoding();
188		assert_eq!(as_stable_1, as_stable_2);
189
190		let as_os_string_1 = unsafe { OsString::from_raw_stable_encoding(as_stable_1) };
191		assert_eq!(as_os_string_1, start.into_os_string());
192	}
193
194	#[test]
195	fn to_from_path_buf() {
196		let start = PathBuf::from(OsString::from("test"));
197		let as_stable_1 = start.to_stable_encoding();
198		let as_stable_2 = start.clone().into_stable_encoding();
199		assert_eq!(as_stable_1, as_stable_2);
200
201		let as_path_buf_1 = as_stable_1.into_path_buf();
202		assert_eq!(as_path_buf_1, start);
203	}
204
205	#[test]
206	fn assert_trait_impls() {
207		let _ = OsString::new().to_stable_encoding();
208		let _ = OsString::new().into_stable_encoding();
209		let _ = OsString::new().to_raw_stable_encoding();
210		let _ = OsString::new().into_raw_stable_encoding();
211		let _ = OsStr::new("").to_stable_encoding();
212		let _ = OsStr::new("").to_raw_stable_encoding();
213		let _ = PathBuf::new().to_stable_encoding();
214		let _ = PathBuf::new().into_stable_encoding();
215		let _ = PathBuf::new().to_raw_stable_encoding();
216		let _ = PathBuf::new().into_raw_stable_encoding();
217		let _ = Path::new("").to_stable_encoding();
218		let _ = Path::new("").to_raw_stable_encoding();
219		let _ = Cow::Borrowed(OsStr::new("")).to_stable_encoding();
220		let _ = Cow::Borrowed(OsStr::new("")).into_stable_encoding();
221		let _ = Cow::Borrowed(OsStr::new("")).to_raw_stable_encoding();
222		let _ = Cow::Borrowed(OsStr::new("")).into_raw_stable_encoding();
223		let _ = Cow::Owned::<OsStr>(OsString::new()).to_stable_encoding();
224		let _ = Cow::Owned::<OsStr>(OsString::new()).into_stable_encoding();
225		let _ = Cow::Owned::<OsStr>(OsString::new()).to_raw_stable_encoding();
226		let _ = Cow::Owned::<OsStr>(OsString::new()).into_raw_stable_encoding();
227		let _ = Cow::Borrowed(Path::new("")).to_stable_encoding();
228		let _ = Cow::Borrowed(Path::new("")).into_stable_encoding();
229		let _ = Cow::Borrowed(Path::new("")).to_raw_stable_encoding();
230		let _ = Cow::Borrowed(Path::new("")).into_raw_stable_encoding();
231		let _ = Cow::Owned::<Path>(PathBuf::new()).to_stable_encoding();
232		let _ = Cow::Owned::<Path>(PathBuf::new()).into_stable_encoding();
233		let _ = Cow::Owned::<Path>(PathBuf::new()).to_raw_stable_encoding();
234		let _ = Cow::Owned::<Path>(PathBuf::new()).into_raw_stable_encoding();
235	}
236}