Skip to content

Commit 3033239

Browse files
authored
update #31: Remove lazy_static dependency (#135)
1 parent 1517dee commit 3033239

File tree

9 files changed

+50
-53
lines changed

9 files changed

+50
-53
lines changed

Cargo.lock

Lines changed: 0 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ rust-embed = { workspace = true }
2727
unic-langid = { workspace = true }
2828
env_logger = { workspace = true }
2929
log = { workspace = true }
30-
lazy_static = { workspace = true }
3130

3231
[dev-dependencies]
3332
doc-comment = { workspace = true }
@@ -57,7 +56,6 @@ i18n-embed-fl = { version = "0.9.1", path = "./i18n-embed-fl" }
5756
thiserror = "1.0"
5857
log = "0.4"
5958
unic-langid = "0.9"
60-
lazy_static = "1.4.0"
6159
anyhow = "1.0"
6260
gettext = "0.4"
6361
tr = "0.1"

i18n-build/Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,9 @@ i18n-config = { workspace = true }
2929
gettext = { workspace = true, optional = true }
3030
log = { workspace = true }
3131
rust-embed = { workspace = true }
32-
lazy_static = { workspace = true, optional = true }
3332

3433
[features]
3534
default = []
3635

3736
# A feature to localize this library
38-
localize = ["i18n-embed", "gettext", "lazy_static"]
37+
localize = ["i18n-embed", "gettext"]

i18n-build/src/lib.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,30 +63,31 @@ pub fn run(crt: Crate) -> Result<()> {
6363

6464
#[cfg(feature = "localize")]
6565
mod localize_feature {
66-
use lazy_static::lazy_static;
67-
6866
use i18n_embed::{
6967
gettext::{gettext_language_loader, GettextLanguageLoader},
7068
DefaultLocalizer,
7169
};
70+
use std::sync::OnceLock;
7271

7372
use rust_embed::RustEmbed;
7473

7574
#[derive(RustEmbed)]
7675
#[folder = "i18n/mo"]
7776
pub struct Translations;
7877

79-
lazy_static! {
80-
static ref LANGUAGE_LOADER: GettextLanguageLoader = gettext_language_loader!();
81-
}
82-
8378
static TRANSLATIONS: Translations = Translations {};
8479

80+
fn language_loader() -> &'static GettextLanguageLoader {
81+
static LANGUAGE_LOADER: OnceLock<GettextLanguageLoader> = OnceLock::new();
82+
83+
LANGUAGE_LOADER.get_or_init(|| gettext_language_loader!())
84+
}
85+
8586
/// Obtain a [Localizer](i18n_embed::Localizer) for localizing this library.
8687
///
8788
/// ⚠️ *This API requires the following crate features to be activated: `localize`.*
8889
pub fn localizer() -> DefaultLocalizer<'static> {
89-
DefaultLocalizer::new(&*LANGUAGE_LOADER, &TRANSLATIONS)
90+
DefaultLocalizer::new(&*language_loader(), &TRANSLATIONS)
9091
}
9192
}
9293

i18n-embed-fl/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ fluent = { workspace = true }
1717
fluent-syntax = { workspace = true }
1818
i18n-config = { workspace = true }
1919
i18n-embed = { workspace = true, features = ["fluent-system", "filesystem-assets"]}
20-
lazy_static = { workspace = true }
2120
proc-macro2 = { workspace = true }
2221
proc-macro-error2 = "2.0.1"
2322
quote = { workspace = true }

i18n-embed-fl/src/lib.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use quote::quote;
77
use std::{
88
collections::{HashMap, HashSet},
99
path::Path,
10+
sync::OnceLock,
1011
};
1112
use syn::{parse::Parse, parse_macro_input, spanned::Spanned};
1213
use unic_langid::LanguageIdentifier;
@@ -164,11 +165,10 @@ struct DomainSpecificData {
164165
_assets: FileSystemAssets,
165166
}
166167

167-
lazy_static::lazy_static! {
168-
/// Cached data specific to each localization domain, to improve
169-
/// performance of subsequent macro invocations.
170-
static ref DOMAINS: dashmap::DashMap<String, DomainSpecificData> =
171-
dashmap::DashMap::new();
168+
fn domains() -> &'static dashmap::DashMap<String, DomainSpecificData> {
169+
static DOMAINS: OnceLock<dashmap::DashMap<String, DomainSpecificData>> = OnceLock::new();
170+
171+
DOMAINS.get_or_init(|| dashmap::DashMap::new())
172172
}
173173

174174
/// A macro to obtain localized messages and optionally their attributes, and check the `message_id`, `attribute_id`
@@ -347,7 +347,7 @@ pub fn fl(input: TokenStream) -> TokenStream {
347347
)
348348
};
349349

