#dynamic #load #shared #library

snek

A simple library and macro for using dynamic libraries

6 releases

Uses old Rust 2015

0.3.0 Mar 18, 2016
0.2.0 Feb 18, 2016
0.1.3 Feb 18, 2016

#176 in #load

48 downloads per month

Apache-2.0

18KB
199 lines

Snek

A simple Rust library for defining interfaces to and then loading dynamic libraries.

Unstable at the moment, I don't recommend you use this in serious code yet.

Now with Windows support!

For more information, view the documentation here or via cargo doc

Todo

  • Proper errors
  • More?

lib.rs:

This library provides a convenient interface for loading dynamic libraries at runtime, and for retrieving symbols from them. The recommended use is via the snek! macro, which defines a structure which loads symbols on construction and has methods for each one, however it is also possible to manually load libraries and symbols using a Snek instance.

Safety

There is no way of verifying the type of loaded symbols, so both methods of using them assume that the given type is correct - this library should be used very carefully. Consider everything very unstable at the moment.

Example

#[macro_use] extern crate snek;
extern crate libc;

use libc::c_int;

snek! {
    Example {
        hello: () -> (),
        add: (x: c_int, y: c_int) -> c_int
    }
}

fn main() {
    if let Ok(example) = Example::load("libexample.so") {
        unsafe { example.hello() };
        println!("2 + 4 = {}", unsafe { example.add(2, 4) });
    }
}

Dependencies

~115KB