CleanDir

Struct CleanDir 

Source
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: T

Trait Implementations§

Source§

impl<T> AssertEq for CleanDir<T>
where for<'__trivial> T: AssertEq<T> + Debug,

Source§

fn assert_eq( &self, other: &Self, path: &mut AssertPath, init_left: &impl Display, init_right: &impl Display, )

Asserts that self is equal to other, panicking if they are not equal. Read more
Source§

impl<T: Clone> Clone for CleanDir<T>

Source§

fn clone(&self) -> CleanDir<T>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Debug> Debug for CleanDir<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T> DynamicHasField for CleanDir<T>
where T: DynamicHasField,

Available on crate feature resolve-path only.
Source§

type Inner = <T as DynamicHasField>::Inner

The type of the field.
Source§

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,

Source§

type Inner = T

The inner type to cast.
Source§

type Wr = CleanDirRefWr<'a, 'vfs, T, Vfs>

The reference type to cast to.
Source§

fn from_ref_for_writer(value: &'a Self::Inner) -> Self::Wr

Casts the reference to the inner type to a WriteTo reference type.
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.
Source§

type Inner = T

The inner type to cast.
Source§

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

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.
Source§

type Inner = <T as HasField<NAME>>::Inner

The type of the field.
Source§

fn resolve_path<P: OwnedPathType>(p: P) -> P

How to resolve the path for the field, from the path of Self.
Source§

impl<T: Hash> Hash for CleanDir<T>

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<T> NewtypeToInner for CleanDir<T>

Source§

type Inner = T

The inner type.
Source§

fn into_inner(self) -> Self::Inner

Converts the newtype to its inner type.
Source§

impl<T: Ord> Ord for CleanDir<T>

Source§

fn cmp(&self, other: &CleanDir<T>) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl<T: PartialEq> PartialEq for CleanDir<T>

Source§

fn eq(&self, other: &CleanDir<T>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T: PartialOrd> PartialOrd for CleanDir<T>

Source§

fn partial_cmp(&self, other: &CleanDir<T>) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<'a, T, Vfs: Vfs<'a>> ReadFrom<'a, Vfs> for CleanDir<T>
where T: ReadFrom<'a, Vfs>,

Source§

fn read_from(path: &Vfs::Path, vfs: Pin<&'a Vfs>) -> VfsResult<Self, Vfs>
where Self: Sized,

Reads the structure from the specified path, which can be either a file or a directory.
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.
Source§

type Future = Pin<Box<dyn Future<Output = Result<CleanDir<T>, Error<<<Vfs as VfsCore>::Path as PathType>::OwnedPath>>> + Send + 'a>>

The future type returned by the async read function.
Source§

fn read_from_async( path: <<Vfs as VfsCore>::Path as PathType>::OwnedPath, vfs: Pin<&'a Vfs>, ) -> Self::Future

Asynchronously reads the structure from the specified path, which can be either a file or a directory.
Source§

impl<'a, T, Vfs: WriteSupportingVfs<'a>> WriteTo<'a, Vfs> for CleanDir<T>
where T: WriteTo<'a, Vfs>,

Source§

fn write_to(&self, path: &Vfs::Path, vfs: Pin<&'a Vfs>) -> VfsResult<(), Vfs>

Writes the structure to the specified path.
Source§

impl<'a, T, Vfs: WriteSupportingVfsAsync + 'static> WriteToAsync<'a, Vfs> for CleanDir<T>
where T: WriteToAsync<'a, Vfs> + Send + Sync + 'static,

Available on crate feature async only.
Source§

type Future = Pin<Box<dyn Future<Output = Result<(), Error<<<Vfs as VfsCore>::Path as PathType>::OwnedPath>>> + Send + 'a>>

The future type returned by the async write function.
Source§

fn write_to_async( self, path: <<Vfs as VfsCore>::Path as PathType>::OwnedPath, vfs: Pin<&'a Vfs>, ) -> Self::Future

Asynchronously writes the structure to the specified path.
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.
Source§

type Future<'b> = CleanDirWriteRefFuture<'a, 'b, T, Vfs> where Self: 'b, Vfs: 'b, 'a: 'b

The future type returned by the async write function.
Source§

fn write_to_async_ref<'b>( &'b self, path: <<Vfs as VfsCore>::Path as PathType>::OwnedPath, vfs: Pin<&'b Vfs>, ) -> Self::Future<'b>
where 'a: 'b,

Asynchronously writes the structure to the specified path.
Source§

impl<T: Copy> Copy for CleanDir<T>

Source§

impl<T: Eq> Eq for CleanDir<T>

Source§

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> 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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<Q, K> Comparable<K> for Q
where Q: Ord + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn compare(&self, key: &K) -> Ordering

Compare self to key and return their ordering.
Source§

impl<T> DirStructureItem for T

Source§

fn read(path: impl AsRef<<FsVfs as VfsCore>::Path>) -> VfsResult<Self, FsVfs>
where Self: ReadFrom<'static, FsVfs> + Sized,

Uses the ReadFrom implementation to read the structure from disk, from the specified path.
Source§

fn write<'a, 'vfs: 'a>( &'a self, path: impl AsRef<<FsVfs as VfsCore>::Path>, ) -> VfsResult<(), FsVfs>
where Self: WriteTo<'vfs, FsVfs>,

Uses the WriteTo implementation to write the structure to disk at the specified path.
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
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> IntoEither for T

Source§

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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.
Source§

impl<T> ErasedDestructor for T
where T: 'static,