Skip to main content

SystemPathBuf

Struct SystemPathBuf 

Source
pub struct SystemPathBuf(/* private fields */);
Expand description

An owned, mutable path on System (akin to String).

The path is guaranteed to be valid UTF-8.

Implementations§

Source§

impl SystemPathBuf

Source

pub fn new() -> Self

Source

pub fn from_utf8_path_buf(path: Utf8PathBuf) -> Self

Source

pub fn from_path_buf(path: PathBuf) -> Result<Self, PathBuf>

Source

pub fn from_path_buf_lossy(path: PathBuf) -> Self

Try to convert from path directly, falling back to the lossy string representation on error.

Source

pub fn push(&mut self, path: impl AsRef<SystemPath>)

Extends self with path.

If path is absolute, it replaces the current path.

On Windows:

  • if path has a root but no prefix (e.g., \windows), it replaces everything except for the prefix (if any) of self.
  • if path has a prefix but no root, it replaces self.
§Examples

Pushing a relative path extends the existing path:

use ruff_db::system::SystemPathBuf;

let mut path = SystemPathBuf::from("/tmp");
path.push("file.bk");
assert_eq!(path, SystemPathBuf::from("/tmp/file.bk"));

Pushing an absolute path replaces the existing path:


use ruff_db::system::SystemPathBuf;

let mut path = SystemPathBuf::from("/tmp");
path.push("/etc");
assert_eq!(path, SystemPathBuf::from("/etc"));
Source

pub fn into_utf8_path_buf(self) -> Utf8PathBuf

Source

pub fn into_std_path_buf(self) -> PathBuf

Source

pub fn into_string(self) -> String

Source

pub fn as_path(&self) -> &SystemPath

Methods from Deref<Target = SystemPath>§

Source

pub fn simplified(&self) -> &SystemPath

Takes any path, and when possible, converts Windows UNC paths to regular paths. If the path can’t be converted, it’s returned unmodified.

On non-Windows this is no-op.

\\?\C:\Windows will be converted to C:\Windows, but \\?\C:\COM will be left as-is (due to a reserved filename).

Use this to pass arbitrary paths to programs that may not be UNC-aware.

It’s generally safe to pass UNC paths to legacy programs, because these paths contain a reserved prefix, so will gracefully fail if used with legacy APIs that don’t support UNC.

This function does not perform any I/O.

Currently paths with unpaired surrogates aren’t converted even if they could be, due to limitations of Rust’s OsStr API.

To check if a path remained as UNC, use path.as_os_str().as_encoded_bytes().starts_with(b"\\\\").

Source

pub fn is_absolute(&self) -> bool

Returns true if the SystemPath is absolute, i.e., if it is independent of the current directory.

  • On Unix, a path is absolute if it starts with the root, so is_absolute and has_root are equivalent.

  • On Windows, a path is absolute if it has a prefix and starts with the root: c:\windows is absolute, while c:temp and \temp are not.

§Examples
use ruff_db::system::SystemPath;

assert!(!SystemPath::new("foo.txt").is_absolute());
Source

pub fn extension(&self) -> Option<&str>

Extracts the file extension, if possible.

The extension is:

  • None, if there is no file name;
  • None, if there is no embedded .;
  • None, if the file name begins with . and has no other .s within;
  • Otherwise, the portion of the file name after the final .
§Examples
use ruff_db::system::SystemPath;

assert_eq!("rs", SystemPath::new("foo.rs").extension().unwrap());
assert_eq!("gz", SystemPath::new("foo.tar.gz").extension().unwrap());

See Path::extension for more details.

Source

pub fn starts_with(&self, base: impl AsRef<SystemPath>) -> bool

Determines whether base is a prefix of self.

Only considers whole path components to match.

§Examples
use ruff_db::system::SystemPath;

let path = SystemPath::new("/etc/passwd");

assert!(path.starts_with("/etc"));
assert!(path.starts_with("/etc/"));
assert!(path.starts_with("/etc/passwd"));
assert!(path.starts_with("/etc/passwd/")); // extra slash is okay
assert!(path.starts_with("/etc/passwd///")); // multiple extra slashes are okay

assert!(!path.starts_with("/e"));
assert!(!path.starts_with("/etc/passwd.txt"));

assert!(!SystemPath::new("/etc/foo.rs").starts_with("/etc/foo"));
Source

