Struct cd_manager::CdManager[][src]

pub struct CdManager<'a> { /* fields omitted */ }

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.

Methods

impl<'a> CdManager<'a>
[src]

Starts a new context from the given PathBuf

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

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

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();
}

Returns a clone of the inner PathBuf.

Trait Implementations

impl<'a> Debug for CdManager<'a>
[src]

Formats the value using the given formatter. Read more

impl<'a, P: AsRef<Path>> PartialEq<P> for CdManager<'a>
[src]

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

impl<'a> Eq for CdManager<'a>
[src]

impl<'a> PartialEq<CdManager<'a>> for PathBuf
[src]

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

impl<'a> Drop for CdManager<'a>
[src]

Executes the destructor for this type. Read more

impl<'a> AsRef<Path> for CdManager<'a>
[src]

Performs the conversion.

Auto Trait Implementations

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

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