Skip to content

Commit f2476cb

Browse files
zbranieckialerque
authored andcommitted
refactor: Separate sync feature
1 parent 706a688 commit f2476cb

File tree

9 files changed

+63
-6
lines changed

9 files changed

+63
-6
lines changed

fluent-bundle/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ rand = "0.8"
4343
serde_yaml = "0.9"
4444

4545
[features]
46-
default = ["icu_provider/sync"]
47-
sync = ["icu_provider/sync"]
46+
default = []
47+
sync = ["intl-memoizer/sync", "icu_provider/sync"]
4848
all-benchmarks = []
4949

5050
[[bench]]

fluent-bundle/examples/custom_type.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,8 @@ impl FluentType for DateTime {
107107
})
108108
.expect("Failed to format a date.")
109109
}
110+
111+
#[cfg(feature = "sync")]
110112
fn as_string_threadsafe(
111113
&self,
112114
_: &intl_memoizer::concurrent::IntlLangMemoizer,

fluent-bundle/src/bundle.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -653,6 +653,7 @@ impl crate::memoizer::MemoizerKind for IntlLangMemoizer {
653653
Self::new(lang)
654654
}
655655

656+
#[cfg(feature = "sync")]
656657
fn with_try_get_threadsafe<I, R, U>(&self, args: I::Args, cb: U) -> Result<R, I::Error>
657658
where
658659
Self: Sized,
@@ -663,6 +664,17 @@ impl crate::memoizer::MemoizerKind for IntlLangMemoizer {
663664
self.with_try_get(args, cb)
664665
}
665666

667+
#[cfg(not(feature = "sync"))]
668+
fn with_try_get<I, R, U>(&self, args: I::Args, cb: U) -> Result<R, I::Error>
669+
where
670+
Self: Sized,
671+
I: intl_memoizer::Memoizable + 'static,
672+
I::Args: 'static,
673+
U: FnOnce(&I) -> R,
674+
{
675+
self.with_try_get(args, cb)
676+
}
677+
666678
fn stringify_value(
667679
&self,
668680
value: &dyn crate::types::FluentType,

fluent-bundle/src/concurrent.rs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
use icu_locid::LanguageIdentifier;
2-
use intl_memoizer::{concurrent::IntlLangMemoizer, Memoizable};
2+
#[cfg(feature = "sync")]
3+
use intl_memoizer::concurrent::IntlLangMemoizer;
4+
#[cfg(not(feature = "sync"))]
5+
use intl_memoizer::IntlLangMemoizer;
6+
use intl_memoizer::Memoizable;
37
use rustc_hash::FxHashMap;
48

59
use crate::memoizer::MemoizerKind;
@@ -51,14 +55,26 @@ impl MemoizerKind for IntlLangMemoizer {
5155
Self::new(lang)
5256
}
5357

58+
#[cfg(feature = "sync")]
5459
fn with_try_get_threadsafe<I, R, U>(&self, args: I::Args, cb: U) -> Result<R, I::Error>
5560
where
5661
Self: Sized,
5762
I: Memoizable + Send + Sync + 'static,
5863
I::Args: Send + Sync + 'static,
5964
U: FnOnce(&I) -> R,
6065
{
61-
self.with_try_get(args, cb)
66+
Self::with_try_get(self, args, cb)
67+
}
68+
69+
#[cfg(not(feature = "sync"))]
70+
fn with_try_get<I, R, U>(&self, args: I::Args, cb: U) -> Result<R, I::Error>
71+
where
72+
Self: Sized,
73+
I: Memoizable + 'static,
74+
I::Args: 'static,
75+
U: FnOnce(&I) -> R,
76+
{
77+
Self::with_try_get(self, args, cb)
6278
}
6379

6480
fn stringify_value(&self, value: &dyn FluentType) -> std::borrow::Cow<'static, str> {

fluent-bundle/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@
101101
mod args;
102102
pub mod builtins;
103103
pub mod bundle;
104+
#[cfg(feature = "sync")]
104105
pub mod concurrent;
105106
mod entry;
106107
mod errors;

fluent-bundle/src/memoizer.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,22 @@ pub trait MemoizerKind: 'static {
1818
///
1919
/// `U` - The callback that accepts the instance of the intl formatter, and generates
2020
/// some kind of results `R`.
21+
#[cfg(feature = "sync")]
2122
fn with_try_get_threadsafe<I, R, U>(&self, args: I::Args, callback: U) -> Result<R, I::Error>
2223
where
2324
Self: Sized,
2425
I: Memoizable + Send + Sync + 'static,
2526
I::Args: Send + Sync + 'static,
2627
U: FnOnce(&I) -> R;
2728

29+
#[cfg(not(feature = "sync"))]
30+
fn with_try_get<I, R, U>(&self, args: I::Args, callback: U) -> Result<R, I::Error>
31+
where
32+
Self: Sized,
33+
I: Memoizable + 'static,
34+
I::Args: 'static,
35+
U: FnOnce(&I) -> R;
36+
2837
/// Wires up the `as_string` or `as_string_threadsafe` variants for [`FluentType`].
2938
fn stringify_value(&self, value: &dyn FluentType) -> std::borrow::Cow<'static, str>;
3039
}

fluent-bundle/src/types/mod.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ pub trait FluentType: fmt::Debug + AnyEq + 'static {
4141
/// Convert the custom type into a string value, for instance a custom `DateTime`
4242
/// type could return "Oct. 27, 2022". This operation is provided the threadsafe
4343
/// [`IntlLangMemoizer`](intl_memoizer::concurrent::IntlLangMemoizer).
44+
#[cfg(feature = "sync")]
4445
fn as_string_threadsafe(
4546
&self,
4647
intls: &intl_memoizer::concurrent::IntlLangMemoizer,
@@ -203,14 +204,25 @@ impl<'source> FluentValue<'source> {
203204
FluentNumberType::Cardinal => PluralRuleType::CARDINAL,
204205
FluentNumberType::Ordinal => PluralRuleType::ORDINAL,
205206
};
206-
scope
207+
#[cfg(feature = "sync")]
208+
let result = scope
207209
.bundle
208210
.intls
209211
.with_try_get_threadsafe::<PluralRules, _, _>(
210212
(PluralRuleType::Cardinal,),
211213
|pr| pr.0.category_for(b) == cat,
212214
)
213-
.unwrap()
215+
.unwrap();
216+
217+
#[cfg(not(feature = "sync"))]
218+
let result = scope
219+
.bundle
220+
.intls
221+
.with_try_get::<PluralRules, _, _>((PluralRuleType::Cardinal,), |pr| {
222+
pr.0.category_for(b) == cat
223+
})
224+
.unwrap();
225+
result
214226
}
215227
_ => false,
216228
}

fluent-bundle/tests/custom_types.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ fn fluent_custom_type() {
2626
fn as_string(&self, _: &intl_memoizer::IntlLangMemoizer) -> std::borrow::Cow<'static, str> {
2727
format!("{}", self.epoch).into()
2828
}
29+
30+
#[cfg(feature = "sync")]
2931
fn as_string_threadsafe(
3032
&self,
3133
_: &intl_memoizer::concurrent::IntlLangMemoizer,
@@ -118,6 +120,8 @@ fn fluent_date_time_builtin() {
118120
fn as_string(&self, _: &intl_memoizer::IntlLangMemoizer) -> std::borrow::Cow<'static, str> {
119121
format!("2020-01-20 {}:00", self.epoch).into()
120122
}
123+
124+
#[cfg(feature = "sync")]
121125
fn as_string_threadsafe(
122126
&self,
123127
_intls: &intl_memoizer::concurrent::IntlLangMemoizer,

intl-memoizer/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use std::collections::HashMap;
1111
use std::hash::Hash;
1212
use std::rc::{Rc, Weak};
1313

14+
#[cfg(feature = "sync")]
1415
pub mod concurrent;
1516

1617
/// The trait that needs to be implemented for each intl formatter that needs to be

0 commit comments

Comments
 (0)