bson-rust icon indicating copy to clipboard operation
bson-rust copied to clipboard

How to use in wasm context?

Open i11010520 opened this issue 3 years ago • 2 comments

When used bson = "2.3" directly as dependency, building output the following error message: error: the wasm32-unknown-unknown target is not supported by default, you may need to enable the "js" feature. For more information see: https://docs.rs/getrandom/#webassembly-support

Then, after added getrandom = { version = "0.2.7", features = ["js"] } under bson dependency, building output error message became: error[E0599]: no function or associated item named new_v4found for structuuid::Uuid in the current scope

How can I use this crate in wasm context?

Thx.

i11010520 avatar Jul 21 '22 00:07 i11010520

I believe the error you're seeing may not be related to wasm or bson, but instead that you need to enable the v4 feature flag for your dependency on the uuid crate. Alternatively, you can switch to use bson::Uuid instead.

Note that bson currently doesn't have any interop with v1 of uuid (we currently only support 0.8 via the uuid-0_8 feature flag), but we're adding it soon (see #355). This may also be what you're experiencing. If so, you can work around it by downgrading your uuid dependency to 0.8 until we release support for 1.0.

patrickfreed avatar Jul 22 '22 14:07 patrickfreed

I believe the error you're seeing may not be related to wasm or bson, but instead that you need to enable the v4 feature flag for your dependency on the uuid crate. Alternatively, you can switch to use bson::Uuid instead.

Note that bson currently doesn't have any interop with v1 of uuid (we currently only support 0.8 via the uuid-0_8 feature flag), but we're adding it soon (see #355). This may also be what you're experiencing. If so, you can work around it by downgrading your uuid dependency to 0.8 until we release support for 1.0.

I didn't use uuid for dependency directly. My crate aims to build an serializable entity model with the following id field:

    #[serde(rename = "_id")]
    #[serde(skip_serializing_if = "Option::is_none")]
    pub id: Option<bson::oid::ObjectId>,

Now, I just add bson dependency this way to make it work:

bson = { git = "https://github.com/mongodb/bson-rust", features = ["uuid-1"] }
getrandom = { version = "0.2.7", features = ["js"] }

Looking forward to new bson release. Thanks.

i11010520 avatar Jul 23 '22 08:07 i11010520

FYI, 2.4.0 has been released with the uuid-1 flag.

patrickfreed avatar Aug 31 '22 17:08 patrickfreed