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>
impl<T, P: PathType + ?Sized> Versioned<T, P>
Sourcepub fn new(value: T, path: impl Into<P::OwnedPath>) -> Self
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.
Sourcepub fn is_dirty(&self) -> bool
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());Sourcepub fn is_clean(&self) -> bool
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());Sourcepub fn edit_eq_check(&mut self, f: impl FnOnce(&mut T))
pub fn edit_eq_check(&mut self, f: impl FnOnce(&mut T))
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());Sourcepub unsafe fn reset(&mut self)
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> DynamicHasField for Versioned<T, P>where
T: DynamicHasField,
Available on crate feature resolve-path only.
impl<T, P: PathType + ?Sized> DynamicHasField for Versioned<T, P>where
T: DynamicHasField,
resolve-path only.Source§type Inner = <T as DynamicHasField>::Inner
type Inner = <T as DynamicHasField>::Inner
Source§fn resolve_path<Pt: OwnedPathType>(p: Pt, name: &str) -> Pt
fn resolve_path<Pt: OwnedPathType>(p: Pt, name: &str) -> Pt
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.
impl<const NAME: [char; 32], T, P: PathType + ?Sized> HasField<NAME> for Versioned<T, P>where
T: HasField<NAME>,
resolve-path only.Source§fn resolve_path<Pt: OwnedPathType>(p: Pt) -> Pt
fn resolve_path<Pt: OwnedPathType>(p: Pt) -> Pt
Self.Source§impl<'a, Vfs: Vfs<'a>, T> ReadFrom<'a, Vfs> for Versioned<T, Vfs::Path>where
T: ReadFrom<'a, Vfs>,
impl<'a, Vfs: Vfs<'a>, T> ReadFrom<'a, Vfs> for Versioned<T, Vfs::Path>where
T: ReadFrom<'a, Vfs>,
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.
impl<'a, Vfs: VfsAsync + 'static, T: ReadFromAsync<'a, Vfs> + Send + 'static> ReadFromAsync<'a, Vfs> for Versioned<T, Vfs::Path>
async only.Source§impl<'a, Vfs: WriteSupportingVfs<'a>, T: WriteTo<'a, Vfs>> WriteTo<'a, Vfs> for Versioned<T, Vfs::Path>
impl<'a, Vfs: WriteSupportingVfs<'a>, T: WriteTo<'a, Vfs>> WriteTo<'a, Vfs> for Versioned<T, Vfs::Path>
Source§impl<'a, T, Vfs: WriteSupportingVfsAsync + 'static> WriteToAsync<'a, Vfs> for Versioned<T, Vfs::Path>
Available on crate feature async only.
impl<'a, T, Vfs: WriteSupportingVfsAsync + 'static> WriteToAsync<'a, Vfs> for Versioned<T, Vfs::Path>
async only.Source§impl<'r, T, Vfs: WriteSupportingVfsAsync + 'static> WriteToAsyncRef<'r, Vfs> for Versioned<T, Vfs::Path>
Available on crate feature async only.
impl<'r, T, Vfs: WriteSupportingVfsAsync + 'static> WriteToAsyncRef<'r, Vfs> for Versioned<T, Vfs::Path>
async only.impl<T: Eq, P: Eq + PathType + ?Sized> Eq for Versioned<T, P>
impl<T, P: PathType + ?Sized> StructuralPartialEq for Versioned<T, P>
Auto Trait Implementations§
impl<T, P> Freeze for Versioned<T, P>
impl<T, P> RefUnwindSafe for Versioned<T, P>
impl<T, P> Send for Versioned<T, P>
impl<T, P> Sync for Versioned<T, P>
impl<T, P> Unpin for Versioned<T, P>
impl<T, P> UnwindSafe for Versioned<T, P>
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
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
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>
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>
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