dlopen/lib.rs
1/*!
2
3Library for opening and working with dynamic link libraries (also known as shared object).
4
5# Overview
6
7This library is an effort to make use of dynamic link libraries in Rust simple.
8Previously existing solutions were either unsafe, provided huge overhead of required writing too much code to achieve simple things.
9I hope that this library will help you to quickly get what you need and avoid errors.
10
11# Quick example
12
13```no_run
14extern crate dlopen;
15#[macro_use]
16extern crate dlopen_derive;
17use dlopen::wrapper::{Container, WrapperApi};
18
19#[derive(WrapperApi)]
20struct Api<'a> {
21 example_rust_fun: fn(arg: i32) -> u32,
22 example_c_fun: unsafe extern "C" fn(),
23 example_reference: &'a mut i32,
24}
25
26fn main(){
27 let mut cont: Container<Api> =
28 unsafe { Container::load("libexample.so") }.expect("Could not open library or load symbols");
29 cont.example_rust_fun(5);
30 unsafe{cont.example_c_fun()};
31 *cont.example_reference_mut() = 5;
32}
33```
34
35# Features
36
37## Main features
38
39* Supports majority of platforms and is platform independent.
40* Is consistent with Rust error handling mechanism and makes making mistakes much more difficult.
41* Is very lightweight. It mostly uses zero cost wrappers to create safer abstractions over platform API.
42* Is thread safe.
43* Is object-oriented programming friendly.
44* Has a low-level API that provides full flexibility of using libraries.
45* Has two high-level APIs that protect against dangling symbols - each in its own way.
46* High level APIs support automatic loading of symbols into structures. You only need to define a
47 structure that represents an API. The rest happens automatically and requires only minimal amount of code.
48* Automatic loading of symbols helps you to follow the DRY paradigm.
49
50## Compare with other libraries
51
52|Feature | dlopen | [libloading](https://github.com/nagisa/rust_libloading) | [sharedlib](https://github.com/Tyleo/sharedlib) |
53|------------------------------------|------------|---------------------------------------------------------|-------------------------------------------------|
54| Basic functionality | Yes | Yes | Yes |
55| Multiplatform | Yes | Yes | Yes |
56|Dangling symbol prevention | Yes | Yes | Yes |
57| Thread safety | Yes | **Potential problem with SetErrorMode() on older Windows platforms** | **No support for SetErrorMode (library may block the application on Windows)**|
58| Loading of symbols into structures | Yes | **No** | **No**
59| Overhead | Minimal | Minimal | **Some overhead** |
60| Low-level, unsafe API | Yes | Yes | Yes |
61| Object-oriented friendly | Yes | **No** | Yes |
62| Load from the program itself | Yes | **No** | **No** |
63| Obtaining address information (dladdr) | Yes | **Unix only** | **No**|
64
65## Safety
66
67Please note that while Rust aims at being 100% safe language, it does not yet provide mechanisms that would allow me to create a 100% safe library, so I had to settle on 99%.
68Also the nature of dynamic link libraries requires casting obtained pointers into types that are defined on the application side. And this cannot be safe.
69Having said that I still think that this library provides the best approach and greatest safety possible in Rust.
70
71# Usage:
72
73Cargo.toml:
74
75```toml
76[dependencies]
77dlopen = "0.1"
78```
79
80# Documentation
81
82[Cargo documentation](https://docs.rs/dlopen)
83
84[Examples](../examples)
85
86[Changelog](https://github.com/szymonwieloch/rust-dlopen/releases)
87
88# License
89This code is licensed under [MIT](../LICENSE) license.
90
91# Acknowledgement
92
93Special thanks to [Simonas Kazlauskas](https://github.com/nagisa) whose [libloading](https://github.com/nagisa/rust_libloading) became code base for my project.
94
95# Comparison of APIs:
96
97* [**raw**](./raw/index.html) - a low-level API. It is mainly intended to give you full flexibility
98 if you decide to create you own custom solution for handling dynamic link libraries.
99 For typical operations you probably should use one of high-level APIs.
100
101* [**symbor**](./symbor/index.html) - a high-level API. It prevents dangling symbols by creating
102 zero cost structural wrappers around symbols obtained from the library. These wrappers use
103 Rust borrowing mechanism to make sure that the library will never get released before obtained
104 symbols.
105
106* [**wrapper**](./wrapper/index.html) - a high-level API. It prevents dangling symbols by creating
107 zero cost functional wrappers around symbols obtained from the library. These wrappers prevent
108 accidental copying of raw symbols from library API. Dangling symbols are prevented by keeping
109 library and its API in one structure - this makes sure that symbols and library are released
110 together.
111
112Additionally both high-level APIs provide a way to automatically load symbols into a structure using
113Rust reflection mechanism. Decision which API should be used is a matter of taste - please check
114documentation of both of them and use the one that you prefer.
115At the moment none seems to have any reasonable advantage over the other.
116
117*/
118
119#[macro_use]
120extern crate lazy_static;
121#[cfg(any(unix, test))]
122extern crate libc;
123#[cfg(windows)]
124extern crate winapi;
125
126#[cfg(test)]
127#[macro_use]
128extern crate const_cstr;
129
130pub mod raw;
131pub mod symbor;
132pub mod utils;
133pub mod wrapper;
134mod err;
135pub use err::Error;