VersionedHash

Struct VersionedHash 

Source
pub struct VersionedHash<T: Hash, P: PathType + ?Sized = Path, H: Hasher + Default = DefaultHasher> { /* private fields */ }
Expand description

A value with a hash to determine if it has changed.

You can use any hasher that implements std::hash::Hasher and std::default::Default. By default, it uses std::collections::hash_map::DefaultHasher.

To access the inner value, you can use Deref / DerefMut or the into_inner method.

§Examples

use std::path::PathBuf;
use dir_structure::versioned_hash::VersionedHash;

let mut vh = VersionedHash::<String>::new_clean(PathBuf::from("test.txt"), "Hello, world!".to_owned());
assert!(vh.is_clean());

vh.push_str(" Modified.");
assert!(vh.is_dirty());
assert!(!vh.is_clean());

Implementations§

Source§

impl<T: Hash, P: PathType + ?Sized, H: Hasher + Default> VersionedHash<T, P, H>

Source

pub fn into_inner(self) -> T

Get the inner value. You can also use Deref / DerefMut to get references to the inner value.

§Examples
use std::path::PathBuf;
use dir_structure::versioned_hash::VersionedHash;

let vh = VersionedHash::<String>::new_clean(PathBuf::from("test.txt"), "Hello, world!".to_owned());
assert_eq!(vh.into_inner(), "Hello, world!".to_owned());

let vh = VersionedHash::<String>::new_dirty(PathBuf::from("test.txt"), "Hello, world!".to_owned());
assert_eq!(vh.into_inner(), "Hello, world!".to_owned());
Source

pub fn new_clean(path: P::OwnedPath, value: T) -> Self

Create a new clean value.

§Examples
use std::path::PathBuf;
use dir_structure::versioned_hash::VersionedHash;

let vh = VersionedHash::<String>::new_clean(PathBuf::from("test.txt"), "Hello, world!".to_owned());
assert!(vh.is_clean());
assert!(!vh.is_dirty());
Source

pub fn new_dirty(path: P::OwnedPath, value: T) -> Self

Create a new dirty value.

§Examples
use std::path::PathBuf;
use dir_structure::versioned_hash::VersionedHash;

let vh = VersionedHash::<String>::new_dirty(PathBuf::from("test.txt"), "Hello, world!".to_owned());
assert!(!vh.is_clean());
assert!(vh.is_dirty());
Source

pub fn is_clean(&self) -> bool

Checks if the value has been modified since being read. Returns true if the value is clean (i.e., has not been modified). Returns false if the value is dirty (i.e., has been modified).

§Examples
use std::path::PathBuf;
use dir_structure::versioned_hash::VersionedHash;

let vh = VersionedHash::<String>::new_clean(PathBuf::from("test.txt"), "Hello, world!".to_owned());
assert!(vh.is_clean());

let vh = VersionedHash::<String>::new_dirty(PathBuf::from("test.txt"), "Hello, world!".to_owned());
assert!(!vh.is_clean());
Source

pub fn is_dirty(&self) -> bool

Checks if the value has been modified since being read. Returns true if the value is dirty (i.e., has been modified). Returns false if the value is clean (i.e., has not been modified).

§Examples
use std::path::PathBuf;
use dir_structure::versioned_hash::VersionedHash;

let vh = VersionedHash::<String>::new_clean(PathBuf::from("test.txt"), "Hello, world!".to_owned());
assert!(!vh.is_dirty());

let vh = VersionedHash::<String>::new_dirty(PathBuf::from("test.txt"), "Hello, world!".to_owned());
assert!(vh.is_dirty());
Source

pub unsafe fn reset(&mut self)

Resets the hash to the current value’s hash.

§Safety

This function is unsafe because it can lead to changes not being written if the value has changed. Ideally this function should only be called after writing the value to disk.

§Examples
use std::path::PathBuf;
use dir_structure::versioned_hash::VersionedHash;

let mut vh = VersionedHash::<String>::new_clean(PathBuf::from("test.txt"), "Hello, world!".to_owned());
assert!(vh.is_clean());

vh.push_str(" Modified.");
assert!(vh.is_dirty());
assert!(!vh.is_clean());

unsafe {
  vh.reset();
}
assert!(vh.is_clean());
assert!(!vh.is_dirty());

Trait Implementations§

Source§

impl<T, P: PathType + ?Sized, H: Hasher + Default> AssertEq for VersionedHash<T, P, H>
where for<'__trivial> T: AssertEq<T> + Debug + Hash, for<'__trivial> u64: AssertEq<u64> + Debug, for<'__trivial> P::OwnedPath: AssertEq<P::OwnedPath> + Debug, for<'__trivial> PhantomData<H>: AssertEq<PhantomData<H>> + 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: Hash, P: PathType + ?Sized, H: Hasher + Default> Deref for VersionedHash<T, P, H>

Source§

type Target = T

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.
Source§

impl<T: Hash, P: PathType + ?Sized, H: Hasher + Default> DerefMut for VersionedHash<T, P, H>

Source§

fn deref_mut(&mut self) -> &mut Self::Target

Mutably dereferences the value.
Source§

impl<'a, T, H, Vfs: Vfs<'a>> ReadFrom<'a, Vfs> for VersionedHash<T, Vfs::Path, H>
where T: ReadFrom<'a, Vfs> + Hash + 'a, H: Hasher + Default + 'a,

Source§

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

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

impl<'a, T, H, Vfs: VfsAsync + 'a> ReadFromAsync<'a, Vfs> for VersionedHash<T, Vfs::Path, H>
where T: ReadFromAsync<'a, Vfs> + Hash + 'a, H: Hasher + Default + 'a,

Available on crate feature async only.
Source§

type Future = Pin<Box<dyn Future<Output = Result<VersionedHash<T, <Vfs as VfsCore>::Path, H>, Error<<<Vfs as VfsCore>::Path as PathType>::OwnedPath>>> + Send + 'a>> where Self: '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, H, Vfs: WriteSupportingVfs<'a>> WriteTo<'a, Vfs> for VersionedHash<T, Vfs::Path, H>
where T: WriteTo<'a, Vfs> + Hash, Vfs::Path: PartialEq, H: Hasher + Default,

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, P, H, Vfs: WriteSupportingVfsAsync<Path = P> + 'a> WriteToAsync<'a, Vfs> for VersionedHash<T, P, H>
where T: WriteToAsync<'a, Vfs> + Hash + 'a, P: PathType + ?Sized + PartialEq + 'a, H: Hasher + Default + 'a,

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>> where Self: '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, H, Vfs: WriteSupportingVfsAsync + 'a> WriteToAsyncRef<'a, Vfs> for VersionedHash<T, Vfs::Path, H>
where T: WriteToAsyncRef<'a, Vfs> + Hash + 'a, H: Hasher + Default + 'a,

Available on crate feature async only.
Source§

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

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

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

Asynchronously writes the structure to the specified path.

Auto Trait Implementations§

§

impl<T, P, H> Freeze for VersionedHash<T, P, H>
where T: Freeze, <P as PathType>::OwnedPath: Freeze, P: ?Sized,

§

impl<T, P, H> RefUnwindSafe for VersionedHash<T, P, H>

§

impl<T, P, H> Send for VersionedHash<T, P, H>
where T: Send, H: Send, P: ?Sized,

§

impl<T, P, H> Sync for VersionedHash<T, P, H>
where T: Sync, H: Sync, P: ?Sized,

§

impl<T, P, H> Unpin for VersionedHash<T, P, H>
where T: Unpin, <P as PathType>::OwnedPath: Unpin, H: Unpin, P: ?Sized,

§

impl<T, P, H> UnwindSafe for VersionedHash<T, P, H>

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> 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<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<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
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,