Skip to content

Commit

Permalink
create .bind_or()
Browse files Browse the repository at this point in the history
  • Loading branch information
yoshuawuyts committed Jun 3, 2018
1 parent cf66fd8 commit a2438d0
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,15 @@ impl Port {
_ => Err(io::Error::new(io::ErrorKind::Other, "No port supplied.")),
}
}

/// Create a TCP socket by calling to `.bind()`. If it fails, create a socket
/// on `port`.
///
/// Useful to create a default socket to listen to if none was passed.
pub fn bind_or(&self, port: u16) -> std::io::Result<TcpListener> {
match self.bind() {
Ok(listener) => Ok(listener),
Err(_) => TcpListener::bind(("127.0.0.1", port)),
}
}
}

0 comments on commit a2438d0

Please sign in to comment.