From fa477593fa0055a6dc8cff35970135fe4d7fd0dd Mon Sep 17 00:00:00 2001 From: Milo Turner Date: Sat, 7 Mar 2020 20:43:53 -0500 Subject: [PATCH] goodbye failure crate!! --- Cargo.lock | 74 ++++++++----------------------------------- hptp-recv/Cargo.toml | 4 +-- hptp-recv/src/main.rs | 37 +++++++++++++++++----- 3 files changed, 44 insertions(+), 71 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 1a67913..c1397e3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,27 +1,5 @@ # This file is automatically @generated by Cargo. # 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]] name = "bitflags" version = "1.2.1" @@ -34,40 +12,12 @@ version = "0.5.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "130aac562c0dd69c56b3b1cc8ffd2e17be31d0b6c25b61c96b76231aa23e39e1" -[[package]] -name = "cc" -version = "1.0.50" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "95e28fa049fda1c330bcf9d723be7663a899c4679724b34c81e9f5a326aab8cd" - [[package]] name = "cfg-if" version = "0.1.10" source = "registry+https://github.com/rust-lang/crates.io-index" 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]] name = "fuchsia-zircon" version = "0.3.3" @@ -95,8 +45,8 @@ dependencies = [ name = "hptp-recv" version = "0.1.0" dependencies = [ - "failure", "hptp", + "thiserror", "tokio", ] @@ -219,12 +169,6 @@ dependencies = [ "proc-macro2", ] -[[package]] -name = "rustc-demangle" -version = "0.1.16" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4c691c0e608126e00913e33f0ccf3727d5fc84573623b8d65b2df340b5201783" - [[package]] name = "slab" version = "0.4.2" @@ -243,15 +187,23 @@ dependencies = [ ] [[package]] -name = "synstructure" -version = "0.12.3" +name = "thiserror" +version = "1.0.11" 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 = [ "proc-macro2", "quote", "syn", - "unicode-xid", ] [[package]] diff --git a/hptp-recv/Cargo.toml b/hptp-recv/Cargo.toml index 57b7e1a..5ba6726 100644 --- a/hptp-recv/Cargo.toml +++ b/hptp-recv/Cargo.toml @@ -9,5 +9,5 @@ edition = "2018" [dependencies] hptp = { path = "../hptp" } -tokio = {version = "0.2.*", features = ["rt-core", "io-std", "io-util", "udp"]} -failure = "0.1.7" +tokio = { version = "0.2.*", features = ["rt-core", "udp"] } +thiserror = "*" diff --git a/hptp-recv/src/main.rs b/hptp-recv/src/main.rs index d9df368..a0b8feb 100644 --- a/hptp-recv/src/main.rs +++ b/hptp-recv/src/main.rs @@ -1,23 +1,34 @@ -extern crate failure; extern crate hptp; extern crate tokio; +#[macro_use] +extern crate thiserror; -use failure::Error; use hptp::log::Logger; use std::net::Ipv4Addr; use tokio::net::UdpSocket; -fn main() -> Result<(), Box> { +#[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 log = Logger::new(); - let fut = start(&mut log); - Ok(rt.block_on(fut)?) + rt.block_on(start(&mut log)) +} + +fn main() { + if let Err(e) = entry() { + println!("ERROR: {}", e); + } } async fn start(log: &mut Logger) -> Result<(), Error> { - let host = (Ipv4Addr::LOCALHOST, 3700); - let sock = UdpSocket::bind(host).await?; - + let sock = udp_listen_or_retry(3700, 4).await?; log.bound(sock.local_addr()?.port()).await; let (mut rx, _tx) = sock.split(); @@ -31,3 +42,13 @@ async fn start(log: &mut Logger) -> Result<(), Error> { .await; } } + +async fn udp_listen_or_retry(init_port: u16, max_tries: u16) -> Result { + 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) +}