Enum DeferredReadOrOwn

Source
pub enum DeferredReadOrOwn<T>
where T: ReadFrom,
{ Own(T), Deferred(DeferredRead<T>), }
Expand description

A wrapper that defers the reading of a file until it is actually needed, but can also store the value.

It allows us to read the value from disk, and then store it in memory, and if we ever need it again, we can just return the stored value.

This type exposes 2 functions: DeferredReadOrOwn::get and DeferredReadOrOwn::perform_and_store_read.

The table below summarizes the differences between the two functions:

StateDeferredReadOrOwn::getDeferredReadOrOwn::perform_and_store_read
New, not cachedReads the value, does not cacheReads the value, and caches it
CachedReturns the cached valueReturns the cached value

As such, DeferredReadOrOwn::get has the signature of fn(&self) -> Result<T> and DeferredReadOrOwn::perform_and_store_read has the signature of fn(&mut self) -> Result<&T>.

If you never call DeferredReadOrOwn::perform_and_store_read, and only ever call DeferredReadOrOwn::get, that would effectively be the same as using a DeferredRead, and that should be preferred instead.

Variants§

§

Own(T)

§

Deferred(DeferredRead<T>)

Implementations§

Source§

impl<T> DeferredReadOrOwn<T>
where T: ReadFrom,

Source

pub fn get(&self) -> Result<T>
where T: Clone,

Gets the value. If it is not already read, it will read it, but without saving it.

This is useful if you want to read the value, but you don’t want to store it.

Though never calling DeferredReadOrOwn::perform_and_store_read and only calling DeferredReadOrOwn::get is equivalent to using a DeferredRead, and that should be preferred.

See DeferredReadOrOwn for more details.

§Examples
use std::path::Path;
use dir_structure::DirStructureItem;
use dir_structure::DeferredRead;
use dir_structure::DeferredReadOrOwn;
use dir_structure::ReadFrom;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let d = Path::new("dir");
    std::fs::create_dir_all(&d)?;
    let deferred = DeferredReadOrOwn::<String>::Deferred(
        DeferredRead::read_from(&d.join("f.txt")).unwrap()
    );
    assert!(deferred.get().is_err());
    std::fs::write(d.join("f.txt"), "Hello, world!")?;
    assert_eq!(deferred.get()?, "Hello, world!");
    std::fs::write(d.join("f.txt"), "Goodbye, world!")?;
    assert_eq!(deferred.get()?, "Goodbye, world!");
    Ok(())
}
Source

pub fn perform_and_store_read(&mut self) -> Result<&T>

Performs the read and stores the value. If the value is already read, it will just return a reference to it.

See DeferredReadOrOwn for more details.

§Examples
use std::path::Path;
use dir_structure::DirStructureItem;
use dir_structure::DeferredRead;
use dir_structure::DeferredReadOrOwn;
use dir_structure::ReadFrom;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let d = Path::new("dir");
    std::fs::create_dir_all(&d)?;
    let mut deferred = DeferredReadOrOwn::<String>::Deferred(
        DeferredRead::read_from(&d.join("f.txt")).unwrap()
    );
    assert!(deferred.perform_and_store_read().is_err());
    std::fs::write(d.join("f.txt"), "Hello, world!")?;
    assert_eq!(deferred.perform_and_store_read()?, "Hello, world!");
    std::fs::write(d.join("f.txt"), "Goodbye, world!")?;
    assert_eq!(deferred.perform_and_store_read()?, "Hello, world!");
    Ok(())
}

Trait Implementations§

Source§

impl<T> Clone for DeferredReadOrOwn<T>
where T: ReadFrom + Clone,

Source§

fn clone(&self) -> DeferredReadOrOwn<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 for DeferredReadOrOwn<T>
where T: ReadFrom + Debug,

Source§

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

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

impl<T> Hash for DeferredReadOrOwn<T>
where T: ReadFrom + 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> ReadFrom for DeferredReadOrOwn<T>
where T: ReadFrom,

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> WriteTo for DeferredReadOrOwn<T>
where T: ReadFrom + WriteTo,

Source§

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

Writes the structure to the specified path.

Auto Trait Implementations§

§

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

§

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

§

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

§

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

§

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

§

impl<T> UnwindSafe for DeferredReadOrOwn<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<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.