Created
March 29, 2016 21:28
-
-
Save anonymous/9cd4e611e99f65a91a2076b917f7f3c6 to your computer and use it in GitHub Desktop.
Shared via Rust Playground
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::sync::{Arc, Mutex}; | |
trait Get<T> { | |
fn get(&mut self) -> T; | |
} | |
trait Set { | |
fn set(&mut self, t: Str); | |
} | |
struct Str<'a>(&'a str); | |
struct X(u64); | |
struct Future<T>(Arc<Mutex<Get<T>>>); | |
struct Port(Arc<Mutex<Set>>); | |
impl Get<u64> for X { | |
fn get(&mut self) -> u64 { | |
self.0 | |
} | |
} | |
impl Set for X { | |
fn set(&mut self, x: Str) { | |
self.0 = x.0.parse().unwrap(); | |
} | |
} | |
struct Fun<T, F:FnOnce(Str) -> T>(Option<T>, Option<F>); | |
impl<T, F:FnOnce(Str) -> T> Set for Fun<T, F> { | |
fn set(&mut self, t: Str) { | |
self.0 = Some((self.1).take().unwrap()(t)); | |
} | |
} | |
impl<T, F:FnOnce(Str) -> T> Get<T> for Fun<T, F> { | |
fn get(&mut self) -> T { | |
self.0.take().unwrap() | |
} | |
} | |
fn pair<T: 'static, F:FnOnce(Str) -> T + 'static>(fun: F) -> (Future<T>, Port) { | |
let arc = Arc::new(Mutex::new(Fun(None, Some(fun)))); | |
return (Future(arc.clone()), Port(arc)) | |
} | |
fn main() { | |
let (future, port) = pair(|s| s.0.parse().unwrap()); | |
{ | |
let s = String::from("135152"); | |
port.0.lock().unwrap().set(Str(&s)) | |
} | |
let v: u64 = future.0.lock().unwrap().get(); | |
println!("VALUE {}", v); | |
/* | |
let x = Arc::new(Mutex::new(X(7))); | |
let y: Arc<Mutex<Get<u64>>> = x.clone(); | |
let z: Arc<Mutex<Set>> = x.clone(); | |
println!("GET {}", y.lock().unwrap().get()); | |
z.lock().unwrap().set(Str(&s[..])); | |
println!("GET {}", y.lock().unwrap().get()); | |
*/ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment