rustix::path

Trait Arg

Source
pub trait Arg {
    // Required methods
    fn as_str(&self) -> Result<&str>;
    fn to_string_lossy(&self) -> Cow<'_, str>;
    fn as_cow_c_str(&self) -> Result<Cow<'_, CStr>>;
    fn into_c_str<'b>(self) -> Result<Cow<'b, CStr>>
       where Self: 'b;
    fn into_with_c_str<T, F>(self, f: F) -> Result<T>
       where Self: Sized,
             F: FnOnce(&CStr) -> Result<T>;
}
Available on crate features fs or mount or net only.
Expand description

A trait for passing path arguments.

This is similar to AsRef<Path>, but is implemented for more kinds of strings and can convert into more kinds of strings.

§Examples

use rustix::ffi::CStr;
use rustix::io;
use rustix::path::Arg;

pub fn touch<P: Arg>(path: P) -> io::Result<()> {
    let path = path.into_c_str()?;
    _touch(&path)
}

fn _touch(path: &CStr) -> io::Result<()> {
    // implementation goes here
    Ok(())
}

Users can then call touch("foo"), touch(cstr!("foo")), touch(Path::new("foo")), or many other things.

Required Methods§

Source

fn as_str(&self) -> Result<&str>

Returns a view of this string as a string slice.

Source

fn to_string_lossy(&self) -> Cow<'_, str>

Returns a potentially-lossy rendering of this string as a Cow<'_, str>.

Source

fn as_cow_c_str(&self) -> Result<Cow<'_, CStr>>

Returns a view of this string as a maybe-owned CStr.

Source

fn into_c_str<'b>(self) -> Result<Cow<'b, CStr>>
where Self: 'b,

Consumes self and returns a view of this string as a maybe-owned CStr.

Source

fn into_with_c_str<T, F>(self, f: F) -> Result<T>
where Self: Sized, F: FnOnce(&CStr) -> Result<T>,

Runs a closure with self passed in as a &CStr.

Implementations on Foreign Types§

Source§

impl Arg for &str

Source§

fn as_str(&self) -> Result<&str>

Source§

fn to_string_lossy(&self) -> Cow<'_, str>

Source§

fn as_cow_c_str(&self) -> Result<Cow<'_, CStr>>

Source§

fn into_c_str<'b>(self) -> Result<Cow<'b, CStr>>
where Self: 'b,

Source§

fn into_with_c_str<T, F>(self, f: F) -> Result<T>
where Self: Sized, F: FnOnce(&CStr) -> Result<T>,

Source§

impl Arg for &String

Source§

fn as_str(&self) -> Result<&str>

Source§

fn to_string_lossy(&self) -> Cow<'_, str>

Source§

fn as_cow_c_str(&self) -> Result<Cow<'_, CStr>>

Source§

fn into_c_str<'b>(self) -> Result<Cow<'b, CStr>>
where Self: 'b,

Source§

fn into_with_c_str<T, F>(self, f: F) -> Result<T>
where Self: Sized, F: FnOnce(&CStr) -> Result<T>,

Source§

impl Arg for &Vec<u8>

Source§

fn as_str(&self) -> Result<&str>

Source§

fn to_string_lossy(&self) -> Cow<'_, str>

Source§

fn as_cow_c_str(&self) -> Result<Cow<'_, CStr>>

Source§

fn into_c_str<'b>(self) -> Result<Cow<'b, CStr>>
where Self: 'b,

Source§

fn into_with_c_str<T, F>(self, f: F) -> Result<T>
where Self: Sized, F: FnOnce(&CStr) -> Result<T>,

Source§

impl Arg for &OsStr

Source§

fn as_str(&self) -> Result<&str>

Source§

fn to_string_lossy(&self) -> Cow<'_, str>

Source§

fn as_cow_c_str(&self) -> Result<Cow<'_, CStr>>

Source§

