Guest User

Untitled

a guest
May 22nd, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.79 KB | None | 0 0
  1. use std::sync::{Arc, Mutex};
  2.  
  3. thread_local! {
  4. static APP_STATE: Arc<Mutex<Box<Trait>>> = Arc::new(Mutex::new(Box::new(Foo {})));
  5. }
  6.  
  7. fn main() {
  8. println!("Hello, world!");
  9.  
  10. APP_STATE.with(|state_mutex| {
  11. let mut foo_or_bar = state_mutex.lock().unwrap();
  12. *foo_or_bar = foo_or_bar.consume();
  13. });
  14. }
  15.  
  16. trait Trait {
  17. fn consume(self: Box<Self>) -> Box<Trait>;
  18. fn get_name(self: Box<Self>) -> &'static str;
  19. }
  20.  
  21. #[derive(Debug)]
  22. struct Foo {}
  23.  
  24. impl Trait for Foo {
  25. fn consume(self: Box<Self>) -> Box<Trait> { Box::new(Bar {}) }
  26. fn get_name(self: Box<Self>) -> &'static str { "foo" }
  27. }
  28.  
  29. #[derive(Debug)]
  30. struct Bar {}
  31.  
  32. impl Trait for Bar {
  33. fn consume(self: Box<Self>) -> Box<Trait> { self as Box<Trait> }
  34. fn get_name(self: Box<Self>) -> &'static str { "bar" }
  35. }
Add Comment
Please, Sign In to add comment