Struct DeferredRead

Source
pub struct DeferredRead<T>(pub PathBuf, _)
where
    T: ReadFrom;
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.

Tuple Fields§

§0: PathBuf

Implementations§

Source§

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

Source

pub fn perform_read(&self) -> Result<T>

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 dir_structure::DirStructureItem;
use dir_structure::DeferredRead;

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

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

Trait Implementations§

Source§

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

Source§

fn clone(&self) -> DeferredRead<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 DeferredRead<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 DeferredRead<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 DeferredRead<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 DeferredRead<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 DeferredRead<T>

§

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

§

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

§

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

§

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

§

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