From 33801f1d23ba36573b004a9eb8989cc7515b1f37 Mon Sep 17 00:00:00 2001 From: Kerollmops Date: Sat, 12 May 2018 13:28:43 +0200 Subject: [PATCH] chore: Move Streams to map file --- src/lib.rs | 94 +----------------------------------------------------- src/map.rs | 91 +++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 91 insertions(+), 94 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 890614e55..b7d15bd6b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,102 +3,10 @@ extern crate bincode; extern crate fst; extern crate serde; -mod map; - -use fst::Automaton; +pub mod map; pub use self::map::{Map, MapBuilder, Values}; pub use self::map::{ OpBuilder, IndexedValues, OpWithStateBuilder, IndexedValuesWithState, }; - -pub struct StreamBuilder<'m, 'v, T: 'v, A> { - inner: fst::map::StreamBuilder<'m, A>, - values: &'v Values, -} - -impl<'m, 'v, T: 'v, A> StreamBuilder<'m, 'v, T, A> { - pub fn with_state(self) -> StreamWithStateBuilder<'m, 'v, T, A> { - StreamWithStateBuilder { - inner: self.inner.with_state(), - values: self.values, - } - } -} - -impl<'m, 'v, 'a, T: 'v + 'a, A: Automaton> fst::IntoStreamer<'a> for StreamBuilder<'m, 'v, T, A> { - type Item = >::Item; - type Into = Stream<'m, 'v, T, A>; - - fn into_stream(self) -> Self::Into { - Stream { - inner: self.inner.into_stream(), - values: self.values, - } - } -} - -pub struct Stream<'m, 'v, T: 'v, A: Automaton = fst::automaton::AlwaysMatch> { - inner: fst::map::Stream<'m, A>, - values: &'v Values, -} - -impl<'m, 'v, 'a, T: 'v + 'a, A: Automaton> fst::Streamer<'a> for Stream<'m, 'v, T, A> { - type Item = (&'a [u8], &'a [T]); - - fn next(&'a mut self) -> Option { - // Here we can't just `map` because of some borrow rules - match self.inner.next() { - Some((key, i)) => { - let values = unsafe { self.values.get_unchecked(i as usize) }; - Some((key, values)) - }, - None => None, - } - } -} - -pub struct StreamWithStateBuilder<'m, 'v, T: 'v, A> { - inner: fst::map::StreamWithStateBuilder<'m, A>, - values: &'v Values, -} - -impl<'m, 'v, 'a, T: 'v + 'a, A: 'a> fst::IntoStreamer<'a> for StreamWithStateBuilder<'m, 'v, T, A> -where - A: Automaton, - A::State: Clone, -{ - type Item = >::Item; - type Into = StreamWithState<'m, 'v, T, A>; - - fn into_stream(self) -> Self::Into { - StreamWithState { - inner: self.inner.into_stream(), - values: self.values, - } - } -} - -pub struct StreamWithState<'m, 'v, T: 'v, A: Automaton = fst::automaton::AlwaysMatch> { - inner: fst::map::StreamWithState<'m, A>, - values: &'v Values, -} - -impl<'m, 'v, 'a, T: 'v + 'a, A: 'a> fst::Streamer<'a> for StreamWithState<'m, 'v, T, A> -where - A: Automaton, - A::State: Clone, -{ - type Item = (&'a [u8], &'a [T], A::State); - - fn next(&'a mut self) -> Option { - match self.inner.next() { - Some((key, i, state)) => { - let values = unsafe { self.values.get_unchecked(i as usize) }; - Some((key, values, state)) - }, - None => None, - } - } -} diff --git a/src/map.rs b/src/map.rs index 2b81c8212..0ea006ae6 100644 --- a/src/map.rs +++ b/src/map.rs @@ -6,7 +6,6 @@ use std::fs::File; use std::io::{Write, BufReader}; use std::ops::Range; use std::path::Path; -use {StreamBuilder, Stream}; #[derive(Debug)] pub struct Map { @@ -312,3 +311,93 @@ pub struct IndexedValuesWithState<'a, T: 'a, U> { pub values: &'a [T], pub state: U, } + +pub struct StreamBuilder<'m, 'v, T: 'v, A> { + inner: fst::map::StreamBuilder<'m, A>, + values: &'v Values, +} + +impl<'m, 'v, T: 'v, A> StreamBuilder<'m, 'v, T, A> { + pub fn with_state(self) -> StreamWithStateBuilder<'m, 'v, T, A> { + StreamWithStateBuilder { + inner: self.inner.with_state(), + values: self.values, + } + } +} + +impl<'m, 'v, 'a, T: 'v + 'a, A: Automaton> fst::IntoStreamer<'a> for StreamBuilder<'m, 'v, T, A> { + type Item = >::Item; + type Into = Stream<'m, 'v, T, A>; + + fn into_stream(self) -> Self::Into { + Stream { + inner: self.inner.into_stream(), + values: self.values, + } + } +} + +pub struct Stream<'m, 'v, T: 'v, A: Automaton = fst::automaton::AlwaysMatch> { + inner: fst::map::Stream<'m, A>, + values: &'v Values, +} + +impl<'m, 'v, 'a, T: 'v + 'a, A: Automaton> fst::Streamer<'a> for Stream<'m, 'v, T, A> { + type Item = (&'a [u8], &'a [T]); + + fn next(&'a mut self) -> Option { + // Here we can't just `map` because of some borrow rules + match self.inner.next() { + Some((key, i)) => { + let values = unsafe { self.values.get_unchecked(i as usize) }; + Some((key, values)) + }, + None => None, + } + } +} + +pub struct StreamWithStateBuilder<'m, 'v, T: 'v, A> { + inner: fst::map::StreamWithStateBuilder<'m, A>, + values: &'v Values, +} + +impl<'m, 'v, 'a, T: 'v + 'a, A: 'a> fst::IntoStreamer<'a> for StreamWithStateBuilder<'m, 'v, T, A> +where + A: Automaton, + A::State: Clone, +{ + type Item = >::Item; + type Into = StreamWithState<'m, 'v, T, A>; + + fn into_stream(self) -> Self::Into { + StreamWithState { + inner: self.inner.into_stream(), + values: self.values, + } + } +} + +pub struct StreamWithState<'m, 'v, T: 'v, A: Automaton = fst::automaton::AlwaysMatch> { + inner: fst::map::StreamWithState<'m, A>, + values: &'v Values, +} + +impl<'m, 'v, 'a, T: 'v + 'a, A: 'a> fst::Streamer<'a> for StreamWithState<'m, 'v, T, A> +where + A: Automaton, + A::State: Clone, +{ + type Item = (&'a [u8], &'a [T], A::State); + + fn next(&'a mut self) -> Option { + match self.inner.next() { + Some((key, i, state)) => { + let values = unsafe { self.values.get_unchecked(i as usize) }; + Some((key, values, state)) + }, + None => None, + } + } +}