fst 0.1.0

Use finite state transducers to compactly represents sets or maps of many strings (> 1 billion is possible).
Documentation
use std::io;
use std::path::Path;

use raw::{Builder, Fst, FstStream, FstStreamBuilder};
use Result;

pub struct Set(Fst);

impl Set {
    pub fn from_file_path<P: AsRef<Path>>(path: P) -> Result<Self> {
        Fst::from_file_path(path).map(Set)
    }

    pub fn from_bytes(bytes: Vec<u8>) -> Result<Self> {
        Fst::from_bytes(bytes).map(Set)
    }

    pub fn contains<B: AsRef<[u8]>>(&self, key: B) -> bool {
        self.0.find(key).is_some()
    }

    pub fn stream(&self) -> SetStream {
        SetStream(self.0.stream())
    }

    pub fn range(&self) -> SetStreamBuilder {
        SetStreamBuilder(self.0.range())
    }
}

pub struct SetBuilder<W>(Builder<W>);

impl SetBuilder<Vec<u8>> {
    pub fn memory() -> Self {
        SetBuilder(Builder::memory())
    }
}

impl<W: io::Write> SetBuilder<W> {
    pub fn new(wtr: W) -> Result<SetBuilder<W>> {
        Builder::new_type(wtr, 1).map(SetBuilder)
    }

    pub fn insert<B: AsRef<[u8]>>(&mut self, bytes: B) -> Result<()> {
        self.0.add(bytes)
    }

    pub fn finish(self) -> Result<()> {
        self.0.finish()
    }

    pub fn into_inner(self) -> Result<W> {
        self.0.into_inner()
    }
}

pub struct SetStream<'s>(FstStream<'s>);

impl<'s> SetStream<'s> {
    pub fn next(&mut self) -> Option<&[u8]> {
        self.0.next().map(|(key, _)| key)
    }
}

pub struct SetStreamBuilder<'s>(FstStreamBuilder<'s>);

impl<'s> SetStreamBuilder<'s> {
    pub fn into_stream(self) -> SetStream<'s> {
        SetStream(self.0.into_stream())
    }

    pub fn ge<T: AsRef<[u8]>>(mut self, bound: T) -> Self {
        SetStreamBuilder(self.0.ge(bound))
    }

    pub fn gt<T: AsRef<[u8]>>(mut self, bound: T) -> Self {
        SetStreamBuilder(self.0.gt(bound))
    }

    pub fn le<T: AsRef<[u8]>>(mut self, bound: T) -> Self {
        SetStreamBuilder(self.0.le(bound))
    }

    pub fn lt<T: AsRef<[u8]>>(mut self, bound: T) -> Self {
        SetStreamBuilder(self.0.lt(bound))
    }
}