aloxide 0.0.8

A helper tool for Cargo build scripts (build.rs) for compiling and/or linking to a given Ruby library version for use in Rust code.
use std::fs;
use std::path::{Path, PathBuf};
use std::process::Command;
use std::io;

#[inline]
pub fn memchr(needle: u8, haystack: &[u8]) -> Option<usize> {
    #[cfg(feature = "memchr")]
    return memchr::memchr(needle, haystack);

    #[cfg(not(feature = "memchr"))]
    {
        for (i, &byte) in haystack.iter().enumerate() {
            if byte == needle {
                return Some(i);
            }
        }
        None
    }
}

#[inline]
pub fn nmake(_target: &str) -> Option<Command> {
    // Requires statements since expressions can't have attributes
    #[cfg(target_os = "windows")]
    return cc::windows_registry::find(_target, "nmake.exe");

    #[cfg(not(target_os = "windows"))]
    return None;
}

pub fn walk_files<F>(dir: &Path, mut f: F) -> io::Result<()>
    where for<'a> F: FnMut(PathBuf) -> io::Result<()>
{
    _walk_files(dir, &mut f)
}

// Takes `&mut F` to avoid infinite type-level recursion
fn _walk_files<F>(dir: &Path, f: &mut F) -> io::Result<()>
    where for<'a> F: FnMut(PathBuf) -> io::Result<()>
{
    for entry in fs::read_dir(dir)? {
        let entry = entry?;
        let path = entry.path();
        if entry.metadata()?.is_dir() {
            _walk_files(&path, f)?;
        } else {
            f(path)?;
        }
    }
    Ok(())
}