Struct tiny_std::UnixString
source · pub struct UnixString(/* private fields */);
Implementations§
source§impl UnixString
impl UnixString
pub fn as_ptr(&self) -> *const u8
sourcepub fn try_from_string(s: String) -> Result<UnixString, Error>
pub fn try_from_string(s: String) -> Result<UnixString, Error>
sourcepub fn try_from_vec(s: Vec<u8>) -> Result<UnixString, Error>
pub fn try_from_vec(s: Vec<u8>) -> Result<UnixString, Error>
sourcepub fn try_from_bytes(s: &[u8]) -> Result<UnixString, Error>
pub fn try_from_bytes(s: &[u8]) -> Result<UnixString, Error>
Create a UnixString
from a &[u8]
.
Will allocate and push a null byte if not null terminated
Errors
Bytes aren’t properly null terminated, several nulls contained.
sourcepub fn try_from_str(s: &str) -> Result<UnixString, Error>
pub fn try_from_str(s: &str) -> Result<UnixString, Error>
Create a UnixString
from a &str
.
Will allocate and push a null byte if not null terminated
Errors
String isn’t properly null terminated, several nulls contained.
sourcepub fn from_format(args: Arguments<'_>) -> UnixString
pub fn from_format(args: Arguments<'_>) -> UnixString
A slightly less efficient that creating a format string way of
creating a UnixString
, since it’ll in all likelihood lead to two allocations.
Can’t do much since fmt internals are feature gated in core
.
Still a bit more ergonomic than creating a format-String and then creating a UnixString
from that.
More efficient if a null byte is added to the format strings.
Example
use rusl::string::unix_str::UnixString;
fn create_format_unix_string() {
let ins_with = "gramar";
let good = UnixString::from_format(format_args!("/home/{ins_with}/code"));
assert_eq!("/home/gramar/code", good.as_str().unwrap());
let great = UnixString::from_format(format_args!("/home/{ins_with}/code\0"));
assert_eq!("/home/gramar/code", great.as_str().unwrap());
}
Methods from Deref<Target = UnixStr>§
pub const EMPTY: &'static UnixStr = _
pub fn match_up_to(&self, other: &UnixStr) -> usize
pub fn match_up_to_str(&self, other: &str) -> usize
pub fn find(&self, other: &UnixStr) -> Option<usize>
pub fn find_buf(&self, other: &[u8]) -> Option<usize>
pub fn ends_with(&self, other: &UnixStr) -> bool
sourcepub fn path_file_name(&self) -> Option<&UnixStr>
pub fn path_file_name(&self) -> Option<&UnixStr>
Get the last component of a path, if possible.
Example
use rusl::string::unix_str::UnixStr;
use rusl::unix_lit;
fn get_file_paths() {
// Has no filename, just a root path
let a = unix_lit!("/");
assert_eq!(None, a.path_file_name());
// Has a 'filename'
let a = unix_lit!("/etc");
assert_eq!(Some(unix_lit!("etc")), a.path_file_name());
}
sourcepub fn path_join(&self, ext: &UnixStr) -> UnixString
pub fn path_join(&self, ext: &UnixStr) -> UnixString
Joins this UnixStr
with some other UnixStr
adding a slash if necessary.
Will make sure that there’s at most one slash at the boundary but won’t check
either string for “path validity” in any other case
Example
use rusl::string::unix_str::UnixStr;
fn join_paths() {
// Combines slash
let a = UnixStr::try_from_str("path/").unwrap();
let b = UnixStr::try_from_str("/ext").unwrap();
let combined = a.path_join(b);
assert_eq!("path/ext", combined.as_str().unwrap());
// Adds slash
let combined_other_way = b.path_join(a);
assert_eq!("/ext/path/", combined_other_way.as_str().unwrap());
// Doesn't truncate other slashes, only works at the boundary between the two paths
let a = UnixStr::try_from_str("path//").unwrap();
let combined_many_slashes = a.path_join(b);
assert_eq!("path//ext", combined_many_slashes.as_str().unwrap());
}
sourcepub fn path_join_fmt(&self, args: Arguments<'_>) -> UnixString
pub fn path_join_fmt(&self, args: Arguments<'_>) -> UnixString
Joins this UnixStr
with some format string adding a slash if necessary.
Follows the same rules as UnixStr::path_join
.
Example
use rusl::string::unix_str::UnixStr;
fn join_paths() {
// Combines slash
let a = UnixStr::try_from_str("path/").unwrap();
let combined = a.path_join_fmt(format_args!("ext"));
assert_eq!("path/ext", combined.as_str().unwrap());
}
sourcepub fn parent_path(&self) -> Option<UnixString>
pub fn parent_path(&self) -> Option<UnixString>
Treats this UnixStr
as a path, then tries to find its parent.
Will treat any double slash as a path with no parent
Example
use rusl::string::unix_str::UnixStr;
fn find_parent() {
let well_formed = UnixStr::try_from_str("/home/gramar/code/").unwrap();
let up_one = well_formed.parent_path().unwrap();
assert_eq!("/home/gramar", up_one.as_str().unwrap());
let up_two = up_one.parent_path().unwrap();
assert_eq!("/home", up_two.as_str().unwrap());
let up_three = up_two.parent_path().unwrap();
assert_eq!("/", up_three.as_str().unwrap());
assert!(up_three.parent_path().is_none());
let ill_formed = UnixStr::try_from_str("/home/gramar/code//").unwrap();
assert!(ill_formed.parent_path().is_none());
}
Trait Implementations§
source§impl AsRef<UnixStr> for UnixString
impl AsRef<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 From<&UnixStr> for UnixString
impl From<&UnixStr> for UnixString
source§fn from(s: &UnixStr) -> UnixString
fn from(s: &UnixStr) -> UnixString
source§impl FromStr for UnixString
impl FromStr for UnixString
source§impl Hash for UnixString
impl Hash for UnixString
source§impl Ord for UnixString
impl Ord for UnixString
source§fn cmp(&self, other: &UnixString) -> Ordering
fn cmp(&self, other: &UnixString) -> Ordering
1.21.0 · source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere Self: Sized,
source§impl PartialEq for UnixString
impl PartialEq for UnixString
source§fn eq(&self, other: &UnixString) -> bool
fn eq(&self, other: &UnixString) -> bool
self
and other
values to be equal, and is used
by ==
.source§impl PartialOrd for UnixString
impl PartialOrd for UnixString
source§fn partial_cmp(&self, other: &UnixString) -> Option<Ordering>
fn partial_cmp(&self, other: &UnixString) -> Option<Ordering>
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read more