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::DirStructureItem;
use dir_structure::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<'a, T> FromRefForWriter<'a> for FmtWrapper<T>where
T: Display + 'a,
impl<'a, T> FromRefForWriter<'a> for FmtWrapper<T>where
T: Display + 'a,
Source§impl<T> NewtypeToInner for FmtWrapper<T>
impl<T> NewtypeToInner for FmtWrapper<T>
Source§impl<T> ReadFrom for FmtWrapper<T>
impl<T> ReadFrom for FmtWrapper<T>
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> 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
Mutably borrows from an owned value. Read more