lib.rs 875 B

1234567891011121314151617181920212223242526272829303132333435
  1. #![feature(drop_types_in_const)]
  2. extern crate libloading;
  3. use std::sync::{Once, ONCE_INIT};
  4. use std::env;
  5. use libloading::Library;
  6. fn compiler_rt() -> &'static Library {
  7. let dir = env::current_exe().unwrap();
  8. let cdylib = dir.parent().unwrap().read_dir().unwrap().map(|c| {
  9. c.unwrap().path()
  10. }).find(|path| {
  11. path.file_name().unwrap().to_str().unwrap().contains("compiler_rt_cdylib")
  12. }).unwrap();
  13. unsafe {
  14. static mut COMPILER_RT: Option<Library> = None;
  15. static INIT: Once = ONCE_INIT;
  16. INIT.call_once(|| {
  17. COMPILER_RT = Some(Library::new(&cdylib).unwrap());
  18. });
  19. COMPILER_RT.as_ref().unwrap()
  20. }
  21. }
  22. pub fn get(sym: &str) -> usize {
  23. unsafe {
  24. let sym = format!("_{}", sym);
  25. let f: fn() -> usize = *compiler_rt().get(sym.as_bytes()).unwrap();
  26. f()
  27. }
  28. }