Skip to content

Instantly share code, notes, and snippets.

@kenOfYugen
Created December 30, 2016 21:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kenOfYugen/0ea7174e26a6e65c47f60a96019afb4b to your computer and use it in GitHub Desktop.
Save kenOfYugen/0ea7174e26a6e65c47f60a96019afb4b to your computer and use it in GitHub Desktop.
#![feature(conservative_impl_trait)]
use std::{io, fs, ffi};
#[derive(Debug)]
enum ReadDirErrors {
IOError(io::Error),
OSString(ffi::OsString),
}
impl From<io::Error> for ReadDirErrors {
fn from(err: io::Error) -> ReadDirErrors {
ReadDirErrors::IOError(err)
}
}
impl From<ffi::OsString> for ReadDirErrors {
fn from(err: ffi::OsString) -> ReadDirErrors {
ReadDirErrors::OSString(err)
}
}
fn get_dir_contents(path: &str) -> Result<Vec<String>, ReadDirErrors> {
let contents = fs::read_dir(path)?
.map(|entry| Ok(entry?.file_name().into_string()?))
.collect::<Result<Vec<String>, ReadDirErrors>>()?;
Ok(contents)
}
fn single_run(path: &str) {
let contents = get_dir_contents(path);
for entry in contents.unwrap() {
println!("{:?}", entry);
}
}
extern crate rayon;
use rayon::prelude::*;
fn multi_run(paths: Vec<&str>) {
match paths.len() {
1 => single_run(paths[0]),
_ => paths.par_iter().for_each(|&path| single_run(&path)),
}
}
fn readdir_closure<'a>(path: &'a str) -> impl Fn() -> Result<Vec<String>, ReadDirErrors> {
move || {
let contents = fs::read_dir(path)?
.map(|entry| Ok(entry?.file_name().into_string()?))
.collect::<Result<Vec<String>, ReadDirErrors>>()?;
Ok(contents)
}
}
fn main() {
let mut path = "./";
let contents = readdir_closure(path)();
println!("{:?}", contents);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment