pub struct UnixStr { /* private fields */ }
Expand description
Borrowed reference to a Unix string (see UnixString
).
This type represents a borrowed reference to a string in Unix’s preferred representation.
&UnixStr
is to UnixString
as &str
is to String
: the former
in each pair are borrowed references; the latter are owned strings.
See the module’s toplevel documentation about conversions
for a discussion on the traits which UnixStr
implements for conversions
from/to native representations.
Implementations§
Source§impl UnixStr
impl UnixStr
Sourcepub fn to_str(&self) -> Option<&str>
pub fn to_str(&self) -> Option<&str>
Yields a &str
slice if the UnixStr
is valid Unicode.
This conversion may entail doing a check for UTF-8 validity.
§Examples
use unix_str::UnixStr;
let unix_str = UnixStr::new("foo");
assert_eq!(unix_str.to_str(), Some("foo"));
Sourcepub fn to_string_lossy(&self) -> Cow<'_, str>
pub fn to_string_lossy(&self) -> Cow<'_, str>
Converts an UnixStr
to a Cow<str>
.
Any non-Unicode sequences are replaced with
U+FFFD REPLACEMENT CHARACTER
.
§Examples
Calling to_string_lossy
on an UnixStr
with invalid unicode:
use unix_str::UnixStr;
// Here, the values 0x66 and 0x6f correspond to 'f' and 'o'
// respectively. The value 0x80 is a lone continuation byte, invalid
// in a UTF-8 sequence.
let source = [0x66, 0x6f, 0x80, 0x6f];
let unix_str = UnixStr::from_bytes(&source[..]);
assert_eq!(unix_str.to_string_lossy(), "fo�o");
Sourcepub fn to_unix_string(&self) -> UnixString
pub fn to_unix_string(&self) -> UnixString
Copies the slice into an owned UnixString
.
§Examples
use unix_str::{UnixStr, UnixString};
let unix_str = UnixStr::new("foo");
let unix_string = unix_str.to_unix_string();
assert_eq!(unix_string, UnixString::from("foo"));
Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Checks whether the UnixStr
is empty.
§Examples
use unix_str::UnixStr;
let unix_str = UnixStr::new("");
assert!(unix_str.is_empty());
let unix_str = UnixStr::new("foo");
assert!(!unix_str.is_empty());
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the length of this UnixStr
.
Note that this does not return the number of bytes in the string in OS string form.
The length returned is that of the underlying storage used by UnixStr
.
As discussed in the UnixString
introduction, UnixString
and
UnixStr
store strings in a form best suited for cheap inter-conversion
between native-platform and Rust string forms, which may differ
significantly from both of them, including in storage size and encoding.
This number is simply useful for passing to other methods, like
UnixString::with_capacity
to avoid reallocations.
§Examples
use unix_str::UnixStr;
let unix_str = UnixStr::new("");
assert_eq!(unix_str.len(), 0);
let unix_str = UnixStr::new("foo");
assert_eq!(unix_str.len(), 3);
Sourcepub fn into_unix_string(self: Box<UnixStr>) -> UnixString
pub fn into_unix_string(self: Box<UnixStr>) -> UnixString
Converts a Box<UnixStr>
into an UnixString
without copying
allocating.
Sourcepub fn from_bytes(slice: &[u8]) -> &Self
pub fn from_bytes(slice: &[u8]) -> &Self
Creates a UnixStr
from a byte slice.
See the module documentation for an example.
Trait Implementations§
Source§impl AsRef<UnixStr> for UnixString
impl AsRef<UnixStr> for UnixString
Source§impl Borrow<UnixStr> for UnixString
impl Borrow<UnixStr> for UnixString
Source§impl From<UnixString> for Box<UnixStr>
impl From<UnixString> for Box<UnixStr>
Source§fn from(s: UnixString) -> Self
fn from(s: UnixString) -> Self
Converts a UnixString
into a Box<UnixStr>
without copying or
allocating.