DeferredReadOrOwn

Enum DeferredReadOrOwn 

Source
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:

StateDeferredReadOrOwn::getDeferredReadOrOwn::perform_and_store_read
New, not cachedReads the value, does not cacheReads the value, and caches it, returns reference
CachedClones the cached valueReturns 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§

§

Own(T)

An owned value.

§

Deferred(DeferredRead<'a, T, Vfs, CHECK_ON_READ>)

A deferred read.

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

Source

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(())
}
Source

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(())
}
Source

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,

Source

pub async fn get_async(&'a self) -> VfsResult<T, Vfs>
where T: Clone,

Available on crate feature async only.

Gets the value, asynchronously. This is an async version of get.

Source

pub async fn perform_and_store_read_async( &'a mut self, ) -> VfsResult<&'a mut T, Vfs>

Available on crate feature 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,

Source

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,

Available on crate feature 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>
where T: AssertEq, P: PathType + ?Sized, P::OwnedPath: AssertEq + Debug,

Source§

fn assert_eq( &self, other: &Self, path: &mut AssertPath, init_left: &impl Display, init_right: &impl Display, )

Asserts that self is equal to other, panicking if they are not equal. Read more
Source§

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>

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<'a, const CHECK_ON_READ: bool, T, Vfs: VfsCore<Path = P>, P> Debug for DeferredReadOrOwn<'a, T, Vfs, CHECK_ON_READ>
where T: Debug, P: PathType + ?Sized, P::OwnedPath: Debug,

Source§

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

Formats the value using the given formatter. Read more
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.
Source§

type Inner = <T as DynamicHasField>::Inner

The type of the field.
Source§

fn resolve_path<P: OwnedPathType>(p: P, name: &str) -> P

How to resolve the path for the field, from the path of 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>
where T: HasField<NAME>, Vfs: VfsCore,

Available on crate feature resolve-path only.
Source§

type Inner = <T as HasField<NAME>>::Inner

The type of the field.
Source§

fn resolve_path<P: OwnedPathType>(p: P) -> P

How to resolve the path for the field, from the path of Self.
Source§

impl<'a, T, Vfs: VfsCore, const CHECK_ON_READ: bool> Hash for DeferredReadOrOwn<'a, T, Vfs, CHECK_ON_READ>
where T: Hash, <Vfs::Path as PathType>::OwnedPath: 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<'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§

fn read_from(path: &Vfs::Path, vfs: Pin<&'a Vfs>) -> VfsResult<Self, Vfs>
where Self: Sized,

Reads the structure from the specified path, which can be either a file or a directory.
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>
where T: ReadFromAsync<'a, Vfs> + Send + 'static, P::OwnedPath: Send + Sync,

Available on crate feature async only.
Source§

type Future = Pin<Box<dyn Future<Output = Result<DeferredReadOrOwn<'a, T, Vfs, CHECK_ON_READ>, Error<<<Vfs as VfsCore>::Path as PathType>::OwnedPath>>> + Send + 'a>>

The future type returned by the async read function.
Source§

fn read_from_async(path: P::OwnedPath, vfs: Pin<&'a Vfs>) -> Self::Future

Asynchronously reads the structure from the specified path, which can be either a file or a directory.
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>
where T: ReadFrom<'a, SelfVfs> + WriteTo<'t, TargetVfs>,

Source§

fn write_to( &self, path: &P, vfs: Pin<&'t TargetVfs>, ) -> Result<(), P::OwnedPath>

Writes the structure to the specified path.
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.
Source§

type Future = DeferredReadOrOwnWriteFuture<'a, T, Vfs, CHECK_ON_READ>

The future type returned by the async write function.
Source§

fn write_to_async( self, path: <<Vfs as VfsCore>::Path as PathType>::OwnedPath, vfs: Pin<&'a Vfs>, ) -> Self::Future

Asynchronously writes the structure to the specified path.

Auto Trait Implementations§

§

impl<'a, T, Vfs, const CHECK_ON_READ: bool> Freeze for DeferredReadOrOwn<'a, T, Vfs, CHECK_ON_READ>
where T: Freeze, <<Vfs as VfsCore>::Path as PathType>::OwnedPath: Freeze,

§

impl<'a, T, Vfs, const CHECK_ON_READ: bool> RefUnwindSafe for DeferredReadOrOwn<'a, T, Vfs, CHECK_ON_READ>

§

impl<'a, T, Vfs, const CHECK_ON_READ: bool> Send for DeferredReadOrOwn<'a, T, Vfs, CHECK_ON_READ>
where T: Send, Vfs: Sync,

§

impl<'a, T, Vfs, const CHECK_ON_READ: bool> Sync for DeferredReadOrOwn<'a, T, Vfs, CHECK_ON_READ>
where T: Sync, Vfs: Sync,

§

impl<'a, T, Vfs, const CHECK_ON_READ: bool> Unpin for DeferredReadOrOwn<'a, T, Vfs, CHECK_ON_READ>
where T: Unpin, <<Vfs as VfsCore>::Path as PathType>::OwnedPath: Unpin,

§

impl<'a, T, Vfs, const CHECK_ON_READ: bool> UnwindSafe for DeferredReadOrOwn<'a, T, Vfs, CHECK_ON_READ>

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

Source§

fn read(path: impl AsRef<<FsVfs as VfsCore>::Path>) -> VfsResult<Self, FsVfs>
where Self: ReadFrom<'static, FsVfs> + Sized,

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

fn write<'a, 'vfs: 'a>( &'a self, path: impl AsRef<<FsVfs as VfsCore>::Path>, ) -> VfsResult<(), FsVfs>
where Self: WriteTo<'vfs, FsVfs>,

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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
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.
Source§

impl<T> ErasedDestructor for T
where T: 'static,