goodbye failure crate!!

This commit is contained in:
Milo Turner 2020-03-07 20:43:53 -05:00
parent 520938abb2
commit fa477593fa
3 changed files with 44 additions and 71 deletions

74
Cargo.lock generated
View File

@ -1,27 +1,5 @@
# This file is automatically @generated by Cargo. # This file is automatically @generated by Cargo.
# It is not intended for manual editing. # It is not intended for manual editing.
[[package]]
name = "backtrace"
version = "0.3.45"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ad235dabf00f36301792cfe82499880ba54c6486be094d1047b02bacb67c14e8"
dependencies = [
"backtrace-sys",
"cfg-if",
"libc",
"rustc-demangle",
]
[[package]]
name = "backtrace-sys"
version = "0.1.33"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e17b52e737c40a7d75abca20b29a19a0eb7ba9fc72c5a72dd282a0a3c2c0dc35"
dependencies = [
"cc",
"libc",
]
[[package]] [[package]]
name = "bitflags" name = "bitflags"
version = "1.2.1" version = "1.2.1"
@ -34,40 +12,12 @@ version = "0.5.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "130aac562c0dd69c56b3b1cc8ffd2e17be31d0b6c25b61c96b76231aa23e39e1" checksum = "130aac562c0dd69c56b3b1cc8ffd2e17be31d0b6c25b61c96b76231aa23e39e1"
[[package]]
name = "cc"
version = "1.0.50"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "95e28fa049fda1c330bcf9d723be7663a899c4679724b34c81e9f5a326aab8cd"
[[package]] [[package]]
name = "cfg-if" name = "cfg-if"
version = "0.1.10" version = "0.1.10"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822" checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822"
[[package]]
name = "failure"
version = "0.1.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b8529c2421efa3066a5cbd8063d2244603824daccb6936b079010bb2aa89464b"
dependencies = [
"backtrace",
"failure_derive",
]
[[package]]
name = "failure_derive"
version = "0.1.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "030a733c8287d6213886dd487564ff5c8f6aae10278b3588ed177f9d18f8d231"
dependencies = [
"proc-macro2",
"quote",
"syn",
"synstructure",
]
[[package]] [[package]]
name = "fuchsia-zircon" name = "fuchsia-zircon"
version = "0.3.3" version = "0.3.3"
@ -95,8 +45,8 @@ dependencies = [
name = "hptp-recv" name = "hptp-recv"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"failure",
"hptp", "hptp",
"thiserror",
"tokio", "tokio",
] ]
@ -219,12 +169,6 @@ dependencies = [
"proc-macro2", "proc-macro2",
] ]
[[package]]
name = "rustc-demangle"
version = "0.1.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4c691c0e608126e00913e33f0ccf3727d5fc84573623b8d65b2df340b5201783"
[[package]] [[package]]
name = "slab" name = "slab"
version = "0.4.2" version = "0.4.2"
@ -243,15 +187,23 @@ dependencies = [
] ]
[[package]] [[package]]
name = "synstructure" name = "thiserror"
version = "0.12.3" version = "1.0.11"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "67656ea1dc1b41b1451851562ea232ec2e5a80242139f7e679ceccfb5d61f545" checksum = "ee14bf8e6767ab4c687c9e8bc003879e042a96fd67a3ba5934eadb6536bef4db"
dependencies = [
"thiserror-impl",
]
[[package]]
name = "thiserror-impl"
version = "1.0.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a7b51e1fbc44b5a0840be594fbc0f960be09050f2617e61e6aa43bef97cd3ef4"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn", "syn",
"unicode-xid",
] ]
[[package]] [[package]]

View File

@ -9,5 +9,5 @@ edition = "2018"
[dependencies] [dependencies]
hptp = { path = "../hptp" } hptp = { path = "../hptp" }
tokio = {version = "0.2.*", features = ["rt-core", "io-std", "io-util", "udp"]} tokio = { version = "0.2.*", features = ["rt-core", "udp"] }
failure = "0.1.7" thiserror = "*"

View File

@ -1,23 +1,34 @@
extern crate failure;
extern crate hptp; extern crate hptp;
extern crate tokio; extern crate tokio;
#[macro_use]
extern crate thiserror;
use failure::Error;
use hptp::log::Logger; use hptp::log::Logger;
use std::net::Ipv4Addr; use std::net::Ipv4Addr;
use tokio::net::UdpSocket; use tokio::net::UdpSocket;
fn main() -> Result<(), Box<dyn std::error::Error>> { #[derive(Error, Debug)]
enum Error {
#[error("io error: {0}")]
Io(#[from] tokio::io::Error),
#[error("no UDP port available for listening")]
NoPortAvail,
}
fn entry() -> Result<(), Error> {
let mut rt = tokio::runtime::Runtime::new()?; let mut rt = tokio::runtime::Runtime::new()?;
let mut log = Logger::new(); let mut log = Logger::new();
let fut = start(&mut log); rt.block_on(start(&mut log))
Ok(rt.block_on(fut)?) }
fn main() {
if let Err(e) = entry() {
println!("ERROR: {}", e);
}
} }
async fn start(log: &mut Logger) -> Result<(), Error> { async fn start(log: &mut Logger) -> Result<(), Error> {
let host = (Ipv4Addr::LOCALHOST, 3700); let sock = udp_listen_or_retry(3700, 4).await?;
let sock = UdpSocket::bind(host).await?;
log.bound(sock.local_addr()?.port()).await; log.bound(sock.local_addr()?.port()).await;
let (mut rx, _tx) = sock.split(); let (mut rx, _tx) = sock.split();
@ -31,3 +42,13 @@ async fn start(log: &mut Logger) -> Result<(), Error> {
.await; .await;
} }
} }
async fn udp_listen_or_retry(init_port: u16, max_tries: u16) -> Result<UdpSocket, Error> {
for tries in 0..max_tries {
let port = init_port + tries;
if let Ok(sock) = UdpSocket::bind((Ipv4Addr::LOCALHOST, port)).await {
return Ok(sock);
}
}
Err(Error::NoPortAvail)
}