Giter VIP home page Giter VIP logo

deno_bindgen's Introduction

deno_bindgen

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

QuickStart

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

// add.rs
use deno_bindgen::deno_bindgen;

#[deno_bindgen]
pub struct Input {
  a: i32,
  b: i32,
}

#[deno_bindgen]
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

Installation

  • 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
[dependencies]
deno_bindgen = "0.2"
serde = { version = "1", features = ["derive"] }

Bindings

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

struct (named fields)

These transform into Typescript types.

// lib.rs
#[deno_bindgen]
pub struct A {
  b: Vec<Vec<String>>,
}

becomes:

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

enum

Enums become type unions in Typescript.

#[deno_bindgen]
pub enum Event {
  Quit,
  MouseMove {
    x: i32,
    y: i32,
  }
}

becomes:

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

fn

Functions are exposed through the FFI boundaries.

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

becomes:

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

Notes

  • 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.

    #[deno_bindgen]
    pub struct Me {
      /// My name...
      /// ...it is
      name: String,
    }

    becomes:

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

CLI

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"

LICENSE

MIT

deno_bindgen's People

Contributors

grian32 avatar littledivy avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.