Skip to content

Commit

Permalink
Introduce TryAccept trait.
Browse files Browse the repository at this point in the history
As per discussion in tokio-rs#204.
  • Loading branch information
dpc authored and carllerche committed Jul 24, 2015
1 parent c4e0237 commit 478cef3
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 4 deletions.
6 changes: 6 additions & 0 deletions src/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ impl<T: Write> TryWrite for T {
}
}

pub trait TryAccept {
type Output;

fn accept(&self) -> Result<Option<Self::Output>>;
}

/*
*
* ===== Helpers =====
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ pub use io::{
TryRead,
TryWrite,
Evented,
TryAccept,
};
pub use net::{
tcp,
Expand Down
10 changes: 9 additions & 1 deletion src/net/tcp.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use {io, sys, Evented, EventSet, PollOpt, Selector, Token};
use {io, sys, Evented, EventSet, PollOpt, Selector, Token, TryAccept};
use std::io::{Read, Write};
use std::net::SocketAddr;

Expand Down Expand Up @@ -261,6 +261,14 @@ impl Evented for TcpListener {
}
}

impl TryAccept for TcpListener {
type Output = TcpStream;

fn accept(&self) -> io::Result<Option<TcpStream>> {
TcpListener::accept(self)
}
}

/*
*
* ===== UNIX ext =====
Expand Down
10 changes: 9 additions & 1 deletion src/net/unix.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use {io, sys, Evented, EventSet, Io, PollOpt, Selector, Token};
use {io, sys, Evented, EventSet, Io, PollOpt, Selector, Token, TryAccept};
use std::io::{Read, Write};
use std::path::Path;

Expand Down Expand Up @@ -161,6 +161,14 @@ impl Evented for UnixListener {
}
}

impl TryAccept for UnixListener {
type Output = UnixStream;

fn accept(&self) -> io::Result<Option<UnixStream>> {
UnixListener::accept(self)
}
}

impl From<sys::UnixSocket> for UnixListener {
fn from(sys: sys::UnixSocket) -> UnixListener {
UnixListener { sys: sys }
Expand Down
10 changes: 9 additions & 1 deletion src/sys/unix/tcp.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use {io, Evented, EventSet, Io, PollOpt, Selector, Token};
use {io, Evented, EventSet, Io, PollOpt, Selector, Token, TryAccept};
use sys::unix::{net, nix, Socket};
use std::io::{Read, Write};
use std::net::SocketAddr;
Expand Down Expand Up @@ -123,6 +123,14 @@ impl Evented for TcpSocket {
}
}

impl TryAccept for TcpSocket {
type Output = TcpSocket;

fn accept(&self) -> io::Result<Option<TcpSocket>> {
TcpSocket::accept(self)
}
}

impl Socket for TcpSocket {
}

Expand Down
10 changes: 9 additions & 1 deletion src/sys/unix/uds.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use {io, Evented, EventSet, Io, PollOpt, Selector, Token};
use {io, Evented, EventSet, Io, PollOpt, Selector, Token, TryAccept};
use sys::unix::{net, nix, Socket};
use std::io::{Read, Write};
use std::path::Path;
Expand Down Expand Up @@ -77,6 +77,14 @@ impl Evented for UnixSocket {
}
}

impl TryAccept for UnixSocket {
type Output = UnixSocket;

fn accept(&self) -> io::Result<Option<UnixSocket>> {
UnixSocket::accept(self)
}
}

impl Socket for UnixSocket {
}

Expand Down

0 comments on commit 478cef3

Please sign in to comment.