pub struct CurrentDirGuard { /* private fields */ }Expand description
An RAII guard that pins the process working directory for its lifetime and restores the previous directory when dropped.
Construct one with CurrentDirGuard::change_to (change the directory)
or CurrentDirGuard::pin (hold the current directory without changing it).
While the guard is alive it owns a process-wide reentrant lock,
so guards on other threads block until it drops; nested guards on the same thread are allowed.
Implementations§
Source§impl CurrentDirGuard
impl CurrentDirGuard
Sourcepub fn pin() -> AppResult<Self>
pub fn pin() -> AppResult<Self>
Acquire the lock and pin the current working directory without changing it, restoring it on drop.
§Errors
Returns an error if the current working directory cannot be read.
Sourcepub fn change_to(dir: impl AsRef<Path>) -> AppResult<Self>
pub fn change_to(dir: impl AsRef<Path>) -> AppResult<Self>
Acquire the lock, record the current directory, and switch to dir.
The previous directory is restored when the guard drops.
§Errors
Returns an error if the current directory cannot be read
or if dir cannot be set as the working directory.