Skip to content

Commit cd8d6c4

Browse files
committed
Separate prefetch to sync and async
1 parent 1b41e96 commit cd8d6c4

File tree

4 files changed

+45
-13
lines changed

4 files changed

+45
-13
lines changed

fluent-fallback/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ fluent-bundle = { version = "0.14", path = "../fluent-bundle" }
2323
futures = "0.3"
2424
pin-cell = { git = "https://github.com/withoutboats/pin-cell", rev = "4d5803d83c6b3bffd62c9e73a0ceaf5ec6f01495" }
2525
once_cell = "1.5"
26+
async-trait = "0.1"
2627

2728
[dev-dependencies]
2829
fluent-langneg = "0.13"

fluent-fallback/src/cache.rs

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

9-
use crate::generator::BundleIterator;
9+
use crate::generator::{BundleIterator, BundleStream};
1010
use chunky_vec::ChunkyVec;
1111
use futures::{ready, Stream};
1212
use pin_cell::{PinCell, PinMut};
@@ -60,7 +60,7 @@ where
6060
I: BundleIterator + Iterator,
6161
{
6262
pub fn prefetch(&self) {
63-
self.iter.borrow_mut().prefetch();
63+
self.iter.borrow_mut().prefetch_sync();
6464
}
6565
}
6666

@@ -174,11 +174,13 @@ where
174174

175175
impl<S, R> AsyncCache<S, R>
176176
where
177-
S: BundleIterator + Stream,
177+
S: BundleStream + Stream,
178178
{
179-
pub fn prefetch(&self) {
179+
pub async fn prefetch(&self) {
180180
let pin = unsafe { Pin::new_unchecked(&self.stream) };
181-
unsafe { PinMut::as_mut(&mut pin.borrow_mut()).get_unchecked_mut() }.prefetch();
181+
unsafe { PinMut::as_mut(&mut pin.borrow_mut()).get_unchecked_mut() }
182+
.prefetch_async()
183+
.await
182184
}
183185
}
184186

fluent-fallback/src/generator.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,12 @@ use std::borrow::Borrow;
55
pub type FluentBundleResult<R> = Result<FluentBundle<R>, (FluentBundle<R>, Vec<FluentError>)>;
66

77
pub trait BundleIterator {
8-
fn prefetch(&mut self) {}
8+
fn prefetch_sync(&mut self) {}
9+
}
10+
11+
#[async_trait::async_trait(?Send)]
12+
pub trait BundleStream {
13+
async fn prefetch_async(&mut self) {}
914
}
1015

1116
pub trait BundleGenerator {

fluent-fallback/src/localization.rs

Lines changed: 31 additions & 7 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, BundleIterator};
3+
use crate::generator::{BundleGenerator, BundleIterator, BundleStream};
44
use crate::types::{L10nAttribute, L10nKey, L10nMessage};
55
use fluent_bundle::{FluentArgs, FluentBundle, FluentError};
66
use once_cell::sync::OnceCell;
@@ -18,12 +18,26 @@ impl<G> Bundles<G>
1818
where
1919
G: BundleGenerator,
2020
G::Iter: BundleIterator,
21-
G::Stream: BundleIterator,
2221
{
23-
fn prefetch(&self) {
22+
fn prefetch_sync(&self) {
2423
match self {
2524
Self::Iter(iter) => iter.prefetch(),
26-
Self::Stream(stream) => stream.prefetch(),
25+
Self::Stream(_) => todo!(),
26+
}
27+
}
28+
}
29+
30+
impl<G> Bundles<G>
31+
where
32+
G: BundleGenerator,
33+
G::Stream: BundleStream,
34+
{
35+
async fn prefetch_async(&self) {
36+
match self {
37+
Self::Iter(_) => {
38+
todo!();
39+
}
40+
Self::Stream(stream) => stream.prefetch().await,
2741
}
2842
}
2943
}
@@ -173,11 +187,21 @@ impl<G> Localization<G>
173187
where
174188
G: BundleGenerator,
175189
G::Iter: BundleIterator,
176-
G::Stream: BundleIterator,
177190
{
178-
pub fn prefetch(&self) {
191+
pub fn prefetch_sync(&self) {
192+
let bundles = self.get_bundles();
193+
bundles.prefetch_sync();
194+
}
195+
}
196+
197+
impl<G> Localization<G>
198+
where
199+
G: BundleGenerator,
200+
G::Stream: BundleStream,
201+
{
202+
pub async fn prefetch_async(&self) {
179203
let bundles = self.get_bundles();
180-
bundles.prefetch();
204+
bundles.prefetch_async().await
181205
}
182206
}
183207

0 commit comments

Comments
 (0)