{"action":"created","comment":{"url":"https://api.github.com/repos/alexcrichton/futures-rs/pulls/comments/70353518","id":70353518,"diff_hunk":"@@ -0,0 +1,342 @@\n+use std::cell::{Cell, RefCell};\n+use std::io::{self, ErrorKind};\n+use std::sync::Arc;\n+use std::sync::atomic::{AtomicUsize, ATOMIC_USIZE_INIT, Ordering};\n+use std::sync::mpsc;\n+\n+use mio;\n+use slab::Slab;\n+use futures::{Future, Tokens, Wake, PollResult};\n+\n+pub type Waiter = Arc<Wake>;\n+pub type Source = Arc<mio::Evented + Send + Sync>;\n+\n+static NEXT_LOOP_ID: AtomicUsize = ATOMIC_USIZE_INIT;\n+scoped_thread_local!(static CURRENT_LOOP: Loop);\n+\n+const SLAB_CAPACITY: usize = 1024 * 64;\n+\n+pub struct Loop {\n+    id: usize,\n+    active: Cell<bool>,\n+    io: RefCell<mio::Poll>,\n+    tx: mio::channel::Sender<Message>,\n+    rx: mio::channel::Receiver<Message>,\n+    dispatch: RefCell<Slab<Scheduled, usize>>,\n+}\n+\n+#[derive(Clone)]\n+pub struct LoopHandle {\n+    id: usize,\n+    tx: mio::channel::Sender<Message>,\n+}\n+\n+#[derive(Copy, Clone)]\n+pub enum Direction {\n+    Read,\n+    Write,\n+}\n+\n+struct Scheduled {\n+    source: Source,\n+    reader: Option<Waiter>,\n+    writer: Option<Waiter>,\n+}\n+\n+impl Scheduled {\n+    fn waiter_for(&mut self, dir: Direction) -> &mut Option<Waiter> {\n+        match dir {\n+            Direction::Read => &mut self.reader,\n+            Direction::Write => &mut self.writer,\n+        }\n+    }\n+\n+    fn event_set(&self) -> mio::EventSet {\n+        let mut set = mio::EventSet::none();\n+        if self.reader.is_some() {\n+            set = set | mio::EventSet::readable()\n+        }\n+        if self.writer.is_some() {\n+            set = set | mio::EventSet::writable()\n+        }\n+        set\n+    }\n+}\n+\n+enum Message {\n+    AddSource(Source, Arc<AtomicUsize>, Waiter),\n+    DropSource(usize),\n+    Schedule(usize, Direction, Waiter),\n+    Deschedule(usize, Direction),\n+    Shutdown,\n+}\n+\n+fn register(poll: &mut mio::Poll, token: usize, sched: &Scheduled) {\n+    // TODO: handle error\n+    poll.register(&*sched.source,\n+                  mio::Token(token),\n+                  mio::EventSet::none(),\n+                  mio::PollOpt::level())\n+        .unwrap();\n+}\n+\n+fn reregister(poll: &mut mio::Poll, token: usize, sched: &Scheduled) {\n+    // TODO: handle error\n+    poll.reregister(&*sched.source,\n+                    mio::Token(token),\n+                    sched.event_set(),\n+                    mio::PollOpt::edge() | mio::PollOpt::oneshot())\n+        .unwrap();\n+}\n+\n+fn deregister(poll: &mut mio::Poll, sched: &Scheduled) {\n+    // TODO: handle error\n+    poll.deregister(&*sched.source).unwrap();\n+}\n+\n+impl Loop {\n+    pub fn new() -> io::Result<Loop> {\n+        let (tx, rx) = mio::channel::from_std_channel(mpsc::channel());\n+        let io = try!(mio::Poll::new());\n+        try!(io.register(&rx,\n+                         mio::Token(0),\n+                         mio::EventSet::readable(),\n+                         mio::PollOpt::edge()));\n+        Ok(Loop {\n+            id: NEXT_LOOP_ID.fetch_add(1, Ordering::Relaxed),\n+            active: Cell::new(true),\n+            io: RefCell::new(io),\n+            tx: tx,\n+            rx: rx,\n+            dispatch: RefCell::new(Slab::new_starting_at(1, SLAB_CAPACITY)),\n+        })\n+    }\n+\n+    pub fn handle(&self) -> LoopHandle {\n+        LoopHandle {\n+            id: self.id,\n+            tx: self.tx.clone(),\n+        }\n+    }\n+\n+    pub fn run<F: Future>(self, f: F) -> Result<F::Item, F::Error> {\n+        let (tx_res, rx_res) = mpsc::channel();\n+        let handle = self.handle();\n+        f.then(move |res| {\n+            handle.shutdown();","path":"mio/src/event_loop.rs","position":126,"original_position":126,"commit_id":"bc60d83117b1f7aa23017fedcf99997af46596a1","original_commit_id":"bc60d83117b1f7aa23017fedcf99997af46596a1","user":{"login":"alexcrichton","id":64996,"avatar_url":"https://avatars.githubusercontent.com/u/64996?v=3","gravatar_id":"","url":"https://api.github.com/users/alexcrichton","html_url":"https://github.com/alexcrichton","followers_url":"https://api.github.com/users/alexcrichton/followers","following_url":"https://api.github.com/users/alexcrichton/following{/other_user}","gists_url":"https://api.github.com/users/alexcrichton/gists{/gist_id}","starred_url":"https://api.github.com/users/alexcrichton/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/alexcrichton/subscriptions","organizations_url":"https://api.github.com/users/alexcrichton/orgs","repos_url":"https://api.github.com/users/alexcrichton/repos","events_url":"https://api.github.com/users/alexcrichton/events{/privacy}","received_events_url":"https://api.github.com/users/alexcrichton/received_events","type":"User","site_admin":false},"body":"oh lol I see `self` now, may be a bit aggressive though!\r\n","created_at":"2016-07-11T23:09:17Z","updated_at":"2016-07-11T23:09:17Z","html_url":"https://github.com/alexcrichton/futures-rs/pull/6#discussion_r70353518","pull_request_url":"https://api.github.com/repos/alexcrichton/futures-rs/pulls/6","_links":{"self":{"href":"https://api.github.com/repos/alexcrichton/futures-rs/pulls/comments/70353518"},"html":{"href":"https://github.com/alexcrichton/futures-rs/pull/6#discussion_r70353518"},"pull_request":{"href":"https://api.github.com/repos/alexcrichton/futures-rs/pulls/6"}}},"pull_request":{"url":"https://api.github.com/repos/alexcrichton/futures-rs/pulls/6","id":76993141,"html_url":"https://github.com/alexcrichton/futures-rs/pull/6","diff_url":"https://github.com/alexcrichton/futures-rs/pull/6.diff","patch_url":"https://github.com/alexcrichton/futures-rs/pull/6.patch","issue_url":"https://api.github.com/repos/alexcrichton/futures-rs/issues/6","number":6,"state":"open","locked":false,"title":"Mio reintegration","user":{"login":"aturon","id":709807,"avatar_url":"https://avatars.githubusercontent.com/u/709807?v=3","gravatar_id":"","url":"https://api.github.com/users/aturon","html_url":"https://github.com/aturon","followers_url":"https://api.github.com/users/aturon/followers","following_url":"https://api.github.com/users/aturon/following{/other_user}","gists_url":"https://api.github.com/users/aturon/gists{/gist_id}","starred_url":"https://api.github.com/users/aturon/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/aturon/subscriptions","organizations_url":"https://api.github.com/users/aturon/orgs","repos_url":"https://api.github.com/users/aturon/repos","events_url":"https://api.github.com/users/aturon/events{/privacy}","received_events_url":"https://api.github.com/users/aturon/received_events","type":"User","site_admin":false},"body":"Here's the second stab at mio integration.\r\n\r\n- Talks to event loop via TLS when possible.\r\n- Uses synchronization with the event loop (via futures) to acquire tokens, therefore allowing us to use a simple slab instead of a hashmap.\r\n- Generic `ReadinessStream` abstraction: a stream of `()` values to signify readiness, which in general can include spurious notifications.\r\n- A `TcpListener`, which wraps up a readiness stream with the ability to accept incoming connections.\r\n- `tcp_connect`, which acquires a `TcpStream`\r\n\r\nI've also updated the `main.rs` to use these features. Right now, that means building directly on top of readiness streams (and hence using `read` directly). The plan from here is to build a simple tokio-like implementation of the /plaintext test, which should point the way toward using additional stream combinators etc. internally to structure things. Note, however, that you always have the option of continuing to work in \"manual mode\", basically pulling together readiness streams and IO actions into your own custom scheduler/dispatch within the futures framework. That means we have a great deal of flexibility in finding the right factoring.\r\n\r\nFinally, adds a few stream combinators along the way.","created_at":"2016-07-11T17:36:50Z","updated_at":"2016-07-11T23:09:17Z","closed_at":null,"merged_at":null,"merge_commit_sha":"a3fe12677fa2ca35110113ce78260d7a8e634d7b","assignee":null,"assignees":[],"milestone":null,"commits_url":"https://api.github.com/repos/alexcrichton/futures-rs/pulls/6/commits","review_comments_url":"https://api.github.com/repos/alexcrichton/futures-rs/pulls/6/comments","review_comment_url":"https://api.github.com/repos/alexcrichton/futures-rs/pulls/comments{/number}","comments_url":"https://api.github.com/repos/alexcrichton/futures-rs/issues/6/comments","statuses_url":"https://api.github.com/repos/alexcrichton/futures-rs/statuses/bc60d83117b1f7aa23017fedcf99997af46596a1","head":{"label":"aturon:mio-reintegration","ref":"mio-reintegration","sha":"bc60d83117b1f7aa23017fedcf99997af46596a1","user":{"login":"aturon","id":709807,"avatar_url":"https://avatars.githubusercontent.com/u/709807?v=3","gravatar_id":"","url":"https://api.github.com/users/aturon","html_url":"https://github.com/aturon","followers_url":"https://api.github.com/users/aturon/followers","following_url":"https://api.github.com/users/aturon/following{/other_user}","gists_url":"https://api.github.com/users/aturon/gists{/gist_id}","starred_url":"https://api.github.com/users/aturon/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/aturon/subscriptions","organizations_url":"https://api.github.com/users/aturon/orgs","repos_url":"https://api.github.com/users/aturon/repos","events_url":"https://api.github.com/users/aturon/events{/privacy}","received_events_url":"https://api.github.com/users/aturon/received_events","type":"User","site_admin":false},"repo":{"id":62409917,"name":"futures-rs","full_name":"aturon/futures-rs","owner":{"login":"aturon","id":709807,"avatar_url":"https://avatars.githubusercontent.com/u/709807?v=3","gravatar_id":"","url":"https://api.github.com/users/aturon","html_url":"https://github.com/aturon","followers_url":"https://api.github.com/users/aturon/followers","following_url":"https://api.github.com/users/aturon/following{/other_user}","gists_url":"https://api.github.com/users/aturon/gists{/gist_id}","starred_url":"https://api.github.com/users/aturon/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/aturon/subscriptions","organizations_url":"https://api.github.com/users/aturon/orgs","repos_url":"https://api.github.com/users/aturon/repos","events_url":"https://api.github.com/users/aturon/events{/privacy}","received_events_url":"https://api.github.com/users/aturon/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/aturon/futures-rs","description":"WIP implementation of futures in Rust","fork":true,"url":"https://api.github.com/repos/aturon/futures-rs","forks_url":"https://api.github.com/repos/aturon/futures-rs/forks","keys_url":"https://api.github.com/repos/aturon/futures-rs/keys{/key_id}","collaborators_url":"https://api.github.com/repos/aturon/futures-rs/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/aturon/futures-rs/teams","hooks_url":"https://api.github.com/repos/aturon/futures-rs/hooks","issue_events_url":"https://api.github.com/repos/aturon/futures-rs/issues/events{/number}","events_url":"https://api.github.com/repos/aturon/futures-rs/events","assignees_url":"https://api.github.com/repos/aturon/futures-rs/assignees{/user}","branches_url":"https://api.github.com/repos/aturon/futures-rs/branches{/branch}","tags_url":"https://api.github.com/repos/aturon/futures-rs/tags","blobs_url":"https://api.github.com/repos/aturon/futures-rs/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/aturon/futures-rs/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/aturon/futures-rs/git/refs{/sha}","trees_url":"https://api.github.com/repos/aturon/futures-rs/git/trees{/sha}","statuses_url":"https://api.github.com/repos/aturon/futures-rs/statuses/{sha}","languages_url":"https://api.github.com/repos/aturon/futures-rs/languages","stargazers_url":"https://api.github.com/repos/aturon/futures-rs/stargazers","contributors_url":"https://api.github.com/repos/aturon/futures-rs/contributors","subscribers_url":"https://api.github.com/repos/aturon/futures-rs/subscribers","subscription_url":"https://api.github.com/repos/aturon/futures-rs/subscription","commits_url":"https://api.github.com/repos/aturon/futures-rs/commits{/sha}","git_commits_url":"https://api.github.com/repos/aturon/futures-rs/git/commits{/sha}","comments_url":"https://api.github.com/repos/aturon/futures-rs/comments{/number}","issue_comment_url":"https://api.github.com/repos/aturon/futures-rs/issues/comments{/number}","contents_url":"https://api.github.com/repos/aturon/futures-rs/contents/{+path}","compare_url":"https://api.github.com/repos/aturon/futures-rs/compare/{base}...{head}","merges_url":"https://api.github.com/repos/aturon/futures-rs/merges","archive_url":"https://api.github.com/repos/aturon/futures-rs/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/aturon/futures-rs/downloads","issues_url":"https://api.github.com/repos/aturon/futures-rs/issues{/number}","pulls_url":"https://api.github.com/repos/aturon/futures-rs/pulls{/number}","milestones_url":"https://api.github.com/repos/aturon/futures-rs/milestones{/number}","notifications_url":"https://api.github.com/repos/aturon/futures-rs/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/aturon/futures-rs/labels{/name}","releases_url":"https://api.github.com/repos/aturon/futures-rs/releases{/id}","deployments_url":"https://api.github.com/repos/aturon/futures-rs/deployments","created_at":"2016-07-01T17:42:34Z","updated_at":"2016-07-01T17:42:34Z","pushed_at":"2016-07-11T22:47:29Z","git_url":"git://github.com/aturon/futures-rs.git","ssh_url":"git@github.com:aturon/futures-rs.git","clone_url":"https://github.com/aturon/futures-rs.git","svn_url":"https://github.com/aturon/futures-rs","homepage":null,"size":888,"stargazers_count":0,"watchers_count":0,"language":"Rust","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"alexcrichton:master","ref":"master","sha":"ce292be0a03755e7b08dc76b520e9b8058ea05d4","user":{"login":"alexcrichton","id":64996,"avatar_url":"https://avatars.githubusercontent.com/u/64996?v=3","gravatar_id":"","url":"https://api.github.com/users/alexcrichton","html_url":"https://github.com/alexcrichton","followers_url":"https://api.github.com/users/alexcrichton/followers","following_url":"https://api.github.com/users/alexcrichton/following{/other_user}","gists_url":"https://api.github.com/users/alexcrichton/gists{/gist_id}","starred_url":"https://api.github.com/users/alexcrichton/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/alexcrichton/subscriptions","organizations_url":"https://api.github.com/users/alexcrichton/orgs","repos_url":"https://api.github.com/users/alexcrichton/repos","events_url":"https://api.github.com/users/alexcrichton/events{/privacy}","received_events_url":"https://api.github.com/users/alexcrichton/received_events","type":"User","site_admin":false},"repo":{"id":55077261,"name":"futures-rs","full_name":"alexcrichton/futures-rs","owner":{"login":"alexcrichton","id":64996,"avatar_url":"https://avatars.githubusercontent.com/u/64996?v=3","gravatar_id":"","url":"https://api.github.com/users/alexcrichton","html_url":"https://github.com/alexcrichton","followers_url":"https://api.github.com/users/alexcrichton/followers","following_url":"https://api.github.com/users/alexcrichton/following{/other_user}","gists_url":"https://api.github.com/users/alexcrichton/gists{/gist_id}","starred_url":"https://api.github.com/users/alexcrichton/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/alexcrichton/subscriptions","organizations_url":"https://api.github.com/users/alexcrichton/orgs","repos_url":"https://api.github.com/users/alexcrichton/repos","events_url":"https://api.github.com/users/alexcrichton/events{/privacy}","received_events_url":"https://api.github.com/users/alexcrichton/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/alexcrichton/futures-rs","description":"WIP implementation of futures in Rust","fork":false,"url":"https://api.github.com/repos/alexcrichton/futures-rs","forks_url":"https://api.github.com/repos/alexcrichton/futures-rs/forks","keys_url":"https://api.github.com/repos/alexcrichton/futures-rs/keys{/key_id}","collaborators_url":"https://api.github.com/repos/alexcrichton/futures-rs/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/alexcrichton/futures-rs/teams","hooks_url":"https://api.github.com/repos/alexcrichton/futures-rs/hooks","issue_events_url":"https://api.github.com/repos/alexcrichton/futures-rs/issues/events{/number}","events_url":"https://api.github.com/repos/alexcrichton/futures-rs/events","assignees_url":"https://api.github.com/repos/alexcrichton/futures-rs/assignees{/user}","branches_url":"https://api.github.com/repos/alexcrichton/futures-rs/branches{/branch}","tags_url":"https://api.github.com/repos/alexcrichton/futures-rs/tags","blobs_url":"https://api.github.com/repos/alexcrichton/futures-rs/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/alexcrichton/futures-rs/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/alexcrichton/futures-rs/git/refs{/sha}","trees_url":"https://api.github.com/repos/alexcrichton/futures-rs/git/trees{/sha}","statuses_url":"https://api.github.com/repos/alexcrichton/futures-rs/statuses/{sha}","languages_url":"https://api.github.com/repos/alexcrichton/futures-rs/languages","stargazers_url":"https://api.github.com/repos/alexcrichton/futures-rs/stargazers","contributors_url":"https://api.github.com/repos/alexcrichton/futures-rs/contributors","subscribers_url":"https://api.github.com/repos/alexcrichton/futures-rs/subscribers","subscription_url":"https://api.github.com/repos/alexcrichton/futures-rs/subscription","commits_url":"https://api.github.com/repos/alexcrichton/futures-rs/commits{/sha}","git_commits_url":"https://api.github.com/repos/alexcrichton/futures-rs/git/commits{/sha}","comments_url":"https://api.github.com/repos/alexcrichton/futures-rs/comments{/number}","issue_comment_url":"https://api.github.com/repos/alexcrichton/futures-rs/issues/comments{/number}","contents_url":"https://api.github.com/repos/alexcrichton/futures-rs/contents/{+path}","compare_url":"https://api.github.com/repos/alexcrichton/futures-rs/compare/{base}...{head}","merges_url":"https://api.github.com/repos/alexcrichton/futures-rs/merges","archive_url":"https://api.github.com/repos/alexcrichton/futures-rs/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/alexcrichton/futures-rs/downloads","issues_url":"https://api.github.com/repos/alexcrichton/futures-rs/issues{/number}","pulls_url":"https://api.github.com/repos/alexcrichton/futures-rs/pulls{/number}","milestones_url":"https://api.github.com/repos/alexcrichton/futures-rs/milestones{/number}","notifications_url":"https://api.github.com/repos/alexcrichton/futures-rs/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/alexcrichton/futures-rs/labels{/name}","releases_url":"https://api.github.com/repos/alexcrichton/futures-rs/releases{/id}","deployments_url":"https://api.github.com/repos/alexcrichton/futures-rs/deployments","created_at":"2016-03-30T16:03:08Z","updated_at":"2016-07-07T11:08:33Z","pushed_at":"2016-07-11T22:47:30Z","git_url":"git://github.com/alexcrichton/futures-rs.git","ssh_url":"git@github.com:alexcrichton/futures-rs.git","clone_url":"https://github.com/alexcrichton/futures-rs.git","svn_url":"https://github.com/alexcrichton/futures-rs","homepage":null,"size":954,"stargazers_count":16,"watchers_count":16,"language":"Rust","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":true,"forks_count":2,"mirror_url":null,"open_issues_count":1,"forks":2,"open_issues":1,"watchers":16,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/alexcrichton/futures-rs/pulls/6"},"html":{"href":"https://github.com/alexcrichton/futures-rs/pull/6"},"issue":{"href":"https://api.github.com/repos/alexcrichton/futures-rs/issues/6"},"comments":{"href":"https://api.github.com/repos/alexcrichton/futures-rs/issues/6/comments"},"review_comments":{"href":"https://api.github.com/repos/alexcrichton/futures-rs/pulls/6/comments"},"review_comment":{"href":"https://api.github.com/repos/alexcrichton/futures-rs/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/alexcrichton/futures-rs/pulls/6/commits"},"statuses":{"href":"https://api.github.com/repos/alexcrichton/futures-rs/statuses/bc60d83117b1f7aa23017fedcf99997af46596a1"}}}}
{"actor":{"display_login":"alexcrichton"}}