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

WASM tracking #35

Closed
8 tasks done
stoically opened this issue May 12, 2020 · 6 comments
Closed
8 tasks done

WASM tracking #35

stoically opened this issue May 12, 2020 · 6 comments

Comments

@stoically
Copy link
Contributor

stoically commented May 12, 2020

Extracted from: #31 (review)

@stoically stoically mentioned this issue May 12, 2020
15 tasks
@stoically
Copy link
Contributor Author

stoically commented Jun 2, 2020

A possible workaround for the Send+Sync issue could be a macro attribute like

#[cfg_attr(not(target_arch = "wasm32"), send_sync)]

which adds the bound for non-wasm targets.

@MTRNord
Copy link
Contributor

MTRNord commented Jun 4, 2020

Required proc macro: (Requires syn and quote crates)

use proc_macro::TokenStream;
use quote::quote;
use syn::{parse_macro_input, parse_quote, ItemTrait};

/// Attribute to use `Send + Sync` for everything but wasm32
#[proc_macro_attribute]
pub fn send_sync(_attr: TokenStream, input: TokenStream) -> TokenStream {
    // Parse the input tokens into a syntax tree
    let mut input = parse_macro_input!(input as ItemTrait);

    let send_trait_bound = parse_quote!(std::marker::Send);
    let sync_trait_bound = parse_quote!(std::marker::Sync);
    input.supertraits.push(send_trait_bound);
    input.supertraits.push(sync_trait_bound);

    TokenStream::from(quote!(#input))
}

@MTRNord
Copy link
Contributor

MTRNord commented Jun 4, 2020

And a specific macro for async_trait on EventEmitter as the send_sync macro doesn't work:

/// A wasm32 compatible wrapper for the async_trait::async_trait macro
#[proc_macro_attribute]
pub fn async_trait(_attr: TokenStream, item: TokenStream) -> TokenStream {
    let attrs = r#"
        #[cfg_attr(target_arch = "wasm32", async_trait::async_trait(?Send))]
        #[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)]
    "#;

    let mut out: TokenStream = attrs.parse().unwrap();
    out.extend(item);
    out
}

it replaces #[async_trait::async_trait] and Send + Sync can get removed

@MTRNord
Copy link
Contributor

MTRNord commented Jun 4, 2020

Also the async_trait macro needs to be used on all impl. You might use async_trait::async_trait in non wasm targets directly and can also use on things that only compile to wasm32 async_trait::async_trait(?Send). Just the main definition of EventEmitter needs to use this macro (or the attribures)

@poljar
Copy link
Contributor

poljar commented Sep 8, 2020

I forgot to mention this over here, but we have a prototype CryptoStore that uses indexeddb over here.

It uses a fork of indexeddb-rs since the upstream one doesn't compile, fork is over here.

@stoically
Copy link
Contributor Author

Closing as all boxes are checked. Thanks @gnunicorn for the missing piece.

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

No branches or pull requests

3 participants