#![warn(missing_docs, clippy::all)]
#![doc = include_str!("../readme.md")]
#[cfg(not(any(unix, windows)))]
compile_error!(
"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."
);
use std::{borrow::Cow, ffi::OsStr};
#[cfg(unix)]
pub mod impl_unix;
#[cfg(windows)]
pub mod impl_windows;
#[cfg(unix)]
pub type EncodingWidth = u8;
#[cfg(windows)]
pub type EncodingWidth = u16;
pub type StableOsString = Vec<EncodingWidth>;
pub trait ToStableEncoding {
fn to_stable_encoding(&self) -> StableOsString;
}
pub trait IntoStableEncoding {
fn into_stable_encoding(self) -> StableOsString;
}
pub trait FromStableEncoding {
unsafe fn from_stable_encoding<'a>(encoded: impl Into<Cow<'a, [EncodingWidth]>>) -> Self;
}
impl<'a> IntoStableEncoding for Cow<'a, OsStr> {
fn into_stable_encoding(self) -> StableOsString {
match self {
Cow::Borrowed(os_str) => os_str.to_stable_encoding(),
Cow::Owned(os_string) => os_string.into_stable_encoding(),
}
}
}
#[cfg(test)]
mod test {
use crate::{FromStableEncoding, IntoStableEncoding, ToStableEncoding};
use std::ffi::OsString;
#[test]
fn basics() {
let start = OsString::from("test");
let as_stable_1 = start.to_stable_encoding();
let as_stable_2 = start.into_stable_encoding();
assert_eq!(as_stable_1, as_stable_2);
let as_stable_1 = &*as_stable_1;
let as_os_string_1 = unsafe { OsString::from_stable_encoding(as_stable_1) };
let as_os_string_2 = unsafe { OsString::from_stable_encoding(as_stable_2) };
assert_eq!(as_os_string_1, as_os_string_2);
let as_str = as_os_string_1.to_str();
assert_eq!(as_str, Some("test"));
}
}