Struct Versioned

Source
pub struct Versioned<T: DirStructureItem> { /* 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 dir_structure::VersionedString;

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

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

Implementations§

Source§

impl<T: DirStructureItem> Versioned<T>

Source

pub fn new(value: T, path: impl Into<PathBuf>) -> 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<PathBuf>) -> Self

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

§Example
use dir_structure::VersionedString;

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

pub fn is_dirty(&self) -> bool

Checks if the value has been changed.

Source

pub fn is_clean(&self) -> bool

Checks if the value has not been changed.

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 dir_structure::VersionedString;

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

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());

Trait Implementations§

Source§

impl<T: Clone + DirStructureItem> Clone for Versioned<T>

Source§

fn clone(&self) -> Versioned<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 + DirStructureItem> Debug for Versioned<T>

Source§

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

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

impl<T: DirStructureItem> Deref for Versioned<T>

Source§

type Target = T

The resulting type after dereferencing.
Source§

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

Dereferences the value.
Source§

impl<T: DirStructureItem> DerefMut for Versioned<T>

Source§

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

Mutably dereferences the value.
Source§

impl<T: Hash + DirStructureItem> Hash for Versioned<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: DirStructureItem> ReadFrom for Versioned<T>

Source§

fn read_from(path: &Path) -> Result<Self>
where Self: Sized,

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

impl<T: DirStructureItem> WriteTo for Versioned<T>

Source§

fn write_to(&self, path: &Path) -> Result<()>

Writes the structure to the specified path.

Auto Trait Implementations§

§

impl<T> Freeze for Versioned<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for Versioned<T>
where T: RefUnwindSafe,

§

impl<T> Send for Versioned<T>
where T: Send,

§

impl<T> Sync for Versioned<T>
where T: Sync,

§

impl<T> Unpin for Versioned<T>
where T: Unpin,

§

impl<T> UnwindSafe for Versioned<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<T> DirStructureItem for T
where T: ReadFrom + WriteTo,

Source§

fn read(path: impl AsRef<Path>) -> Result<Self>
where Self: Sized,

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

fn write(&self, path: impl AsRef<Path>) -> Result<()>

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