Skip to content

Commit ea47b54

Browse files
committed
Separate sync feature
1 parent 348b598 commit ea47b54

File tree

9 files changed

+63
-6
lines changed

9 files changed

+63
-6
lines changed

fluent-bundle/Cargo.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ rand = "0.8"
4545
serde_yaml = "0.8"
4646

4747
[features]
48-
default = ["icu_provider/sync"]
49-
sync = ["icu_provider/sync"]
48+
default = []
49+
sync = ["intl-memoizer/sync", "icu_provider/sync"]
5050
all-benchmarks = []
5151

5252
[[bench]]

fluent-bundle/examples/custom_type.rs

+2
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,8 @@ impl FluentType for DateTime {
112112
})
113113
.expect("Failed to format a date.")
114114
}
115+
116+
#[cfg(feature = "sync")]
115117
fn as_string_threadsafe(
116118
&self,
117119
_: &intl_memoizer::concurrent::IntlLangMemoizer,

fluent-bundle/src/bundle.rs

+12
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,7 @@ impl crate::memoizer::MemoizerKind for IntlLangMemoizer {
598598
Self::new(lang)
599599
}
600600

601+
#[cfg(feature = "sync")]
601602
fn with_try_get_threadsafe<I, R, U>(&self, args: I::Args, cb: U) -> Result<R, I::Error>
602603
where
603604
Self: Sized,
@@ -608,6 +609,17 @@ impl crate::memoizer::MemoizerKind for IntlLangMemoizer {
608609
self.with_try_get(args, cb)
609610
}
610611

612+
#[cfg(not(feature = "sync"))]
613+
fn with_try_get<I, R, U>(&self, args: I::Args, cb: U) -> Result<R, I::Error>
614+
where
615+
Self: Sized,
616+
I: intl_memoizer::Memoizable + 'static,
617+
I::Args: 'static,
618+
U: FnOnce(&I) -> R,
619+
{
620+
self.with_try_get(args, cb)
621+
}
622+
611623
fn stringify_value(
612624
&self,
613625
value: &dyn crate::types::FluentType,

fluent-bundle/src/concurrent.rs

+18-2
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

+1
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@
100100
//! matures and higher level APIs are being developed.
101101
mod args;
102102
pub mod bundle;
103+
#[cfg(feature = "sync")]
103104
pub mod concurrent;
104105
mod entry;
105106
mod errors;

fluent-bundle/src/memoizer.rs

+9
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

+14-2
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,
@@ -199,14 +200,25 @@ impl<'source> FluentValue<'source> {
199200
};
200201
// This string matches a plural rule keyword. Check if the number
201202
// matches the plural rule category.
202-
scope
203+
#[cfg(feature = "sync")]
204+
let result = scope
203205
.bundle
204206
.intls
205207
.with_try_get_threadsafe::<PluralRules, _, _>(
206208
(PluralRuleType::Cardinal,),
207209
|pr| pr.0.category_for(b) == cat,
208210
)
209-
.unwrap()
211+
.unwrap();
212+
213+
#[cfg(not(feature = "sync"))]
214+
let result = scope
215+
.bundle
216+
.intls
217+
.with_try_get::<PluralRules, _, _>((PluralRuleType::Cardinal,), |pr| {
218+
pr.0.category_for(b) == cat
219+
})
220+
.unwrap();
221+
result
210222
}
211223
_ => false,
212224
}

fluent-bundle/tests/custom_types.rs

+4
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,
@@ -123,6 +125,8 @@ fn fluent_date_time_builtin() {
123125
fn as_string(&self, _: &intl_memoizer::IntlLangMemoizer) -> std::borrow::Cow<'static, str> {
124126
format!("2020-01-20 {}:00", self.epoch).into()
125127
}
128+
129+
#[cfg(feature = "sync")]
126130
fn as_string_threadsafe(
127131
&self,
128132
_intls: &intl_memoizer::concurrent::IntlLangMemoizer,

intl-memoizer/src/lib.rs

+1
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)