pub enum DeferredReadOrOwn<'a, T, Vfs: VfsCore = FsVfs, const CHECK_ON_READ: bool = false> {
Own(T),
Deferred(DeferredRead<'a, T, Vfs, CHECK_ON_READ>),
}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, returns reference |
| Cached | Clones the cached value | Returns a reference to 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<&mut 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§
Implementations§
Source§impl<'a, const CHECK_ON_READ: bool, T, Vfs: Vfs<'a, Path = P>, P: PathType + ?Sized + 'a> DeferredReadOrOwn<'a, T, Vfs, CHECK_ON_READ>where
T: ReadFrom<'a, Vfs>,
impl<'a, const CHECK_ON_READ: bool, T, Vfs: Vfs<'a, Path = P>, P: PathType + ?Sized + 'a> DeferredReadOrOwn<'a, T, Vfs, CHECK_ON_READ>where
T: ReadFrom<'a, Vfs>,
Sourcepub fn get(&self) -> VfsResult<T, Vfs>where
T: Clone,
pub fn get(&self) -> VfsResult<T, Vfs>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 std::pin::Pin;
use dir_structure::traits::sync::DirStructureItem;
use dir_structure::deferred_read::DeferredRead;
use dir_structure::deferred_read_or_own::DeferredReadOrOwn;
use dir_structure::prelude::*;
use dir_structure::vfs::fs_vfs::FsVfs;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let d = Path::new("dir");
std::fs::create_dir_all(&d)?;
let deferred = DeferredReadOrOwn::<String, FsVfs>::Deferred(
DeferredRead::read_from(d.join("f.txt").as_ref(), Pin::new(&FsVfs)).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) -> VfsResult<&mut T, Vfs>
pub fn perform_and_store_read(&mut self) -> VfsResult<&mut T, Vfs>
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 std::pin::Pin;
use dir_structure::traits::sync::DirStructureItem;
use dir_structure::deferred_read::DeferredRead;
use dir_structure::deferred_read_or_own::DeferredReadOrOwn;
use dir_structure::prelude::*;
use dir_structure::vfs::fs_vfs::FsVfs;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let d = Path::new("dir");
std::fs::create_dir_all(&d)?;
let mut deferred = DeferredReadOrOwn::<String, FsVfs>::Deferred(
DeferredRead::read_from(d.join("f.txt").as_ref(), Pin::new(&FsVfs)).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(())
}Sourcepub fn flush_to<'t, TargetVfs: WriteSupportingVfs<'t, Path = P>>(
&self,
path: &P,
vfs: Pin<&'t TargetVfs>,
) -> VfsResult<(), Vfs>where
T: WriteTo<'t, TargetVfs>,
pub fn flush_to<'t, TargetVfs: WriteSupportingVfs<'t, Path = P>>(
&self,
path: &P,
vfs: Pin<&'t TargetVfs>,
) -> VfsResult<(), Vfs>where
T: WriteTo<'t, TargetVfs>,
Flushes the current value to the specified path.
Source§impl<'a, const CHECK_ON_READ: bool, T, Vfs: VfsAsync<Path = P> + 'a, P: PathType + ?Sized + 'a> DeferredReadOrOwn<'a, T, Vfs, CHECK_ON_READ>where
T: ReadFromAsync<'a, Vfs> + Send + 'static,
impl<'a, const CHECK_ON_READ: bool, T, Vfs: VfsAsync<Path = P> + 'a, P: PathType + ?Sized + 'a> DeferredReadOrOwn<'a, T, Vfs, CHECK_ON_READ>where
T: ReadFromAsync<'a, Vfs> + Send + 'static,
Sourcepub async fn get_async(&'a self) -> VfsResult<T, Vfs>where
T: Clone,
Available on crate feature async only.
pub async fn get_async(&'a self) -> VfsResult<T, Vfs>where
T: Clone,
async only.Gets the value, asynchronously. This is an async version of get.
Sourcepub async fn perform_and_store_read_async(
&'a mut self,
) -> VfsResult<&'a mut T, Vfs>
Available on crate feature async only.
pub async fn perform_and_store_read_async( &'a mut self, ) -> VfsResult<&'a mut T, Vfs>
async only.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.
This is an async version of perform_and_store_read.
Source§impl<'a, const CHECK_ON_READ: bool, T, Vfs: VfsAsync<Path = P> + 'a, P: PathType + ?Sized + 'a> DeferredReadOrOwn<'a, T, Vfs, CHECK_ON_READ>where
P::OwnedPath: 'a,
impl<'a, const CHECK_ON_READ: bool, T, Vfs: VfsAsync<Path = P> + 'a, P: PathType + ?Sized + 'a> DeferredReadOrOwn<'a, T, Vfs, CHECK_ON_READ>where
P::OwnedPath: 'a,
Sourcepub async fn flush_to_async<TargetVfs: WriteSupportingVfsAsync<Path = P> + 'a, ReadFutTy>(
&'a self,
path: P::OwnedPath,
vfs: Pin<&'a TargetVfs>,
) -> VfsResult<(), Vfs>where
for<'b> T: ReadFromAsync<'b, Vfs, Future = ReadFutTy> + WriteToAsync<'b, TargetVfs> + WriteToAsyncRef<'b, TargetVfs> + Send + 'b,
ReadFutTy: Future<Output = VfsResult<T, Vfs>> + Unpin + 'static,
Available on crate feature async only.
pub async fn flush_to_async<TargetVfs: WriteSupportingVfsAsync<Path = P> + 'a, ReadFutTy>(
&'a self,
path: P::OwnedPath,
vfs: Pin<&'a TargetVfs>,
) -> VfsResult<(), Vfs>where
for<'b> T: ReadFromAsync<'b, Vfs, Future = ReadFutTy> + WriteToAsync<'b, TargetVfs> + WriteToAsyncRef<'b, TargetVfs> + Send + 'b,
ReadFutTy: Future<Output = VfsResult<T, Vfs>> + Unpin + 'static,
async only.Flushes the current value to the specified path. Async version of flush_to.
Trait Implementations§
Source§impl<'a, T, Vfs: VfsCore<Path = P>, P, const CHECK_ON_READ: bool> AssertEq for DeferredReadOrOwn<'a, T, Vfs, CHECK_ON_READ>
impl<'a, T, Vfs: VfsCore<Path = P>, P, const CHECK_ON_READ: bool> AssertEq for DeferredReadOrOwn<'a, T, Vfs, CHECK_ON_READ>
Source§impl<'a, T: Clone, Vfs: Clone + VfsCore, const CHECK_ON_READ: bool> Clone for DeferredReadOrOwn<'a, T, Vfs, CHECK_ON_READ>
impl<'a, T: Clone, Vfs: Clone + VfsCore, const CHECK_ON_READ: bool> Clone for DeferredReadOrOwn<'a, T, Vfs, CHECK_ON_READ>
Source§fn clone(&self) -> DeferredReadOrOwn<'a, T, Vfs, CHECK_ON_READ>
fn clone(&self) -> DeferredReadOrOwn<'a, T, Vfs, CHECK_ON_READ>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl<'a, const CHECK_ON_READ: bool, T, Vfs: VfsCore<Path = P>, P> Debug for DeferredReadOrOwn<'a, T, Vfs, CHECK_ON_READ>
impl<'a, const CHECK_ON_READ: bool, T, Vfs: VfsCore<Path = P>, P> Debug for DeferredReadOrOwn<'a, T, Vfs, CHECK_ON_READ>
Source§impl<'a, const CHECK_ON_READ: bool, T, Vfs> DynamicHasField for DeferredReadOrOwn<'a, T, Vfs, CHECK_ON_READ>where
T: DynamicHasField,
Vfs: VfsCore,
Available on crate feature resolve-path only.
impl<'a, const CHECK_ON_READ: bool, T, Vfs> DynamicHasField for DeferredReadOrOwn<'a, T, Vfs, CHECK_ON_READ>where
T: DynamicHasField,
Vfs: VfsCore,
resolve-path only.Source§type Inner = <T as DynamicHasField>::Inner
type Inner = <T as DynamicHasField>::Inner
Source§fn resolve_path<P: OwnedPathType>(p: P, name: &str) -> P
fn resolve_path<P: OwnedPathType>(p: P, name: &str) -> P
Self, given the name
passed into the resolve_path! macro.Source§impl<'a, const CHECK_ON_READ: bool, const NAME: [char; 32], T, Vfs> HasField<NAME> for DeferredReadOrOwn<'a, T, Vfs, CHECK_ON_READ>
Available on crate feature resolve-path only.
impl<'a, const CHECK_ON_READ: bool, const NAME: [char; 32], T, Vfs> HasField<NAME> for DeferredReadOrOwn<'a, T, Vfs, CHECK_ON_READ>
resolve-path only.Source§fn resolve_path<P: OwnedPathType>(p: P) -> P
fn resolve_path<P: OwnedPathType>(p: P) -> P
Self.Source§impl<'a, T, Vfs: VfsCore, const CHECK_ON_READ: bool> Hash for DeferredReadOrOwn<'a, T, Vfs, CHECK_ON_READ>
impl<'a, T, Vfs: VfsCore, const CHECK_ON_READ: bool> Hash for DeferredReadOrOwn<'a, T, Vfs, CHECK_ON_READ>
Source§impl<'a, const CHECK_ON_READ: bool, T, Vfs: Vfs<'a>> ReadFrom<'a, Vfs> for DeferredReadOrOwn<'a, T, Vfs, CHECK_ON_READ>where
T: ReadFrom<'a, Vfs>,
impl<'a, const CHECK_ON_READ: bool, T, Vfs: Vfs<'a>> ReadFrom<'a, Vfs> for DeferredReadOrOwn<'a, T, Vfs, CHECK_ON_READ>where
T: ReadFrom<'a, Vfs>,
Source§impl<'a, const CHECK_ON_READ: bool, T, Vfs: VfsAsync<Path = P> + 'static, P: PathType + ?Sized + 'a> ReadFromAsync<'a, Vfs> for DeferredReadOrOwn<'a, T, Vfs, CHECK_ON_READ>
Available on crate feature async only.
impl<'a, const CHECK_ON_READ: bool, T, Vfs: VfsAsync<Path = P> + 'static, P: PathType + ?Sized + 'a> ReadFromAsync<'a, Vfs> for DeferredReadOrOwn<'a, T, Vfs, CHECK_ON_READ>
async only.Source§impl<'a, 't, const CHECK_ON_READ: bool, T, P: PathType + ?Sized + 'a, SelfVfs: Vfs<'a, Path = P>, TargetVfs: WriteSupportingVfs<'t, Path = P>> WriteTo<'t, TargetVfs> for DeferredReadOrOwn<'a, T, SelfVfs, CHECK_ON_READ>
impl<'a, 't, const CHECK_ON_READ: bool, T, P: PathType + ?Sized + 'a, SelfVfs: Vfs<'a, Path = P>, TargetVfs: WriteSupportingVfs<'t, Path = P>> WriteTo<'t, TargetVfs> for DeferredReadOrOwn<'a, T, SelfVfs, CHECK_ON_READ>
Source§impl<'a, const CHECK_ON_READ: bool, T, Vfs: WriteSupportingVfsAsync + 'static> WriteToAsync<'a, Vfs> for DeferredReadOrOwn<'a, T, Vfs, CHECK_ON_READ>where
T: for<'b> ReadFromAsync<'b, Vfs> + for<'b> WriteToAsync<'b, Vfs> + Send + 'static,
for<'b> <T as ReadFromAsync<'b, Vfs>>::Future: Future<Output = VfsResult<T, Vfs>> + Unpin + 'b,
for<'b> <T as WriteToAsync<'b, Vfs>>::Future: Future<Output = VfsResult<(), Vfs>> + Unpin + 'b,
Available on crate feature async only.
impl<'a, const CHECK_ON_READ: bool, T, Vfs: WriteSupportingVfsAsync + 'static> WriteToAsync<'a, Vfs> for DeferredReadOrOwn<'a, T, Vfs, CHECK_ON_READ>where
T: for<'b> ReadFromAsync<'b, Vfs> + for<'b> WriteToAsync<'b, Vfs> + Send + 'static,
for<'b> <T as ReadFromAsync<'b, Vfs>>::Future: Future<Output = VfsResult<T, Vfs>> + Unpin + 'b,
for<'b> <T as WriteToAsync<'b, Vfs>>::Future: Future<Output = VfsResult<(), Vfs>> + Unpin + 'b,
async only.Auto Trait Implementations§
impl<'a, T, Vfs, const CHECK_ON_READ: bool> Freeze for DeferredReadOrOwn<'a, T, Vfs, CHECK_ON_READ>
impl<'a, T, Vfs, const CHECK_ON_READ: bool> RefUnwindSafe for DeferredReadOrOwn<'a, T, Vfs, CHECK_ON_READ>where
T: RefUnwindSafe,
<<Vfs as VfsCore>::Path as PathType>::OwnedPath: RefUnwindSafe,
Vfs: RefUnwindSafe,
impl<'a, T, Vfs, const CHECK_ON_READ: bool> Send for DeferredReadOrOwn<'a, T, Vfs, CHECK_ON_READ>
impl<'a, T, Vfs, const CHECK_ON_READ: bool> Sync for DeferredReadOrOwn<'a, T, Vfs, CHECK_ON_READ>
impl<'a, T, Vfs, const CHECK_ON_READ: bool> Unpin for DeferredReadOrOwn<'a, T, Vfs, CHECK_ON_READ>
impl<'a, T, Vfs, const CHECK_ON_READ: bool> UnwindSafe for DeferredReadOrOwn<'a, T, Vfs, CHECK_ON_READ>where
T: UnwindSafe,
<<Vfs as VfsCore>::Path as PathType>::OwnedPath: UnwindSafe,
Vfs: RefUnwindSafe,
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<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