OwnedPathType

Trait OwnedPathType 

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

Source

type RefType: PathType<OwnedPath = Self> + ?Sized

The reference type corresponding to this owned path type.

This reference type must implement the PathType trait, and its associated OwnedPath must be Self.

Required Methods§

Source

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"));
Source

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§

Source

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.

Implementations on Foreign Types§

Source§

impl OwnedPathType for PathBuf

Source§

type RefType = Path

Source§

fn parent(&self) -> Option<&Self::RefType>

Source§

fn insert_in_front( &mut self, new_fragment: &<Self::RefType as PathType>::PathSegmentRef, )

Source§

fn push_segment_str(&mut self, new_fragment: &str)

Implementors§