-
Notifications
You must be signed in to change notification settings - Fork 269
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Start addressing some wasm32-unknown-unknown errors for matrix-sdk-ui crate #4122
Closed
+176
−129
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,8 +14,6 @@ | |
|
||
//! Abstraction over an executor so we can spawn tasks under WASM the same way | ||
//! we do usually. | ||
|
||
#[cfg(target_arch = "wasm32")] | ||
use std::{ | ||
future::Future, | ||
pin::Pin, | ||
|
@@ -25,13 +23,17 @@ use std::{ | |
#[cfg(target_arch = "wasm32")] | ||
pub use futures_util::future::Aborted as JoinError; | ||
#[cfg(target_arch = "wasm32")] | ||
use futures_util::{ | ||
future::{AbortHandle, Abortable, RemoteHandle}, | ||
FutureExt, | ||
}; | ||
use futures_util::future::{AbortHandle, Abortable, RemoteHandle}; | ||
use futures_util::FutureExt; | ||
#[cfg(not(target_arch = "wasm32"))] | ||
pub use tokio::task::{spawn, JoinError, JoinHandle}; | ||
|
||
/// A `Box::pin` future that is `Send` on non-wasm, and without `Send` on wasm. | ||
#[cfg(target_arch = "wasm32")] | ||
pub type BoxFuture<'a, T> = Pin<Box<dyn Future<Output = T> + 'a>>; | ||
#[cfg(not(target_arch = "wasm32"))] | ||
pub type BoxFuture<'a, T> = Pin<Box<dyn Future<Output = T> + Send + 'a>>; | ||
|
||
#[cfg(target_arch = "wasm32")] | ||
pub fn spawn<F, T>(future: F) -> JoinHandle<T> | ||
where | ||
|
@@ -50,6 +52,32 @@ where | |
JoinHandle { remote_handle, abort_handle } | ||
} | ||
|
||
pub trait BoxFutureExt<'a, T: 'a> { | ||
fn box_future(self) -> BoxFuture<'a, T>; | ||
} | ||
|
||
#[cfg(not(target_arch = "wasm32"))] | ||
impl<'a, F, T> BoxFutureExt<'a, T> for F | ||
where | ||
F: Future<Output = T> + 'a + Send, | ||
T: 'a, | ||
{ | ||
fn box_future(self) -> BoxFuture<'a, T> { | ||
self.boxed() | ||
} | ||
} | ||
|
||
#[cfg(target_arch = "wasm32")] | ||
impl<'a, F, T> BoxFutureExt<'a, T> for F | ||
where | ||
F: Future<Output = T> + 'a, | ||
T: 'a, | ||
{ | ||
fn box_future(self) -> BoxFuture<'a, T> { | ||
self.boxed_local() | ||
} | ||
} | ||
Comment on lines
+59
to
+79
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. impl blocks can be merged the same way. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was struggling to get it to compile with the extra |
||
|
||
#[cfg(target_arch = "wasm32")] | ||
#[derive(Debug)] | ||
pub struct JoinHandle<T> { | ||
|
@@ -78,6 +106,54 @@ impl<T: 'static> Future for JoinHandle<T> { | |
} | ||
} | ||
|
||
#[derive(Debug)] | ||
pub struct AbortOnDrop<T>(JoinHandle<T>); | ||
|
||
impl<T> AbortOnDrop<T> { | ||
pub fn new(join_handle: JoinHandle<T>) -> Self { | ||
Self(join_handle) | ||
} | ||
} | ||
|
||
impl<T> Drop for AbortOnDrop<T> { | ||
fn drop(&mut self) { | ||
self.0.abort(); | ||
} | ||
} | ||
|
||
#[cfg(not(target_arch = "wasm32"))] | ||
impl<T> Future for AbortOnDrop<T> { | ||
type Output = Result<T, JoinError>; | ||
|
||
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { | ||
Pin::new(&mut self.0).poll(cx) | ||
} | ||
} | ||
|
||
#[cfg(target_arch = "wasm32")] | ||
impl<T: 'static> Future for AbortOnDrop<T> { | ||
type Output = Result<T, JoinError>; | ||
|
||
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { | ||
if self.0.abort_handle.is_aborted() { | ||
// The future has been aborted. It is not possible to poll it again. | ||
Poll::Ready(Err(JoinError)) | ||
} else { | ||
Pin::new(&mut self.0.remote_handle).poll(cx).map(Ok) | ||
} | ||
} | ||
} | ||
|
||
/// Trait to create a `AbortOnDrop` from a `JoinHandle`. | ||
pub trait JoinHandleExt<T> { | ||
fn abort_on_drop(self) -> AbortOnDrop<T>; | ||
} | ||
|
||
impl<T> JoinHandleExt<T> for JoinHandle<T> { | ||
fn abort_on_drop(self) -> AbortOnDrop<T> { | ||
AbortOnDrop::new(self) | ||
} | ||
} | ||
#[cfg(test)] | ||
mod tests { | ||
use assert_matches::assert_matches; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see this definition already existed though, why did you move it here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I needed to expose the
.boxed
method on the wasm version of the code (it was present on the real future, but not the Wasm version). It seemed like this was where the wasm future was being defined, so I wanted to move the BoxFuture method here so it can be exposed in the same place.