stable-osstring-encoding 2.1.0

Converts OsString (and more) to and from raw data with an encoding that will not change based on the compiler version.
Documentation
#![warn(
	missing_docs,
	clippy::all,
	clippy::nursery,
	clippy::perf,
	clippy::pedantic,
	unreachable_pub
)]
#![allow(clippy::doc_markdown)] // this is needed otherwise "MacOs" and "OsString" (in the crate name) creates errors
#![doc = include_str!("../readme.md")]



// How this crate works:
// 1: `src/traits.rs` holds all the important traits which define functionality
// 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()`
// 3: `src/lib.rs` expands the impls so all useful types have all impls possible
// 4: `src/stable_os_string.rs` builds a safe wrapper around all the other functionality



#[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},
};



/// Contains the implementation for unix (Linux, MacOs, and more) and wasi builds, which is basically everything except for Windows
#[cfg(any(unix, target_os = "wasi"))]
pub mod impl_unix_wasi;
/// Contains the implementation for windows builds
#[cfg(windows)]
pub mod impl_windows;

/// 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.
pub mod stable_os_string;
pub use stable_os_string::*;

/// All of this crate's traits
pub mod traits;
pub use traits::*;



/// Defines the encoding width
#[cfg(unix)]
pub type EncodingWidth = u8;
/// Defines the encoding width
#[cfg(windows)]
pub type EncodingWidth = u16;



// impl ToStable for all ToRawStable

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()) }
	}
}



// additional ToRawStable impls

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; // make sure &[EncodingWidth] can be given to from_raw_stable_encoding()

		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();
	}
}