Expand description
Derive-based cross-language type bridging for Rust.
typewire provides the Typewire trait and a derive macro that generates
platform-specific conversion methods and compile-time schema records from
your Rust types. Define types once in Rust, get type-safe foreign-language
bindings and declarations automatically.
Currently supported targets:
- WebAssembly (wasm32) —
to_js/from_js/patch_jsviawasm-bindgen, with TypeScript.d.tsgeneration - Kotlin and Swift — planned
§Quick start
use typewire::Typewire;
#[derive(Typewire)]
#[typewire(rename_all = "camelCase")]
struct CreateUser {
user_name: String,
age: u32,
email: Option<String>,
}
// Option<T> fields implicitly default to None when absent
assert_eq!(<Option<String>>::or_default(), Some(None));
// Non-optional types have no implicit default
assert!(String::or_default().is_none());On wasm32, this generates conversions matching the serde wire shape
({ "userName": ..., "age": ..., "email": ... }), plus a patch_js
that updates foreign objects in place by diffing only changed fields.
§Pipeline
#[derive(Typewire)] → encode (link section) → decode → declarations
(derive) (typewire-schema) (CLI) (codegen)#[derive(Typewire)]analyzes types and emits platform-gated conversion methods + schema records in link sections (when theschemasfeature is enabled)- The
typewireCLI extracts schema records from compiled binaries and generates typed declarations for the target language - The generated declarations match the wire format of the Rust types
§Built-in type implementations
| Category | Types |
|---|---|
| Booleans | bool |
| Integers | i8, i16, i32, u8, u16, u32 (exact) |
| Big integers | i64, u64, i128, u128, isize, usize |
| Floats | f32, f64 |
| Strings | String, Cow<str>, char |
| Unit | () |
| Options | Option<T> |
| Sequences | Vec<T>, [T; N] |
| Maps | HashMap, BTreeMap |
| Tuples | (A, B, ...) up to 12 elements |
| Smart pointers | Box<T>, Arc<T>, Rc<T> |
With optional features: uuid, chrono, url, bytes, indexmap,
serde_json, fractional_index.
§Features
| Feature | What it enables |
|---|---|
derive (default) | Re-exports #[derive(Typewire)] |
schemas | Embeds schema records in link sections for codegen |
uuid | Typewire impl for uuid::Uuid |
chrono | Typewire impl for chrono::DateTime |
url | Typewire impl for url::Url |
bytes | Typewire impl for bytes::Bytes |
indexmap | Typewire impls for IndexMap / IndexSet |
base64 | base64_encode / base64_decode helpers |
serde_json | Typewire impl for serde_json::Value |
cli | Binary target for schema extraction + declaration generation |
Re-exports§
pub use typewire_schema as schema;
Enums§
- Error
- Conversion errors produced during foreign-language value decoding.
Traits§
- Typewire
- Bidirectional conversion between Rust types and foreign-language values.
Functions§
- base64_
decode - Base64-decode a string to bytes using the standard alphabet.
- base64_
encode - Base64-encode bytes to a string using the standard alphabet.