Skip to content

Commit 1b41e96

Browse files
committed
Separate out prefetching to a trait and brings back regular Iterator/Stream for Generator.
1 parent d5ebe4d commit 1b41e96

File tree

7 files changed

+43
-44
lines changed

7 files changed

+43
-44
lines changed

fluent-fallback/examples/simple-fallback.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use std::{env, fs, io, path::PathBuf, str::FromStr};
2222

2323
use fluent_bundle::{FluentArgs, FluentBundle, FluentResource, FluentValue};
2424
use fluent_fallback::{
25-
generator::{BundleGenerator, BundleIterator, BundleStream, FluentBundleResult},
25+
generator::{BundleGenerator, FluentBundleResult},
2626
Localization,
2727
};
2828
use fluent_langneg::{negotiate_languages, NegotiationStrategy};
@@ -178,8 +178,6 @@ struct BundleIter {
178178
res_ids: Vec<String>,
179179
}
180180

181-
impl BundleIterator<FluentResource> for BundleIter {}
182-
183181
impl Iterator for BundleIter {
184182
type Item = FluentBundleResult<FluentResource>;
185183

@@ -214,8 +212,6 @@ impl Iterator for BundleIter {
214212
}
215213
}
216214

217-
impl BundleStream<FluentResource> for BundleIter {}
218-
219215
impl futures::Stream for BundleIter {
220216
type Item = FluentBundleResult<FluentResource>;
221217

fluent-fallback/src/cache.rs

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ use std::{
66
task::Poll,
77
};
88

9-
use crate::generator::{BundleIterator, BundleStream};
9+
use crate::generator::BundleIterator;
1010
use chunky_vec::ChunkyVec;
1111
use futures::{ready, Stream};
1212
use pin_cell::{PinCell, PinMut};
1313

1414
pub struct Cache<I, R>
1515
where
16-
I: BundleIterator<R>,
16+
I: Iterator,
1717
{
1818
iter: RefCell<I>,
1919
items: UnsafeCell<ChunkyVec<I::Item>>,
@@ -22,7 +22,7 @@ where
2222

2323
impl<I, R> Cache<I, R>
2424
where
25-
I: BundleIterator<R>,
25+
I: Iterator,
2626
{
2727
pub fn new(iter: I) -> Self {
2828
Self {
@@ -53,23 +53,28 @@ where
5353
(*items).push_get(new_value)
5454
}
5555
}
56+
}
5657

58+
impl<I, R> Cache<I, R>
59+
where
60+
I: BundleIterator + Iterator,
61+
{
5762
pub fn prefetch(&self) {
5863
self.iter.borrow_mut().prefetch();
5964
}
6065
}
6166

6267
pub struct CacheIter<'a, I, R>
6368
where
64-
I: BundleIterator<R>,
69+
I: Iterator,
6570
{
6671
cache: &'a Cache<I, R>,
6772
curr: usize,
6873
}
6974

7075
impl<'a, I, R> Iterator for CacheIter<'a, I, R>
7176
where
72-
I: BundleIterator<R>,
77+
I: Iterator,
7378
{
7479
type Item = &'a I::Item;
7580

@@ -101,7 +106,7 @@ where
101106

102107
impl<'a, I, R> IntoIterator for &'a Cache<I, R>
103108
where
104-
I: BundleIterator<R>,
109+
I: Iterator,
105110
{
106111
type Item = &'a I::Item;
107112
type IntoIter = CacheIter<'a, I, R>;
@@ -118,7 +123,7 @@ where
118123

119124
pub struct AsyncCache<S, R>
120125
where
121-
S: BundleStream<R>,
126+
S: Stream,
122127
{
123128
stream: PinCell<S>,
124129
items: UnsafeCell<ChunkyVec<S::Item>>,
@@ -127,7 +132,7 @@ where
127132

128133
impl<S, R> AsyncCache<S, R>
129134
where
130-
S: BundleStream<R>,
135+
S: Stream,
131136
{
132137
pub fn new(stream: S) -> Self {
133138
Self {
@@ -165,7 +170,12 @@ where
165170
curr: 0,
166171
}
167172
}
173+
}
168174

175+
impl<S, R> AsyncCache<S, R>
176+
where
177+
S: BundleIterator + Stream,
178+
{
169179
pub fn prefetch(&self) {
170180
let pin = unsafe { Pin::new_unchecked(&self.stream) };
171181
unsafe { PinMut::as_mut(&mut pin.borrow_mut()).get_unchecked_mut() }.prefetch();
@@ -174,7 +184,7 @@ where
174184

175185
impl<S, R> AsyncCache<S, R>
176186
where
177-
S: BundleStream<R>,
187+
S: Stream,
178188
{
179189
// Helper function that gets the next value from wrapped stream.
180190
fn poll_next_item(&self, cx: &mut Context<'_>) -> Poll<Option<S::Item>> {
@@ -185,15 +195,15 @@ where
185195

186196
pub struct AsyncCacheStream<'a, S, R>
187197
where
188-
S: BundleStream<R>,
198+
S: Stream,
189199
{
190200
cache: &'a AsyncCache<S, R>,
191201
curr: usize,
192202
}
193203

194204
impl<'a, S, R> Stream for AsyncCacheStream<'a, S, R>
195205
where
196-
S: BundleStream<R>,
206+
S: Stream,
197207
{
198208
type Item = &'a S::Item;
199209

fluent-fallback/src/generator.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,14 @@ use std::borrow::Borrow;
44

55
pub type FluentBundleResult<R> = Result<FluentBundle<R>, (FluentBundle<R>, Vec<FluentError>)>;
66

7-
pub trait BundleIterator<R>: Iterator<Item = FluentBundleResult<R>> {
8-
fn prefetch(&mut self) {}
9-
}
10-
11-
pub trait BundleStream<R>: Stream<Item = FluentBundleResult<R>> {
7+
pub trait BundleIterator {
128
fn prefetch(&mut self) {}
139
}
1410

1511
pub trait BundleGenerator {
1612
type Resource: Borrow<FluentResource>;
17-
type Iter: BundleIterator<Self::Resource>;
18-
type Stream: BundleStream<Self::Resource>;
13+
type Iter: Iterator<Item = FluentBundleResult<Self::Resource>>;
14+
type Stream: Stream<Item = FluentBundleResult<Self::Resource>>;
1915

2016
// Can we make it a slice?
2117
fn bundles_iter(&self, res_ids: Vec<String>) -> Self::Iter;

fluent-fallback/src/localization.rs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::cache::{AsyncCache, Cache};
22
use crate::errors::LocalizationError;
3-
use crate::generator::BundleGenerator;
3+
use crate::generator::{BundleGenerator, BundleIterator};
44
use crate::types::{L10nAttribute, L10nKey, L10nMessage};
55
use fluent_bundle::{FluentArgs, FluentBundle, FluentError};
66
use once_cell::sync::OnceCell;
@@ -17,6 +17,8 @@ where
1717
impl<G> Bundles<G>
1818
where
1919
G: BundleGenerator,
20+
G::Iter: BundleIterator,
21+
G::Stream: BundleIterator,
2022
{
2123
fn prefetch(&self) {
2224
match self {
@@ -83,11 +85,6 @@ where
8385
self.res_ids.len()
8486
}
8587

86-
pub fn prefetch(&self) {
87-
let bundles = self.get_bundles();
88-
bundles.prefetch();
89-
}
90-
9188
pub fn set_async(&mut self) {
9289
if self.sync {
9390
self.bundles.take();
@@ -172,6 +169,18 @@ where
172169
}
173170
}
174171

172+
impl<G> Localization<G>
173+
where
174+
G: BundleGenerator,
175+
G::Iter: BundleIterator,
176+
G::Stream: BundleIterator,
177+
{
178+
pub fn prefetch(&self) {
179+
let bundles = self.get_bundles();
180+
bundles.prefetch();
181+
}
182+
}
183+
175184
impl<G> Localization<G>
176185
where
177186
G: BundleGenerator,

fluent-fallback/tests/localization_test.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::fs;
33

44
use fluent_bundle::{FluentBundle, FluentResource};
55
use fluent_fallback::{
6-
generator::{BundleGenerator, BundleIterator, BundleStream, FluentBundleResult},
6+
generator::{BundleGenerator, FluentBundleResult},
77
Localization,
88
};
99
use std::cell::RefCell;
@@ -46,8 +46,6 @@ struct BundleIter {
4646
res_ids: Vec<String>,
4747
}
4848

49-
impl BundleIterator<FluentResource> for BundleIter {}
50-
5149
impl Iterator for BundleIter {
5250
type Item = FluentBundleResult<FluentResource>;
5351

@@ -78,8 +76,6 @@ impl Iterator for BundleIter {
7876
}
7977
}
8078

81-
impl BundleStream<FluentResource> for BundleIter {}
82-
8379
impl futures::Stream for BundleIter {
8480
type Item = FluentBundleResult<FluentResource>;
8581

fluent-resmgr/src/resource_manager.rs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
use elsa::FrozenMap;
22
use fluent_bundle::{FluentBundle, FluentResource};
3-
use fluent_fallback::generator::{
4-
BundleGenerator, BundleIterator, BundleStream, FluentBundleResult,
5-
};
3+
use fluent_fallback::generator::{BundleGenerator, FluentBundleResult};
64
use futures::stream::Stream;
75
use std::fs;
86
use std::io;
@@ -85,9 +83,6 @@ pub struct BundleIter {
8583
resource_ids: Vec<String>,
8684
}
8785

88-
impl BundleIterator<FluentResource> for BundleIter {
89-
}
90-
9186
impl Iterator for BundleIter {
9287
type Item = FluentBundleResult<FluentResource>;
9388

@@ -106,9 +101,6 @@ impl Iterator for BundleIter {
106101
}
107102
}
108103

109-
impl BundleStream<FluentResource> for BundleIter {
110-
}
111-
112104
impl Stream for BundleIter {
113105
type Item = FluentBundleResult<FluentResource>;
114106

fluent-resmgr/tests/localization_test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use fluent_fallback::Localization;
22
use fluent_resmgr::resource_manager::ResourceManager;
3-
use unic_langid::langid;
43
use std::borrow::Cow;
4+
use unic_langid::langid;
55

66
#[test]
77
fn localization_format_value() {

0 commit comments

Comments
 (0)