[−][src]Trait os_str_bytes::OsStrBytes
A platform agnostic variant of OsStrExt
.
For more information, see the module-level documentation.
Required methods
fn from_bytes(string: &[u8]) -> Result<Cow<Self>, EncodingError>
Converts a byte slice into an equivalent platform-native string reference.
This method returns Cow<Self>
to account for platform differences.
However, no guarantee is made that the same variant of that enum will
always be returned for the same platform. Whichever can be constructed
most efficiently will be returned.
Examples
use os_str_bytes::OsStrBytes; let string = b"foo\xED\xA0\xBDbar"; assert_eq!(string.len(), OsStr::from_bytes(string)?.len());
#[must_use]
unsafe fn from_bytes_unchecked(string: &[u8]) -> Cow<Self>
The unsafe equivalent of from_bytes
.
More information is given in that method's documentation.
Safety
This method is unsafe, because it does not check that the bytes passed
are representable in the platform encoding. If this constraint is
violated, it may cause memory unsafety issues with future uses of this
string, as the rest of the standard library assumes that OsStr
and
OsString
will be usable for the platform. However, the most likely
issue is that the data gets corrupted.
#[must_use]
fn to_bytes(&self) -> Cow<[u8]>
Converts the internal byte representation into a byte slice.
For more information, see from_bytes
.
Examples
use os_str_bytes::OsStrBytes; let string = b"foo\xED\xA0\xBDbar"; let os_string = OsStr::from_bytes(string)?.into_owned(); assert_eq!(string, os_string.to_bytes().as_ref());