1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
mod test;
extern crate libloading;
use std::collections::HashMap;
use std::mem::transmute;

pub type Result<T> = libloading::Result<T>;

pub struct Library<'a> {
    lib: libloading::Library,
    fn_map: HashMap<&'a str, libloading::Symbol<'a, *const ()>>
}

impl<'a> Library<'a> {
    pub fn new(filename: &str) -> Result<Library<'a>> {
        Ok(Library {
            lib: libloading::Library::new(filename)?,
            fn_map: HashMap::new()
        })
    }
    pub unsafe fn get<T>(&'a mut self, symbol: &'a str) -> Result<&'a libloading::Symbol<'a, T>> {
        match self.fn_map.get::<str>(symbol) {
            Some(func) => {
                let func = transmute::<&libloading::Symbol<*const()>, &libloading::Symbol<T>>(func);
                return Ok(func);
            },
            None => {}
        };
        let func = self.lib.get(symbol.as_bytes())?;
        self.fn_map.insert(symbol, func);
        let func = self.fn_map.get::<str>(symbol).unwrap();
        let func = transmute::<&libloading::Symbol<*const()>, &libloading::Symbol<T>>(func);
        return Ok(func);
    }
}