ah-yu / deno_bindgen

Simplified glue code generation for Deno FFI libraries written in Rust.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool


This tool aims to simplify glue code generation for Deno FFI libraries written in Rust.


Annotate on top of Rust fn, struct and enum to make them avaiable to Deno.

// add.rs
use deno_bindgen::deno_bindgen;

pub struct Input {
  a: i32,
  b: i32,

fn add(input: Input) -> i32 {
  input.a + input.b

Invoke the CLI to compile and generate bindings:

$ deno_bindgen

And finally import the generated bindings in your JS

// add.ts
import { add } from "./bindings/bindings.ts";

add({ a: 1, b: 2 }); // 3


  • Install the deno_bindgen CLI with Deno.
deno install -Afq -n deno_bindgen https://deno.land/x/deno_bindgen/cli.ts

Add the following dependencies to your crate.

# Cargo.toml
deno_bindgen = "0.4.1"
serde = { version = "1", features = ["derive"] }

Change your crate-type to cdylib and set your package name as well.

name = "___"
crate-type = ["cdylib"]


Put #[deno_bindgen] on top of a "serde-deriavable" struct, enum or fn.

struct (named fields)

These transform into Typescript types.

// lib.rs
pub struct A {
  b: Vec<Vec<String>>,


// bindings/bindings.ts
export type A = {
  b: Array<Array<string>>;


Enums become type unions in Typescript.

pub enum Event {
  MouseMove {
    x: i32,
    y: i32,


export type Enum =
  | "quit"
  | {
    mouse_move: {
      x: number;
      y: number;


Functions are exposed through the FFI boundaries.

fn greet(name: &str) {
  println!("Hello, {}!", name);


export function greet(name: string) {
  // ... glue code for calling the
  // symbol.


  • Use #[deno_bindgen(non_blocking)] attribute to call symbol without blocking JS event loop. Exposed as an async funtion from bindings.

  • Rust doc comments transform to JS docs.

    pub struct Me {
      /// My name...
      /// ...it is
      name: String,


    export type Me = {
       * My name...
       * ...it is
      name: string;


The deno_bindgen CLI tool provides the following flags:

  • Pass --release to create a release build.

  • --release=URL will load library artifacts from a remote location. This is useful for updating bindings for end users after a release:

    deno_bindgen --release=https://github.com/littledivy/deno_sdl2/releases/download/0.2-alpha.1

    Under the hood this uses x/plug to fetch and cache the artifact.

  • Flags after -- will be passed to cargo build. Example:

    deno_bindgen -- --features "cool_stuff"


Simplified glue code generation for Deno FFI libraries written in Rust.

License:MIT License


Language:Rust 62.3%Language:TypeScript 37.4%Language:Makefile 0.3%