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]
impl<'a> CdManager<'a>
pub fn new(path: &'a mut PathBuf) -> Self
[src]
pub fn new(path: &'a mut PathBuf) -> Self
Starts a new context from the given PathBuf
pub fn push<P: AsRef<Path>>(&mut self, path: P)
[src]
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);
pub fn pop(&mut self) -> Result<(), Error>
[src]
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);
pub fn layer(&mut self) -> CdManager
[src]
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(); }
pub fn clone_inner(&self) -> PathBuf
[src]
pub fn clone_inner(&self) -> PathBuf
Returns a clone of the inner PathBuf
.
Trait Implementations
impl<'a> Debug for CdManager<'a>
[src]
impl<'a> Debug for CdManager<'a>
fn fmt(&self, f: &mut Formatter) -> Result
[src]
fn fmt(&self, f: &mut Formatter) -> Result
Formats the value using the given formatter. Read more
impl<'a, P: AsRef<Path>> PartialEq<P> for CdManager<'a>
[src]
impl<'a, P: AsRef<Path>> PartialEq<P> for CdManager<'a>
fn eq(&self, other: &P) -> bool
[src]
fn eq(&self, other: &P) -> bool
This method tests for self
and other
values to be equal, and is used by ==
. Read more
fn ne(&self, other: &Rhs) -> bool
1.0.0[src]
fn ne(&self, other: &Rhs) -> bool
This method tests for !=
.
impl<'a> Eq for CdManager<'a>
[src]
impl<'a> Eq for CdManager<'a>
impl<'a> PartialEq<CdManager<'a>> for PathBuf
[src]
impl<'a> PartialEq<CdManager<'a>> for PathBuf
fn eq(&self, other: &CdManager) -> bool
[src]
fn eq(&self, other: &CdManager) -> bool
This method tests for self
and other
values to be equal, and is used by ==
. Read more
fn ne(&self, other: &Rhs) -> bool
1.0.0[src]
fn ne(&self, other: &Rhs) -> bool
This method tests for !=
.
impl<'a> Drop for CdManager<'a>
[src]
impl<'a> Drop for CdManager<'a>
impl<'a> AsRef<Path> for CdManager<'a>
[src]
impl<'a> AsRef<Path> for CdManager<'a>