Trait OsStringBytes

Source
pub trait OsStringBytes: Sealed + Sized {
    // Required methods
    fn assert_from_raw_vec(string: Vec<u8>) -> Self;
    fn from_io_vec(string: Vec<u8>) -> Option<Self>;
    fn from_raw_vec(string: Vec<u8>) -> Result<Self, EncodingError>;
    fn into_io_vec(self) -> Option<Vec<u8>>;
    fn into_io_vec_lossy(self) -> Vec<u8> ;
    fn into_raw_vec(self) -> Vec<u8> ;
}
Expand description

A platform agnostic variant of OsStringExt.

For more information, see the module-level documentation.

Required Methods§

Source

fn assert_from_raw_vec(string: Vec<u8>) -> Self

Available on crate feature 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::OsString;

use os_str_bytes::OsStringBytes;

let os_string = env::current_exe()?;
let os_bytes = os_string.clone().into_raw_vec();
assert_eq!(os_string, OsString::assert_from_raw_vec(os_bytes));
Source

fn from_io_vec(string: Vec<u8>) -> Option<Self>

Converts a byte string into an equivalent platform-native string, if it is IO-safe.

§Examples
use std::ffi::OsString;
use std::io;
use std::io::Read;

use os_str_bytes::OsStringBytes;

let mut io_string = Vec::new();
let _ = io::stdin().read_to_end(&mut io_string)?;
let os_string = OsString::from_io_vec(io_string).ok_or_else(|| {
    io::Error::new(io::ErrorKind::InvalidInput, "invalid input")
})?;
println!("{:?}", os_string);
Source

fn from_raw_vec(string: Vec<u8>) -> Result<Self, EncodingError>

Available on crate feature checked_conversions only.

Converts a byte string into an equivalent platform-native string.

assert_from_raw_vec should almost always be used instead. For more information, see EncodingError.

§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_raw_vec();
assert_eq!(
    os_string,
    OsString::from_raw_vec(os_bytes).unwrap(),
);
Source

fn into_io_vec(self) -> Option<Vec<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::OsStringBytes;

let os_string = env::current_exe()?;
let io_string = os_string.into_io_vec().ok_or_else(|| {
    io::Error::new(io::ErrorKind::InvalidInput, "invalid input")
})?;
io::stdout().write_all(&io_string)?;
Source

fn into_io_vec_lossy(self) -> Vec<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::OsStringBytes;

let os_string = env::current_exe()?;
let io_string = os_string.into_io_vec_lossy();
io::stdout().write_all(&io_string)?;
Source

fn into_raw_vec(self) -> Vec<u8>

Available on crate feature conversions only.

Converts a platform-native string into an equivalent byte string.

The returned string will use an unspecified encoding.

§Examples
use std::ffi::OsString;

use os_str_bytes::OsStringBytes;

let string = "foobar".to_owned();
let os_string: OsString = string.clone().into();
assert_eq!(string.into_bytes(), os_string.into_raw_vec());

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl OsStringBytes for OsString

Source§

fn assert_from_raw_vec(string: Vec<u8>) -> Self

Available on crate feature conversions only.
Source§

fn from_raw_vec(string: Vec<u8>) -> Result<Self, EncodingError>

Available on crate feature checked_conversions only.
Source§

fn from_io_vec(string: Vec<u8>) -> Option<Self>

Source§

fn into_io_vec(self) -> Option<Vec<u8>>

Source§

fn into_io_vec_lossy(self) -> Vec<u8>

Source§

fn into_raw_vec(self) -> Vec<u8>

Available on crate feature conversions only.
Source§

impl OsStringBytes for PathBuf

Source§

fn assert_from_raw_vec(string: Vec<u8>) -> Self

Available on crate feature conversions only.
Source§

fn from_raw_vec(string: Vec<u8>) -> Result<Self, EncodingError>

Available on crate feature checked_conversions only.
Source§

fn from_io_vec(string: Vec<u8>) -> Option<Self>

Source§

fn into_io_vec(self) -> Option<Vec<u8>>

Source§

fn into_io_vec_lossy(self) -> Vec<u8>

Source§

fn into_raw_vec(self) -> Vec<u8>

Available on crate feature conversions only.

Implementors§