Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use std::sync::RwLock;
- struct MyStruct {
- value: String,
- }
- impl MyStruct {
- // other things that aren't thread-safe
- fn get_value(&self) -> &str {
- self.value.as_ref()
- }
- }
- struct MySyncStruct {
- my_struct: RwLock<MyStruct>
- }
- impl MySyncStruct {
- fn get_value(&self) -> &str {
- self.my_struct.read().unwrap().get_value()
- }
- }
- fn main() {
- let my_struct = RwLock::new(MyStruct {
- value: "test".to_owned()
- });
- let my_sync_struct = MySyncStruct { my_struct };
- let val = my_sync_struct.get_value();
- }
Add Comment
Please, Sign In to add comment