[][src]Struct libloading::Library

pub struct Library(_);

A loaded dynamic library.

Methods

impl Library[src]

pub fn new<P: AsRef<OsStr>>(filename: P) -> Result<Library>[src]

Find and load a dynamic library.

The filename argument may be any of:

  • A library filename;
  • Absolute path to the library;
  • Relative (to the current working directory) path to the library.

Thread-safety

The implementation strives to be as MT-safe as sanely possible, however due to certain error-handling related resources not always being safe, this library is not MT-safe either.

  • On Windows Vista and earlier error handling falls back to SetErrorMode, which is not MT-safe. MT-scenarios involving this function may cause a traditional data race;
  • On some UNIX targets dlerror might not be MT-safe, resulting in garbage error messages in certain MT-scenarios.

Calling this function from multiple threads is not safe if used in conjunction with path-less filename and library search path is modified (SetDllDirectory function on Windows, {DY,}LD_LIBRARY_PATH environment variable on UNIX).

Platform-specific behaviour

When a plain library filename is supplied, locations where library is searched for is platform specific and cannot be adjusted in a portable manner.

Windows

If the filename specifies a library filename without path and with extension omitted, .dll extension is implicitly added. This behaviour may be suppressed by appending a trailing . to the filename.

If the library contains thread local variables (MSVC’s _declspec(thread), Rust’s #[thread_local] attributes), loading the library will fail on versions prior to Windows Vista.

Tips

Distributing your dynamic libraries under a filename common to all platforms (e.g. awesome.module) allows to avoid code which has to account for platform’s conventional library filenames.

Strive to specify absolute or relative path to your library, unless system-wide libraries are being loaded. Platform-dependent library search locations combined with various quirks related to path-less filenames may cause flaky code.

Examples

// Any of the following are valid.
let _ = Library::new("/path/to/awesome.module").unwrap();
let _ = Library::new("../awesome.module").unwrap();
let _ = Library::new("libsomelib.so.1").unwrap();

pub unsafe fn get<'lib, T>(&'lib self, symbol: &[u8]) -> Result<Symbol<'lib, T>>[src]

Get a pointer to function or static variable by symbol name.

The symbol may not contain any null bytes, with an exception of last byte. A null terminated symbol may avoid a string allocation in some cases.

Symbol is interpreted as-is; no mangling is done. This means that symbols like x::y are most likely invalid.

Unsafety

Pointer to a value of arbitrary type is returned. Using a value with wrong type is undefined.

Platform-specific behaviour

On Linux and Windows, a TLS variable acts just like any regular static variable. OS X uses some sort of lazy initialization scheme, which makes loading TLS variables this way impossible. Using a TLS variable loaded this way on OS X is undefined behaviour.

Examples

Given a loaded library:

let lib = Library::new("/path/to/awesome.module").unwrap();

Loading and using a function looks like this:

unsafe {
    let awesome_function: Symbol<unsafe extern fn(f64) -> f64> =
        lib.get(b"awesome_function\0").unwrap();
    awesome_function(0.42);
}

A static variable may also be loaded and inspected:

unsafe {
    let awesome_variable: Symbol<*mut f64> = lib.get(b"awesome_variable\0").unwrap();
    **awesome_variable = 42.0;
};

Trait Implementations

impl Debug for Library[src]

impl From<Library> for Library[src]

impl From<Library> for Library[src]

impl Send for Library[src]

impl Sync for Library[src]

Auto Trait Implementations

impl RefUnwindSafe for Library

impl Unpin for Library

impl UnwindSafe for Library

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.