Struct UnixString

Source
pub struct UnixString(/* private fields */);

Implementations§

Source§

impl UnixString

Source

pub fn as_ptr(&self) -> *const u8

Source

pub fn try_from_string(s: String) -> Result<UnixString, Error>

Create a UnixString from a String.

§Errors

String has nulls in other places than end.

Source

pub fn try_from_vec(s: Vec<u8>) -> Result<UnixString, Error>

Create a UnixString from a Vec<u8>.

§Errors

Vec has nulls in other places than end.

Source

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.

Source

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.

Source

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>§

Source

pub const EMPTY: &'static UnixStr

Source

pub fn as_str(&self) -> Result<&str, Error>

Try to convert this &UnixStr to a utf8 &str

§Errors

Not utf8

Source

pub fn as_slice(&self) -> &[u8]

Get this &UnixStr as a slice, including the null byte

Source

pub fn len(&self) -> usize

Get the length of this &UnixStr, including the null byte

Source

pub fn as_ptr(&self) -> *const u8

Get the length of this &UnixStr, including the null byte

Source

pub fn match_up_to(&self, other: &UnixStr) -> usize

Source

pub fn match_up_to_str(&self, other: &str) -> usize

Source

pub fn find(&self, other: &UnixStr) -> Option<usize>

Source

pub fn find_buf(&self, other: &[u8]) -> Option<usize>

Source

pub fn ends_with(&self, other: &UnixStr) -> bool

Source

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());
}
Source

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());
}
Source

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());
}
Source

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

Source§

fn as_ref(&self) -> &UnixStr

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl Clone for UnixString

Source§

fn clone(&self) -> UnixString

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for UnixString

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl Deref for UnixString

Source§

type Target = UnixStr

The resulting type after dereferencing.
Source§

fn deref(&self) -> &<UnixString as Deref>::Target

Dereferences the value.
Source§

impl From<&UnixStr> for UnixString

Source§

fn from(s: &UnixStr) -> UnixString

Converts to this type from the input type.
Source§

impl FromStr for UnixString

Source§

type Err = Error

The associated error which can be returned from parsing.
Source§

fn from_str(s: &str) -> Result<UnixString, <UnixString as FromStr>::Err>

Parses a string s to return a value of this type. Read more
Source§

impl Hash for UnixString

Source§

fn hash<__H>(&self, state: &mut __H)
where __H: Hasher,

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl Ord for UnixString

Source§

fn cmp(&self, other: &UnixString) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl PartialEq for UnixString

Source§

fn eq(&self, other: &UnixString) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialOrd for UnixString

Source§

fn partial_cmp(&self, other: &UnixString) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl Eq for UnixString

Source§

impl StructuralPartialEq for UnixString

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.