fn into_c_str<'b>(self) -> Result<Cow<'b, CStr>>
where Self: 'b,

Source§

fn into_with_c_str<T, F>(self, f: F) -> Result<T>
where Self: Sized, F: FnOnce(&CStr) -> Result<T>,

Source§

impl Arg for &OsString

Source§

fn as_str(&self) -> Result<&str>

Source§

fn to_string_lossy(&self) -> Cow<'_, str>

Source§

fn as_cow_c_str(&self) -> Result<Cow<'_, CStr>>

Source§

fn into_c_str<'b>(self) -> Result<Cow<'b, CStr>>
where Self: 'b,

Source§

fn into_with_c_str<T, F>(self, f: F) -> Result<T>
where Self: Sized, F: FnOnce(&CStr) -> Result<T>,

Source§

impl Arg for &Path

Source§

fn as_str(&self) -> Result<&str>

Source§

fn to_string_lossy(&self) -> Cow<'_, str>

Source§

fn as_cow_c_str(&self) -> Result<Cow<'_, CStr>>

Source§

fn into_c_str<'b>(self) -> Result<Cow<'b, CStr>>
where Self: 'b,

Source§

fn into_with_c_str<T, F>(self, f: F) -> Result<T>
where Self: Sized, F: FnOnce(&CStr) -> Result<T>,

Source§

impl Arg for &PathBuf

Source§

fn as_str(&self) -> Result<&str>

Source§

fn to_string_lossy(&self) -> Cow<'_, str>

Source§

fn as_cow_c_str(&self) -> Result<Cow<'_, CStr>>

Source§

fn into_c_str<'b>(self) -> Result<Cow<'b, CStr>>
where Self: 'b,

Source§

fn into_with_c_str<T, F>(self, f: F) -> Result<T>
where Self: Sized, F: FnOnce(&CStr) -> Result<T>,

Source§

impl Arg for &[u8]

Source§

fn as_str(&self) -> Result<&str>

Source§

fn to_string_lossy(&self) -> Cow<'_, str>

Source§

fn as_cow_c_str(&self) -> Result<Cow<'_, CStr>>

Source§

fn into_c_str<'b>(self) -> Result<Cow<'b, CStr>>
where Self: 'b,

Source§

fn into_with_c_str<T, F>(self, f: F) -> Result<T>
where Self: Sized, F: FnOnce(&CStr) -> Result<T>,

Source§

impl Arg for String

Source§

fn as_str(&self) -> Result<&str>

Source§

fn to_string_lossy(&self) -> Cow<'_, str>

Source§

fn as_cow_c_str(&self) -> Result<Cow<'_, CStr>>

Source§

fn into_c_str<'b>(self) -> Result<Cow<'b, CStr>>
where Self: 'b,

Source§

fn into_with_c_str<T, F>(self, f: F) -> Result<T>
where Self: Sized, F: FnOnce(&CStr) -> Result<T>,

Source§

impl Arg for Vec<u8>

Source§

fn as_str(&self) -> Result<&str>

Source§

fn to_string_lossy(&self) -> Cow<'_, str>

Source§

fn as_cow_c_str(&self) -> Result<Cow<'_, CStr>>

Source§

fn into_c_str<'b>(self) -> Result<Cow<'b, CStr>>
where Self: 'b,

Source§

fn into_with_c_str<T, F>(self, f: F) -> Result<T>
where Self: Sized, F: FnOnce(&CStr) -> Result<T>,

Source§

impl Arg for OsString

Source§

fn as_str(&self) -> Result<&str>

Source§

fn to_string_lossy(&self) -> Cow<'_, str>

Source§

fn as_cow_c_str(&self) -> Result<Cow<'_, CStr>>

Source§

fn into_c_str<'b>(self) -> Result<Cow<'b, CStr>>
where Self: 'b,

Source§

fn into_with_c_str<T, F>(self, f: F) -> Result<T>
where Self: Sized, F: FnOnce(&CStr) -> Result<T>,

