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:
State | DeferredReadOrOwn::get | DeferredReadOrOwn::perform_and_store_read |
---|---|---|
New, not cached | Reads the value, does not cache | Reads the value, and caches it |
Cached | Returns the cached value | Returns 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,
impl<T> DeferredReadOrOwn<T>where
T: ReadFrom,
Sourcepub fn get(&self) -> Result<T>where
T: Clone,
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(())
}
Sourcepub fn perform_and_store_read(&mut self) -> Result<&T>
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>
impl<T> Clone for DeferredReadOrOwn<T>
Source§fn clone(&self) -> DeferredReadOrOwn<T>
fn clone(&self) -> DeferredReadOrOwn<T>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more