pub fn ends_with(&self, child: impl AsRef<SystemPath>) -> bool

Determines whether child is a suffix of self.

Only considers whole path components to match.

§Examples
use ruff_db::system::SystemPath;

let path = SystemPath::new("/etc/resolv.conf");

assert!(path.ends_with("resolv.conf"));
assert!(path.ends_with("etc/resolv.conf"));
assert!(path.ends_with("/etc/resolv.conf"));

assert!(!path.ends_with("/resolv.conf"));
assert!(!path.ends_with("conf")); // use .extension() instead
Source

pub fn parent(&self) -> Option<&SystemPath>

Returns the FileSystemPath without its final component, if there is one.

Returns None if the path terminates in a root or prefix.

§Examples
use ruff_db::system::SystemPath;

let path = SystemPath::new("/foo/bar");
let parent = path.parent().unwrap();
assert_eq!(parent, SystemPath::new("/foo"));

let grand_parent = parent.parent().unwrap();
assert_eq!(grand_parent, SystemPath::new("/"));
assert_eq!(grand_parent.parent(), None);
Source

pub fn ancestors(&self) -> impl Iterator<Item = &SystemPath>

Produces an iterator over SystemPath and its ancestors.

The iterator will yield the SystemPath that is returned if the parent method is used zero or more times. That means, the iterator will yield &self, &self.parent().unwrap(), &self.parent().unwrap().parent().unwrap() and so on. If the parent method returns None, the iterator will do likewise. The iterator will always yield at least one value, namely &self.

§Examples
use ruff_db::system::SystemPath;

let mut ancestors = SystemPath::new("/foo/bar").ancestors();
assert_eq!(ancestors.next(), Some(SystemPath::new("/foo/bar")));
assert_eq!(ancestors.next(), Some(SystemPath::new("/foo")));
assert_eq!(ancestors.next(), Some(SystemPath::new("/")));
assert_eq!(ancestors.next(), None);

let mut ancestors = SystemPath::new("../foo/bar").ancestors();
assert_eq!(ancestors.next(), Some(SystemPath::new("../foo/bar")));
assert_eq!(ancestors.next(), Some(SystemPath::new("../foo")));
assert_eq!(ancestors.next(), Some(SystemPath::new("..")));
assert_eq!(ancestors.next(), Some(SystemPath::new("")));
assert_eq!(ancestors.next(), None);
Source

pub fn components(&self) -> Utf8Components<'_>

Produces an iterator over the camino::Utf8Components of the path.

When parsing the path, there is a small amount of normalization:

  • Repeated separators are ignored, so a/b and a//b both have a and b as components.

  • Occurrences of . are normalized away, except if they are at the beginning of the path. For example, a/./b, a/b/, a/b/. and a/b all have a and b as components, but ./a/b starts with an additional CurDir component.

  • A trailing slash is normalized away, /a/b and /a/b/ are equivalent.

Note that no other normalization takes place; in particular, a/c and a/b/../c are distinct, to account for the possibility that b is a symbolic link (so its parent isn’t a).

§Examples
use camino::{Utf8Component};
use ruff_db::system::SystemPath;

let mut components = SystemPath::new("/tmp/foo.txt").components();

assert_eq!(components.next(), Some(Utf8Component::RootDir));
assert_eq!(components.next(), Some(Utf8Component::Normal("tmp")));
assert_eq!(components.next(), Some(Utf8Component::Normal("foo.txt")));
assert_eq!(components.next(), None)
Source

pub fn file_name(&self) -> Option<&str>

Returns the final component of the FileSystemPath, if there is one.

If the path is a normal file, this is the file name. If it’s the path of a directory, this is the directory name.

Returns None if the path terminates in ...

§Examples
use camino::Utf8Path;
use ruff_db::system::SystemPath;

assert_eq!(Some("bin"), SystemPath::new("/usr/bin/").file_name());
assert_eq!(Some("foo.txt"), SystemPath::new("tmp/foo.txt").file_name());
assert_eq!(Some("foo.txt"), SystemPath::new("foo.txt/.").file_name());
assert_eq!(Some("foo.txt"), SystemPath::new("foo.txt/.//").file_name());
assert_eq!(None, SystemPath::new("foo.txt/..").file_name());
assert_eq!(None, SystemPath::new("/").file_name());
Source

