linecount 0.1.0

Quickly count lines in a file
Documentation
#![crate_name = "linecount"]
#![deny(missing_docs,
        missing_debug_implementations, missing_copy_implementations,
        trivial_casts, trivial_numeric_casts,
        unsafe_code,
        unstable_features,
        unused_import_braces, unused_qualifications)]

//! Provides a fast line counting function.

use std::io;
use std::io::{BufRead, BufReader};

const LF: u8 = '\n' as u8;

/// Counts lines in the source `handle`. 
/// 
/// # Examples
/// ```
/// use linecount::count_lines
/// let lines: usize = count_lines(std::fs::File.open("foo.txt").unwrap()).unwrap()
/// ```
pub fn count_lines<R: io::Read>(handle: R) -> Result<usize, io::Error> {
    let mut reader = BufReader::new(handle);
    let mut count = 0;
    let mut line: Vec<u8> = Vec::new();
    while match reader.read_until(LF, &mut line) {
        Ok(n) if n > 0 => true,
        Err(e) => return Err(e),
        _ => false,
    } {
        if *line.last().unwrap() == LF {
            count += 1;
        };
    }
    Ok(count)
}

#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn test_count_lines() {
        let f: &[u8] = b"some text\nwith\nfour\nlines\n";
        assert_eq!(count_lines(f).unwrap(), 4);
    }
}