Versioned

Struct Versioned 

Source
pub struct Versioned<T, P: PathType + ?Sized = Path> { /* private fields */ }
Expand description

A versioned value. This is a wrapper around a value that will keep track of how many times it has been changed. This is useful to not write the value to disk if it hasn’t changed.

You can get a reference to the value via its Deref implementation, and you can get a mutable reference to the value via its DerefMut implementation.

The version is incremented every time DerefMut::deref_mut is called.

Alternatively, for Eq types, you can use the Versioned::edit_eq_check method to edit the value, and it will increment the version if the value has changed.

§Example

use std::path::Path;
use dir_structure::versioned::VersionedString;

let mut v = VersionedString::<Path>::new("value".to_owned(), "path".to_owned());
assert!(v.is_clean());
assert!(!v.is_dirty());

*v = "new value".to_owned();
assert!(v.is_dirty());

Implementations§

Source§

impl<T, P: PathType + ?Sized> Versioned<T, P>

Source

pub fn new(value: T, path: impl Into<P::OwnedPath>) -> Self

Creates a new Versioned with the specified value.

The version is set to the default value.

Source

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

Creates a new Versioned with the specified value, and in a dirty state.

§Example
use std::path::Path;
use dir_structure::versioned::VersionedString;

let v = VersionedString::<Path>::new_dirty("value".to_owned(), "path".to_owned());
assert!(v.is_dirty());
Source

pub fn is_dirty(&self) -> bool

Checks if the value has been changed.

§Example
use std::path::Path;
use dir_structure::versioned::VersionedString;

let mut v = VersionedString::<Path>::new("value".to_owned(), "path".to_owned());
assert!(!v.is_dirty());
*v = "new value".to_owned();
assert!(v.is_dirty());
Source

pub fn is_clean(&self) -> bool

Checks if the value has not been changed.

§Example
use std::path::Path;
use dir_structure::versioned::VersionedString;

let mut v = VersionedString::<Path>::new("value".to_owned(), "path".to_owned());
assert!(v.is_clean());
*v = "new value".to_owned();
assert!(!v.is_clean());
Source

pub fn edit_eq_check(&mut self, f: impl FnOnce(&mut T))
where T: Eq + Clone,

Edits the value using the provided closure, and increments the version if the value has changed.

§Example
use std::path::Path;
use dir_structure::versioned::VersionedString;

let mut v = VersionedString::<Path>::new("value".to_owned(), "path".to_owned());

v.edit_eq_check(|s| *s = "value".to_owned());
assert!(v.is_clean());
v.edit_eq_check(|s| *s = "new value".to_owned());
assert!(v.is_dirty());
Source

pub unsafe fn reset(&mut self)

Resets the version to the default value, making the value clean. This is useful if you want to mark the value as not changed, without actually changing it.

§Safety

This function is unsafe because it allows you to reset the version to 0, which means that the value will be considered clean, and any unsaved changes will be lost. Trying to save a clean value (e.g. after calling this function) will not write it to disk!

Use with caution!

§Examples
use std::path::Path;
use dir_structure::{traits::sync::DirStructureItem, versioned::VersionedString};
std::fs::write("path", "value").unwrap();

let mut v = VersionedString::<Path>::new("value".to_owned(), "path".to_owned());
assert!(v.is_clean());
v.edit_eq_check(|s| *s = "new value".to_owned());
assert!(v.is_dirty());
unsafe { v.reset(); }
assert!(v.is_clean());

// if you try to write it now, it won't write anything,
v.write("path").unwrap();

assert_eq!(std::fs::read_to_string("path").unwrap(), "value");

Trait Implementations§

Source§

impl<T, P: PathType + ?Sized> AssertEq for Versioned<T, P>
where for<'__trivial> T: AssertEq<T> + Debug, for<'__trivial> usize: AssertEq<usize> + Debug, for<'__trivial> P::OwnedPath: AssertEq<P::OwnedPath> + 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, P: PathType + ?Sized> Clone for Versioned<T, P>
where T: Clone, P::OwnedPath: Clone,

Source§

fn clone(&self) -> Self

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, P: Debug + PathType + ?Sized> Debug for Versioned<T, P>
where P::OwnedPath: Debug,

Source§

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

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

impl<T, P: PathType + ?Sized> Deref for Versioned<T, P>

Source§

type Target = T

The resulting type after dereferencing.
Source§

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

Dereferences the value.
Source§

impl<T, P: PathType + ?Sized> DerefMut for Versioned<T, P>

Source§

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

Mutably dereferences the value.
Source§

impl<T, P: PathType + ?Sized> DynamicHasField for Versioned<T, P>
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<Pt: OwnedPathType>(p: Pt, name: &str) -> Pt

How to resolve the path for the field, from the path of Self, given the name passed into the resolve_path! macro.
Source§

impl<const NAME: [char; 32], T, P: PathType + ?Sized> HasField<NAME> for Versioned<T, P>
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<Pt: OwnedPathType>(p: Pt) -> Pt

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

impl<T: Hash, P: Hash + PathType + ?Sized> Hash for Versioned<T, P>
where P::OwnedPath: Hash,

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: PartialEq, P: PartialEq + PathType + ?Sized> PartialEq for Versioned<T, P>
where P::OwnedPath: PartialEq,

Source§

fn eq(&self, other: &Versioned<T, P>) -> 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<'a, Vfs: Vfs<'a>, T> ReadFrom<'a, Vfs> for Versioned<T, Vfs::Path>
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, Vfs: VfsAsync + 'static, T: ReadFromAsync<'a, Vfs> + Send + 'static> ReadFromAsync<'a, Vfs> for Versioned<T, Vfs::Path>

Available on crate feature async only.
Source§

type Future = VersionedReadFuture<'a, Vfs, T>

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, Vfs: WriteSupportingVfs<'a>, T: WriteTo<'a, Vfs>> WriteTo<'a, Vfs> for Versioned<T, Vfs::Path>
where Vfs::Path: PartialEq,

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 Versioned<T, Vfs::Path>
where T: WriteToAsync<'a, Vfs> + Send + Sync + 'static, <T as WriteToAsync<'a, Vfs>>::Future: Future<Output = VfsResult<(), Vfs>> + Unpin, Vfs::Path: PartialEq,

Available on crate feature async only.
Source§

type Future = VersionedWriteFuture<'a, T, Vfs>

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<'r, T, Vfs: WriteSupportingVfsAsync + 'static> WriteToAsyncRef<'r, Vfs> for Versioned<T, Vfs::Path>
where T: WriteToAsyncRef<'r, Vfs> + Send + Sync + 'static, for<'f> <T as WriteToAsyncRef<'r, Vfs>>::Future<'f>: Future<Output = VfsResult<(), Vfs>> + Unpin + 'f, Vfs::Path: PartialEq,

Available on crate feature async only.
Source§

type Future<'a> = VersionedWriteRefFuture<'r, 'a, T, Vfs> where Self: 'a, Vfs: 'a, 'r: 'a

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

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

Asynchronously writes the structure to the specified path.
Source§

impl<T: Eq, P: Eq + PathType + ?Sized> Eq for Versioned<T, P>
where P::OwnedPath: Eq,

Source§

impl<T, P: PathType + ?Sized> StructuralPartialEq for Versioned<T, P>

Auto Trait Implementations§

§

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

§

impl<T, P> RefUnwindSafe for Versioned<T, P>

§

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

§

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

§

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

§

impl<T, P> UnwindSafe for Versioned<T, P>
where T: UnwindSafe, <P as PathType>::OwnedPath: UnwindSafe, P: ?Sized,

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<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<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> 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,