Macro snek::snek
[−]
[src]
macro_rules! snek { ($sname:ident { $($symbol:ident : ($($pn: ident : $pt:ty),*) -> $ot:ty),* }) => { ... }; }
This macro is used to generate a struct that wraps a dynamic library with generated loading code. Each defined function will be loaded as a symbol from the library when an instance of the struct is constructed, and can be called via functions of the same name attached to the struct.
In the same way as a Snek
instance, when an instance
of a struct defined by this macro is dropped, the library is unloaded.
Safety
As with Symbol::with
, there is no way
of verifying the types of the functions so care should be taken to ensure
they are correct. All the imported functions will be unsafe as a result
of this.
Example
This example loads the same function as given in the Snek
usage example:
snek! { Example { add: (x: c_int, y: c_int) -> c_int } } fn main() { if let Ok(example) = Example::load("libexample.so") { println!("{}", unsafe { example.add(3, 7) }) } }
Additional functions can be loaded by simply adding them in the macro usage:
snek! { Example { add: (x: c_int, y: c_int) -> c_int, hello: () -> () } }