move infallible handling into error

Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
Jason Volk 2024-07-12 20:13:55 +00:00
parent 4cc92dd175
commit 4600c7f32d
3 changed files with 19 additions and 18 deletions

View file

@ -196,7 +196,11 @@ impl UnwindSafe for Error {}
impl RefUnwindSafe for Error {}
impl From<Infallible> 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<E: fmt::Debug>(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();

View file

@ -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<T: Ord>(val: T, min: T, max: T) -> T { cmp::min(cmp::max(val, min), max) }
/// Boilerplate for wraps which are typed to never error.
///
/// * <https://doc.rust-lang.org/std/convert/enum.Infallible.html>
#[must_use]
pub fn unwrap_infallible<T>(result: Result<T, std::convert::Infallible>) -> T {
match result {
Ok(val) => val,
Err(err) => match err {},
}
}
#[must_use]
pub fn generate_keypair() -> Vec<u8> {
let mut value = rand::string(8).as_bytes().to_vec();

View file

@ -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<Router, net::SocketAddr>;
@ -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<Incoming>| called.clone().oneshot(req));
let called = app
.call(NULL_ADDR)
.await
.inspect_err(infallible)
.expect("infallible");
let service = move |req: Request<Incoming>| 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;