Skip to content

Instantly share code, notes, and snippets.

@Newlifer
Created February 2, 2017 16:05
Show Gist options
  • Save Newlifer/328eedafd2cdee7bad5eee50e7357327 to your computer and use it in GitHub Desktop.
Save Newlifer/328eedafd2cdee7bad5eee50e7357327 to your computer and use it in GitHub Desktop.
shared.rs
use std::sync::{Arc, RwLock};
struct Foo {
f: Arc<RwLock<i32>>
}
trait Formattable {
fn own_format(&mut self);
}
impl Formattable for Foo {
fn own_format(&mut self) {
let value = *self.f.read().unwrap();
self.f = Arc::new(RwLock::new(value));
}
}
fn main() {
let mut foo = Foo { f: Arc::new(RwLock::new(21)) };
let mut bar = Foo { f: foo.f.clone() };
{
let mut x = foo.f.write().unwrap();
*x = 22;
}
{
let y = bar.f.read().unwrap();
println!("{}", *y);
}
bar.own_format();
{
let mut x = bar.f.write().unwrap();
*x = 122;
}
{
let y = bar.f.read().unwrap();
println!("{}", *y);
}
{
let y = foo.f.read().unwrap();
println!("{}", *y);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment