Advertisement
Guest User

Untitled

a guest
Mar 25th, 2019
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.88 KB | None | 0 0
  1. use std::sync::Arc;
  2. use std::sync::Mutex;
  3.  
  4. #[derive(Debug)]
  5. struct AppState {
  6. services: Option<Arc<Mutex<AppServices>>>,
  7. }
  8.  
  9. impl AppState {
  10. pub fn new() -> Self {
  11. AppState {
  12. services: None,
  13. }
  14. }
  15.  
  16. pub fn set_services(&mut self, services: Option<Arc<Mutex<AppServices>>>){
  17. self.services = services;
  18. }
  19. }
  20.  
  21. #[derive(Debug)]
  22. struct AppServices {
  23. id: IdService,
  24. }
  25.  
  26. #[derive(Debug)]
  27. struct IdService {
  28. name: String,
  29. state: Arc<Mutex<AppState>>,
  30. }
  31.  
  32. fn main() {
  33. let app_state = AppState::new();
  34. let arc = Arc::new(Mutex::new(app_state));
  35.  
  36. let id_service = IdService {
  37. name: "test".to_string(),
  38. state: arc.clone(),
  39. };
  40.  
  41. let app_services = AppServices {
  42. id: id_service,
  43. };
  44.  
  45. let mut app_state_ref = arc.lock().unwrap();
  46. app_state_ref.set_services(Some(Arc::new(Mutex::new(app_services))));
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement