adapter_libloading/
lib.rs

1use adapter::Adapter;
2use libloading::{Library, Symbol};
3
4pub struct LibloadingAdapter {
5    library: Library,
6}
7
8impl LibloadingAdapter {
9    fn from_path(path: &str) -> Result<Self, libloading::Error> {
10        match unsafe { libloading::Library::new(path) } {
11            Ok(library) => Ok(Self { library }),
12            Err(error) => Err(error),
13        }
14    }
15}
16
17impl<'a, Input, Output> Adapter<'a, Input, Output> for LibloadingAdapter {
18    type Error = libloading::Error;
19    type Identifier = &'a [u8];
20
21    fn call(&'a mut self, identifier: Self::Identifier, input: Input) -> Result<Output, Self::Error>
22    where
23        Self::Identifier: AsRef<[u8]>,
24    {
25        // may be a good idea to cache these.
26        let symbol: Symbol<fn(Input) -> Output> =
27            match unsafe { self.library.get(identifier.as_ref()) } {
28                Ok(symbol) => symbol,
29                Err(error) => return Err(error),
30            };
31
32        Ok(symbol(input))
33    }
34}
35
36#[cfg(test)]
37mod tests {
38    use super::*;
39
40    #[test]
41    fn test_libloading_works() {
42        // TODO: impl testing
43
44        // let mut adapter = LibloadingAdapter::from_path("some_path").unwrap();
45        //
46        // let res:  = adapter
47        //     .call("some_identifier", "some_input".to_string())
48        //     .unwrap();
49    }
50}