350-
let domain_data = if let Some(domain_data) = DOMAINS.get(&domain) {
350+
let domain_data = if let Some(domain_data) = domains().get(&domain) {
351351
domain_data
352352
} else {
353353
let crate_paths = i18n_config::locate_crate_paths()
@@ -421,7 +421,7 @@ pub fn fl(input: TokenStream) -> TokenStream {
421421
_assets: assets,
422422
};
423423

424-
DOMAINS.entry(domain.clone()).or_insert(data).downgrade()
424+
domains().entry(domain.clone()).or_insert(data).downgrade()
425425
};
426426

427427
let message_id_string = match &message_id {

i18n-embed/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ fluent-syntax = { workspace = true, optional = true }
2525
gettext = { workspace = true, optional = true }
2626
i18n-embed-impl = { workspace = true, optional = true }
2727
intl-memoizer = "0.5"
28-
lazy_static = { workspace = true }
2928
locale_config = { version = "0.3", optional = true }
3029
log = { workspace = true }
3130
notify = { version = "6.1.1", optional = true }

i18n-embed/src/lib.rs

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -248,32 +248,34 @@
248248
//! ```
249249
//! # #[cfg(all(feature = "fluent-system", feature = "desktop-requester"))]
250250
//! # {
251-
//! use std::sync::Arc;
251+
//! use std::sync::{Arc, OnceLock};
252252
//! use i18n_embed::{
253253
//! DesktopLanguageRequester, LanguageRequester,
254254
//! DefaultLocalizer, Localizer, fluent::FluentLanguageLoader
255255
//! };
256-
//! use rust_embed::RustEmbed; use lazy_static::lazy_static;
256+
//! use rust_embed::RustEmbed;
257257
//! use unic_langid::LanguageIdentifier;
258258
//!
259259
//! #[derive(RustEmbed)]
260260
//! #[folder = "i18n/ftl"] // path to localization resources
261261
//! struct Localizations;
262262
//!
263-
//! lazy_static! {
264-
//! static ref LANGUAGE_LOADER: FluentLanguageLoader = {
263+
//! pub fn language_loader() -> &'static FluentLanguageLoader {
264+
//! static LANGUAGE_LOADER: OnceLock<FluentLanguageLoader> = OnceLock::new();
265+
//!
266+
//! LANGUAGE_LOADER.get_or_init(|| {
265267
//! // Usually you could use the fluent_language_loader!() macro
266268
//! // to pull values from i18n.toml configuration and current
267269
//! // module here at compile time, but instantiating the loader
268270
//! // manually here instead so the example compiles.
269271
//! let fallback: LanguageIdentifier = "en-US".parse().unwrap();
270272
//! FluentLanguageLoader::new("test", fallback)
271-
//! };
273+
//! })
272274
//! }
273275
//!
274276
//! # #[allow(dead_code)]
275277
//! fn main() {
276-
//! let localizer = DefaultLocalizer::new(&*LANGUAGE_LOADER, &Localizations);
278+
//! let localizer = DefaultLocalizer::new(&*language_loader(), &Localizations);
277279
//!
278280
//! let localizer_arc: Arc<dyn Localizer> = Arc::new(localizer);
279281
//!
@@ -297,7 +299,7 @@
297299
//! [DefaultLocalizer](DefaultLocalizer), but you can also implement
298300
//! the [Localizer](Localizer) trait yourself for a custom solution.
299301
//! It also makes use of
300-
//! [lazy_static](https://crates.io/crates/lazy_static) to allow the
302+
//! [OnceLock](https://doc.rust-lang.org/beta/std/sync/struct.OnceLock.html) to allow the
301303
//! [LanguageLoader](LanguageLoader) implementation to be stored
302304
//! statically, because its constructor is not `const`.
303305
//!
@@ -309,21 +311,23 @@
309311
//! ```
310312
//! # #[cfg(feature = "fluent-system")]
311313
//! # {
312-
//! use std::sync::Arc;
314+
//! use std::sync::{Arc, OnceLock};
313315
//! use i18n_embed::{
314316
//! DefaultLocalizer, Localizer, LanguageLoader,
315317
//! fluent::{
316318
//! fluent_language_loader, FluentLanguageLoader
317319
//! }};
318-
//! use rust_embed::RustEmbed; use lazy_static::lazy_static;
320+
//! use rust_embed::RustEmbed;
319321
//!
320322
//! #[derive(RustEmbed)]
321323
//! #[folder = "i18n/mo"] // path to the compiled localization resources
322324
//! struct Localizations;
323325
//!
324-
//! lazy_static! {
325-
//! static ref LANGUAGE_LOADER: FluentLanguageLoader = {
326-
//! let loader = fluent_language_loader!();
326+
//! fn language_loader() -> &'static FluentLanguageLoader {
327+
//! static LANGUAGE_LOADER: OnceLock<FluentLanguageLoader> = OnceLock::new();
328+
//!
329+
//! LANGUAGE_LOADER.get_or_init(|| {
330+
//! let loader = fluent_language_loader!();
327331
//!
328332
//! // Load the fallback langauge by default so that users of the
329333
//! // library don't need to if they don't care about localization.
@@ -332,13 +336,13 @@
332336
//! .expect("Error while loading fallback language");
333337
//!
334338
//! loader
335-
//! };
339+
//! })
336340
//! }
337341
//!
338342
//! // Get the `Localizer` to be used for localizing this library.
339343
//! # #[allow(unused)]
340344
//! pub fn localizer() -> Arc<dyn Localizer> {
341-
//! Arc::new(DefaultLocalizer::new(&*LANGUAGE_LOADER, &Localizations))
345+
//! Arc::new(DefaultLocalizer::new(&*language_loader(), &Localizations))
342346
//! }
343347
//! # }
344348
//! ```
@@ -355,27 +359,27 @@
355359
//! for the library:
356360
//!
357361
//! ```
358-
//! # #[cfg(feature = "gettext-system")]
362+
//! #[cfg(feature = "gettext-system")]
359363
//! # {
360-
//! use std::sync::Arc;
364+
//! use std::sync::{Arc, OnceLock};
361365
//! use i18n_embed::{
362366
//! DefaultLocalizer, Localizer, gettext::{
363367
//! gettext_language_loader, GettextLanguageLoader
364368
//! }};
365369
//! use i18n_embed::I18nAssets;
366-
//! use lazy_static::lazy_static;
367370
//!
368-
//! lazy_static! {
369-
//! static ref LANGUAGE_LOADER: GettextLanguageLoader =
370-
//! gettext_language_loader!();
371+
//! fn language_loader() -> &'static GettextLanguageLoader {
372+
//! static LANGUAGE_LOADER: OnceLock<GettextLanguageLoader> = OnceLock::new();
373+
//!
374+
//! LANGUAGE_LOADER.get_or_init(|| gettext_language_loader!())
371375
//! }
372376
//!
373377
//! /// Get the `Localizer` to be used for localizing this library,
374378
//! /// using the provided embedded source of language files `embed`.
375379
//! # #[allow(unused)]
376380
//! pub fn localizer<'a>(embed: &'a (dyn I18nAssets + Send + Sync + 'static)) -> Arc<dyn Localizer + 'a> {
377381
//! Arc::new(DefaultLocalizer::new(
378-
//! &*LANGUAGE_LOADER,
382+
//! &*language_loader(),
379383
//! embed
380384
//! ))
381385
//! }

src/main.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,10 @@ use i18n_embed::{
66
gettext::{gettext_language_loader, GettextLanguageLoader},
77
DefaultLocalizer, DesktopLanguageRequester, LanguageLoader, LanguageRequester, Localizer,
88
};
9-
use lazy_static::lazy_static;
109
use rust_embed::RustEmbed;
1110
use std::{
1211
path::{Path, PathBuf},
13-
sync::Arc,
12+
sync::{Arc, OnceLock},
1413
};
1514
use tr::tr;
1615
use unic_langid::LanguageIdentifier;
@@ -19,12 +18,14 @@ use unic_langid::LanguageIdentifier;
1918
#[folder = "i18n/mo"]
2019
struct Translations;
2120

22-
lazy_static! {
23-
static ref LANGUAGE_LOADER: GettextLanguageLoader = gettext_language_loader!();
24-
}
25-
2621
static TRANSLATIONS: Translations = Translations {};
2722

23+
fn language_loader() -> &'static GettextLanguageLoader {
24+
static LANGUAGE_LOADER: OnceLock<GettextLanguageLoader> = OnceLock::new();
25+
26+
LANGUAGE_LOADER.get_or_init(|| gettext_language_loader!())
27+
}
28+
2829
/// Produce the message to be displayed when running `cargo i18n -h`.
2930
fn short_about() -> String {
3031
// The help message displayed when running `cargo i18n -h`.
@@ -76,7 +77,7 @@ fn main() -> Result<()> {
7677
let mut language_requester = DesktopLanguageRequester::new();
7778

7879
let cargo_i18n_localizer: DefaultLocalizer<'static> =
79-
DefaultLocalizer::new(&*LANGUAGE_LOADER, &TRANSLATIONS);
80+
DefaultLocalizer::new(&*language_loader(), &TRANSLATIONS);
8081

8182
let cargo_i18n_localizer_rc: Arc<dyn Localizer> = Arc::new(cargo_i18n_localizer);
8283
let i18n_build_localizer_rc: Arc<dyn Localizer> = Arc::new(i18n_build::localizer());
@@ -86,7 +87,7 @@ fn main() -> Result<()> {
8687
language_requester.poll()?;
8788

8889
let fallback_locale: &'static str =
89-
String::leak(LANGUAGE_LOADER.fallback_language().to_string());
90+
String::leak(language_loader().fallback_language().to_string());
9091
let available_languages = available_languages(&*cargo_i18n_localizer_rc)?;
9192
let available_languages_slice: Vec<&'static str> = available_languages
9293
.into_iter()

0 commit comments

Comments
 (0)