DeferredRead

Struct DeferredRead 

Source
pub struct DeferredRead<'a, T, Vfs: VfsCore = FsVfs, const CHECK_ON_READ: bool = false>(pub <<Vfs as VfsCore>::Path as PathType>::OwnedPath, _, _);
Expand description

A wrapper that defers the reading of a file until it is actually needed.

The only thing you can do with a DeferredRead is to call DeferredRead::perform_read, which will read the file and return the value.

See the DeferredRead::perform_read method for more details.

For a version that also caches the read value, see DeferredReadOrOwn.

Tuple Fields§

§0: <<Vfs as VfsCore>::Path as PathType>::OwnedPath

Implementations§

Source§

impl<'a, const CHECK_ON_READ: bool, T, Vfs: Vfs<'a, Path = P>, P: PathType + ?Sized + 'a> DeferredRead<'a, T, Vfs, CHECK_ON_READ>
where T: ReadFrom<'a, Vfs>,

Source

pub fn perform_read(&self) -> VfsResult<T, Vfs>

Performs the read and returns the value.

If the value changed on disk since the DeferredRead was created, then the new value will be read from disk and returned.

For a cached version see DeferredReadOrOwn.

§Examples
use std::path::Path;
use std::pin::Pin;
use dir_structure::traits::sync::DirStructureItem;
use dir_structure::deferred_read::DeferredRead;
use dir_structure::prelude::*;

#[derive(dir_structure::DirStructure)]
struct Dir<'vfs, Vfs: VfsCore> {
    #[dir_structure(path = "f.txt")]
    f: DeferredRead<'vfs, String, Vfs>,
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let d = Path::new("dir");

    std::fs::create_dir_all(&d)?;
    std::fs::write(d.join("f.txt"), "Hello, world!")?;

    let dir = Dir::read(&d)?;
    assert_eq!(dir.f.perform_read()?, "Hello, world!");

    std::fs::write(d.join("f.txt"), "Goodbye, world!")?;
    assert_eq!(dir.f.perform_read()?, "Goodbye, world!");

    Ok(())
}
Source§

impl<'a, const CHECK_ON_READ: bool, T, Vfs: VfsAsync<Path = P> + 'a, P: PathType + ?Sized + 'a> DeferredRead<'a, T, Vfs, CHECK_ON_READ>
where T: ReadFromAsync<'a, Vfs> + Send + 'static,

Source

pub async fn perform_read_async(&self) -> VfsResult<T, Vfs>

Available on crate feature async only.

Performs the read asynchronously and returns the value.

If the value changed on disk since the DeferredRead was created, then the new value will be read from disk and returned.

For a cached version see DeferredReadOrOwn.

Asynchronous version of DeferredRead::perform_read.

Trait Implementations§

Source§

impl<'a, T, Vfs: VfsCore, const CHECK_ON_READ: bool> AssertEq for DeferredRead<'a, T, Vfs, CHECK_ON_READ>
where for<'__trivial> <<Vfs as VfsCore>::Path as PathType>::OwnedPath: AssertEq<<<Vfs as VfsCore>::Path as PathType>::OwnedPath> + 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 DeferredRead<'a, T, Vfs, CHECK_ON_READ>

Source§

fn clone(&self) -> DeferredRead<'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 DeferredRead<'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<const CHECK_ON_READ: bool, T, Vfs: VfsCore> DynamicHasField for DeferredRead<'_, T, Vfs, CHECK_ON_READ>
where T: DynamicHasField,

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<const CHECK_ON_READ: bool, const NAME: [char; 32], T, Vfs: VfsCore> HasField<NAME> for DeferredRead<'_, T, Vfs, CHECK_ON_READ>
where T: HasField<NAME>,

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, const CHECK_ON_READ: bool, T, Vfs: VfsCore> Hash for DeferredRead<'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 DeferredRead<'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 DeferredRead<'a, T, Vfs, CHECK_ON_READ>
where T: Send + ReadFromAsync<'a, Vfs> + 'static,

Available on crate feature async only.
Source§

type Future = Pin<Box<dyn Future<Output = Result<DeferredRead<'a, T, Vfs, CHECK_ON_READ>, Error<<<Vfs as VfsCore>::Path as PathType>::OwnedPath>>> + Send + 'a>> where Self: '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 DeferredRead<'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<'f, const CHECK_ON_READ: bool, T, P: PathType + ?Sized + 'f, SelfVfs: VfsAsync<Path = P> + 'f, TargetVfs: WriteSupportingVfsAsync<Path = P> + 'f> WriteToAsync<'f, TargetVfs> for DeferredRead<'f, T, SelfVfs, CHECK_ON_READ>
where T: for<'a> ReadFromAsync<'a, SelfVfs> + for<'a> WriteToAsync<'a, TargetVfs> + Send + 'static, for<'a> <T as ReadFromAsync<'a, SelfVfs>>::Future: Future<Output = VfsResult<T, SelfVfs>> + Unpin + 'a, for<'a> <T as WriteToAsync<'a, TargetVfs>>::Future: Future<Output = VfsResult<(), TargetVfs>> + Unpin + 'a,

Available on crate feature async only.
Source§

type Future = DeferredReadWriteFuture<'f, T, P, SelfVfs, TargetVfs>

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

fn write_to_async( self, path: P::OwnedPath, vfs: Pin<&'f TargetVfs>, ) -> Self::Future

Asynchronously writes the structure to the specified path.
Source§

impl<'f, const CHECK_ON_READ: bool, T, P: PathType + ?Sized + 'f, SelfVfs: VfsAsync<Path = P> + 'f, TargetVfs: WriteSupportingVfsAsync<Path = P> + 'f> WriteToAsyncRef<'f, TargetVfs> for DeferredRead<'f, T, SelfVfs, CHECK_ON_READ>
where for<'a> T: ReadFromAsync<'a, SelfVfs> + WriteToAsync<'a, TargetVfs> + Send + 'a, for<'a> <T as ReadFromAsync<'a, SelfVfs>>::Future: Future<Output = VfsResult<T, SelfVfs>> + Unpin + 'a, for<'a> <T as WriteToAsync<'a, TargetVfs>>::Future: Future<Output = VfsResult<(), TargetVfs>> + Unpin + 'a,

Available on crate feature async only.
Source§

type Future<'a> = DeferredReadWriteFuture<'a, T, P, SelfVfs, TargetVfs> where Self: 'a, 'f: 'a

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

fn write_to_async_ref<'a>( &'a self, path: P::OwnedPath, vfs: Pin<&'a TargetVfs>, ) -> Self::Future<'a>
where 'f: 'a,

Asynchronously writes the structure to the specified path.

Auto Trait Implementations§

§

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

§

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

§

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

§

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

§

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

§

impl<'a, T, Vfs, const CHECK_ON_READ: bool> UnwindSafe for DeferredRead<'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,