From c49da613eac2f0f0b3ac0c2d08c30e27a2b9e137 Mon Sep 17 00:00:00 2001 From: Tpt Date: Fri, 17 Jan 2020 15:04:04 +0100 Subject: [PATCH] Reduces lazy_static usages --- lib/src/sparql/algebra.rs | 8 ++--- lib/src/sparql/parser.rs | 62 ++++++++++++++++----------------------- 2 files changed, 30 insertions(+), 40 deletions(-) diff --git a/lib/src/sparql/algebra.rs b/lib/src/sparql/algebra.rs index 0af8c0a2..5416ef48 100644 --- a/lib/src/sparql/algebra.rs +++ b/lib/src/sparql/algebra.rs @@ -2,7 +2,6 @@ use crate::model::*; use crate::sparql::model::*; -use lazy_static::lazy_static; use rio_api::iri::Iri; use rio_api::model as rio; use std::collections::BTreeMap; @@ -1201,9 +1200,10 @@ impl fmt::Display for DatasetSpec { } } -lazy_static! { - static ref EMPTY_DATASET: DatasetSpec = DatasetSpec::default(); -} +const EMPTY_DATASET: DatasetSpec = DatasetSpec { + default: Vec::new(), + named: Vec::new(), +}; #[derive(Eq, PartialEq, Debug, Clone, Hash)] pub enum QueryVariants { diff --git a/lib/src/sparql/parser.rs b/lib/src/sparql/parser.rs index c2e8e8fe..aba6ccd7 100644 --- a/lib/src/sparql/parser.rs +++ b/lib/src/sparql/parser.rs @@ -14,7 +14,6 @@ mod grammar { use crate::model::*; use crate::sparql::algebra::*; use crate::sparql::model::*; - use lazy_static::lazy_static; use rio_api::iri::{Iri, IriParseError}; use std::borrow::Cow; use std::char; @@ -416,7 +415,7 @@ mod grammar { pub fn unescape_characters<'a>( input: &'a str, characters: &'static [u8], - replacement: &'static StaticSliceMap, + replacement: &'static StaticCharSliceMap, ) -> Cow<'a, str> { if needs_unescape_characters(input, characters) { UnescapeCharsIterator::new(input, replacement).collect() @@ -438,11 +437,11 @@ mod grammar { struct UnescapeCharsIterator<'a> { iter: Chars<'a>, buffer: Option, - replacement: &'static StaticSliceMap, + replacement: &'static StaticCharSliceMap, } impl<'a> UnescapeCharsIterator<'a> { - fn new(string: &'a str, replacement: &'static StaticSliceMap) -> Self { + fn new(string: &'a str, replacement: &'static StaticCharSliceMap) -> Self { Self { iter: string.chars(), buffer: None, @@ -475,22 +474,17 @@ mod grammar { } } - pub struct StaticSliceMap { - keys: &'static [K], - values: &'static [V], + pub struct StaticCharSliceMap { + keys: &'static [char], + values: &'static [char], } - impl StaticSliceMap { - pub fn new(keys: &'static [K], values: &'static [V]) -> Self { - assert_eq!( - keys.len(), - values.len(), - "keys and values slices of StaticSliceMap should have the same size" - ); + impl StaticCharSliceMap { + pub const fn new(keys: &'static [char], values: &'static [char]) -> Self { Self { keys, values } } - pub fn get(&self, key: K) -> Option { + pub fn get(&self, key: char) -> Option { for i in 0..self.keys.len() { if self.keys[i] == key { return Some(self.values[i]); @@ -501,15 +495,13 @@ mod grammar { } const UNESCAPE_CHARACTERS: [u8; 8] = [b't', b'b', b'n', b'r', b'f', b'"', b'\'', b'\\']; - lazy_static! { - static ref UNESCAPE_REPLACEMENT: StaticSliceMap = StaticSliceMap::new( - &['t', 'b', 'n', 'r', 'f', '"', '\'', '\\'], - &[ - '\u{0009}', '\u{0008}', '\u{000A}', '\u{000D}', '\u{000C}', '\u{0022}', '\u{0027}', - '\u{005C}' - ] - ); - } + const UNESCAPE_REPLACEMENT: StaticCharSliceMap = StaticCharSliceMap::new( + &['t', 'b', 'n', 'r', 'f', '"', '\'', '\\'], + &[ + '\u{0009}', '\u{0008}', '\u{000A}', '\u{000D}', '\u{000C}', '\u{0022}', '\u{0027}', + '\u{005C}', + ], + ); fn unescape_echars(input: &str) -> Cow<'_, str> { unescape_characters(input, &UNESCAPE_CHARACTERS, &UNESCAPE_REPLACEMENT) @@ -519,18 +511,16 @@ mod grammar { b'_', b'~', b'.', b'-', b'!', b'$', b'&', b'\'', b'(', b')', b'*', b'+', b',', b';', b'=', b'/', b'?', b'#', b'@', b'%', ]; - lazy_static! { - static ref UNESCAPE_PN_REPLACEMENT: StaticSliceMap = StaticSliceMap::new( - &[ - '_', '~', '.', '-', '!', '$', '&', '\'', '(', ')', '*', '+', ',', ';', '=', '/', - '?', '#', '@', '%' - ], - &[ - '_', '~', '.', '-', '!', '$', '&', '\'', '(', ')', '*', '+', ',', ';', '=', '/', - '?', '#', '@', '%' - ] - ); - } + const UNESCAPE_PN_REPLACEMENT: StaticCharSliceMap = StaticCharSliceMap::new( + &[ + '_', '~', '.', '-', '!', '$', '&', '\'', '(', ')', '*', '+', ',', ';', '=', '/', '?', + '#', '@', '%', + ], + &[ + '_', '~', '.', '-', '!', '$', '&', '\'', '(', ')', '*', '+', ',', ';', '=', '/', '?', + '#', '@', '%', + ], + ); pub fn unescape_pn_local(input: &str) -> Cow<'_, str> { unescape_characters(input, &UNESCAPE_PN_CHARACTERS, &UNESCAPE_PN_REPLACEMENT)