From 4600c7f32de2683c465d3dcfaf4ddac8a7c250c6 Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Fri, 12 Jul 2024 20:13:55 +0000 Subject: [PATCH] move infallible handling into error Signed-off-by: Jason Volk --- src/core/error.rs | 12 +++++++++++- src/core/utils/mod.rs | 13 ------------- src/router/serve/unix.rs | 12 ++++++++---- 3 files changed, 19 insertions(+), 18 deletions(-) diff --git a/src/core/error.rs b/src/core/error.rs index 77faae3a..01460217 100644 --- a/src/core/error.rs +++ b/src/core/error.rs @@ -196,7 +196,11 @@ impl UnwindSafe for Error {} impl RefUnwindSafe for Error {} impl From for Error { - fn from(i: Infallible) -> Self { match i {} } + #[cold] + #[inline(never)] + fn from(_i: Infallible) -> Self { + panic!("infallible error should never exist"); + } } impl fmt::Debug for Error { @@ -273,6 +277,12 @@ pub fn inspect_debug_log(error: &E) { debug_error!("{error:?}"); } +#[cold] +#[inline(never)] +pub fn infallible(_e: &Infallible) { + panic!("infallible error should never exist"); +} + impl axum::response::IntoResponse for Error { fn into_response(self) -> axum::response::Response { let response: UiaaResponse = self.into(); diff --git a/src/core/utils/mod.rs b/src/core/utils/mod.rs index 4e5ba480..bbd52829 100644 --- a/src/core/utils/mod.rs +++ b/src/core/utils/mod.rs @@ -26,21 +26,8 @@ pub use string::{str_from_bytes, string_from_bytes}; pub use sys::available_parallelism; pub use time::now_millis as millis_since_unix_epoch; -use crate::Result; - pub fn clamp(val: T, min: T, max: T) -> T { cmp::min(cmp::max(val, min), max) } -/// Boilerplate for wraps which are typed to never error. -/// -/// * -#[must_use] -pub fn unwrap_infallible(result: Result) -> T { - match result { - Ok(val) => val, - Err(err) => match err {}, - } -} - #[must_use] pub fn generate_keypair() -> Vec { let mut value = rand::string(8).as_bytes().to_vec(); diff --git a/src/router/serve/unix.rs b/src/router/serve/unix.rs index 8373b749..e876b2ac 100644 --- a/src/router/serve/unix.rs +++ b/src/router/serve/unix.rs @@ -10,7 +10,7 @@ use axum::{ extract::{connect_info::IntoMakeServiceWithConnectInfo, Request}, Router, }; -use conduit::{debug_error, trace, utils, Error, Result, Server}; +use conduit::{debug_error, error::infallible, trace, Error, Result, Server}; use hyper::{body::Incoming, service::service_fn}; use hyper_util::{ rt::{TokioExecutor, TokioIo}, @@ -24,7 +24,6 @@ use tokio::{ }; use tower::{Service, ServiceExt}; use tracing::{debug, info, warn}; -use utils::unwrap_infallible; type MakeService = IntoMakeServiceWithConnectInfo; @@ -62,9 +61,14 @@ async fn accept( let socket = TokioIo::new(socket); trace!(?listener, ?socket, ?remote, "accepted"); - let called = unwrap_infallible(app.call(NULL_ADDR).await); - let handler = service_fn(move |req: Request| called.clone().oneshot(req)); + let called = app + .call(NULL_ADDR) + .await + .inspect_err(infallible) + .expect("infallible"); + let service = move |req: Request| called.clone().oneshot(req); + let handler = service_fn(service); let task = async move { // bug on darwin causes all results to be errors. do not unwrap this _ = builder.serve_connection(socket, handler).await;