Deprecated: The each() function is deprecated. This message will be suppressed on further calls in /home/zhenxiangba/zhenxiangba.com/public_html/phproxy-improved-master/index.php on line 456
criterion 0.1.2 - Docs.rs
[go: Go Back, main page]

criterion 0.1.2

Statistics-driven micro-benchmarking library
Documentation
use std::fs::{File, ReadDir, self};
use std::io::Read;
use std::path::Path;
use serde_json;
use serde::Serialize;
use serde::de::DeserializeOwned;

use error::{AccessError, Result};

pub fn load<A, P: ?Sized>(path: &P) -> Result<A> where
    A: DeserializeOwned,
    P: AsRef<Path>,
{
    let mut f = File::open(path)
        .map_err(|inner| AccessError { inner, path: path.as_ref().to_owned() })?;
    let mut string = String::new();
    let _ = f.read_to_string(&mut string);
    let result: A = serde_json::from_str(string.as_str())?;

    Ok(result)
}

pub fn ls(dir: &Path) -> Result<ReadDir> {
    Ok(fs::read_dir(dir)?)
}

pub fn mkdirp<P>(path: &P) -> Result<()> where
    P: AsRef<Path>,
{
    Ok(fs::create_dir_all(path.as_ref())?)
}

pub fn mv(from: &Path, to: &Path) -> Result<()> {
    Ok(fs::rename(from, to)?)
}

pub fn rmrf(path: &Path) -> Result<()> {
    fs::remove_dir_all(path)?;

    Ok(())
}

pub fn save<D, P>(data: &D, path: &P) -> Result<()> where
    D: Serialize,
    P: AsRef<Path>,
{
    use std::io::Write;

    let buf = serde_json::to_string(&data)?;
    File::create(path)
        .and_then(|mut f| f.write_all(buf.as_bytes()))
        .map_err(|inner| AccessError { inner, path: path.as_ref().to_owned() })?;

    Ok(())
}