Skip to content

Instantly share code, notes, and snippets.

@Cobrand
Created August 13, 2016 21:09
Show Gist options
  • Save Cobrand/f81b5e3608235dafdc2bd71d468890e7 to your computer and use it in GitHub Desktop.
Save Cobrand/f81b5e3608235dafdc2bd71d468890e7 to your computer and use it in GitHub Desktop.
extern crate iron;
use std::sync::{Arc,Weak,RwLock};
use iron::prelude::*;
use iron::{Listening,status};
use std::ops::Drop ;
use std::net::ToSocketAddrs;
pub struct State {
pub whatever : String
}
pub struct Manager {
listening:Listening,
state:Weak<RwLock<State>>
}
impl Manager {
pub fn new<A : ToSocketAddrs>(address: A, weak_state: Weak<RwLock<State>>) -> Manager {
let handler = |_ : &mut Request| {
let weak_cloned = state.clone(); // Doesnt work
//Ok(Response::with((status::Ok, "Hello world!")))// Works
// Answer with whatever is in State, answer None if the data of Weak has been destroyed
};
let listening = Iron::new(handler).http(address).unwrap();
Manager {
listening : listening,
state : weak_state
}
}
}
impl Drop for Manager {
fn drop(&mut self) {
self.listening.close();
}
}
fn main() {
let state = Arc::new(RwLock::new(State {
whatever : String::from("test")
}));
let manager = Manager::new("0.0.0.0:8080",Arc::downgrade(&state));
loop {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment