Skip to content

Commit c0fdd69

Browse files
authored
Merge pull request #66 from whisperfish/once-cell
Once cell instead of lazy_static
2 parents 1f54239 + 7d84cc3 commit c0fdd69

File tree

15 files changed

+377
-374
lines changed

15 files changed

+377
-374
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ bincode = "1.3"
1717
either = "1.11"
1818
fnv = "1"
1919
itertools = ">=0.10, <= 0.12"
20-
lazy_static = "1.4"
20+
once_cell = "1"
2121
nom = "7.1"
2222
quick-xml = ">=0.28, <= 0.31"
2323
regex = "1.7"

clippy.toml

Whitespace-only changes.

src/carrier.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ impl AsRef<str> for Carrier {
4141
}
4242

4343
impl fmt::Display for Carrier {
44-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
44+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
4545
write!(f, "{}", self.0)
4646
}
4747
}

src/consts.rs

Lines changed: 329 additions & 324 deletions
Large diffs are not rendered by default.

src/country.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,4 +328,4 @@ pub enum Id {
328328
ZW,
329329
}
330330

331-
pub use self::Id::*;
331+
pub use Id::*;

src/extension.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ impl AsRef<str> for Extension {
4141
}
4242

4343
impl fmt::Display for Extension {
44-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
44+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
4545
write!(f, "{}", self.0)
4646
}
4747
}

src/formatter.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ pub fn format_with<'d, 'n>(
9898
}
9999

100100
impl<'n, 'd, 'f> fmt::Display for Formatter<'n, 'd, 'f> {
101-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
101+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
102102
let db = self.database.unwrap_or(&DATABASE);
103103

104104
// If the country code is invalid, return an error.
@@ -247,9 +247,9 @@ fn replace(
247247
.get(1)
248248
.unwrap()
249249
.as_str();
250-
let format = transform.replace(*consts::NP, meta.national_prefix().unwrap_or(""));
251-
let format = format.replace(*consts::FG, &format!("${}", first));
252-
let format = format.replace(*consts::CC, carrier.unwrap_or(""));
250+
let format = transform.replace(consts::NP, meta.national_prefix().unwrap_or(""));
251+
let format = format.replace(consts::FG, &format!("${}", first));
252+
let format = format.replace(consts::CC, carrier.unwrap_or(""));
253253

254254
consts::FIRST_GROUP.replace(formatter.format(), &*format)
255255
} else {

src/metadata/database.rs

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,29 +12,33 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
use crate::error;
16+
use crate::metadata::loader;
17+
use crate::Metadata;
18+
use bincode;
19+
use bincode::Options;
20+
use fnv::FnvHashMap;
21+
use once_cell::sync::Lazy;
22+
use regex_cache::{CachedRegex, CachedRegexBuilder, RegexCache};
1523
use std::borrow::Borrow;
1624
use std::fs::File;
1725
use std::hash::Hash;
1826
use std::io::{BufReader, Cursor};
1927
use std::path::Path;
2028
use std::sync::{Arc, Mutex};
2129

22-
use bincode::Options;
23-
use fnv::FnvHashMap;
24-
use regex_cache::{CachedRegex, CachedRegexBuilder, RegexCache};
25-
26-
use crate::error;
27-
use crate::metadata::loader;
28-
use crate::Metadata;
29-
3030
const DATABASE: &[u8] = include_bytes!(concat!(env!("OUT_DIR"), "/database.bin"));
3131

32-
lazy_static::lazy_static! {
33-
/// The Google provided metadata database, used as default.
34-
pub static ref DEFAULT: Database =
35-
Database::from(bincode::options()
36-
.with_varint_encoding().deserialize(DATABASE).unwrap()).unwrap();
37-
}
32+
/// The Google provided metadata database, used as default.
33+
pub static DEFAULT: Lazy<Database> = Lazy::new(|| {
34+
Database::from(
35+
bincode::options()
36+
.with_varint_encoding()
37+
.deserialize(DATABASE)
38+
.unwrap(),
39+
)
40+
.unwrap()
41+
});
3842

3943
/// Representation of a database of metadata for phone number.
4044
#[derive(Clone, Debug)]

src/national_number.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ impl From<NationalNumber> for u64 {
6767
}
6868

6969
impl fmt::Display for NationalNumber {
70-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
70+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
7171
for _ in 0..self.zeros() {
7272
write!(f, "0")?;
7373
}

src/parser/helper.rs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,22 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
use crate::consts;
16+
use crate::country;
17+
use crate::error;
18+
use crate::metadata::{Database, Metadata};
19+
use crate::phone_number::Type;
20+
use crate::validator;
21+
use fnv::FnvHashMap;
1522
use nom::{
1623
character::complete::*,
1724
combinator::*,
1825
error::{make_error, ErrorKind},
1926
multi::*,
2027
AsChar, IResult,
2128
};
22-
use std::borrow::Cow;
23-
24-
use fnv::FnvHashMap;
2529
use regex_cache::CachedRegex;
26-
27-
use crate::consts;
28-
use crate::country;
29-
use crate::error;
30-
use crate::metadata::{Database, Metadata};
31-
use crate::phone_number::Type;
32-
use crate::validator;
30+
use std::borrow::Cow;
3331

3432
macro_rules! parse {
3533
($input:ident => ) => ();
@@ -369,7 +367,7 @@ pub fn normalize<'a>(mut number: Number<'a>, mappings: &FnvHashMap<char, char>)
369367
number
370368
}
371369

372-
pub fn trim(value: Cow<str>, start: usize) -> Cow<str> {
370+
pub fn trim(value: Cow<'_, str>, start: usize) -> Cow<'_, str> {
373371
match value {
374372
Cow::Borrowed(value) => Cow::Borrowed(&value[start..]),
375373

0 commit comments

Comments
 (0)