Skip to content
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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

zzorba
Copy link
Contributor

@zzorba zzorba commented Oct 12, 2024

This change consists mostly of type-wrangling to support building the matrix-sdk-ui crate for wasm32-unknown-unknown. It is a step along the way of (ideally) supporting wasm32 throughout the entire project. The matrix-sdk-ui packages is not yet compiling due to some blocking issues, but these changes should be able to be landed now as a step towards that goal.

The changes consist of one of two main types.

  1. Promoting some types related to futures to the matrix-sdk-base, where an existing 'polyfill' of sorts for Futures already existed. This includes stuff around 'boxing' futures and aborting them.
  2. Switching to use the matrix-sdk-base spawn instead of tokio::spawn. On non-wasm32 platforms this should be a noop.
  • Public API changes documented in changelogs (optional)

Signed-off-by: Daniel Salinas

@zzorba zzorba force-pushed the matrix-sdk-ui-support-wasm32 branch 2 times, most recently from 55f7b61 to 617c622 Compare October 12, 2024 18:48
crates/matrix-sdk-base/src/event_cache_store/traits.rs Outdated Show resolved Hide resolved
crates/matrix-sdk-base/src/read_receipts.rs Outdated Show resolved Hide resolved
crates/matrix-sdk-base/src/store/traits.rs Outdated Show resolved Hide resolved
Comment on lines +33 to +35
#[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>>;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#[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>>;
pub type BoxFuture<'a, T> = Pin<Box<dyn Future<Output = T> + SendOutsideWasm + 'a>>;

Copy link
Collaborator

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?

Copy link
Contributor Author

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.

Comment on lines +61 to +79
#[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()
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

impl blocks can be merged the same way.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was struggling to get it to compile with the extra + Send trait needed on the not-wasm32 version.

This commit adjusts types and traits to support targeting wasm32.
@zzorba zzorba force-pushed the matrix-sdk-ui-support-wasm32 branch from 617c622 to b93d3b7 Compare October 13, 2024 14:21
@zzorba zzorba changed the title Support compilation of wasm32-unknown-unknown for matrix-sdk-ui crate Start addressing some wasm32-unknown-unknown errors for matrix-sdk-ui crate Oct 13, 2024
@zzorba zzorba force-pushed the matrix-sdk-ui-support-wasm32 branch 4 times, most recently from c9def0d to 26943b1 Compare October 13, 2024 14:37
@zzorba zzorba force-pushed the matrix-sdk-ui-support-wasm32 branch from 26943b1 to ac6b065 Compare October 13, 2024 14:49
Copy link

codecov bot commented Oct 13, 2024

Codecov Report

Attention: Patch coverage is 40.90909% with 13 lines in your changes missing coverage. Please review.

Project coverage is 84.67%. Comparing base (a4bda1a) to head (ac6b065).

Files with missing lines Patch % Lines
crates/matrix-sdk-common/src/executor.rs 56.25% 7 Missing ⚠️
crates/matrix-sdk-base/src/store/observable_map.rs 0.00% 5 Missing ⚠️
crates/matrix-sdk/src/sliding_sync/mod.rs 0.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #4122      +/-   ##
==========================================
- Coverage   84.69%   84.67%   -0.03%     
==========================================
  Files         269      268       -1     
  Lines       28816    28830      +14     
==========================================
+ Hits        24405    24411       +6     
- Misses       4411     4419       +8     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@zzorba zzorba marked this pull request as ready for review October 13, 2024 17:05
@zzorba zzorba requested review from a team as code owners October 13, 2024 17:05
@zzorba zzorba requested review from poljar and richvdh and removed request for a team October 13, 2024 17:05
@zzorba
Copy link
Contributor Author

zzorba commented Oct 13, 2024

With an additional change to relax the use of Send+Sync in the eyeball package, I have the rest of this crate building locally.

remaining.patch

@poljar poljar removed the request for review from richvdh October 16, 2024 09:54
Copy link
Contributor

@poljar poljar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks sensible, left a couple of nits.

@@ -28,7 +28,7 @@ pub(in crate::timeline) struct LocalEventTimelineItem {
pub send_state: EventSendState,
/// The transaction ID.
pub transaction_id: OwnedTransactionId,
/// A handle to manipulate this event before it is sent, if possible.
// A handle to manipulate this event before it is sent, if possible.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's this about? That looks like an accident.

@@ -512,7 +509,7 @@ macro_rules! impl_event_handler {
impl<Ev, Fun, Fut, $($ty),*> EventHandler<Ev, ($($ty,)*)> for Fun
where
Ev: SyncEvent,
Fun: FnOnce(Ev, $($ty),*) -> Fut + Clone + SendOutsideWasm + SyncOutsideWasm + 'static,
Fun: FnOnce(Ev, $($ty),*) -> Fut + Clone + Send + Sync + 'static,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wait, why would you need Send + Sync unconditionally here?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants