pubstruct CacheIter<'a, I, R> where
I: Iterator,
{
cache: &'a Cache<I, R>,
curr: usize,
}
impl<'a, I, R> Iterator for CacheIter<'a, I, R> where
I: Iterator,
{ type Item = &'a I::Item;
fn next(&mutself) -> Option<Self::Item> { let cache_len = self.cache.len(); matchself.curr.cmp(&cache_len) {
Ordering::Less => { // Cached value self.curr += 1; self.cache.get(self.curr - 1)
}
Ordering::Equal => { // Get the next item from the iterator let item = self.cache.iter.borrow_mut().next(); self.curr += 1; iflet Some(item) = item {
Some(self.cache.push_get(item))
} else {
None
}
}
Ordering::Greater => { // Ran off the end of the cache
None
}
}
}
}
impl<'a, I, R> IntoIterator for &'a Cache<I, R> where
I: Iterator,
{ type Item = &'a I::Item; type IntoIter = CacheIter<'a, I, R>;
pubstruct AsyncCache<S, R> where
S: Stream,
{
stream: PinCell<S>,
items: UnsafeCell<ChunkyVec<S::Item>>, // TODO: Should probably be an SmallVec<[Waker; 1]> or something? I guess // multiple pending wakes are not really all that common.
pending_wakes: RefCell<Vec<Waker>>,
res: std::marker::PhantomData<R>,
}
/// Push, immediately getting a reference to the element pubfn push_get(&self, new_value: S::Item) -> &S::Item { unsafe { let items = self.items.get();
(*items).push_get(new_value)
}
}
impl<S, R> AsyncCache<S, R> where
S: Stream,
{ // Helper function that gets the next value from wrapped stream. fn poll_next_item(&self, cx: &mut Context<'_>) -> Poll<Option<S::Item>> { let pin = unsafe { Pin::new_unchecked(&self.stream) }; let poll = PinMut::as_mut(&mut pin.borrow_mut()).poll_next(cx); if poll.is_ready() { let wakers = std::mem::take(&mut *self.pending_wakes.borrow_mut()); for waker in wakers {
waker.wake();
}
} else { self.pending_wakes.borrow_mut().push(cx.waker().clone());
}
poll
}
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.