Skip to content

Commit

Permalink
Simplify impls of AsSocket.
Browse files Browse the repository at this point in the history
whitequark committed Jul 24, 2017
1 parent c5aa371 commit ed0828b
Showing 1 changed file with 21 additions and 43 deletions.
64 changes: 21 additions & 43 deletions src/socket/mod.rs
Original file line number Diff line number Diff line change
@@ -109,50 +109,28 @@ pub trait AsSocket<T> {
fn try_as_socket(&mut self) -> Option<&mut T>;
}

impl<'a, 'b> AsSocket<RawSocket<'a, 'b>> for Socket<'a, 'b> {
fn as_socket(&mut self) -> &mut RawSocket<'a, 'b> {
match self {
&mut Socket::Raw(ref mut socket) => socket,
_ => panic!(".as_socket::<RawSocket> called on wrong socket type")
}
}

fn try_as_socket(&mut self) -> Option<&mut RawSocket<'a, 'b>> {
match self {
&mut Socket::Raw(ref mut socket) => Some(socket),
_ => None,
}
}
}

impl<'a, 'b> AsSocket<UdpSocket<'a, 'b>> for Socket<'a, 'b> {
fn as_socket(&mut self) -> &mut UdpSocket<'a, 'b> {
match self {
&mut Socket::Udp(ref mut socket) => socket,
_ => panic!(".as_socket::<UdpSocket> called on wrong socket type")
}
}

fn try_as_socket(&mut self) -> Option<&mut UdpSocket<'a, 'b>> {
match self {
&mut Socket::Udp(ref mut socket) => Some(socket),
_ => None,
macro_rules! as_socket {
($socket:ty, $variant:ident) => {
impl<'a, 'b> AsSocket<$socket> for Socket<'a, 'b> {
fn as_socket(&mut self) -> &mut $socket {
match self {
&mut Socket::$variant(ref mut socket) => socket,
_ => panic!(concat!(".as_socket::<",
stringify!($socket),
"> called on wrong socket type"))
}
}

fn try_as_socket(&mut self) -> Option<&mut $socket> {
match self {
&mut Socket::$variant(ref mut socket) => Some(socket),
_ => None,
}
}
}
}
}

impl<'a, 'b> AsSocket<TcpSocket<'a>> for Socket<'a, 'b> {
fn as_socket(&mut self) -> &mut TcpSocket<'a> {
match self {
&mut Socket::Tcp(ref mut socket) => socket,
_ => panic!(".as_socket::<TcpSocket> called on wrong socket type")
}
}

fn try_as_socket(&mut self) -> Option<&mut TcpSocket<'a>> {
match self {
&mut Socket::Tcp(ref mut socket) => Some(socket),
_ => None,
}
}
}
as_socket!(RawSocket<'a, 'b>, Raw);
as_socket!(UdpSocket<'a, 'b>, Udp);
as_socket!(TcpSocket<'a>, Tcp);

0 comments on commit ed0828b

Please sign in to comment.