Skip to content

Commit

Permalink
dep: replace lazy_static with once_cell
Browse files Browse the repository at this point in the history
This is in general a good idea, but was motivated
specifically by BurntSushi/bstr#124

PR #52
  • Loading branch information
dtolnay committed Sep 3, 2022
1 parent 6d3aae3 commit 2c98902
Show file tree
Hide file tree
Showing 21 changed files with 145 additions and 142 deletions.
12 changes: 6 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Expand Up @@ -37,7 +37,7 @@ default-features = false
features = ["suggestions"]

[dev-dependencies]
lazy_static = "1"
once_cell = "1"

[profile.release]
debug = true
2 changes: 0 additions & 2 deletions benches/bench.rs
@@ -1,7 +1,5 @@
#![feature(test)]

#[macro_use]
extern crate lazy_static;
extern crate test;

use std::cmp::Ordering;
Expand Down
13 changes: 8 additions & 5 deletions benches/tables/fst/general_category.rs
Expand Up @@ -38,8 +38,11 @@ pub const GENERAL_CATEGORY_ENUM: &'static [&'static str] = &[
"Uppercase_Letter",
];

lazy_static::lazy_static! {
pub static ref GENERAL_CATEGORY: ::fst::Map<&'static [u8]> =
::fst::Map::from(::fst::raw::Fst::new(
&include_bytes!("general_category.fst")[..]).unwrap());
}
pub static GENERAL_CATEGORY: ::once_cell::sync::Lazy<
::fst::Map<&'static [u8]>,
> = ::once_cell::sync::Lazy::new(|| {
::fst::Map::from(
::fst::raw::Fst::new(&include_bytes!("general_category.fst")[..])
.unwrap(),
)
});
13 changes: 8 additions & 5 deletions benches/tables/fst/jamo_short_name.rs
Expand Up @@ -6,8 +6,11 @@
//
// ucd-generate 0.2.10 is available on crates.io.

lazy_static::lazy_static! {
pub static ref JAMO_SHORT_NAME: ::fst::Map<&'static [u8]> =
::fst::Map::from(::fst::raw::Fst::new(
&include_bytes!("jamo_short_name.fst")[..]).unwrap());
}
pub static JAMO_SHORT_NAME: ::once_cell::sync::Lazy<
::fst::Map<&'static [u8]>,
> = ::once_cell::sync::Lazy::new(|| {
::fst::Map::from(
::fst::raw::Fst::new(&include_bytes!("jamo_short_name.fst")[..])
.unwrap(),
)
});
11 changes: 6 additions & 5 deletions benches/tables/fst/names.rs
Expand Up @@ -6,8 +6,9 @@
//
// ucd-generate 0.2.10 is available on crates.io.

lazy_static::lazy_static! {
pub static ref NAMES: ::fst::Map<&'static [u8]> =
::fst::Map::from(::fst::raw::Fst::new(
&include_bytes!("names.fst")[..]).unwrap());
}
pub static NAMES: ::once_cell::sync::Lazy<::fst::Map<&'static [u8]>> =
::once_cell::sync::Lazy::new(|| {
::fst::Map::from(
::fst::raw::Fst::new(&include_bytes!("names.fst")[..]).unwrap(),
)
});
22 changes: 10 additions & 12 deletions src/writer.rs
Expand Up @@ -916,19 +916,19 @@ impl Writer {
File::create(fst_file_path)?.write_all(&fst.to_vec())?;

let ty = if map { "Map" } else { "Set" };
writeln!(self.wtr, "lazy_static::lazy_static! {{")?;
writeln!(
self.wtr,
" pub static ref {}: ::fst::{}<&'static [u8]> = ",
"pub static {}: ::once_cell::sync::Lazy<::fst::{}<&'static [u8]>> =",
const_name, ty
)?;
writeln!(self.wtr, " ::once_cell::sync::Lazy::new(|| {{")?;
writeln!(self.wtr, " ::fst::{}::from(::fst::raw::Fst::new(", ty)?;
writeln!(
self.wtr,
" &include_bytes!({:?})[..]).unwrap());",
" &include_bytes!({:?})[..]).unwrap())",
fst_file_name
)?;
writeln!(self.wtr, "}}")?;
writeln!(self.wtr, " }});")?;
Ok(())
}

