Created
March 29, 2016 21:55
-
-
Save anonymous/57927ddef4f17d1b395c739a314d9d76 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 Set { | |
fn set(&mut self, t: Str); | |
} | |
struct Str<'a>(&'a str); | |
pub struct Port(Arc<Mutex<Set>>); | |
use int::{Fun, Future}; | |
pub fn pair<T: 'static, F:FnOnce(Str) -> T + 'static>(fun: F) -> (Future<T>, Port) { | |
let arc = Arc::new(Mutex::new(Fun::new(fun))); | |
return (Future(arc.clone()), Port(arc)) | |
} | |
mod int { | |
use std::sync::{Arc, Mutex}; | |
use std::marker::PhantomData; | |
pub trait Get<T> { | |
fn get(&mut self) -> T; | |
} | |
pub struct Future<T>(pub Arc<Mutex<Get<T>>>); | |
pub struct Fun<T, S, F:FnOnce(S) -> T>(Option<T>, Option<F>, PhantomData<*const S>); | |
impl<T, S, F:FnOnce(S) -> T> Fun<T, S, F> { | |
pub fn put(&mut self, t: T) { | |
self.0 = Some(t); | |
} | |
pub fn convert(&mut self) -> F { | |
(self.1).take().unwrap() | |
} | |
} | |
impl<T, S, F:FnOnce(S) -> T> Fun<T, S, F> { | |
pub fn new(fun: F) -> Fun<T, S, F> { | |
Fun(None, Some(fun), PhantomData) | |
} | |
} | |
impl<T, S, F:FnOnce(S) -> T> Get<T> for Fun<T, S, F> { | |
fn get(&mut self) -> T { | |
self.0.take().unwrap() | |
} | |
} | |
} | |
impl<'a, T, F:FnOnce(Str) -> T> Set for Fun<T, Str<'a>, F> { | |
fn set(&mut self, t: Str) { | |
let v = self.convert()(t); | |
self.put(v) | |
} | |
} | |
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); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment