pub struct UnixString { /* private fields */ }
Expand description
A type that can represent owned, mutable Unix strings, but is cheaply inter-convertible with Rust strings.
The need for this type arises from the fact that:
-
On Unix systems, strings are often arbitrary sequences of non-zero bytes, in many cases interpreted as UTF-8.
-
In Rust, strings are always valid UTF-8, which may contain zeros.
UnixString
and UnixStr
bridge this gap by simultaneously representing
Rust and platform-native string values, and in particular allowing a Rust
string to be converted into a “Unix” string with no cost if possible.
A consequence of this is that UnixString
instances are not NULL
terminated; in order to pass to e.g., Unix system call, you should create
a CStr
.
UnixString
is to &UnixStr
as String
is to &str
: the former
in each pair are owned strings; the latter are borrowed references.
Note, UnixString
and UnixStr
internally do not hold in the form native
to the platform: UnixString
s are stored as a sequence of 8-bit values.
§Creating an UnixString
From a Rust string: UnixString
implements From<String>
, so you can
use my_string.from
to create an UnixString
from a normal Rust string.
From slices: Just like you can start with an empty Rust String
and then [push_str
][String.push_str] &str
sub-string slices into it,
you can create an empty UnixString
with the new
method and then push
string slices into it with the push
method.
§Extracting a borrowed reference to the whole OS string
You can use the as_unix_str
method to get a &UnixStr
from
a UnixString
; this is effectively a borrowed reference to the whole
string.
§Conversions
See the module’s toplevel documentation about conversions
for a discussion on the traits which UnixString
implements for
conversions from/to native representations.
Implementations§
Source§impl UnixString
impl UnixString
Sourcepub fn new() -> Self
pub fn new() -> Self
Constructs a new empty UnixString
.
§Examples
use unix_str::UnixString;
let unix_string = UnixString::new();
Sourcepub fn as_unix_str(&self) -> &UnixStr
pub fn as_unix_str(&self) -> &UnixStr
Sourcepub fn into_string(self) -> Result<String, UnixString>
pub fn into_string(self) -> Result<String, UnixString>
Converts the UnixString
into a String
if it contains valid Unicode data.
On failure, ownership of the original UnixString
is returned.
§Examples
use unix_str::UnixString;
let unix_string = UnixString::from("foo");
let string = unix_string.into_string();
assert_eq!(string, Ok(String::from("foo")));
Sourcepub fn with_capacity(capacity: usize) -> Self
pub fn with_capacity(capacity: usize) -> Self
Creates a new UnixString
with the given capacity.
The string will be able to hold exactly capacity
length units of other
OS strings without reallocating. If capacity
is 0, the string will not
allocate.
See main UnixString
documentation information about encoding.
§Examples
use unix_str::UnixString;
let mut unix_string = UnixString::with_capacity(10);
let capacity = unix_string.capacity();
// This push is done without reallocating
unix_string.push("foo");
assert_eq!(capacity, unix_string.capacity());
Sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Truncates the UnixString
to zero length.
§Examples
use unix_str::UnixString;
let mut unix_string = UnixString::from("foo");
assert_eq!(&unix_string, "foo");
unix_string.clear();
assert_eq!(&unix_string, "");
Sourcepub fn capacity(&self) -> usize
pub fn capacity(&self) -> usize
Returns the capacity this UnixString
can hold without reallocating.
See UnixString
introduction for information about encoding.
§Examples
use unix_str::UnixString;
let unix_string = UnixString::with_capacity(10);
assert!(unix_string.capacity() >= 10);
Sourcepub fn reserve(&mut self, additional: usize)
pub fn reserve(&mut self, additional: usize)
Reserves capacity for at least additional
more capacity to be inserted
in the given UnixString
.
The collection may reserve more space to avoid frequent reallocations.
§Examples
use unix_str::UnixString;
let mut s = UnixString::new();
s.reserve(10);
assert!(s.capacity() >= 10);
Sourcepub fn reserve_exact(&mut self, additional: usize)
pub fn reserve_exact(&mut self, additional: usize)
Reserves the minimum capacity for exactly additional
more capacity to
be inserted in the given UnixString
. Does nothing if the capacity is
already sufficient.
Note that the allocator may give the collection more space than it requests. Therefore, capacity can not be relied upon to be precisely minimal. Prefer reserve if future insertions are expected.
§Examples
use unix_str::UnixString;
let mut s = UnixString::new();
s.reserve_exact(10);
assert!(s.capacity() >= 10);
Sourcepub fn shrink_to_fit(&mut self)
pub fn shrink_to_fit(&mut self)
Shrinks the capacity of the UnixString
to match its length.
§Examples
use unix_str::UnixString;
let mut s = UnixString::from("foo");
s.reserve(100);
assert!(s.capacity() >= 100);
s.shrink_to_fit();
assert_eq!(3, s.capacity());
Sourcepub fn into_boxed_unix_str(self) -> Box<UnixStr>
pub fn into_boxed_unix_str(self) -> Box<UnixStr>
Methods from Deref<Target = 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);
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 Clone for UnixString
impl Clone for UnixString
Source§fn clone(&self) -> UnixString
fn clone(&self) -> UnixString
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moreSource§impl Debug for UnixString
impl Debug for UnixString
Source§impl Deref for UnixString
impl Deref for UnixString
Source§impl DerefMut for UnixString
impl DerefMut for UnixString
Source§impl<'a> From<&'a UnixString> for Cow<'a, UnixStr>
impl<'a> From<&'a UnixString> for Cow<'a, UnixStr>
Source§fn from(s: &'a UnixString) -> Self
fn from(s: &'a UnixString) -> Self
Source§impl From<String> for UnixString
impl From<String> for UnixString
Source§fn from(s: String) -> Self
fn from(s: String) -> Self
Converts a String
into a UnixString
.
The conversion copies the data, and includes an allocation on the heap.
Source§impl From<UnixString> for Arc<UnixStr>
impl From<UnixString> for Arc<UnixStr>
Source§fn from(s: UnixString) -> Self
fn from(s: UnixString) -> Self
Converts a UnixString
into a Arc<UnixStr>
without copying or
allocating.
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.
Source§impl<'a> From<UnixString> for Cow<'a, UnixStr>
impl<'a> From<UnixString> for Cow<'a, UnixStr>
Source§fn from(s: UnixString) -> Self
fn from(s: UnixString) -> Self
Source§impl From<UnixString> for Rc<UnixStr>
impl From<UnixString> for Rc<UnixStr>
Source§fn from(s: UnixString) -> Self
fn from(s: UnixString) -> Self
Converts a UnixString
into a Rc<UnixStr>
without copying or
allocating.