Expand Down Expand Up @@ -1109,12 +1109,12 @@ impl Writer {
file_name_fwd: &str,
file_name_rev: &str,
) -> Result<()> {
writeln!(self.wtr, "lazy_static::lazy_static! {{")?;
writeln!(
self.wtr,
" pub static ref {}: ::regex_automata::{} = {{",
"pub static {}: ::once_cell::sync::Lazy<::regex_automata::{}> =",
const_name, full_regex_ty
)?;
writeln!(self.wtr, " ::once_cell::sync::Lazy::new(|| {{")?;

writeln!(self.wtr, " let fwd =")?;
self.write_dfa_deserialize(short_dfa_ty, align_to, file_name_fwd)?;
Expand All @@ -1128,8 +1128,7 @@ impl Writer {
self.wtr,
" ::regex_automata::Regex::from_dfas(fwd, rev)"
)?;
writeln!(self.wtr, " }};")?;
writeln!(self.wtr, "}}")?;
writeln!(self.wtr, " }});")?;

Ok(())
}
Expand All @@ -1142,15 +1141,14 @@ impl Writer {
align_to: &str,
file_name: &str,
) -> Result<()> {
writeln!(self.wtr, "lazy_static::lazy_static! {{")?;
writeln!(
self.wtr,
" pub static ref {}: ::regex_automata::{} = {{",
"pub static {}: ::once_cell::sync::Lazy<::regex_automata::{}> =",
const_name, full_dfa_ty
)?;
writeln!(self.wtr, " ::once_cell::sync::Lazy::new(|| {{")?;
self.write_dfa_deserialize(short_dfa_ty, align_to, file_name)?;
writeln!(self.wtr, " }};")?;
writeln!(self.wtr, "}}")?;
writeln!(self.wtr, " }});")?;

Ok(())
}
Expand Down
2 changes: 1 addition & 1 deletion ucd-parse/Cargo.toml
Expand Up @@ -14,7 +14,7 @@ license = "MIT OR Apache-2.0"
edition = "2018"

[dependencies]
lazy_static = "1"
once_cell = "1"

[dependencies.regex]
version = "1"
Expand Down
12 changes: 6 additions & 6 deletions ucd-parse/src/arabic_shaping.rs
@@ -1,7 +1,7 @@
use std::path::Path;
use std::str::FromStr;

use lazy_static::lazy_static;
use once_cell::sync::Lazy;
use regex::Regex;

