Guest User

Untitled

a guest
Jul 20th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.55 KB | None | 0 0
  1. use std::sync::RwLock;
  2.  
  3. struct MyStruct {
  4. value: String,
  5. }
  6.  
  7. impl MyStruct {
  8. // other things that aren't thread-safe
  9.  
  10. fn get_value(&self) -> &str {
  11. self.value.as_ref()
  12. }
  13. }
  14.  
  15. struct MySyncStruct {
  16. my_struct: RwLock<MyStruct>
  17. }
  18.  
  19. impl MySyncStruct {
  20. fn get_value(&self) -> &str {
  21. self.my_struct.read().unwrap().get_value()
  22. }
  23. }
  24.  
  25. fn main() {
  26. let my_struct = RwLock::new(MyStruct {
  27. value: "test".to_owned()
  28. });
  29.  
  30. let my_sync_struct = MySyncStruct { my_struct };
  31. let val = my_sync_struct.get_value();
  32. }
Add Comment
Please, Sign In to add comment