Struct CdManager

Source
pub struct CdManager<'a> { /* private fields */ }
Expand description

A “Change Directory” manager or CdManager prevents you from forgetting to pop directories at the end of a block.

It takes a reference to a PathBuf and, upon going out of scope, will manually pop all elements of the PathBuf off that were added during its life.

The only supported operations are push or pop, more complex operations such as cannot easily be managed.

Note that the CdManager uses a path’s Components to determine how many times to call pop, so this may cause some inconsistency if your path includes ..

A CdManager implements AsRef<Path> so it may be used anywhere a Path is needed.

Implementations§

Source§

impl<'a> CdManager<'a>

Source

pub fn new(path: &'a mut PathBuf) -> Self

Starts a new context from the given PathBuf

Source

pub fn push<P: AsRef<Path>>(&mut self, path: P)

Pushes a Path onto the PathBuf, to be undone when the CdManager goes out of scope.

extern crate cd_manager;
use cd_manager::CdManager;
use std::path::PathBuf;

let mut path = PathBuf::from("a/path/to/something".to_string());
let mut p2 = path.clone();

{
    let mut manager = CdManager::new(&mut p2);

    path.push("foo/bar");
    manager.push("foo/bar");

    assert_eq!(path, manager);
} // Automatically pop "foo" from the manager

path.pop();
path.pop();
assert_eq!(path, p2);
Source

pub fn pop(&mut self) -> Result<(), Error>

Pops a single link from the underlying PathBuf. This will return an error if this is identical to the PathBuf the CdManager was constructured with (that is, the number of pop calls matches the number of pushed Path components).

extern crate cd_manager;
use cd_manager::CdManager;
use std::path::PathBuf;

let mut path = PathBuf::from("a/path".to_string());
let mut p2 = path.clone();
{
    let mut cd = CdManager::new(&mut p2);
    cd.push("foo");

    cd.pop().unwrap();
    assert_eq!(cd, path);

    assert!(cd.pop().is_err());
}

assert_eq!(path, p2);
Source

pub fn layer(&mut self) -> CdManager<'_>

Creates a new “layer” of the manager, for scoping purposes

That is, if you call CdManager::layer in a loop body or function call, it ensures that any behavior done to the returned CdManager will be undone for you.

You can think of this as a cheaper, scoped Clone.

extern crate cd_manager;
use cd_manager::CdManager;
use std::path::PathBuf;

let mut path = PathBuf::from("a/path".to_string());
let mut p2 = path.clone();

let mut cd = CdManager::new(&mut p2);

for _ in 0..100 {
    assert_eq!(cd, path);
    let mut cd = cd.layer();
    cd.push("bar");

    path.push("bar");
    assert_eq!(cd, path);
    path.pop();
}
Source

pub fn clone_inner(&self) -> PathBuf

Returns a clone of the inner PathBuf.

Trait Implementations§

Source§

impl<'a> AsRef<Path> for CdManager<'a>

Source§

fn as_ref(&self) -> &Path

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

impl<'a> Debug for CdManager<'a>

Source§

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

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

impl<'a> Drop for CdManager<'a>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl<'a> PartialEq<CdManager<'a>> for PathBuf

Source§

fn eq(&self, other: &CdManager<'_>) -> 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<'a, P: AsRef<Path>> PartialEq<P> for CdManager<'a>

Source§

fn eq(&self, other: &P) -> 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<'a> Eq for CdManager<'a>

Auto Trait Implementations§

§

impl<'a> Freeze for CdManager<'a>

§

impl<'a> RefUnwindSafe for CdManager<'a>

§

impl<'a> Send for CdManager<'a>

§

impl<'a> Sync for CdManager<'a>

§

impl<'a> Unpin for CdManager<'a>

§

impl<'a> !UnwindSafe for CdManager<'a>

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