WithDir

Struct WithDir 

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

Scoped modifier of the current working directory. This uses RAII to set the current working directory back to what it was when the instance is dropped. This struct uses a static parking_lot::ReentrantMutex to prevent WithDir on other threads from updating the current working directory while any WithDir instances exist. However there is nothing stopping other threads from calling std::env::set_current_dir directly which would override the working directory.

WithDir should be created with new which returns a result. Result couldbe Err if the directory doesn’t exist, or if the user does not have permission to access.

use with_dir::WithDir;

// create a directory
let path = std::env::current_dir().unwrap().join("a");
if !path.exists() {
    std::fs::create_dir(&path);
}

// enter that directory
WithDir::new(&path).map( |_| {
    assert_eq!(std::env::current_dir().unwrap(), path);
}).unwrap();

// cwd is reset

// enter it again
let cwd = WithDir::new("a").unwrap();
// exit it
cwd.leave().unwrap();

Implementations§

Source§

impl<'a> WithDir<'a>

Source

pub fn new(path: impl AsRef<Path>) -> Result<WithDir<'a>, Error>

On creation, the current working directory is set to path and a ReentrantMutexGuard is claimed.

Source

pub fn temp() -> Result<WithDir<'a>, Error>

Uses TempDir to create a temporary directory that with the same lifetime as the returned WithDir. The current working dir is change to the temp_dir

Source

pub fn create(path: impl AsRef<Path>) -> Result<WithDir<'a>, Error>

Makes a directory and changes the current working dir to that directory, the directory will persist after this WithDir is dropped. Use create_all if you want to also make the parent directories

Source

pub fn create_all(path: impl AsRef<Path>) -> Result<WithDir<'a>, Error>

See create for docs

Source

pub fn path(&self) -> &Path

Get that path that was changed to when this instance was created

Source

pub fn leave(self) -> Result<(), Error>

Return to original working directory. This is exactly the same as dropping the instance but will not panic.

Trait Implementations§

Source§

impl AsRef<Path> for WithDir<'_>

Source§

fn as_ref(&self) -> &Path

Returns the current working directory that was set when this instance was created.

Source§

impl Drop for WithDir<'_>

Source§

fn drop(&mut self)

Resets current working directory to whatever it was when this instance was created.

§Panics

Panics if the original directory is no longer accesible (has been deleted, etc.)

Auto Trait Implementations§

§

impl<'a> Freeze for WithDir<'a>

§

impl<'a> !RefUnwindSafe for WithDir<'a>

§

impl<'a> !Send for WithDir<'a>

§

impl<'a> Sync for WithDir<'a>

§

impl<'a> Unpin for WithDir<'a>

§

impl<'a> !UnwindSafe for WithDir<'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.