pub trait OwnedPathType:
Clone
+ AsRef<Self::RefType>
+ PartialEq
+ Send
+ Sync {
type RefType: PathType<OwnedPath = Self> + ?Sized;
// Required methods
fn insert_in_front(
&mut self,
new_fragment: &<Self::RefType as PathType>::PathSegmentRef,
);
fn push_segment_str(&mut self, new_fragment: &str);
// Provided method
fn parent(&self) -> Option<&Self::RefType> { ... }
}Expand description
A trait representing an owned path in a virtual file system.
This can be implemented for custom owned path types, allowing you to create Vfs implementations that
work with owned path types other than PathBuf. OwnedPathType is implemented for the owned type,
which is “linked” to the reference type via the associated type RefType.
Once you have a value of that owned type (e.g., MyPathBuf), you can get a reference to the path type
(e.g., &MyPath) via the AsRef trait, which is a super-trait of OwnedPathType.
A couple of methods are provided to manipulate the path in place, such as inserting a new fragment at the front of the path, or pushing a new segment at the end of the path.
Required Associated Types§
Required Methods§
Sourcefn insert_in_front(
&mut self,
new_fragment: &<Self::RefType as PathType>::PathSegmentRef,
)
fn insert_in_front( &mut self, new_fragment: &<Self::RefType as PathType>::PathSegmentRef, )
Inserts a new path fragment at the front of this path.
This is needed when reading a DirDescendants,
to prepend the base path to the relative paths of the entries found recursively.
§Examples
use std::path::{Path, PathBuf};
use std::ffi::OsStr;
use dir_structure::traits::vfs::OwnedPathType;
let mut path = PathBuf::from("dir/file.txt");
path.insert_in_front(OsStr::new("some"));
assert_eq!(path, PathBuf::from("some/dir/file.txt"));
let mut path = PathBuf::from("file.txt");
path.insert_in_front(OsStr::new("some"));
assert_eq!(path, PathBuf::from("some/file.txt"));
let mut path = PathBuf::from("");
path.insert_in_front(OsStr::new("some"));
assert_eq!(path, PathBuf::from("some"));Sourcefn push_segment_str(&mut self, new_fragment: &str)
fn push_segment_str(&mut self, new_fragment: &str)
Pushes a new path segment at the end of this path.
§Example
use std::path::{Path, PathBuf};
use dir_structure::traits::vfs::OwnedPathType;
let mut path = PathBuf::from("some/dir");
path.push_segment_str("file.txt");
assert_eq!(path, PathBuf::from("some/dir/file.txt"));
let mut path = PathBuf::from("some/dir/");
path.push_segment_str("file.txt");
assert_eq!(path, PathBuf::from("some/dir/file.txt"));Provided Methods§
Sourcefn parent(&self) -> Option<&Self::RefType>
fn parent(&self) -> Option<&Self::RefType>
Returns the parent path, if it exists.
Convenience method that calls the parent method on the reference type.
§Example
use std::path::{Path, PathBuf};
use dir_structure::traits::vfs::OwnedPathType;
let path = PathBuf::from("some/dir/file.txt");
let parent = OwnedPathType::parent(&path);
assert_eq!(parent, Some(Path::new("some/dir")));
let path = PathBuf::from("file.txt");
let parent = OwnedPathType::parent(&path);
assert_eq!(parent, Some(Path::new("")));
let empty = PathBuf::from("");
let parent = OwnedPathType::parent(&empty);
assert_eq!(parent, None);Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.