use super::*;
use std::env::consts::{DLL_PREFIX, DLL_SUFFIX};
pub struct RawLibrary {
path: PathBuf,
library: LibLoadingLibrary,
}
impl RawLibrary {
pub fn path_in_directory(directory: &Path, base_name: &str, suffix: LibrarySuffix) -> PathBuf {
let formatted: String;
let is_64_bits =
cfg!(any(x86_64, powerpc64, aarch64)) || ::std::mem::size_of::<usize>() == 8;
let bits = if is_64_bits { "64" } else { "32" };
let maybe_suffixed_name = match suffix {
LibrarySuffix::Suffix => {
formatted = format!("{}-{}", base_name, bits);
&*formatted
}
LibrarySuffix::NoSuffix => base_name,
};
let name = format!("{}{}{}", DLL_PREFIX, maybe_suffixed_name, DLL_SUFFIX);
directory.join(name)
}
pub fn load_at(full_path: &Path) -> Result<Self, LibraryError> {
match unsafe { LibLoadingLibrary::new(full_path) } {
Ok(library) => Ok(Self {
path: full_path.to_owned(),
library,
}),
Err(err) => Err(LibraryError::OpenError {
path: full_path.to_owned(),
err: Box::new(err),
}),
}
}
pub unsafe fn get<T>(&self, symbol_name: &[u8]) -> Result<LLSymbol<'_, T>, LibraryError> {
match unsafe { self.library.get::<T>(symbol_name) } {
Ok(symbol) => Ok(symbol),
Err(io) => {
let symbol = symbol_name.to_owned();
Err(LibraryError::GetSymbolError {
library: self.path.clone(),
symbol,
err: Box::new(io),
})
}
}
}
}