pub struct TempLibrary { /* private fields */ }
Expand description
A wrapper around a libloading::Library
that cleans up the library on Drop
.
Implementations§
Source§impl TempLibrary
impl TempLibrary
Sourcepub fn lib(&self) -> &Library
pub fn lib(&self) -> &Library
The inner libloading::Library
.
This may also be accessed via the Deref
implementation.
Sourcepub fn build_timestamp(&self) -> SystemTime
pub fn build_timestamp(&self) -> SystemTime
The time at which the original library was built.
Methods from Deref<Target = Library>§
Sourcepub unsafe fn get<'lib, T>(
&'lib self,
symbol: &[u8],
) -> Result<Symbol<'lib, T>, Error>
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?
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}