1extern crate dynamic_reload;
2
3use dynamic_reload::{DynamicReload, Lib, PlatformName, Search, Symbol, UpdateState};
4use std::sync::Arc;
5use std::thread;
6use std::time::Duration;
7
8struct Plugins {
9 plugins: Vec<Arc<Lib>>,
10}
11
12impl Plugins {
13 fn add_plugin(&mut self, plugin: &Arc<Lib>) {
14 self.plugins.push(plugin.clone());
15 }
16
17 fn unload_plugins(&mut self, lib: &Arc<Lib>) {
18 for i in (0..self.plugins.len()).rev() {
19 if &self.plugins[i] == lib {
20 self.plugins.swap_remove(i);
21 }
22 }
23 }
24
25 fn reload_plugin(&mut self, lib: &Arc<Lib>) {
26 Self::add_plugin(self, lib);
27 }
28
29 fn reload_callback(&mut self, state: UpdateState, lib: Option<&Arc<Lib>>) {
31 match state {
32 UpdateState::Before => Self::unload_plugins(self, lib.unwrap()),
33 UpdateState::After => Self::reload_plugin(self, lib.unwrap()),
34 UpdateState::ReloadFailed(_) => println!("Failed to reload"),
35 }
36 }
37}
38
39fn main() {
40 let mut plugs = Plugins {
41 plugins: Vec::new(),
42 };
43
44 let mut reload_handler = DynamicReload::new(
48 Some(vec!["target/debug"]),
49 Some("target/debug"),
50 Search::Default,
51 Duration::from_secs(2),
52 );
53
54 match unsafe { reload_handler.add_library("test_shared", PlatformName::Yes) } {
56 Ok(lib) => plugs.add_plugin(&lib),
57 Err(e) => {
58 println!("Unable to load dynamic lib, err {:?}", e);
59 return;
60 }
61 }
62
63 loop {
66 unsafe {
67 reload_handler.update(&Plugins::reload_callback, &mut plugs);
68 }
69
70 if plugs.plugins.len() > 0 {
71 let fun: Symbol<extern "C" fn() -> i32> =
74 unsafe { plugs.plugins[0].lib.get(b"shared_fun\0").unwrap() };
75
76 println!("Value {}", fun());
77 }
78
79 thread::sleep(Duration::from_millis(500));
81 }
82}