use crate::common::{Codepoint, CodepointIter, UcdFile, UcdFileByCodepoint};
Expand Down Expand Up @@ -93,19 +93,19 @@ impl FromStr for ArabicShaping {
type Err = Error;

fn from_str(line: &str) -> Result<ArabicShaping, Error> {
lazy_static! {
static ref PARTS: Regex = Regex::new(
static PARTS: Lazy<Regex> = Lazy::new(|| {
Regex::new(
r"(?x)
^
\s*(?P<codepoint>[A-F0-9]+)\s*;
\s*(?P<name>[^;]+)\s*;
\s*(?P<joining_type>[^;]+)\s*;
\s*(?P<joining_group>[^;]+)
$
"
",
)
.unwrap();
};
.unwrap()
});
let caps = match PARTS.captures(line.trim()) {
Some(caps) => caps,
None => return err!("invalid ArabicShaping line"),
Expand Down
12 changes: 6 additions & 6 deletions ucd-parse/src/bidi_mirroring_glyph.rs
Expand Up @@ -2,7 +2,7 @@ use std::fmt;
use std::path::Path;
use std::str::FromStr;

use lazy_static::lazy_static;
use once_cell::sync::Lazy;
use regex::Regex;

use crate::common::{Codepoint, CodepointIter, UcdFile, UcdFileByCodepoint};
Expand Down Expand Up @@ -36,19 +36,19 @@ impl FromStr for BidiMirroring {
type Err = Error;

fn from_str(line: &str) -> Result<BidiMirroring, Error> {
lazy_static! {
static ref PARTS: Regex = Regex::new(
static PARTS: Lazy<Regex> = Lazy::new(|| {
Regex::new(
r"(?x)
^
\s*(?P<codepoint>[A-F0-9]+)\s*;
\s*(?P<substitute_codepoint>[A-F0-9]+)
\s+
\#(?:.+)
$
"
",
)
.unwrap();
};
.unwrap()
});
let caps = match PARTS.captures(line.trim()) {
Some(caps) => caps,
None => return err!("invalid BidiMirroring line"),
Expand Down
12 changes: 6 additions & 6 deletions ucd-parse/src/case_folding.rs
@@ -1,7 +1,7 @@
use std::path::Path;
use std::str::FromStr;

use lazy_static::lazy_static;
use once_cell::sync::Lazy;
use regex::Regex;

use crate::common::{Codepoint, CodepointIter, UcdFile, UcdFileByCodepoint};
Expand Down Expand Up @@ -42,17 +42,17 @@ impl FromStr for CaseFold {
type Err = Error;

fn from_str(line: &str) -> Result<CaseFold, Error> {
lazy_static! {
static ref PARTS: Regex = Regex::new(
static PARTS: Lazy<Regex> = Lazy::new(|| {
Regex::new(
r"(?x)
^
\s*(?P<codepoint>[^\s;]+)\s*;
\s*(?P<status>[^\s;]+)\s*;
\s*(?P<mapping>[^;]+)\s*;
"
",
)
.unwrap();
};
.unwrap()
});

let caps = match PARTS.captures(line.trim()) {
Some(caps) => caps,
Expand Down
46 changes: 23 additions & 23 deletions ucd-parse/src/common.rs
Expand Up @@ -7,7 +7,7 @@ use std::marker::PhantomData;
use std::path::{Path, PathBuf};
use std::str::FromStr;

use lazy_static::lazy_static;
use once_cell::sync::Lazy;
use regex::Regex;

use crate::error::{Error, ErrorKind};
Expand Down Expand Up @@ -85,10 +85,9 @@ pub fn ucd_directory_version<D: ?Sized + AsRef<Path>>(
fn ucd_directory_version_inner(
ucd_dir: &Path,
) -> Result<(u64, u64, u64), Error> {
lazy_static::lazy_static! {
static ref VERSION_RX: Regex =
Regex::new(r"-([0-9]+).([0-9]+).([0-9]+).txt").unwrap();
}
static VERSION_RX: Lazy<Regex> = Lazy::new(|| {
Regex::new(r"-([0-9]+).([0-9]+).([0-9]+).txt").unwrap()
});

let proplist = ucd_dir.join("PropList.txt");
let contents = first_line(&proplist)?;
Expand Down Expand Up @@ -140,16 +139,16 @@ fn first_line(path: &Path) -> Result<String, Error> {
pub fn parse_codepoint_association<'a>(
line: &'a str,
) -> Result<(Codepoints, &'a str), Error> {
lazy_static! {
static ref PARTS: Regex = Regex::new(
static PARTS: Lazy<Regex> = Lazy::new(|| {
Regex::new(
r"(?x)
^
\s*(?P<codepoints>[^\s;]+)\s*;
\s*(?P<property>[^;\x23]+)\s*
"
",
)
.unwrap();
};
.unwrap()
});

let caps = match PARTS.captures(line.trim()) {
Some(caps) => caps,
Expand Down Expand Up @@ -184,25 +183,27 @@ pub fn parse_codepoint_sequence(s: &str) -> Result<Vec<Codepoint>, Error> {
/// with the comment associated with the test. The comment is a human readable
/// description of the test that may prove useful for debugging.
pub fn parse_break_test(line: &str) -> Result<(Vec<String>, String), Error> {
lazy_static! {
static ref PARTS: Regex = Regex::new(
static PARTS: Lazy<Regex> = Lazy::new(|| {
Regex::new(
r"(?x)
^
(?:÷|×)
(?P<groups>(?:\s[0-9A-Fa-f]{4,5}\s(?:÷|×))+)
\s+
\#(?P<comment>.+)
$
"
",
)
.unwrap();
static ref GROUP: Regex = Regex::new(
.unwrap()
});
static GROUP: Lazy<Regex> = Lazy::new(|| {
Regex::new(
r"(?x)
(?P<codepoint>[0-9A-Fa-f]{4,5})\s(?P<kind>÷|×)
"
",
)
.unwrap();
}
.unwrap()
});

let caps = match PARTS.captures(line.trim()) {
Some(caps) => caps,
Expand Down Expand Up @@ -458,11 +459,10 @@ impl FromStr for CodepointRange {
type Err = Error;

fn from_str(s: &str) -> Result<CodepointRange, Error> {
lazy_static! {
static ref PARTS: Regex =
Regex::new(r"^(?P<start>[A-Z0-9]+)\.\.(?P<end>[A-Z0-9]+)$")
.unwrap();
}
static PARTS: Lazy<Regex> = Lazy::new(|| {
Regex::new(r"^(?P<start>[A-Z0-9]+)\.\.(?P<end>[A-Z0-9]+)$")
.unwrap()
});
let caps = match PARTS.captures(s) {
Some(caps) => caps,
None => return err!("invalid codepoint range: '{}'", s),
Expand Down

0 comments on commit 2c98902

Please sign in to comment.