Skip to main content

Crate typewire

Crate typewire 

Source
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_js via wasm-bindgen, with TypeScript .d.ts generation
  • 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)
  1. #[derive(Typewire)] analyzes types and emits platform-gated conversion methods + schema records in link sections (when the schemas feature is enabled)
  2. The typewire CLI extracts schema records from compiled binaries and generates typed declarations for the target language
  3. The generated declarations match the wire format of the Rust types

§Built-in type implementations

CategoryTypes
Booleansbool
Integersi8, i16, i32, u8, u16, u32 (exact)
Big integersi64, u64, i128, u128, isize, usize
Floatsf32, f64
StringsString, Cow<str>, char
Unit()
OptionsOption<T>
SequencesVec<T>, [T; N]
MapsHashMap, BTreeMap
Tuples(A, B, ...) up to 12 elements
Smart pointersBox<T>, Arc<T>, Rc<T>

With optional features: uuid, chrono, url, bytes, indexmap, serde_json, fractional_index.

§Features

FeatureWhat it enables
derive (default)Re-exports #[derive(Typewire)]
schemasEmbeds schema records in link sections for codegen
uuidTypewire impl for uuid::Uuid
chronoTypewire impl for chrono::DateTime
urlTypewire impl for url::Url
bytesTypewire impl for bytes::Bytes
indexmapTypewire impls for IndexMap / IndexSet
base64base64_encode / base64_decode helpers
serde_jsonTypewire impl for serde_json::Value
cliBinary 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.

Derive Macros§

Typewire
Derive macro for the Typewire trait.