pub fn file_stem(&self) -> Option<&str>

Extracts the stem (non-extension) portion of self.file_name.

The stem is:

  • None, if there is no file name;
  • The entire file name if there is no embedded .;
  • The entire file name if the file name begins with . and has no other .s within;
  • Otherwise, the portion of the file name before the final .
§Examples
use ruff_db::system::SystemPath;

assert_eq!("foo", SystemPath::new("foo.rs").file_stem().unwrap());
assert_eq!("foo.tar", SystemPath::new("foo.tar.gz").file_stem().unwrap());
Source

pub fn strip_prefix( &self, base: impl AsRef<SystemPath>, ) -> Result<&SystemPath, StripPrefixError>

Returns a path that, when joined onto base, yields self.

§Errors

If base is not a prefix of self (i.e., starts_with returns false), returns Err.

§Examples
use ruff_db::system::{SystemPath, SystemPathBuf};

let path = SystemPath::new("/test/haha/foo.txt");

assert_eq!(path.strip_prefix("/"), Ok(SystemPath::new("test/haha/foo.txt")));
assert_eq!(path.strip_prefix("/test"), Ok(SystemPath::new("haha/foo.txt")));
assert_eq!(path.strip_prefix("/test/"), Ok(SystemPath::new("haha/foo.txt")));
assert_eq!(path.strip_prefix("/test/haha/foo.txt"), Ok(SystemPath::new("")));
assert_eq!(path.strip_prefix("/test/haha/foo.txt/"), Ok(SystemPath::new("")));

assert!(path.strip_prefix("test").is_err());
assert!(path.strip_prefix("/haha").is_err());

let prefix = SystemPathBuf::from("/test/");
assert_eq!(path.strip_prefix(prefix), Ok(SystemPath::new("haha/foo.txt")));
Source

pub fn join(&self, path: impl AsRef<SystemPath>) -> SystemPathBuf

Creates an owned SystemPathBuf with path adjoined to self.

See std::path::PathBuf::push for more details on what it means to adjoin a path.

§Examples
use ruff_db::system::{SystemPath, SystemPathBuf};

assert_eq!(SystemPath::new("/etc").join("passwd"), SystemPathBuf::from("/etc/passwd"));
Source

pub fn with_extension(&self, extension: &str) -> SystemPathBuf

Creates an owned SystemPathBuf like self but with the given extension.

See std::path::PathBuf::set_extension for more details.

§Examples
use ruff_db::system::{SystemPath, SystemPathBuf};

let path = SystemPath::new("foo.rs");
assert_eq!(path.with_extension("txt"), SystemPathBuf::from("foo.txt"));

let path = SystemPath::new("foo.tar.gz");
assert_eq!(path.with_extension(""), SystemPathBuf::from("foo.tar"));
assert_eq!(path.with_extension("xz"), SystemPathBuf::from("foo.tar.xz"));
assert_eq!(path.with_extension("").with_extension("txt"), SystemPathBuf::from("foo.txt"));
Source

pub fn to_path_buf(&self) -> SystemPathBuf

Converts the path to an owned SystemPathBuf.

Source

pub fn as_str(&self) -> &str

Returns the path as a string slice.

Source

pub fn as_std_path(&self) -> &Path

Returns the std path for the file.

Source

pub fn as_utf8_path(&self) -> &Utf8Path

Returns the Utf8Path for the file.

Trait Implementations§

Source§

impl AsRef<Path> for SystemPathBuf

Source§

fn as_ref(&self) -> &Path

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

impl AsRef<SystemPath> for SystemPathBuf

Source§

fn as_ref(&self) -> &SystemPath

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

impl Borrow<SystemPath> for SystemPathBuf

Source§

fn borrow(&self) -> &SystemPath

Immutably borrows from an owned value. Read more
Source§

impl Clone for SystemPathBuf

Source§

fn clone(&self) -> SystemPathBuf

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for SystemPathBuf

Source§

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

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

impl Default for SystemPathBuf

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Deref for SystemPathBuf

Source§

type Target = SystemPath

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.
Source§

impl Display for SystemPathBuf

Source§

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

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

impl Eq for SystemPathBuf

Source§

impl<P: AsRef<SystemPath>> Extend<P> for SystemPathBuf

Source§

