pub struct CleanDir<T>(pub T);Expand description
A newtype that will clean the directory it is written to, before writing the value.
This is useful when we want to write a directory structure, but we want to make sure that the directory is clean before writing it, so that there are no old files / directories left in it.
use std::path::Path;
use dir_structure::traits::sync::DirStructureItem;
use dir_structure::clean_dir::CleanDir;
#[derive(dir_structure::DirStructure)]
struct Dir {
#[dir_structure(path = "f.txt")]
f: String,
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
let d = Path::new("dir");
std::fs::create_dir_all(&d)?;
std::fs::write(d.join("f.txt"), "Hello, world!")?;
std::fs::write(d.join("f2.txt"), "Hello, world! (2)")?;
let dir = Dir::read(&d)?;
assert_eq!(dir.f, "Hello, world!");
assert_eq!(std::fs::read_to_string(d.join("f2.txt"))?, "Hello, world! (2)");
CleanDir(dir).write(&d)?;
assert_eq!(std::fs::read_to_string(d.join("f.txt"))?, "Hello, world!");
assert!(!d.join("f2.txt").exists());
Ok(())
}Tuple Fields§
§0: TTrait Implementations§
Source§impl<T> DynamicHasField for CleanDir<T>where
T: DynamicHasField,
Available on crate feature resolve-path only.
impl<T> DynamicHasField for CleanDir<T>where
T: DynamicHasField,
Available on crate feature
resolve-path only.Source§type Inner = <T as DynamicHasField>::Inner
type Inner = <T as DynamicHasField>::Inner
The type of the field.
Source§fn resolve_path<P: OwnedPathType>(p: P, name: &str) -> P
fn resolve_path<P: OwnedPathType>(p: P, name: &str) -> P
How to resolve the path for the field, from the path of
Self, given the name
passed into the resolve_path! macro.Source§impl<'a, 'vfs, T, Vfs> FromRefForWriter<'a, 'vfs, Vfs> for CleanDir<T>where
T: WriteTo<'vfs, Vfs> + 'a,
Vfs: 'vfs + WriteSupportingVfs<'vfs>,
'vfs: 'a,
impl<'a, 'vfs, T, Vfs> FromRefForWriter<'a, 'vfs, Vfs> for CleanDir<T>where
T: WriteTo<'vfs, Vfs> + 'a,
Vfs: 'vfs + WriteSupportingVfs<'vfs>,
'vfs: 'a,
Source§impl<'a, T, Vfs: WriteSupportingVfsAsync + 'static> FromRefForWriterAsync<'a, Vfs> for CleanDir<T>where
T: WriteToAsyncRef<'a, Vfs> + Send + Sync + 'static,
for<'f> <Vfs as VfsAsync>::ExistsFuture<'f>: Future<Output = VfsResult<bool, Vfs>> + Unpin + 'f,
for<'f> <Vfs as WriteSupportingVfsAsync>::RemoveDirAllFuture<'f>: Future<Output = VfsResult<(), Vfs>> + Unpin + 'f,
Available on crate feature async only.
impl<'a, T, Vfs: WriteSupportingVfsAsync + 'static> FromRefForWriterAsync<'a, Vfs> for CleanDir<T>where
T: WriteToAsyncRef<'a, Vfs> + Send + Sync + 'static,
for<'f> <Vfs as VfsAsync>::ExistsFuture<'f>: Future<Output = VfsResult<bool, Vfs>> + Unpin + 'f,
for<'f> <Vfs as WriteSupportingVfsAsync>::RemoveDirAllFuture<'f>: Future<Output = VfsResult<(), Vfs>> + Unpin + 'f,
Available on crate feature
async only.Source§type Wr = CleanDirRefWr<'a, 'a, T, Vfs>
type Wr = CleanDirRefWr<'a, 'a, T, Vfs>
The reference type to cast to.
Source§fn from_ref_for_writer_async(value: &'a Self::Inner) -> Self::Wr
fn from_ref_for_writer_async(value: &'a Self::Inner) -> Self::Wr
Casts the reference to the inner type to a
WriteToAsync
reference type.Source§impl<const NAME: [char; 32], T> HasField<NAME> for CleanDir<T>where
T: HasField<NAME>,
Available on crate feature resolve-path only.
impl<const NAME: [char; 32], T> HasField<NAME> for CleanDir<T>where
T: HasField<NAME>,
Available on crate feature
resolve-path only.Source§fn resolve_path<P: OwnedPathType>(p: P) -> P
fn resolve_path<P: OwnedPathType>(p: P) -> P
How to resolve the path for the field, from the path of
Self.Source§impl<T> NewtypeToInner for CleanDir<T>where
T: DirStructureItem,
impl<T> NewtypeToInner for CleanDir<T>where
T: DirStructureItem,
Source§impl<T: Ord> Ord for CleanDir<T>
impl<T: Ord> Ord for CleanDir<T>
1.21.0 · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Compares and returns the maximum of two values. Read more
Source§impl<T: PartialOrd> PartialOrd for CleanDir<T>
impl<T: PartialOrd> PartialOrd for CleanDir<T>
Source§impl<'a, T, Vfs: VfsAsync + 'a> ReadFromAsync<'a, Vfs> for CleanDir<T>where
T: ReadFromAsync<'a, Vfs> + Send + 'static,
Available on crate feature async only.
impl<'a, T, Vfs: VfsAsync + 'a> ReadFromAsync<'a, Vfs> for CleanDir<T>where
T: ReadFromAsync<'a, Vfs> + Send + 'static,
Available on crate feature
async only.Source§impl<'a, T, Vfs: WriteSupportingVfs<'a>> WriteTo<'a, Vfs> for CleanDir<T>where
T: WriteTo<'a, Vfs>,
impl<'a, T, Vfs: WriteSupportingVfs<'a>> WriteTo<'a, Vfs> for CleanDir<T>where
T: WriteTo<'a, Vfs>,
Source§impl<'a, T, Vfs: WriteSupportingVfsAsync + 'static> WriteToAsync<'a, Vfs> for CleanDir<T>
Available on crate feature async only.
impl<'a, T, Vfs: WriteSupportingVfsAsync + 'static> WriteToAsync<'a, Vfs> for CleanDir<T>
Available on crate feature
async only.Source§impl<'a, T, Vfs: WriteSupportingVfsAsync + 'static> WriteToAsyncRef<'a, Vfs> for CleanDir<T>where
T: WriteToAsyncRef<'a, Vfs> + Send + Sync + 'static,
for<'f> <T as WriteToAsyncRef<'a, Vfs>>::Future<'f>: Future<Output = VfsResult<(), Vfs>> + Unpin + 'f,
for<'f> <Vfs as VfsAsync>::ExistsFuture<'f>: Future<Output = VfsResult<bool, Vfs>> + Unpin + 'f,
for<'f> <Vfs as WriteSupportingVfsAsync>::RemoveDirAllFuture<'f>: Future<Output = VfsResult<(), Vfs>> + Unpin + 'f,
Available on crate feature async only.
impl<'a, T, Vfs: WriteSupportingVfsAsync + 'static> WriteToAsyncRef<'a, Vfs> for CleanDir<T>where
T: WriteToAsyncRef<'a, Vfs> + Send + Sync + 'static,
for<'f> <T as WriteToAsyncRef<'a, Vfs>>::Future<'f>: Future<Output = VfsResult<(), Vfs>> + Unpin + 'f,
for<'f> <Vfs as VfsAsync>::ExistsFuture<'f>: Future<Output = VfsResult<bool, Vfs>> + Unpin + 'f,
for<'f> <Vfs as WriteSupportingVfsAsync>::RemoveDirAllFuture<'f>: Future<Output = VfsResult<(), Vfs>> + Unpin + 'f,
Available on crate feature
async only.impl<T: Copy> Copy for CleanDir<T>
impl<T: Eq> Eq for CleanDir<T>
impl<T> StructuralPartialEq for CleanDir<T>
Auto Trait Implementations§
impl<T> Freeze for CleanDir<T>where
T: Freeze,
impl<T> RefUnwindSafe for CleanDir<T>where
T: RefUnwindSafe,
impl<T> Send for CleanDir<T>where
T: Send,
impl<T> Sync for CleanDir<T>where
T: Sync,
impl<T> Unpin for CleanDir<T>where
T: Unpin,
impl<T> UnwindSafe for CleanDir<T>where
T: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Comparable<K> for Q
impl<Q, K> Comparable<K> for Q
Source§impl<T> DirStructureItem for T
impl<T> DirStructureItem for T
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
Compare self to
key and return true if they are equal.Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more