#![warn(
missing_docs,
clippy::all,
clippy::nursery,
clippy::perf,
clippy::pedantic,
unreachable_pub
)]
#![allow(clippy::doc_markdown)] #![doc = include_str!("../readme.md")]
#[cfg(not(any(unix, windows, target_os = "wasi")))]
compile_error!(
"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."
);
use std::{
borrow::Cow,
ffi::{OsStr, OsString},
path::{Path, PathBuf},
};
#[cfg(any(unix, target_os = "wasi"))]
pub mod impl_unix_wasi;
#[cfg(windows)]
pub mod impl_windows;
pub mod stable_os_string;
pub use stable_os_string::*;
pub mod traits;
pub use traits::*;
#[cfg(unix)]
pub type EncodingWidth = u8;
#[cfg(windows)]
pub type EncodingWidth = u16;
impl<T: ToRawStableEncoding + ?Sized> ToStableEncoding for T {
#[inline]
fn to_stable_encoding(&self) -> StableOsString {
unsafe { StableOsString::from_vec(self.to_raw_stable_encoding()) }
}
}
impl<T: IntoRawStableEncoding> IntoStableEncoding for T {
#[inline]
fn into_stable_encoding(self) -> StableOsString {
unsafe { StableOsString::from_vec(self.into_raw_stable_encoding()) }
}
}
impl ToRawStableEncoding for OsString {
#[inline]
fn to_raw_stable_encoding(&self) -> Vec<EncodingWidth> {
self.as_os_str().to_raw_stable_encoding()
}
}
impl ToRawStableEncoding for Path {
#[inline]
fn to_raw_stable_encoding(&self) -> Vec<EncodingWidth> {
self.as_os_str().to_raw_stable_encoding()
}
}
impl ToRawStableEncoding for PathBuf {
#[inline]
fn to_raw_stable_encoding(&self) -> Vec<EncodingWidth> {
self.as_os_str().to_raw_stable_encoding()
}
}
impl IntoRawStableEncoding for PathBuf {
#[inline]
fn into_raw_stable_encoding(self) -> Vec<EncodingWidth> {
self.into_os_string().into_raw_stable_encoding()
}
}
impl IntoRawStableEncoding for Cow<'_, OsStr> {
#[inline]
fn into_raw_stable_encoding(self) -> Vec<EncodingWidth> {
match self {
Cow::Borrowed(v) => v.to_raw_stable_encoding(),
Cow::Owned(v) => v.into_raw_stable_encoding(),
}
}
}
impl IntoRawStableEncoding for Cow<'_, Path> {
#[inline]
fn into_raw_stable_encoding(self) -> Vec<EncodingWidth> {
match self {
Cow::Borrowed(v) => v.to_raw_stable_encoding(),
Cow::Owned(v) => v.into_raw_stable_encoding(),
}
}
}
impl FromRawStableEncoding for PathBuf {
#[inline]
unsafe fn from_raw_stable_encoding<'a>(encoded: impl Into<Cow<'a, [EncodingWidth]>>) -> Self {
let os_string = unsafe { OsString::from_raw_stable_encoding(encoded) };
Self::from(os_string)
}
}
#[cfg(test)]
mod test {
use crate::{
FromRawStableEncoding, IntoRawStableEncoding, IntoStableEncoding, ToRawStableEncoding,
ToStableEncoding,
};
use std::{
borrow::Cow,
ffi::{OsStr, OsString},
path::{Path, PathBuf},
};
#[test]
fn to_from_raw() {
let start = OsString::from("test");
let as_stable_1 = start.to_raw_stable_encoding();
let as_stable_2 = start.into_raw_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_raw_stable_encoding(as_stable_1) };
let as_os_string_2 = unsafe { OsString::from_raw_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"));
}
#[test]
fn to_from() {
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_os_string_1 = as_stable_1.to_os_string();
let as_os_string_2 = as_stable_1.into_os_string();
assert_eq!(as_os_string_1.to_str(), Some("test"));
assert_eq!(as_os_string_1, as_os_string_2);
}
#[test]
fn to_from_path_buf_raw() {
let start = PathBuf::from(OsString::from("test"));
let as_stable_1 = start.to_raw_stable_encoding();
let as_stable_2 = start.clone().into_raw_stable_encoding();
assert_eq!(as_stable_1, as_stable_2);
let as_os_string_1 = unsafe { OsString::from_raw_stable_encoding(as_stable_1) };
assert_eq!(as_os_string_1, start.into_os_string());
}
#[test]
fn to_from_path_buf() {
let start = PathBuf::from(OsString::from("test"));
let as_stable_1 = start.to_stable_encoding();
let as_stable_2 = start.clone().into_stable_encoding();
assert_eq!(as_stable_1, as_stable_2);
let as_path_buf_1 = as_stable_1.into_path_buf();
assert_eq!(as_path_buf_1, start);
}
#[test]
fn assert_trait_impls() {
let _ = OsString::new().to_stable_encoding();
let _ = OsString::new().into_stable_encoding();
let _ = OsString::new().to_raw_stable_encoding();
let _ = OsString::new().into_raw_stable_encoding();
let _ = OsStr::new("").to_stable_encoding();
let _ = OsStr::new("").to_raw_stable_encoding();
let _ = PathBuf::new().to_stable_encoding();
let _ = PathBuf::new().into_stable_encoding();
let _ = PathBuf::new().to_raw_stable_encoding();
let _ = PathBuf::new().into_raw_stable_encoding();
let _ = Path::new("").to_stable_encoding();
let _ = Path::new("").to_raw_stable_encoding();
let _ = Cow::Borrowed(OsStr::new("")).to_stable_encoding();
let _ = Cow::Borrowed(OsStr::new("")).into_stable_encoding();
let _ = Cow::Borrowed(OsStr::new("")).to_raw_stable_encoding();
let _ = Cow::Borrowed(OsStr::new("")).into_raw_stable_encoding();
let _ = Cow::Owned::<OsStr>(OsString::new()).to_stable_encoding();
let _ = Cow::Owned::<OsStr>(OsString::new()).into_stable_encoding();
let _ = Cow::Owned::<OsStr>(OsString::new()).to_raw_stable_encoding();
let _ = Cow::Owned::<OsStr>(OsString::new()).into_raw_stable_encoding();
let _ = Cow::Borrowed(Path::new("")).to_stable_encoding();
let _ = Cow::Borrowed(Path::new("")).into_stable_encoding();
let _ = Cow::Borrowed(Path::new("")).to_raw_stable_encoding();
let _ = Cow::Borrowed(Path::new("")).into_raw_stable_encoding();
let _ = Cow::Owned::<Path>(PathBuf::new()).to_stable_encoding();
let _ = Cow::Owned::<Path>(PathBuf::new()).into_stable_encoding();
let _ = Cow::Owned::<Path>(PathBuf::new()).to_raw_stable_encoding();
let _ = Cow::Owned::<Path>(PathBuf::new()).into_raw_stable_encoding();
}
}