Skip to content

Commit 7598f94

Browse files
committed
update #131: Make dashmap optional.
You can activate the dashmap implementation by activating the feature flag `performance` in your `Cargo.toml` file. This will cause an additional usage of dashmap. Alternatively, you may compile directly with ``` cargo build --features performance ```
1 parent 412487e commit 7598f94

File tree

2 files changed

+50
-5
lines changed

2 files changed

+50
-5
lines changed

i18n-embed-fl/Cargo.toml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ license = "MIT"
1111
proc-macro = true
1212

1313
[dependencies]
14-
dashmap = "6.0"
14+
dashmap = { version = "6.0", optional = true }
1515
find-crate = { workspace = true }
1616
fluent = { workspace = true }
1717
fluent-syntax = { workspace = true }
@@ -32,3 +32,7 @@ doc-comment = { workspace = true }
3232
env_logger = { workspace = true }
3333
pretty_assertions = { workspace = true }
3434
rust-embed = { workspace = true }
35+
36+
[features]
37+
# Enables more performant version of the crate. May download extra dependencies.
38+
performance = ["dep:dashmap"]

i18n-embed-fl/src/lib.rs

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@ use std::{
99
path::Path,
1010
sync::OnceLock,
1111
};
12+
13+
#[cfg(not(feature = "performance"))]
14+
use std::sync::{Arc, RwLock};
15+
16+
#[cfg(feature = "performance")]
17+
use dashmap::{DashMap, Entry, mapref::one::Ref};
18+
1219
use syn::{parse::Parse, parse_macro_input, spanned::Spanned};
1320
use unic_langid::LanguageIdentifier;
1421

@@ -165,10 +172,44 @@ struct DomainSpecificData {
165172
_assets: FileSystemAssets,
166173
}
167174

168-
fn domains() -> &'static dashmap::DashMap<String, DomainSpecificData> {
169-
static DOMAINS: OnceLock<dashmap::DashMap<String, DomainSpecificData>> = OnceLock::new();
175+
#[derive(Default)]
176+
struct DomainsMap {
177+
#[cfg(not(feature = "performance"))]
178+
map: RwLock<HashMap<String, Arc<DomainSpecificData>>>,
179+
180+
#[cfg(feature = "performance")]
181+
map: dashmap::DashMap<String, DomainSpecificData>
182+
}
183+
184+
impl DomainsMap {
185+
#[cfg(not(feature = "performance"))]
186+
fn get(&self, domain: &String) -> Option<Arc<DomainSpecificData>> {
187+
match self.map.read().unwrap().get(domain) {
188+
None => { None}
189+
Some(data) => { Some(data.clone()) }
190+
}
191+
}
192+
193+
#[cfg(not(feature = "performance"))]
194+
fn entry_or_insert(&self, domain: &String, data: DomainSpecificData) -> Arc<DomainSpecificData> {
195+
self.map.write().unwrap().entry(domain.clone()).or_insert(Arc::new(data)).clone()
196+
}
197+
198+
#[cfg(feature = "performance")]
199+
fn get(&self, domain: &String) -> Option<Ref<String, DomainSpecificData>> {
200+
self.map.get(domain)
201+
}
202+
203+
#[cfg(feature = "performance")]
204+
fn entry_or_insert(&self, domain: &String, data: DomainSpecificData) -> Ref<String, DomainSpecificData> {
205+
self.map.entry(domain.clone()).or_insert(data).downgrade()
206+
}
207+
}
208+
209+
fn domains() -> &'static DomainsMap {
210+
static DOMAINS: OnceLock<DomainsMap> = OnceLock::new();
170211

171-
DOMAINS.get_or_init(|| dashmap::DashMap::new())
212+
DOMAINS.get_or_init(|| DomainsMap::default())
172213
}
173214

174215
/// A macro to obtain localized messages and optionally their attributes, and check the `message_id`, `attribute_id`
@@ -421,7 +462,7 @@ pub fn fl(input: TokenStream) -> TokenStream {
421462
_assets: assets,
422463
};
423464

424-
domains().entry(domain.clone()).or_insert(data).downgrade()
465+
domains().entry_or_insert(&domain, data)
425466
};
426467

427468
let message_id_string = match &message_id {

0 commit comments

Comments
 (0)