1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
use {
    crate::{
        asset::{Asset, Format},
        process::ProcessSlot,
        source::SourceError,
        sync::{BoxFuture, Send},
        Cache, Error, WeakCache,
    },
    alloc::{boxed::Box, vec::Vec},
    core::{
        future::Future,
        pin::Pin,
        task::{Context, Poll},
    },
    futures_core::ready,
    pin_utils::{unsafe_pinned, unsafe_unpinned},
};

trait AnyLoaderTask<K>: Send {
    /// Drives loader task to completion.
    /// Can provoke more work by fetching new assets from provided `Cache`.
    fn poll(self: Pin<&mut Self>, ctx: &mut Context<'_>, cache: &Cache<K>) -> Poll<()>;
}

struct AssetLoaderTask<A: Asset, F, D> {
    loading: BoxFuture<'static, Result<Vec<u8>, SourceError>>,
    format: Option<F>,
    decode: Option<D>,
    slot: Option<ProcessSlot<A>>,
}

impl<A, F, D> AssetLoaderTask<A, F, D>
where
    A: Asset,
{
    unsafe_unpinned!(loading: BoxFuture<'static, Result<Vec<u8>, SourceError>>);
    unsafe_unpinned!(format: Option<F>);
    unsafe_pinned!(decode: Option<D>);
    unsafe_unpinned!(slot: Option<ProcessSlot<A>>);
}

impl<A, K, F> AnyLoaderTask<K> for AssetLoaderTask<A, F, F::DecodeFuture>
where
    A: Asset,
    F: Format<A, K>,
{
    fn poll(mut self: Pin<&mut Self>, ctx: &mut Context<'_>, cache: &Cache<K>) -> Poll<()> {
        if let Some(decode) = self.as_mut().decode().as_pin_mut() {
            match ready!(decode.poll(ctx)) {
                Ok(asset) => {
                    self.slot().take().unwrap().set(Ok(asset));
                }
                Err(err) => {
                    self.slot()
                        .take()
                        .unwrap()
                        .set(Err(Error::Asset(err.into())));
                }
            }
            return Poll::Ready(());
        }

        match ready!(self.as_mut().loading().as_mut().poll(ctx)) {
            Ok(bytes) => {
                let decode = self.as_mut().format().take().unwrap().decode(bytes, cache);
                self.as_mut().decode().set(Some(decode));

                let decode = self.as_mut().decode().as_pin_mut().unwrap();
                match ready!(decode.poll(ctx)) {
                    Ok(asset) => {
                        self.slot().take().unwrap().set(Ok(asset));
                    }
                    Err(err) => {
                        self.slot()
                            .take()
                            .unwrap()
                            .set(Err(Error::Asset(err.into())));
                    }
                }
            }
            Err(SourceError::NotFound) => self.slot().take().unwrap().set(Err(Error::NotFound)),
            Err(SourceError::Error(err)) => {
                self.slot().take().unwrap().set(Err(Error::Source(err)))
            }
        }

        Poll::Ready(())
    }
}

#[repr(transparent)]
pub(crate) struct LoaderTask<K> {
    boxed: Pin<Box<dyn AnyLoaderTask<K>>>,
}

impl<K> LoaderTask<K> {
    pub(crate) fn new<A: Asset, F: Format<A, K>>(
        loading: BoxFuture<'static, Result<Vec<u8>, SourceError>>,
        format: F,
        slot: ProcessSlot<A>,
    ) -> Self {
        LoaderTask {
            boxed: Box::pin(AssetLoaderTask {
                loading,
                format: Some(format),
                decode: None,
                slot: Some(slot),
            }),
        }
    }

    fn poll(&mut self, ctx: &mut Context<'_>, cache: &Cache<K>) -> Poll<()> {
        self.boxed.as_mut().poll(ctx, cache)
    }
}

/// Loader receives loading tasks from asset cache and drives them in the async context.
#[must_use = "futures do nothing unless you `.await` or poll them"]
pub struct Loader<K> {
    tasks: Vec<LoaderTask<K>>,
    scratch: Vec<LoaderTask<K>>,
    cache: WeakCache<K>,
}

impl<K> Loader<K> {
    pub fn new(cache: &Cache<K>) -> Self {
        Loader {
            tasks: Vec::new(),
            scratch: Vec::new(),
            cache: cache.downgrade(),
        }
    }

    /// Drives loading tasks to completion.
    pub fn poll(&mut self, ctx: &mut Context<'_>) -> Poll<()> {
        let cache = match self.cache.upgrade() {
            Some(cache) => cache,
            None => return Poll::Ready(()), // Cannot continue when cache instance is dropped.
        };

        // Receive new tasks.
        match cache.inner.loader.poll(ctx, &mut self.scratch) {
            Poll::Pending => {
                // Poll pending tasks.
                let mut i = 0;
                while i < self.tasks.len() {
                    if let Poll::Ready(()) = self.tasks[i].poll(ctx, &cache) {
                        self.tasks.swap_remove(i);
                    } else {
                        i += 1;
                    }
                }
                Poll::Pending
            }
            Poll::Ready(()) => {
                // Poll pending tasks first.
                let mut i = 0;
                while i < self.tasks.len() {
                    if let Poll::Ready(()) = self.tasks[i].poll(ctx, &cache) {
                        self.tasks.swap_remove(i);
                    } else {
                        i += 1;
                    }
                }

                // Poll new tasks and push pending to the pending tasks list.
                for mut task in self.scratch.drain(..) {
                    if let Poll::Pending = task.poll(ctx, &cache) {
                        self.tasks.push(task);
                    }
                }
                Poll::Pending
            }
        }
    }

    /// Drives all pending loading jobs to completion.
    pub fn flush<'a>(&'a mut self) -> LoaderFlush<'a, K> {
        LoaderFlush { loader: self }
    }
}

/// Future that polls `Loader` from which it was created
/// Until no pending tasks remains.
#[must_use = "futures do nothing unless you `.await` or poll them"]
pub struct LoaderFlush<'a, K> {
    loader: &'a mut Loader<K>,
}

impl<'a, K> Unpin for LoaderFlush<'a, K> {}

impl<'a, K> Future for LoaderFlush<'a, K> {
    type Output = ();

    fn poll(self: Pin<&mut Self>, ctx: &mut Context<'_>) -> Poll<()> {
        let LoaderFlush { loader } = self.get_mut();

        match loader.poll(ctx) {
            Poll::Pending => {
                if loader.tasks.is_empty() {
                    Poll::Ready(())
                } else {
                    Poll::Pending
                }
            }
            Poll::Ready(()) => Poll::Ready(()),
        }
    }
}

impl<K> Future for Loader<K> {
    type Output = ();

    fn poll(self: Pin<&mut Self>, ctx: &mut Context<'_>) -> Poll<()> {
        self.get_mut().poll(ctx)
    }
}