GithubHelp home page GithubHelp logo

outfoxxed / dyntable Goto Github PK

View Code? Open in Web Editor NEW
17.0 1.0 0.0 319 KB

Idiomatic and FFI Safe dyn traits in Rust

License: Apache License 2.0

Rust 97.94% C 1.34% Nix 0.72%
dynamic-dispatch ffi macro proc-macro rust trait trait-object vtable c-abi c-ffi

dyntable's Introduction

dyntable

crates.io docs.rs

Idiomatic and FFI Safe dyn traits.

Overview

This crate is an alternative implementation of Rust trait objects that aims to get around one main limitation: The ABI of trait objects is unspecified.

Usually this limitation is not an issue because the majority of rust code is statically linked, but it means trait objects are largely useless for a variety of situations such as:

  • Implementing a plugin system
  • Interacting with C, or any other language
  • Dynamic linking
  • On the fly codegen

This crate implements idiomatic trait objects, implemented using fat pointers similar to native rust traits, with support for trait bounds (inheritance) and upcasting. Implementing dyntable traits works exactly the same as normal rust traits.

Notable features

  • Uses fat pointers internally
  • Trait bounds and trait upcasting
  • Custom allocator support (optionally using std's allocator_api)
  • Uses existing dyn syntax
  • Only one annotation macro required per trait
  • Extensive testing
  • Passes Miri

Examples

Below is a simple example from the docs

use dyntable::*;

#[dyntable]
trait MessageBuilder {
    // Note that String is not FFI safe, but is used here for simplicity.
    extern "C" fn build(&self) -> String;
}

struct Greeter(&'static str);

impl MessageBuilder for Greeter {
    extern "C" fn build(&self) -> String {
        format!("Hello {}!", self.0)
    }
}

let greeter = Greeter("World");

// move the greeter into a DynBox of MessageBuilder. This box can hold any
// object safe MessageBuilder implementation.
let greeter_box = DynBox::<dyn MessageBuilder>::new(greeter);

// methods implemented on a dyntrait are callable directly from the DynBox.
assert_eq!(greeter_box.build(), "Hello World!");

Stability

This crate is somewhat early in development. Things most likely won't break if you stick to the non-internal types (pointer, ref, box, the macro), but more internal ones may. A semver bump will be required for breaking interface changes.

Alternatives

A few other crates may suit your needs better than dyntable. There is a comparison in COMPARISON.md

dyntable's People

Contributors

outfoxxed avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 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.