Expand description
§Rust Stable OsString Encoding
This was made for one very specific reason: the encoding for the output of OsStr::as_encoded_bytes() might change based on the Rust compiler version the code is built with (according to this comment). If you want the guarantee that the data’s encoding will never change, this crate provides stable conversions between OsStr/OsString and Vec<EncodingWidth> (where EncodingWidth is either u8 or u16 depending on the platform).
This is made possible by the platform-specific OsStrExt and OsStringExt traits, which deal directly with the platform’s encoding instead of Rust’s encoding.
§Important caveat:
- This is not meant for sending data from one platform to another (e.g. from a windows program to a linux program). Sending data from one unix platform to another does work, but that is not the purpose of this crate.
§Example:
use std::{ffi::OsString, path::{Path, PathBuf}};
use stable_osstring_encoding::*;
let some_os_string = OsString::from("My String");
let stable_os_string = some_os_string.to_stable_encoding();
// send stable_os_string to another program
// in another program with a potentially different rustc version:
let received_os_string = stable_os_string.into_os_string();
assert_eq!(received_os_string, OsString::from("My String"));
// also works with Path and PathBuf:
let path_buf = PathBuf::from("My PathBuf");
let stable_path = path_buf.as_path().to_stable_encoding();
let stable_path_buf = path_buf.into_stable_encoding();
assert_eq!(stable_path_buf, stable_path);
let orig_path_buf = PathBuf::from("My PathBuf");
assert_eq!(orig_path_buf, stable_path_buf.into_path_buf());
assert_eq!(orig_path_buf, stable_path.into_path_buf());§Provided methods:
Note: StableOsString is just an alias for Vec<EncodingWidth>, and EncodingWidth is an alias for either u8 or u16 (depending on the platform)
The methods to_stable_encoding(&self) -> StableOsString and to_raw_stable_encoding(&self) -> Vec<EncodingWidth> are implemented for:
OsStrOsStringPathPathBuf
The methods into_stable_encoding(self) -> StableOsString and into_raw_stable_encoding(self) -> Vec<EncodingWidth> (avoids allocation when possible) are implemented for:
OsStringPathBufCow<OsStr>Cow<Path>
The type StableOsString has the methods:
StableOsString::into_os_string(Self) -> OsStringStableOsString::into_path_buf(Self) -> PathBufStableOsString::from_vec(Vec<EncodingWidth>) -> SelfStableOsString::into_vec(Self) -> Vec<EncodingWidth>
The method from_raw_stable_encoding(Into<Cow<[EncodingWidth]>>) is implemented for:
OsStringPathBuf
§Platform-specific details:
On Windows, the encoding is the output of std::os::windows::ffi::OsStrExt::encode_wide(), which is utf-16, and possible malformed. And as is required, turning this output back into an OsString is completely lossless.
On Unix (Linux, MacOs, etc) and Wasi, the encoding is the output of std::os::unix::ffi::OsStrExt::as_bytes(), which can be any arbitrary data. And as is required, turning this output back into an OsString is completely lossless.
Re-exports§
pub use stable_os_string::*;pub use traits::*;
Modules§
- impl_
unix_ wasi - Contains the implementation for unix (Linux, MacOs, and more) and wasi builds, which is basically everything except for Windows
- stable_
os_ string - A safe way to send an
OsString(or something similar) across an abi/ffi boundary. UnlikeOsStringandOsStr, this is guaranteed to have an encoding that will not change based on the compiler version. - traits
- All of this crate’s traits
Type Aliases§
- Encoding
Width - Defines the encoding width