FmtWrapper

Struct FmtWrapper 

Source
pub struct FmtWrapper<T>(pub T);
Expand description

A wrapper around a type which will use the Display and FromStr implementations for serialization / deserialization.

For example: u8, i8, i16, u16, all integer types… bool etc.

§Examples

use std::path::Path;

use dir_structure::traits::sync::DirStructureItem;
use dir_structure::fmt_wrapper::FmtWrapper;

#[derive(dir_structure::DirStructure, PartialEq, Debug)]
struct Dir {
   #[dir_structure(path = "f.txt", with_newtype = FmtWrapper<u8>)]
   f: u8,
   #[dir_structure(path = "b.txt", with_newtype = FmtWrapper<bool>)]
   b: bool,
}

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"), "42")?;
    std::fs::write(d.join("b.txt"), "true")?;
    let mut dir = Dir::read(&d)?;
    assert_eq!(dir.f, 42);
    assert_eq!(dir.b, true);
    dir.f = 100;
    dir.b = false;
    dir.write(&d)?;
    assert_eq!(std::fs::read_to_string(d.join("f.txt"))?, "100");
    assert_eq!(std::fs::read_to_string(d.join("b.txt"))?, "false");
    Ok(())
}

Tuple Fields§

§0: T

Trait Implementations§

Source§

impl<T> AssertEq for FmtWrapper<T>
where for<'__trivial> T: AssertEq<T> + 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, 'vfs, T, Vfs> FromRefForWriter<'a, 'vfs, Vfs> for FmtWrapper<T>
where T: Display + 'a, Vfs: 'vfs + WriteSupportingVfs<'vfs>, 'vfs: 'a,

Source§

type Inner = T

The inner type to cast.
Source§

type Wr = FmtWrapperRefWr<'a, 'vfs, T, Vfs>

The reference type to cast to.
Source§

fn from_ref_for_writer(value: &'a Self::Inner) -> Self::Wr

Casts the reference to the inner type to a WriteTo reference type.
Source§

impl<'a, T, Vfs: WriteSupportingVfsAsync + 'static> FromRefForWriterAsync<'a, Vfs> for FmtWrapper<T>
where T: Display + Send + 'a,

Available on crate feature async only.
Source§

type Inner = T

The inner type to cast.
Source§

type Wr = FmtWrapperRefWr<'a, 'a, T, Vfs>

The reference type to cast to.
Source§

fn from_ref_for_writer_async(value: &'a Self::Inner) -> Self::Wr

Casts the reference to the inner type to a WriteToAsync reference type.
Source§

impl<T> NewtypeToInner for FmtWrapper<T>

Source§

type Inner = T

The inner type.
Source§

fn into_inner(self) -> Self::Inner

Converts the newtype to its inner type.
Source§

impl<'a, T, Vfs: Vfs<'a>> ReadFrom<'a, Vfs> for FmtWrapper<T>
where T: FromStr + 'a, T::Err: Into<Box<dyn Error + Send + Sync>>,

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, T, Vfs: VfsAsync + 'static> ReadFromAsync<'a, Vfs> for FmtWrapper<T>
where T: FromStr + Send + 'static, T::Err: Into<Box<dyn Error + Send + Sync>>,

Available on crate feature async only.
Source§

type Future = Pin<Box<dyn Future<Output = Result<FmtWrapper<T>, 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: <<Vfs as VfsCore>::Path as PathType>::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<'vfs, T, Vfs: WriteSupportingVfs<'vfs>> WriteTo<'vfs, Vfs> for FmtWrapper<T>
where T: Display,

Source§

fn write_to(&self, path: &Vfs::Path, vfs: Pin<&'vfs Vfs>) -> VfsResult<(), Vfs>

Writes the structure to the specified path.
Source§

impl<'a, T, Vfs: WriteSupportingVfsAsync + 'static> WriteToAsync<'a, Vfs> for FmtWrapper<T>
where T: Display + Send + Sync + 'static,

Available on crate feature async only.
Source§

type Future = <FileString as WriteToAsync<'a, Vfs>>::Future

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

impl<'a, T, Vfs: WriteSupportingVfsAsync + 'static> WriteToAsyncRef<'a, Vfs> for FmtWrapper<T>
where T: Display + Send + Sync + 'a,

Available on crate feature async only.
Source§

type Future<'b> = <FileString as WriteToAsync<'b, Vfs>>::Future where Self: 'b, Vfs: 'b, 'a: 'b

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

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

Asynchronously writes the structure to the specified path.

Auto Trait Implementations§

§

impl<T> Freeze for FmtWrapper<T>
where T: Freeze,

§

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

§

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

§

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

§

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

§

impl<T> UnwindSafe for FmtWrapper<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> 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, 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,