Introduction
Bomboni provides comprehensive WebAssembly (WASM) support through three crates:- bomboni_wasm - Main utilities for JavaScript interoperability and console logging
- bomboni_wasm_derive - Derive macros for generating TypeScript bindings
- bomboni_wasm_core - Core utilities for TypeScript type generation
When to Use WASM Features
Use Bomboni’s WASM features when you need to:- Expose Rust APIs to JavaScript/TypeScript - Share complex data structures between Rust and JavaScript
- Generate TypeScript declarations automatically - Keep your TypeScript types in sync with Rust implementations
- Build browser-based applications - Leverage Rust’s performance and safety in web applications
- Create npm packages - Package Rust functionality for the JavaScript ecosystem
Key Features
Automatic TypeScript Generation
TheWasm derive macro automatically generates TypeScript type declarations that match your Rust types:
Type Conversions
Bomboni automatically maps Rust types to their TypeScript equivalents:| Rust Type | TypeScript Type |
|---|---|
i8, i16, i32, i64, u8, u16, u32, u64, f32, f64 | number |
i128, u128 | bigint (with js feature) |
String, &str, char | string |
bool | boolean |
Option<T> | T | null | undefined |
Vec<T>, HashSet<T> | T[] |
HashMap<K, V> | Map<K, V> or Record<K, V> |
Wasm Trait
TheWasm trait provides conversion methods between Rust and JavaScript:
Browser Integration
Console Logging
Bomboni provides macros for logging to the browser console:Building for the Browser
To build your WASM module for browser use:TypeScript Declaration Generation
Accessing Generated Types
Each type with theWasm derive macro exposes its TypeScript declaration:
Generating .d.ts Files
When you build with wasm-bindgen, TypeScript declarations are automatically included in the generated package:pkg/your_crate.js- JavaScript bindingspkg/your_crate.d.ts- TypeScript declarationspkg/your_crate_bg.wasm- WebAssembly binary
Custom TypeScript Sections
Bomboni uses wasm-bindgen’stypescript_custom_section to inject type declarations:
Complete Example
Here’s a complete example showing browser integration:Next Steps
- Learn about TypeScript Bindings for advanced type mapping
- Explore JavaScript Interop for custom conversions