Source§

impl Arg for PathBuf

Source§

fn as_str(&self) -> Result<&str>

Source§

fn to_string_lossy(&self) -> Cow<'_, str>

Source§

fn as_cow_c_str(&self) -> Result<Cow<'_, CStr>>

Source§

fn into_c_str<'b>(self) -> Result<Cow<'b, CStr>>
where Self: 'b,

Source§

fn into_with_c_str<T, F>(self, f: F) -> Result<T>
where Self: Sized, F: FnOnce(&CStr) -> Result<T>,

Source§

impl<'a> Arg for Cow<'a, str>

Source§

fn as_str(&self) -> Result<&str>

Source§

fn to_string_lossy(&self) -> Cow<'_, str>

Source§

fn as_cow_c_str(&self) -> Result<Cow<'_, CStr>>

Source§

fn into_c_str<'b>(self) -> Result<Cow<'b, CStr>>
where Self: 'b,

Source§

fn into_with_c_str<T, F>(self, f: F) -> Result<T>
where Self: Sized, F: FnOnce(&CStr) -> Result<T>,

Source§

impl<'a> Arg for Cow<'a, CStr>

Source§

fn as_str(&self) -> Result<&str>

Source§

fn to_string_lossy(&self) -> Cow<'_, str>

Source§

fn as_cow_c_str(&self) -> Result<Cow<'_, CStr>>

Source§

fn into_c_str<'b>(self) -> Result<Cow<'b, CStr>>
where Self: 'b,

Source§

fn into_with_c_str<T, F>(self, f: F) -> Result<T>
where Self: Sized, F: FnOnce(&CStr) -> Result<T>,

Source§

impl<'a> Arg for Cow<'a, OsStr>

Source§

fn as_str(&self) -> Result<&str>

Source§

fn to_string_lossy(&self) -> Cow<'_, str>

Source§

fn as_cow_c_str(&self) -> Result<Cow<'_, CStr>>

Source§

fn into_c_str<'b>(self) -> Result<Cow<'b, CStr>>
where Self: 'b,

Source§

fn into_with_c_str<T, F>(self, f: F) -> Result<T>
where Self: Sized, F: FnOnce(&CStr) -> Result<T>,

Source§

impl<'a> Arg for Component<'a>

Source§

fn as_str(&self) -> Result<&str>

Source§

fn to_string_lossy(&self) -> Cow<'_, str>

Source§

fn as_cow_c_str(&self) -> Result<Cow<'_, CStr>>

Source§

fn into_c_str<'b>(self) -> Result<Cow<'b, CStr>>
where Self: 'b,

Source§

fn into_with_c_str<T, F>(self, f: F) -> Result<T>
where Self: Sized, F: FnOnce(&CStr) -> Result<T>,

Source§

impl<'a> Arg for Components<'a>

Source§

fn as_str(&self) -> Result<&str>

Source§

fn to_string_lossy(&self) -> Cow<'_, str>

Source§

fn as_cow_c_str(&self) -> Result<Cow<'_, CStr>>

Source§

fn into_c_str<'b>(self) -> Result<Cow<'b, CStr>>
where Self: 'b,

Source§

fn into_with_c_str<T, F>(self, f: F) -> Result<T>
where Self: Sized, F: FnOnce(&CStr) -> Result<T>,

Source§

impl<'a> Arg for Iter<'a>

Source§

fn as_str(&self) -> Result<&str>

Source§

fn to_string_lossy(&self) -> Cow<'_, str>

Source§

fn as_cow_c_str(&self) -> Result<Cow<'_, CStr>>

Source§

fn into_c_str<'b>(self) -> Result<Cow<'b, CStr>>
where Self: 'b,

Source§

fn into_with_c_str<T, F>(self, f: F) -> Result<T>
where Self: Sized, F: FnOnce(&CStr) -> Result<T>,

Implementors§