Skip to content

Commit

Permalink
Adapt to flate2 changing default backend to Rust two years ago
Browse files Browse the repository at this point in the history
When the `-rs` versions of dump creation and loading were introduced in syntect,
`flate2` defaulted to a C implementation and made the Rust backend optional.
Since two years, the Rust backend has been the default backend. See
rust-lang/flate2-rs@c479d064e2. So we can stop using
the `-rs` versions since enabling them makes no difference.

Note that there is no mechanism yet that we can use to mark features as
deprecated. See rust-lang/cargo#7130
  • Loading branch information
Martin Nordholts committed Dec 28, 2021
1 parent ec87599 commit 15ff701
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 45 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,4 @@ jobs:
- name: Run Xi tests
run: |
# Test the build configuration that Xi uses
cargo test --lib --no-default-features --features "assets dump-load-rs"
cargo test --lib --no-default-features --features "assets dump-load"
25 changes: 11 additions & 14 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ lazy_static = "1.0"
bitflags = "1.0.4"
plist = "1"
bincode = { version = "1.0", optional = true }
flate2 = { version = "1.0", optional = true, default-features = false }
flate2 = { version = "1.0", optional = true }
fnv = { version = "1.0", optional = true }
serde = "1.0"
serde_derive = "1.0"
Expand All @@ -47,27 +47,24 @@ pretty_assertions = "0.6"
# will fail, choose one of each. If you are using both creation and loading,
# your binary will be smaller if you choose the same one for each.

# Pure Rust dump loading, slower than regular `dump-load`
dump-load-rs = ["flate2/rust_backend", "bincode"]
# Dump loading using flate2, which depends on the miniz C library.
dump-load = ["flate2/default", "bincode"]
# Dump creation using flate2, which depends on the miniz C library.
dump-create = ["flate2/default", "bincode"]
# Pure Rust dump creation, worse compressor so produces larger dumps than dump-create
dump-create-rs = ["flate2/rust_backend", "bincode"]
# Legacy alias for `dump-load`
dump-load-rs = ["dump-load"]
# Dump loading using flate2
dump-load = ["flate2", "bincode"]
# Dump creation using flate2
dump-create = ["flate2", "bincode"]
# Legacy alias for `dump-create`
dump-create-rs = ["dump-create"]

regex-fancy = ["fancy-regex"]
regex-onig = ["onig"]

# If you enable "parsing", you must also enable either "dump-load" or
# "dump-load-rs", and "dump-create" or "dump-create-rs". Otherwise the code that
# enables lazy-loading of syntaxes will not compile.
parsing = ["regex-syntax", "fnv"]
parsing = ["regex-syntax", "fnv", "dump-create", "dump-load"]

# Support for .tmPreferenes metadata files (indentation, comment syntax, etc)
metadata = ["parsing"]
# The `assets` feature enables inclusion of the default theme and syntax packages.
# For `assets` to do anything, it requires one of `dump-load-rs` or `dump-load` to be set.
# For `assets` to do anything, it requires `dump-load` to be set.
assets = []
html = ["parsing"]
# Support for parsing .sublime-syntax files
Expand Down
52 changes: 26 additions & 26 deletions src/dumps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,43 +13,43 @@
//! [`ThemeSet`]: ../highlighting/struct.ThemeSet.html
//! [`dump_to_file`]: fn.dump_to_file.html
use bincode::Result;
#[cfg(any(feature = "dump-load", feature = "dump-load-rs"))]
#[cfg(feature = "dump-load")]
use bincode::deserialize_from;
#[cfg(any(feature = "dump-create", feature = "dump-create-rs"))]
#[cfg(feature = "dump-create")]
use bincode::serialize_into;
use std::fs::File;
#[cfg(any(feature = "dump-load", feature = "dump-load-rs"))]
#[cfg(feature = "dump-load")]
use std::io::{BufRead};
#[cfg(any(feature = "dump-create", feature = "dump-create-rs"))]
#[cfg(feature = "dump-create")]
use std::io::{BufWriter, Write};
#[cfg(all(feature = "parsing", feature = "assets", any(feature = "dump-load", feature = "dump-load-rs")))]
#[cfg(all(feature = "parsing", feature = "assets", feature = "dump-load"))]
use crate::parsing::SyntaxSet;
#[cfg(all(feature = "assets", any(feature = "dump-load", feature = "dump-load-rs")))]
#[cfg(all(feature = "assets", feature = "dump-load"))]
use crate::highlighting::ThemeSet;
use std::path::Path;
#[cfg(feature = "dump-create")]
use flate2::write::ZlibEncoder;
#[cfg(any(feature = "dump-load", feature = "dump-load-rs"))]
#[cfg(feature = "dump-load")]
use flate2::bufread::ZlibDecoder;
#[cfg(any(feature = "dump-create", feature = "dump-create-rs"))]
#[cfg(feature = "dump-create")]
use flate2::Compression;
#[cfg(any(feature = "dump-create", feature = "dump-create-rs"))]
#[cfg(feature = "dump-create")]
use serde::Serialize;
#[cfg(any(feature = "dump-load", feature = "dump-load-rs"))]
#[cfg(feature = "dump-load")]
use serde::de::DeserializeOwned;