fn extend<I: IntoIterator<Item = P>>(&mut self, iter: I)

Extends a collection with the contents of an iterator. Read more
Source§

fn extend_one(&mut self, item: T)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
Source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
Source§

impl From<&str> for SystemPathBuf

Source§

fn from(value: &str) -> Self

Converts to this type from the input type.
Source§

impl From<String> for SystemPathBuf

Source§

fn from(value: String) -> Self

Converts to this type from the input type.
Source§

impl From<SystemPathBuf> for FilePath

Source§

fn from(value: SystemPathBuf) -> Self

Converts to this type from the input type.
Source§

impl From<SystemPathBuf> for Box<SystemPath>

Source§

fn from(path: SystemPathBuf) -> Self

Converts to this type from the input type.
Source§

impl<P: AsRef<SystemPath>> FromIterator<P> for SystemPathBuf

Source§

fn from_iter<I: IntoIterator<Item = P>>(iter: I) -> Self

Creates a value from an iterator. Read more
Source§

impl GetSize for SystemPathBuf

Source§

fn get_heap_size_with_tracker<T: GetSizeTracker>( &self, tracker: T, ) -> (usize, T)

Determines how many bytes this object occupies inside the heap while using a tracker. Read more
Source§

fn get_stack_size() -> usize

Determines how may bytes this object occupies inside the stack. Read more
Source§

fn get_heap_size(&self) -> usize

Determines how many bytes this object occupies inside the heap. Read more
Source§

fn get_size(&self) -> usize

Determines the total size of the object. Read more
Source§

fn get_size_with_tracker<T>(&self, tracker: T) -> (usize, T)
where T: GetSizeTracker,

Determines the total size of the object while using a tracker. Read more
Source§

impl Hash for SystemPathBuf

Source§

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

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 SystemPathBuf

Source§

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

This method returns an Ordering between self and other. Read more
1.21.0 (const: unstable) · Source§

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

Compares and returns the maximum of two values. Read more
1.21.0 (const: unstable) · Source§

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

Compares and returns the minimum of two values. Read more
1.50.0 (const: unstable) · Source§

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

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

fn clamp_to<R>(self, range: R) -> Self
where Self: Sized, R: ClampBounds<Self>,

🔬This is a nightly-only experimental API. (clamp_to)
Restrict a value to a certain range. Read more
Source§

impl PartialEq for SystemPathBuf

Source§

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

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

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

Inequality operator !=. Read more
Source§

impl PartialEq<FilePath> for SystemPathBuf

Source§

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

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

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

Inequality operator !=. Read more
Source§

impl PartialEq<SystemPathBuf> for FilePath

Source§

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

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

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

Inequality operator !=. Read more
Source§

impl PartialOrd for SystemPathBuf

Source§

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

This method returns an ordering between self and other values if one exists. Read more
1.0.0 (const: unstable) · 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 (const: unstable) · 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 (const: unstable) · 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 (const: unstable) · 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 StructuralPartialEq for SystemPathBuf

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<Q, K> Comparable<K> for Q
where Q: Ord + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn compare(&self, key: &K) -> Ordering

Compare self to key and return their ordering.
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> HashEqLike<&T> for T
where T: Hash + Eq,

Source§

fn hash<H>(&self, h: &mut H)
where H: Hasher,

Source§

fn eq(&self, data: &&T) -> bool

Source§

impl<T> HashEqLike<Cow<'_, T>> for T
where T: Hash + Eq + Clone,

Source§

fn hash<H>(&self, h: &mut H)
where H: Hasher,

Source§

fn eq(&self, data: &Cow<'_, T>) -> bool

Source§

impl<T> HashEqLike<T> for T
where T: Hash + Eq,

Source§

fn hash<H>(&self, h: &mut H)
where H: Hasher,

Source§

fn eq(&self, data: &T) -> bool

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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<T> IntoDiagnosticMessage for T
where T: Display,

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Lookup<T> for T

Source§

fn into_owned(self) -> T

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> ToCharString for T
where T: Display,

Source§

fn try_to_char_string(&self) -> Result<CharString, ToCharStringError>

Attempts to convert the value to a CharString. Read more
Source§

fn to_char_string(&self) -> CharString

Converts the value to a CharString. Read more
Source§

impl<T> ToCompactString for T
where T: Display,

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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more