Skip to content

Commit 564f296

Browse files
authored
update #131: Make dashmap optional. (#141)
* 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 ``` * update #131: Run rustfmt * update #131: Rename feature to `dashmap` and change implementation.
1 parent af478af commit 564f296

File tree

2 files changed

+62
-5
lines changed

2 files changed

+62
-5
lines changed

i18n-embed-fl/Cargo.toml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ repository = "https://github.com/kellpossible/cargo-i18n/tree/master/i18n-embed-
1212
proc-macro = true
1313

1414
[dependencies]
15-
dashmap = "6.0"
15+
dashmap = { version = "6.0", optional = true }
1616
find-crate = { workspace = true }
1717
fluent = { workspace = true }
1818
fluent-syntax = { workspace = true }
@@ -33,3 +33,7 @@ doc-comment = { workspace = true }
3333
env_logger = { workspace = true }
3434
pretty_assertions = { workspace = true }
3535
rust-embed = { workspace = true }
36+
37+
[features]
38+
# Uses dashmap implementation for `fl!()` macro lookups.
39+
dashmap = ["dep:dashmap"]

i18n-embed-fl/src/lib.rs

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

@@ -165,10 +171,57 @@ struct DomainSpecificData {
165171
_assets: FileSystemAssets,
166172
}
167173

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

171-
DOMAINS.get_or_init(|| dashmap::DashMap::new())
224+
DOMAINS.get_or_init(|| DomainsMap::default())
172225
}
173226

174227
/// A macro to obtain localized messages and optionally their attributes, and check the `message_id`, `attribute_id`
@@ -421,7 +474,7 @@ pub fn fl(input: TokenStream) -> TokenStream {
421474
_assets: assets,
422475
};
423476

424-
domains().entry(domain.clone()).or_insert(data).downgrade()
477+
domains().entry_or_insert(&domain, data)
425478
};
426479

427480
let message_id_string = match &message_id {

0 commit comments

Comments
 (0)