/// Dumps an object to the given writer in a compressed binary format
///
/// The writer is encoded with the `bincode` crate and compressed with `flate2`.
#[cfg(any(feature = "dump-create", feature = "dump-create-rs"))]
#[cfg(feature = "dump-create")]
pub fn dump_to_writer<T: Serialize, W: Write>(to_dump: &T, output: W) -> Result<()> {
serialize_to_writer_impl(to_dump, output, true)
}

/// Dumps an object to a binary array in the same format as [`dump_to_writer`]
///
/// [`dump_to_writer`]: fn.dump_to_writer.html
#[cfg(any(feature = "dump-create", feature = "dump-create-rs"))]
#[cfg(feature = "dump-create")]
pub fn dump_binary<T: Serialize>(o: &T) -> Vec<u8> {
let mut v = Vec::new();
dump_to_writer(o, &mut v).unwrap();
Expand All @@ -62,28 +62,28 @@ pub fn dump_binary<T: Serialize>(o: &T) -> Vec<u8> {
/// the `bincode` crate and then compressed with the `flate2` crate.
///
/// [`dump_to_writer`]: fn.dump_to_writer.html
#[cfg(any(feature = "dump-create", feature = "dump-create-rs"))]
#[cfg(feature = "dump-create")]
pub fn dump_to_file<T: Serialize, P: AsRef<Path>>(o: &T, path: P) -> Result<()> {
let out = BufWriter::new(File::create(path)?);
dump_to_writer(o, out)
}

/// A helper function for decoding and decompressing data from a reader
#[cfg(any(feature = "dump-load", feature = "dump-load-rs"))]
#[cfg(feature = "dump-load")]
pub fn from_reader<T: DeserializeOwned, R: BufRead>(input: R) -> Result<T> {
deserialize_from_reader_impl(input, true)
}

/// Returns a fully loaded object from a binary dump.
///
/// This function panics if the dump is invalid.
#[cfg(any(feature = "dump-load", feature = "dump-load-rs"))]
#[cfg(feature = "dump-load")]
pub fn from_binary<T: DeserializeOwned>(v: &[u8]) -> T {
from_reader(v).unwrap()
}

/// Returns a fully loaded object from a binary dump file.
#[cfg(any(feature = "dump-load", feature = "dump-load-rs"))]
#[cfg(feature = "dump-load")]
pub fn from_dump_file<T: DeserializeOwned, P: AsRef<Path>>(path: P) -> Result<T> {
let contents = std::fs::read(path)?;
from_reader(&contents[..])
Expand All @@ -93,15 +93,15 @@ pub fn from_dump_file<T: DeserializeOwned, P: AsRef<Path>>(path: P) -> Result<T>
/// itself shall not be compressed, because the data for its lazy-loaded
/// syntaxes are already compressed. Compressing another time just results in
/// bad performance.
#[cfg(any(feature = "dump-create", feature = "dump-create-rs"))]
#[cfg(feature = "dump-create")]
pub fn dump_to_uncompressed_file<T: Serialize, P: AsRef<Path>>(o: &T, path: P) -> Result<()> {
let out = BufWriter::new(File::create(path)?);
serialize_to_writer_impl(o, out, false)
}

/// To be used when deserializing a [`SyntaxSet`] that was previously written to
/// file using [dump_to_uncompressed_file].
#[cfg(any(feature = "dump-load", feature = "dump-load-rs"))]
#[cfg(feature = "dump-load")]
pub fn from_uncompressed_dump_file<T: DeserializeOwned, P: AsRef<Path>>(path: P) -> Result<T> {
let contents = std::fs::read(path)?;
deserialize_from_reader_impl(&contents[..], false)
Expand All @@ -110,13 +110,13 @@ pub fn from_uncompressed_dump_file<T: DeserializeOwned, P: AsRef<Path>>(path: P)
/// To be used when deserializing a [`SyntaxSet`] from raw data, for example
/// data that has been embedded in your own binary with the [`include_bytes!`]
/// macro.
#[cfg(any(feature = "dump-load", feature = "dump-load-rs"))]
#[cfg(feature = "dump-load")]
pub fn from_uncompressed_data<T: DeserializeOwned>(v: &[u8]) -> Result<T> {
deserialize_from_reader_impl(v, false)
}

/// Private low level helper function used to implement the public API.
#[cfg(any(feature = "dump-create", feature = "dump-create-rs"))]
#[cfg(feature = "dump-create")]
fn serialize_to_writer_impl<T: Serialize, W: Write>(to_dump: &T, output: W, use_compression: bool) -> Result<()> {
if use_compression {
let mut encoder = ZlibEncoder::new(output, Compression::best());
Expand All @@ -127,7 +127,7 @@ fn serialize_to_writer_impl<T: Serialize, W: Write>(to_dump: &T, output: W, use_
}

/// Private low level helper function used to implement the public API.
#[cfg(any(feature = "dump-load", feature = "dump-load-rs"))]
#[cfg(feature = "dump-load")]
fn deserialize_from_reader_impl<T: DeserializeOwned, R: BufRead>(input: R, use_compression: bool) -> Result<T> {
if use_compression {
let mut decoder = ZlibDecoder::new(input);
Expand All @@ -137,7 +137,7 @@ fn deserialize_from_reader_impl<T: DeserializeOwned, R: BufRead>(input: R, use_c
}
}

#[cfg(all(feature = "parsing", feature = "assets", any(feature = "dump-load", feature = "dump-load-rs")))]
#[cfg(all(feature = "parsing", feature = "assets", feature = "dump-load"))]
impl SyntaxSet {
/// Instantiates a new syntax set from a binary dump of Sublime Text's default open source
/// syntax definitions.
Expand Down Expand Up @@ -195,7 +195,7 @@ impl SyntaxSet {
}
}

#[cfg(all(feature = "assets", any(feature = "dump-load", feature = "dump-load-rs")))]
#[cfg(all(feature = "assets", feature = "dump-load"))]
impl ThemeSet {
/// Loads the set of default themes
/// Currently includes (these are the keys for the map):
Expand All @@ -210,7 +210,7 @@ impl ThemeSet {

#[cfg(test)]
mod tests {
#[cfg(all(feature = "yaml-load", any(feature = "dump-create", feature = "dump-create-rs"), any(feature = "dump-load", feature = "dump-load-rs")))]
#[cfg(all(feature = "yaml-load", feature = "dump-create", feature = "dump-load"))]
#[test]
fn can_dump_and_load() {
use super::*;
Expand All @@ -225,7 +225,7 @@ mod tests {
assert_eq!(ss.syntaxes().len(), ss2.syntaxes().len());
}

#[cfg(all(feature = "yaml-load", any(feature = "dump-create", feature = "dump-create-rs"), any(feature = "dump-load", feature = "dump-load-rs")))]
#[cfg(all(feature = "yaml-load", feature = "dump-create", feature = "dump-load"))]
#[test]
fn dump_is_deterministic() {
use super::*;
Expand All @@ -246,7 +246,7 @@ mod tests {
assert_eq!(bin1, bin2);
}

#[cfg(all(feature = "assets", any(feature = "dump-load", feature = "dump-load-rs")))]
#[cfg(all(feature = "assets", feature = "dump-load"))]
#[test]
fn has_default_themes() {
use crate::highlighting::ThemeSet;
Expand Down
2 changes: 1 addition & 1 deletion src/easy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ impl<'a> Iterator for ScopeRegionIterator<'a> {
}
}

#[cfg(all(feature = "assets", any(feature = "dump-load", feature = "dump-load-rs")))]
#[cfg(all(feature = "assets", feature = "dump-load"))]
#[cfg(test)]
mod tests {
use super::*;
Expand Down
2 changes: 1 addition & 1 deletion src/highlighting/highlighter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ impl<'a> Highlighter<'a> {
}
}

#[cfg(all(feature = "assets", feature = "parsing", any(feature = "dump-load", feature = "dump-load-rs")))]
#[cfg(all(feature = "assets", feature = "parsing", feature = "dump-load"))]
#[cfg(test)]
mod tests {
use super::*;
Expand Down
2 changes: 1 addition & 1 deletion src/html.rs
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,7 @@ pub fn start_highlighted_html_snippet(t: &Theme) -> (String, Color) {

#[cfg(all(
feature = "assets",
any(feature = "dump-load", feature = "dump-load-rs")
feature = "dump-load"
))]
#[cfg(test)]
mod tests {
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ extern crate serde_derive;
#[macro_use]
extern crate pretty_assertions;

#[cfg(any(feature = "dump-load-rs", feature = "dump-load", feature = "dump-create", feature = "dump-create-rs"))]
#[cfg(any(feature = "dump-load", feature = "dump-create"))]
pub mod dumps;
#[cfg(feature = "parsing")]
pub mod easy;
Expand Down

0 comments on commit 15ff701

Please sign in to comment.