Skip to content
This repository has been archived by the owner on Feb 12, 2018. It is now read-only.

Commit

Permalink
Abstract dumping fns
Browse files Browse the repository at this point in the history
  • Loading branch information
trishume committed Jun 13, 2016
1 parent 70ebd2a commit 4cc2738
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 47 deletions.
8 changes: 0 additions & 8 deletions benches/loading.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,6 @@ use test::Bencher;

use syntect::package_set::PackageSet;

#[bench]
fn bench_load_syntax_dump(b: &mut Bencher) {
b.iter(|| {
let ps = PackageSet::from_dump_file("assets/default_newlines.packdump");
test::black_box(&ps);
});
}

#[bench]
fn bench_load_internal_dump(b: &mut Bencher) {
b.iter(|| {
Expand Down
5 changes: 3 additions & 2 deletions examples/gendata.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
extern crate syntect;
use syntect::package_set::PackageSet;
use syntect::dumps::*;

fn main() {
let mut ps = PackageSet::new();
ps.load_syntaxes("testdata/Packages", true).unwrap();
ps.dump_to_file("assets/default_newlines.packdump").unwrap();
dump_to_file(&ps, "assets/default_newlines.packdump").unwrap();

let mut ps2 = PackageSet::new();
ps2.load_syntaxes("testdata/Packages", false).unwrap();
ps2.dump_to_file("assets/default_nonewlines.packdump").unwrap();
dump_to_file(&ps2, "assets/default_nonewlines.packdump").unwrap();
}
73 changes: 36 additions & 37 deletions src/dumps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,37 @@ use std::path::Path;
use flate2::write::ZlibEncoder;
use flate2::read::ZlibDecoder;
use flate2::Compression;
use rustc_serialize::{Encodable, Decodable};

pub fn dump_binary<T: Encodable>(o: &T) -> Vec<u8> {
let mut v = Vec::new();
{
let mut encoder = ZlibEncoder::new(&mut v, Compression::Best);
encode_into(o, &mut encoder, SizeLimit::Infinite).unwrap();
}
v
}

pub fn dump_to_file<T: Encodable, P: AsRef<Path>>(o: &T, path: P) -> EncodingResult<()> {
let f = BufWriter::new(try!(File::create(path).map_err(EncodingError::IoError)));
let mut encoder = ZlibEncoder::new(f, Compression::Best);
encode_into(o, &mut encoder, SizeLimit::Infinite)
}

/// Returns a fully loaded and linked package set from
/// a binary dump. Panics if the dump is invalid.
pub fn from_binary<T: Decodable>(v: &[u8]) -> T {
let mut decoder = ZlibDecoder::new(v);
decode_from(&mut decoder, SizeLimit::Infinite).unwrap()
}

/// Returns a fully loaded and linked package set from
/// a binary dump file.
pub fn from_dump_file<T: Decodable, P: AsRef<Path>>(path: P) -> DecodingResult<T> {
let f = try!(File::open(path).map_err(DecodingError::IoError));
let mut decoder = ZlibDecoder::new(BufReader::new(f));
decode_from(&mut decoder, SizeLimit::Infinite)
}

impl PackageSet {
/// Instantiates a new package set from a binary dump of
Expand All @@ -22,7 +53,7 @@ impl PackageSet {
/// use the fact that SyntaxDefinitions are serializable with
/// the bincode crate to cache dumps of additional syntaxes yourself.
pub fn load_defaults_nonewlines() -> PackageSet {
let mut ps = Self::from_binary(include_bytes!("../assets/default_nonewlines.packdump"));
let mut ps: PackageSet = from_binary(include_bytes!("../assets/default_nonewlines.packdump"));
ps.link_syntaxes();
ps
}
Expand All @@ -31,55 +62,23 @@ impl PackageSet {
/// These are separate methods because thanks to linker garbage collection, only the serialized
/// dumps for the method(s) you call will be included in the binary (each is ~200kb for now).
pub fn load_defaults_newlines() -> PackageSet {
let mut ps = Self::from_binary(include_bytes!("../assets/default_newlines.packdump"));
let mut ps: PackageSet = from_binary(include_bytes!("../assets/default_newlines.packdump"));
ps.link_syntaxes();
ps
}

pub fn dump_binary(&self) -> Vec<u8> {
assert!(!self.is_linked);
let mut v = Vec::new();
{
let mut encoder = ZlibEncoder::new(&mut v, Compression::Best);
encode_into(self, &mut encoder, SizeLimit::Infinite).unwrap();
}
v
}

pub fn dump_to_file<P: AsRef<Path>>(&self, path: P) -> EncodingResult<()> {
let f = BufWriter::new(try!(File::create(path).map_err(EncodingError::IoError)));
let mut encoder = ZlibEncoder::new(f, Compression::Best);
encode_into(self, &mut encoder, SizeLimit::Infinite)
}

/// Returns a fully loaded and linked package set from
/// a binary dump. Panics if the dump is invalid.
pub fn from_binary(v: &[u8]) -> PackageSet {
let mut decoder = ZlibDecoder::new(v);
let mut ps: PackageSet = decode_from(&mut decoder, SizeLimit::Infinite).unwrap();
ps.link_syntaxes();
ps
}

/// Returns a fully loaded and linked package set from
/// a binary dump file.
pub fn from_dump_file<P: AsRef<Path>>(path: P) -> DecodingResult<PackageSet> {
let f = try!(File::open(path).map_err(DecodingError::IoError));
let mut decoder = ZlibDecoder::new(BufReader::new(f));
decode_from(&mut decoder, SizeLimit::Infinite)
}
}

#[cfg(test)]
mod tests {
use package_set::PackageSet;
use dumps::*;
#[test]
fn can_dump_and_load() {
let mut ps = PackageSet::new();
ps.load_syntaxes("testdata/Packages", false).unwrap();

let bin = ps.dump_binary();
let ps2 = PackageSet::from_binary(&bin[..]);
let bin = dump_binary(&ps);
let ps2: PackageSet = from_binary(&bin[..]);
assert_eq!(ps.syntaxes.len(), ps2.syntaxes.len());
}
}

0 comments on commit 4cc2738

Please sign in to comment.