All Projects → cloudflare → Serde Wasm Bindgen

cloudflare / Serde Wasm Bindgen

Native integration of Serde with wasm-bindgen

Programming Languages

javascript
184084 projects - #8 most used programming language
rust
11053 projects

Projects that are alternatives of or similar to Serde Wasm Bindgen

Wasm Crypto
A WebAssembly (via AssemblyScript) set of cryptographic primitives for building authentication and key exchange protocols.
Stars: ✭ 146 (-17.05%)
Mutual labels:  webassembly, wasm
Wasm Pdf
Generate PDF files with JavaScript and WASM (WebAssembly)
Stars: ✭ 163 (-7.39%)
Mutual labels:  webassembly, wasm
Libarchivejs
Archive library for browsers
Stars: ✭ 145 (-17.61%)
Mutual labels:  webassembly, wasm
Logging
Microsoft Extension Logging implementation for Blazor
Stars: ✭ 165 (-6.25%)
Mutual labels:  webassembly, wasm
Carton
📦 Watcher, bundler, and test runner for your SwiftWasm apps
Stars: ✭ 171 (-2.84%)
Mutual labels:  webassembly, wasm
Assemblyscript
A TypeScript-like language for WebAssembly.
Stars: ✭ 13,152 (+7372.73%)
Mutual labels:  webassembly, wasm
Deno Sqlite
Deno SQLite module
Stars: ✭ 151 (-14.2%)
Mutual labels:  webassembly, wasm
Nes Rust
NES emulator written in Rust + WASM
Stars: ✭ 141 (-19.89%)
Mutual labels:  webassembly, wasm
Blazor Samples
Explore and learn Syncfusion Blazor components using large collection of demos, example applications and tutorial samples
Stars: ✭ 156 (-11.36%)
Mutual labels:  webassembly, wasm
Seed
A Rust framework for creating web apps
Stars: ✭ 3,069 (+1643.75%)
Mutual labels:  webassembly, wasm
Woz
Woz is a progressive WebAssembly app (PWAA) generator for Rust.
Stars: ✭ 145 (-17.61%)
Mutual labels:  webassembly, wasm
Wasm Micro Runtime
WebAssembly Micro Runtime (WAMR)
Stars: ✭ 2,440 (+1286.36%)
Mutual labels:  webassembly, wasm
Modern Wasm Starter
🛸 Run C++ code on web and create blazingly fast websites! A starter template to easily create WebAssembly packages using type-safe C++ bindings with automatic TypeScript declarations.
Stars: ✭ 140 (-20.45%)
Mutual labels:  webassembly, wasm
Rs Asteroids
A variation on the game Asteroids, written in Rust
Stars: ✭ 146 (-17.05%)
Mutual labels:  webassembly, wasm
Proxy Wasm Go Sdk
Go SDK for WebAssembly-based Envoy extensions
Stars: ✭ 137 (-22.16%)
Mutual labels:  webassembly, wasm
Spec
WebAssembly for Proxies (ABI specification)
Stars: ✭ 150 (-14.77%)
Mutual labels:  webassembly, wasm
Awesome Wasm Tools
😎 A curated list of awesome, language-agnostic WebAssembly tools
Stars: ✭ 139 (-21.02%)
Mutual labels:  webassembly, wasm
Proxy Wasm Rust Sdk
WebAssembly for Proxies (Rust SDK)
Stars: ✭ 137 (-22.16%)
Mutual labels:  webassembly, wasm
Wasmer Java
☕ WebAssembly runtime for Java
Stars: ✭ 152 (-13.64%)
Mutual labels:  webassembly, wasm
Edit Text
Collaborative rich text editor for the web. Written in Rust + WebAssembly.
Stars: ✭ 171 (-2.84%)
Mutual labels:  webassembly, wasm

This is an alternative native integration of Serde with wasm-bindgen.

Why

This library was created to address rustwasm/wasm-bindgen#1258 and provide a native Serde integration for wasm-bindgen to directly convert values between JavaScript and Rust (compiled to WebAssembly).

The primary difference with the built-in implementation is that it leverages direct APIs for JavaScript value manipulation instead of passing data in a JSON format. This allows it to support more types while producing a much leaner Wasm binary. In particular, it saved 26.6KB when comparing size-optimised and Brotli-compressed benchmarks with a stripped debug information.

Performance-wise the library is currently comparable with the original. Specific numbers vary a lot between the engines and used data types and, according to benchmarks, range from 1.6x regression in worst cases to 3.3x improvement in best cases. Your mileage might vary.

These numbers are currently mostly saturated by the overhead of frequent JavaScript <-> Wasm and JavaScript <-> C++ calls. These calls are used for sharing JavaScript values with the Rust side as well as encoding/decoding UTF-8 strings, and will go away in the future when reference types proposal lands natively in Wasm.

Usage

To pass a Rust value to JavaScript, use:

#[wasm_bindgen]
pub fn pass_value_to_js() -> Result<JsValue, JsValue> {
	// ...
	serde_wasm_bindgen::to_value(&some_supported_rust_value)
}

To retrieve a value from JavaScript:

#[wasm_bindgen]
pub fn get_value_from_js(value: JsValue) -> Result<(), JsValue> {
	let value: SomeSupportedRustType = serde_wasm_bindgen::from_value(value)?;
	// ...
	Ok(())
}

Supported types

Note that, even though it might often be the case, this library doesn't attempt to be strictly compatible with either serde_json or, correspondingly, JsValue::from_serde / JsValue::into_serde, instead prioritising better compatibility with common JavaScript idioms and representations.

Supported types and values for the deserialization:

  • () from undefined and null.
  • Option from any value will map undefined or null to None and any other value to Some(...).
  • bool from a JavaScript boolean (false and true).
  • Rust integer (u8/i8/.../u128/i128) from a safe JavaScript integer (as matched by Number.isSafeInteger).
  • Rust floating number (f32/f64) from any JavaScript number.
  • char from a JavaScript string containing a single codepoint.
  • String from any JavaScript string.
  • Rust map (HashMap, BTreeMap, ...) from any JavaScript iterable producing [key, value] pairs (including but not limited to ES2015 Map).
  • HashMap<String, _> from any plain JavaScript object ({ key1: value1, ... }).
  • Rust sequence (tuple, Vec, HashSet, ...) from any JavaScript iterable (including but not limited to Array, ES2015 Set, etc.).
  • Rust byte buffer (see serde_bytes) from JavaScript ArrayBuffer or Uint8Array.
  • Typed Rust structure from any plain JavaScript object ({ key1: value1, ... }).
  • Rust enum from either a string ("Variant") or a plain object. Specific representation is controlled by #[serde(...)] attributes and should be compatible with serde-json.

Serialization is compatible with the deserialization, but it's limited to a single representation, so it chooses:

  • undefined for () or None.
  • ES2015 Map for Rust maps.
  • Array for any Rust sequences.
  • Uint8Array for byte buffers.
  • Plain JavaScript object for typed Rust structures.

License

Licensed under the MIT license. See the LICENSE file for details.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].