Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use std::sync::{Arc, Mutex};
- thread_local! {
- static APP_STATE: Arc<Mutex<Box<Trait>>> = Arc::new(Mutex::new(Box::new(Foo {})));
- }
- fn main() {
- println!("Hello, world!");
- APP_STATE.with(|state_mutex| {
- let mut foo_or_bar = state_mutex.lock().unwrap();
- *foo_or_bar = foo_or_bar.consume();
- });
- }
- trait Trait {
- fn consume(self: Box<Self>) -> Box<Trait>;
- fn get_name(self: Box<Self>) -> &'static str;
- }
- #[derive(Debug)]
- struct Foo {}
- impl Trait for Foo {
- fn consume(self: Box<Self>) -> Box<Trait> { Box::new(Bar {}) }
- fn get_name(self: Box<Self>) -> &'static str { "foo" }
- }
- #[derive(Debug)]
- struct Bar {}
- impl Trait for Bar {
- fn consume(self: Box<Self>) -> Box<Trait> { self as Box<Trait> }
- fn get_name(self: Box<Self>) -> &'static str { "bar" }
- }
Add Comment
Please, Sign In to add comment