Trait os_str_bytes::OsStringBytes
source · [−]pub trait OsStringBytes: Sealed + Sized {
fn from_bytes<TString>(string: TString) -> Result<Self, EncodingError>
where
TString: AsRef<[u8]>;
fn from_vec(string: Vec<u8>) -> Result<Self, EncodingError>;
fn into_vec(self) -> Vec<u8>;
fn from_cow(string: Cow<'_, [u8]>) -> Result<Self, EncodingError> { ... }
}
Expand description
A platform agnostic variant of OsStringExt
.
For more information, see the module-level documentation.
Required methods
fn from_bytes<TString>(string: TString) -> Result<Self, EncodingError> where
TString: AsRef<[u8]>,
fn from_bytes<TString>(string: TString) -> Result<Self, EncodingError> where
TString: AsRef<[u8]>,
Copies a byte slice into an equivalent platform-native string.
It is always better to use from_cow
when the bytes may be owned.
Errors
See documentation for EncodingError
.
Examples
use std::env;
use std::ffi::OsString;
use os_str_bytes::OsStrBytes;
use os_str_bytes::OsStringBytes;
let os_string = env::current_exe()?;
let os_bytes = os_string.to_bytes();
assert_eq!(os_string, OsString::from_bytes(os_bytes).unwrap());
Converts a byte vector into an equivalent platform-native string.
Errors
See documentation for EncodingError
.
Examples
use std::env;
use std::ffi::OsString;
use os_str_bytes::OsStringBytes;
let os_string = env::current_exe()?;
let os_bytes = os_string.clone().into_vec();
assert_eq!(os_string, OsString::from_vec(os_bytes).unwrap());
Provided methods
A convenience method to call either from_bytes
or from_vec
,
depending on whether a byte sequence is owned.
This method can be useful in coordination with
OsStrBytes::to_bytes
, since the parameter type matches that
method’s return type.
Errors
See documentation for EncodingError
.
Examples
use std::env;
use std::ffi::OsString;
use os_str_bytes::OsStrBytes;
use os_str_bytes::OsStringBytes;
let os_string = env::current_exe()?;
let os_bytes = os_string.to_bytes();
assert_eq!(os_string, OsString::from_cow(os_bytes).unwrap());