Skip to content

Commit

Permalink
Rename use_std to std; exclude streaming finds from no_std
Browse files Browse the repository at this point in the history
  • Loading branch information
ZackPierce committed May 3, 2018
1 parent 4cfcb8e commit 327324e
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 95 deletions.
9 changes: 4 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,21 @@ bench = false
[dependencies]
cfg-if = "0.1"
memchr = { version = "2", default-features = false }
# TODO - move to a non-specific-nightly-version-linked core io alternative
core_io = { version = "0.1.20180307", optional = true, default-features = false, features = ["collections"] }

[dev-dependencies]
csv = "1.0.0-beta.5"
docopt = "0.8"
hashmap_core = "0.1"
memmap = "0.6"
quickcheck = { version = "0.6", default-features = false }
serde = "1"
serde_derive = "1"

[features]

default = ["use_std"]
use_std = ["memchr/use_std", "memchr/libc"]
alloc = ["core_io"]
default = ["std"]
std = ["memchr/use_std", "memchr/libc"]
alloc = []

[[bench]]
name = "bench"
Expand Down
10 changes: 5 additions & 5 deletions examples/dict-search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,12 @@ fn main() {
}
}
}
#[cfg(not(feature = "use_std"))]
#[cfg(not(feature = "std"))]
fn run(_: &Args) -> Result<(), Box<Error>> {
Err("The use_std feature is mandatory for the dict-search example to work".into())
Err("The std feature is mandatory for the dict-search example to work".into())
}

#[cfg(feature = "use_std")]
#[cfg(feature = "std")]
fn run(args: &Args) -> Result<(), Box<Error>> {
let aut = try!(build_automaton(&args.flag_dict, args.flag_min_len));
if args.flag_memory_usage {
Expand Down Expand Up @@ -115,7 +115,7 @@ fn run(args: &Args) -> Result<(), Box<Error>> {
Ok(())
}

#[cfg(feature = "use_std")]
#[cfg(feature = "std")]
fn write_matches<A, I>(aut: &A, it: I) -> Result<(), Box<Error>>
where A: Automaton<String>, I: Iterator<Item=io::Result<Match>> {
let mut wtr = csv::Writer::from_writer(io::stdout());
Expand All @@ -128,7 +128,7 @@ fn write_matches<A, I>(aut: &A, it: I) -> Result<(), Box<Error>>
Ok(())
}

#[cfg(feature = "use_std")]
#[cfg(feature = "std")]
fn build_automaton(
dict_path: &str,
min_len: usize,
Expand Down
45 changes: 28 additions & 17 deletions src/autiter.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
cfg_if! {

if #[cfg(feature = "use_std")] {
if #[cfg(feature = "std")] {
use std::io::{self, BufRead};
use std::marker::PhantomData;
use std::io::{BufRead, BufReader, Read, Result as IoResult};
} else {
use core::marker::PhantomData;
use io::{BufRead, BufReader, Read, Result as IoResult};
}
}


use memchr::{memchr, memchr2, memchr3};

use super::{ROOT_STATE, StateIdx};
Expand Down Expand Up @@ -86,29 +83,35 @@ pub trait Automaton<P> {
}

/// Returns an iterator of non-overlapping matches in the given reader.
fn stream_find<'a, R: Read>(
///
/// Only available when the feature flag "std" is present
#[cfg(feature = "std")]
fn stream_find<'a, R: io::Read>(
&'a self,
rdr: R,
) -> StreamMatches<'a, R, P, Self>
where Self: Sized {
StreamMatches {
aut: self,
buf: BufReader::new(rdr),
buf: io::BufReader::new(rdr),
texti: 0,
si: ROOT_STATE,
_m: PhantomData,
}
}

/// Returns an iterator of overlapping matches in the given reader.
fn stream_find_overlapping<'a, R: Read>(
///
/// Only available when the feature flag "std" is present
#[cfg(feature = "std")]
fn stream_find_overlapping<'a, R: io::Read>(
&'a self,
rdr: R,
) -> StreamMatchesOverlapping<'a, R, P, Self>
where Self: Sized {
StreamMatchesOverlapping {
aut: self,
buf: BufReader::new(rdr),
buf: io::BufReader::new(rdr),
texti: 0,
si: ROOT_STATE,
outi: 0,
Expand Down Expand Up @@ -370,20 +373,24 @@ impl<'a, 's, P, A: Automaton<P> + ?Sized> Iterator for Matches<'a, 's, P, A> {
///
/// `'a` is the lifetime of the automaton, `R` is the type of the underlying
/// `io::Read`er, and P is the type of the Automaton's pattern.
///
/// Only available when the feature flag "std" is present
#[cfg(feature = "std")]
#[derive(Debug)]
pub struct StreamMatches<'a, R, P, A: 'a + Automaton<P> + ?Sized> {
aut: &'a A,
buf: BufReader<R>,
buf: io::BufReader<R>,
texti: usize,
si: StateIdx,
_m: PhantomData<P>,
}

impl<'a, R: Read, P, A: Automaton<P>>
#[cfg(feature = "std")]
impl<'a, R: io::Read, P, A: Automaton<P>>
Iterator for StreamMatches<'a, R, P, A> {
type Item = IoResult<Match>;
type Item = io::Result<Match>;

fn next(&mut self) -> Option<IoResult<Match>> {
fn next(&mut self) -> Option<io::Result<Match>> {
let mut m = None;
let mut consumed = 0;
'LOOP: loop {
Expand Down Expand Up @@ -484,21 +491,25 @@ impl<'a, 's, P, A: Automaton<P> + ?Sized>
///
/// `'a` is the lifetime of the automaton, `R` is the type of the underlying
/// `io::Read`er, and P is the type of the Automaton's pattern.
///
/// Only available when the feature flag "std" is present
#[cfg(feature = "std")]
#[derive(Debug)]
pub struct StreamMatchesOverlapping<'a, R, P, A: 'a + Automaton<P> + ?Sized> {
aut: &'a A,
buf: BufReader<R>,
buf: io::BufReader<R>,
texti: usize,
si: StateIdx,
outi: usize,
_m: PhantomData<P>,
}

impl<'a, R: Read, P, A: Automaton<P> + ?Sized>
#[cfg(feature = "std")]
impl<'a, R: io::Read, P, A: Automaton<P> + ?Sized>
Iterator for StreamMatchesOverlapping<'a, R, P, A> {
type Item = IoResult<Match>;
type Item = io::Result<Match>;

fn next(&mut self) -> Option<IoResult<Match>> {
fn next(&mut self) -> Option<io::Result<Match>> {
if self.aut.has_match(self.si, self.outi) {
let m = self.aut.get_match(self.si, self.outi, self.texti);
self.outi += 1;
Expand Down
2 changes: 1 addition & 1 deletion src/full.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
cfg_if! {
if #[cfg(feature = "use_std")] {
if #[cfg(feature = "std")] {
use std::vec::Vec;
use std::fmt;
use std::mem;
Expand Down
Loading

0 comments on commit 327324e

Please sign in to comment.