mirror of
https://github.com/leptos-rs/leptos
synced 2024-09-20 14:32:00 +00:00
clone values for Futures
This commit is contained in:
parent
4df42cbc60
commit
201adb7406
2 changed files with 18 additions and 15 deletions
|
@ -135,8 +135,11 @@ impl<T: Send + Sync + 'static> DefinedAt for AsyncDerived<T> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<T: Send + Sync + Clone + 'static> IntoFuture for AsyncDerived<T> {
|
||||
type Output = ReadGuard<T, Mapped<Plain<AsyncState<T>>, T>>;
|
||||
impl<T: Send + Sync + Clone + 'static> IntoFuture for AsyncDerived<T>
|
||||
where
|
||||
T: Clone,
|
||||
{
|
||||
type Output = T;
|
||||
type IntoFuture = AsyncDerivedFuture<T>;
|
||||
|
||||
#[track_caller]
|
||||
|
|
|
@ -44,8 +44,11 @@ pub struct AsyncDerivedFuture<T> {
|
|||
wakers: Arc<RwLock<Vec<Waker>>>,
|
||||
}
|
||||
|
||||
impl<T: 'static> IntoFuture for ArcAsyncDerived<T> {
|
||||
type Output = ReadGuard<T, Mapped<Plain<AsyncState<T>>, T>>;
|
||||
impl<T> IntoFuture for ArcAsyncDerived<T>
|
||||
where
|
||||
T: Clone + 'static,
|
||||
{
|
||||
type Output = T;
|
||||
type IntoFuture = AsyncDerivedFuture<T>;
|
||||
|
||||
fn into_future(self) -> Self::IntoFuture {
|
||||
|
@ -57,8 +60,13 @@ impl<T: 'static> IntoFuture for ArcAsyncDerived<T> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<T: 'static> Future for AsyncDerivedFuture<T> {
|
||||
type Output = ReadGuard<T, Mapped<Plain<AsyncState<T>>, T>>;
|
||||
// this is implemented to output T by cloning it because read guards should not be held across
|
||||
// .await points, and it's way too easy to trip up by doing that!
|
||||
impl<T> Future for AsyncDerivedFuture<T>
|
||||
where
|
||||
T: Clone + 'static,
|
||||
{
|
||||
type Output = T;
|
||||
|
||||
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
||||
let waker = cx.waker();
|
||||
|
@ -70,15 +78,7 @@ impl<T: 'static> Future for AsyncDerivedFuture<T> {
|
|||
self.wakers.write().or_poisoned().push(waker.clone());
|
||||
Poll::Pending
|
||||
}
|
||||
AsyncState::Complete(_) => {
|
||||
Poll::Ready(ReadGuard::new(Mapped::new(value, |inner| {
|
||||
match inner {
|
||||
AsyncState::Complete(value) => value,
|
||||
// we've just checked this value is Complete
|
||||
_ => unreachable!(),
|
||||
}
|
||||
})))
|
||||
}
|
||||
AsyncState::Complete(value) => Poll::Ready(value.clone()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue