Trait os_str_bytes::OsStrBytes
source · pub trait OsStrBytes: Sealed + ToOwned {
// Required methods
fn assert_from_raw_bytes<'a, S>(string: S) -> Cow<'a, Self>
where S: Into<Cow<'a, [u8]>>;
fn from_io_bytes(string: &[u8]) -> Option<&Self>;
fn from_raw_bytes<'a, S>(string: S) -> Result<Cow<'a, Self>, EncodingError>
where S: Into<Cow<'a, [u8]>>;
fn to_io_bytes(&self) -> Option<&[u8]>;
fn to_io_bytes_lossy(&self) -> Cow<'_, [u8]>;
fn to_raw_bytes(&self) -> Cow<'_, [u8]>;
}
Expand description
A platform agnostic variant of OsStrExt
.
For more information, see the module-level documentation.
Required Methods§
sourcefn assert_from_raw_bytes<'a, S>(string: S) -> Cow<'a, Self>
fn assert_from_raw_bytes<'a, S>(string: S) -> Cow<'a, Self>
conversions
only.Converts a byte string into an equivalent platform-native string.
§Panics
Panics if the string is not valid for the unspecified encoding used by this crate.
§Examples
use std::env;
use std::ffi::OsStr;
use os_str_bytes::OsStrBytes;
let os_string = env::current_exe()?;
let os_bytes = os_string.to_raw_bytes();
assert_eq!(os_string, OsStr::assert_from_raw_bytes(os_bytes));
sourcefn from_io_bytes(string: &[u8]) -> Option<&Self>
fn from_io_bytes(string: &[u8]) -> Option<&Self>
Converts a byte string into an equivalent platform-native string, if it is IO-safe.
§Examples
use std::ffi::OsStr;
use std::io;
use std::io::Read;
use os_str_bytes::OsStrBytes;
let mut io_string = Vec::new();
let _ = io::stdin().read_to_end(&mut io_string)?;
let os_string = OsStr::from_io_bytes(&io_string).ok_or_else(|| {
io::Error::new(io::ErrorKind::InvalidInput, "invalid input")
})?;
println!("{:?}", os_string);
sourcefn from_raw_bytes<'a, S>(string: S) -> Result<Cow<'a, Self>, EncodingError>
fn from_raw_bytes<'a, S>(string: S) -> Result<Cow<'a, Self>, EncodingError>
checked_conversions
only.Converts a byte string into an equivalent platform-native string.
assert_from_raw_bytes
should almost always be used instead. For
more information, see EncodingError
.
§Errors
See documentation for EncodingError
.
§Examples
use std::env;
use std::ffi::OsStr;
use os_str_bytes::OsStrBytes;
let os_string = env::current_exe()?;
let os_bytes = os_string.to_raw_bytes();
assert_eq!(os_string, OsStr::from_raw_bytes(os_bytes).unwrap());
sourcefn to_io_bytes(&self) -> Option<&[u8]>
fn to_io_bytes(&self) -> Option<&[u8]>
Converts a platform-native string into an equivalent byte string, if it is IO-safe.
§Examples
use std::env;
use std::io;
use std::io::Write;
use os_str_bytes::OsStrBytes;
let os_string = env::current_exe()?;
let io_string = os_string.to_io_bytes().ok_or_else(|| {
io::Error::new(io::ErrorKind::InvalidInput, "invalid input")
})?;
io::stdout().write_all(io_string)?;
sourcefn to_io_bytes_lossy(&self) -> Cow<'_, [u8]>
fn to_io_bytes_lossy(&self) -> Cow<'_, [u8]>
Converts a platform-native string into an equivalent byte string.
If the string is not IO-safe, invalid characters will be replaced
with REPLACEMENT_CHARACTER
.
§Examples
use std::env;
use std::io;
use std::io::Write;
use os_str_bytes::OsStrBytes;
let os_string = env::current_exe()?;
let io_string = os_string.to_io_bytes_lossy();
io::stdout().write_all(&io_string)?;
sourcefn to_raw_bytes(&self) -> Cow<'_, [u8]>
fn to_raw_bytes(&self) -> Cow<'_, [u8]>
conversions
only.Converts a platform-native string into an equivalent byte string.
The returned string will use an unspecified encoding.
§Examples
use std::ffi::OsStr;
use os_str_bytes::OsStrBytes;
let string = "foobar";
let os_string = OsStr::new(string);
assert_eq!(string.as_bytes(), &*os_string.to_raw_bytes());
Object Safety§
Implementations on Foreign Types§
source§impl OsStrBytes for OsStr
impl OsStrBytes for OsStr
source§fn assert_from_raw_bytes<'a, S>(string: S) -> Cow<'a, Self>
fn assert_from_raw_bytes<'a, S>(string: S) -> Cow<'a, Self>
conversions
only.fn from_io_bytes(string: &[u8]) -> Option<&Self>
source§fn from_raw_bytes<'a, S>(string: S) -> Result<Cow<'a, Self>, EncodingError>
fn from_raw_bytes<'a, S>(string: S) -> Result<Cow<'a, Self>, EncodingError>
checked_conversions
only.fn to_io_bytes(&self) -> Option<&[u8]>
fn to_io_bytes_lossy(&self) -> Cow<'_, [u8]>
source§fn to_raw_bytes(&self) -> Cow<'_, [u8]>
fn to_raw_bytes(&self) -> Cow<'_, [u8]>
conversions
only.source§impl OsStrBytes for Path
impl OsStrBytes for Path
source§fn assert_from_raw_bytes<'a, S>(string: S) -> Cow<'a, Self>
fn assert_from_raw_bytes<'a, S>(string: S) -> Cow<'a, Self>
conversions
only.fn from_io_bytes(string: &[u8]) -> Option<&Self>
source§fn from_raw_bytes<'a, S>(string: S) -> Result<Cow<'a, Self>, EncodingError>
fn from_raw_bytes<'a, S>(string: S) -> Result<Cow<'a, Self>, EncodingError>
checked_conversions
only.fn to_io_bytes(&self) -> Option<&[u8]>
fn to_io_bytes_lossy(&self) -> Cow<'_, [u8]>
source§fn to_raw_bytes(&self) -> Cow<'_, [u8]>
fn to_raw_bytes(&self) -> Cow<'_, [u8]>
conversions
only.