Struct TempLibrary

Source
pub struct TempLibrary { /* private fields */ }
Expand description

A wrapper around a libloading::Library that cleans up the library on Drop.

Implementations§

Source§

impl TempLibrary

Source

pub fn lib(&self) -> &Library

The inner libloading::Library.

This may also be accessed via the Deref implementation.

Source

pub fn build_timestamp(&self) -> SystemTime

The time at which the original library was built.

Source

pub fn path(&self) -> &Path

The path at which the loaded temporary library is located.

Methods from Deref<Target = Library>§

Source

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

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.

§Safety

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

§Platform-specific behaviour

Implementation of thread local variables is extremely platform specific and uses of these variables that work on e.g. Linux may have unintended behaviour on other POSIX systems or Windows.

On POSIX implementations where the dlerror function is not confirmed to be MT-safe (such as FreeBSD), this function will unconditionally return an error the underlying dlsym call returns a null pointer. There are rare situations where dlsym returns a genuine null pointer without it being an error. If loading a null pointer is something you care about, consider using the os::unix::Library::get_singlethreaded call.

§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;
};
Examples found in repository?
examples/demo.rs (line 10)
1fn main() {
2    let test_crate_path = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
3        .join("test_crate")
4        .join("Cargo.toml");
5    println!("Begin watching for changes to {:?}", test_crate_path);
6    let watch = hotlib::watch(&test_crate_path).unwrap();
7    let mut lib = watch.package().build().unwrap().load().unwrap();
8    loop {
9        unsafe {
10            let foo: libloading::Symbol<fn(i32, i32) -> i32> = lib.get(b"foo").unwrap();
11            let res = foo(6, 7);
12            println!("{}", res);
13        }
14        println!("Awaiting next change...");
15        let pkg = watch.next().unwrap();
16        lib = pkg.build().unwrap().load().unwrap();
17    }
18}

Trait Implementations§

Source§

impl Deref for TempLibrary

Source§

type Target = Library

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.
Source§

impl Drop for TempLibrary

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

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

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> Any for T
where T: Any,

Source§

impl<T> Erased for T