X-Git-Url: https://i11git.iti.kit.edu/anon-gitweb/?p=Mitarbeiter%2FTim-Zeitz%2Fstud-rust-base.git;a=blobdiff_plain;f=src%2Fio.rs;h=52057af8249cca0afaa063a52e8e2269bb34a01d;hp=419fa4520eb67ed5cbf3337bd1824586cc769257;hb=HEAD;hpb=b52fc015957bcfa65d2e8fbb8d4ea44f4923e3a8 diff --git a/src/io.rs b/src/io.rs index 419fa45..3c4d732 100644 --- a/src/io.rs +++ b/src/io.rs @@ -1,26 +1,26 @@ //! This module contains a few traits and blanket implementations -//! for (de)serializing and writing/reading numeric data to/from the disc. +//! for (de)serializing and writing/reading data to/from the disc. //! To use it you should import the `Load` and `Store` traits and use the //! `load_from` and `write_to` methods. //! //! # Example //! -//! ``` -//! use stud_rust_base::io::*; +//! ```no_run +//! # use stud_rust_base::io::*; //! -//! fn test() { -//! let head = Vec::::load_from("head_file_name").expect("could not read head"); -//! let lat = Vec::::load_from("node_latitude_file_name").expect("could not read lat"); -//! head.write_to("output_file").expect("could not write head"); -//! } +//! let head = Vec::::load_from("head_file_name")?; +//! let lat = Vec::::load_from("node_latitude_file_name")?; +//! head.write_to(&"output_file")?; +//! # Ok::<(), Box>(()) //! ``` -use std::fs; -use std::fs::File; -use std::io::prelude::*; -use std::io::Result; -use std::mem; -use std::slice; +use std::{ + fs::{metadata, File}, + io::{prelude::*, Result}, + mem, + path::Path, + slice, +}; /// A trait which allows accessing the data of an object as a slice of bytes. /// The bytes should represent a serialization of the object and allow @@ -49,6 +49,12 @@ impl DataBytes for [T] { } } +impl DataBytes for Vec { + fn data_bytes(&self) -> &[u8] { + self[..].data_bytes() + } +} + impl DataBytesMut for [T] { fn data_bytes_mut(&mut self) -> &mut [u8] { let num_bytes = self.len() * mem::size_of::(); @@ -64,10 +70,10 @@ impl DataBytesMut for Vec { } /// A trait which extends the `DataBytes` trait and exposes a method to write objects to disk. -pub trait Store : DataBytes { - /// Writes the serialized object to the file with the given filename - fn write_to(&self, filename: &str) -> Result<()> { - File::create(filename)?.write_all(self.data_bytes()) +pub trait Store: DataBytes { + /// Writes the serialized object to the file with the given path + fn write_to(&self, path: &dyn AsRef) -> Result<()> { + File::create(path)?.write_all(self.data_bytes()) } } @@ -75,16 +81,16 @@ impl Store for T {} impl Store for [T] where [T]: DataBytes {} /// A trait to load serialized data back into objects. -pub trait Load : DataBytesMut + Sized { +pub trait Load: DataBytesMut + Sized { /// This method must create an object of the correct size for serialized data with the given number of bytes. /// It should not be necessary to call this method directly. fn new_with_bytes(num_bytes: usize) -> Self; /// This method will load serialized data from the disk, create an object of the appropriate size, /// deserialize the bytes into the object and return the object. - fn load_from(filename: &str) -> Result { - let metadata = fs::metadata(filename)?; - let mut file = File::open(filename)?; + fn load_from>(path: P) -> Result { + let metadata = metadata(path.as_ref())?; + let mut file = File::open(path)?; let mut object = Self::new_with_bytes(metadata.len() as usize); assert_eq!(metadata.len() as usize, object.data_bytes_mut().len());