stable_osstring_encoding/
lib.rs1#![warn(missing_docs, clippy::all)]
2#![doc = include_str!("../readme.md")]
3
4
5
6#[cfg(not(any(unix, windows)))]
7compile_error!(
8 "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."
9);
10
11
12
13use std::borrow::Cow;
14
15
16
17#[cfg(unix)]
19pub mod impl_unix;
20#[cfg(windows)]
22pub mod impl_windows;
23
24
25
26#[cfg(unix)]
28pub type EncodingWidth = u8;
29#[cfg(windows)]
31pub type EncodingWidth = u16;
32
33pub type StableOsString = Vec<EncodingWidth>;
35
36
37
38pub trait ToStableEncoding {
40 fn to_stable_encoding(&self) -> StableOsString;
42}
43
44pub trait IntoStableEncoding {
46 fn into_stable_encoding(self) -> StableOsString;
48}
49
50pub trait FromStableEncoding {
52 unsafe fn from_stable_encoding<'a>(encoded: impl Into<Cow<'a, [EncodingWidth]>>) -> Self;
58}
59
60
61
62#[cfg(test)]
63mod test {
64 use crate::{FromStableEncoding, IntoStableEncoding, ToStableEncoding};
65 use std::ffi::OsString;
66
67 #[test]
68 fn basics() {
69 let start = OsString::from("test");
70 let as_stable_1 = start.to_stable_encoding();
71 let as_stable_2 = start.into_stable_encoding();
72 assert_eq!(as_stable_1, as_stable_2);
73
74 let as_stable_1 = &*as_stable_1; let as_os_string_1 = unsafe { OsString::from_stable_encoding(as_stable_1) };
77 let as_os_string_2 = unsafe { OsString::from_stable_encoding(as_stable_2) };
78 assert_eq!(as_os_string_1, as_os_string_2);
79
80 let as_str = as_os_string_1.to_str();
81 assert_eq!(as_str, Some("test"));
82 }
83}