I have the following code where I am storing an App inside a Mutex and an Arc but I also want to be able to swap this App out at any point during runtime. And the following code seems to work just fine.
However, even though the App is marked 'static inside the static declaration and inside the set_app function, I am able to swap the underlying App out without issue. Plus the original App inside the Mutex gets dropped, even though it was marked 'static.
Apologies as I'm new to Rust but clearly I'm missing something. In my mind if an object is able to go out of scope and be dropped it isn't 'static. Why does this work?
use std::sync::{Mutex, Arc};
use lazy_static::lazy_static;
lazy_static! {
pub static ref APP: Arc<Mutex<App<'static>>> = Arc::new(Mutex::new(App { name: "App" }));
}
pub struct App<'a> {
name: &'a str,
}
impl<'a> Drop for App<'a> {
fn drop(&mut self) {
println!("DROPPING {}", self.name)
}
}
fn main() {
println!("CURRENT {}", APP.lock().unwrap().name);
// Drops "App1"
*APP.lock().unwrap() = App { name: "App2" };
println!("CURRENT {}", APP.lock().unwrap().name);
// Drops "App2"
set_app(App { name: "App3" });
println!("CURRENT {}", APP.lock().unwrap().name);
}
fn set_app(app: App<'static>) {
*APP.lock().unwrap() = app;
}
Output:
CURRENT App
DROPPING App
CURRENT App2
DROPPING App2
CURRENT App3