From ca8a3fb848e8feab71e9a5d48f8b69df763dbcc8 Mon Sep 17 00:00:00 2001 From: Hatter Jiang Date: Fri, 1 May 2020 22:16:52 +0800 Subject: [PATCH] move to local_util --- src/local_util.rs | 36 ++++++++++++++++++++++++++++++++++++ src/main.rs | 47 ++++++----------------------------------------- 2 files changed, 42 insertions(+), 41 deletions(-) diff --git a/src/local_util.rs b/src/local_util.rs index 279a5ba..2a139ce 100644 --- a/src/local_util.rs +++ b/src/local_util.rs @@ -1,10 +1,46 @@ use std::{ + cell::Cell, fs::File, path::Path, io::prelude::*, }; use rust_util::{ XResult, new_box_error, }; +#[derive(Debug)] +pub struct MatchLine { + pub line_number: usize, + pub line_string: String, +} + +impl MatchLine { + pub fn new(line_number: usize, line_string: String) -> MatchLine { + MatchLine { line_number, line_string, } + } +} + +pub struct CountCell( Cell ); + +impl CountCell { + pub fn new() -> CountCell { + CountCell( Cell::new(0_u64) ) + } + + #[inline] + pub fn get(&self) -> u64 { + self.0.get() + } + + #[inline] + pub fn add(&self, i: u64) { + self.0.set(self.0.get() + i); + } + + #[inline] + pub fn add_one(&self) { + self.add(1); + } +} + pub fn read_file_content>(p: P, len_of_large_file: u64) -> XResult { let file = p.as_ref(); if !file.exists() { diff --git a/src/main.rs b/src/main.rs index 97d3bb1..11777d8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,7 +6,7 @@ extern crate rust_util; mod opt; mod local_util; -use std::{ cell::Cell, path::Path, time::SystemTime, }; +use std::{ path::Path, time::SystemTime, }; use opt::*; use rust_util::{ iff, @@ -16,51 +16,16 @@ use rust_util::{ util_size::*, util_msg::*, }; -use local_util::read_file_content; +use local_util::{ + CountCell, + MatchLine, + read_file_content, +}; const EMPTY: &str = ""; const VERSION: &str = env!("CARGO_PKG_VERSION"); const GIT_HASH: &str = env!("GIT_HASH"); -#[derive(Debug)] -struct MatchLine { - line_number: usize, - line_string: String, -} - -impl MatchLine { - fn new(line_number: usize, line_string: String) -> MatchLine { - MatchLine { line_number, line_string, } - } -} - -struct CountCell { - cell: Cell -} - -impl CountCell { - fn new() -> CountCell { - CountCell { - cell: Cell::new(0_u64) - } - } - - #[inline] - fn get(&self) -> u64 { - self.cell.get() - } - - #[inline] - fn add(&self, i: u64) { - self.cell.set(self.cell.get() + i); - } - - #[inline] - fn add_one(&self) { - self.add(1); - } -} - fn print_version(options: &Options) { println!(r#"finding {} - {} Copyright (C) 2019-2020 Hatter Jiang.