From bdf00337daf1ed8b343168ea2496352a7788fe25 Mon Sep 17 00:00:00 2001 From: Benedikt Peetz Date: Mon, 14 Oct 2024 18:04:26 +0200 Subject: feat(unreachable): Init trait --- yt/src/main.rs | 1 + yt/src/unreachable.rs | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 yt/src/unreachable.rs diff --git a/yt/src/main.rs b/yt/src/main.rs index 37283a1..e2c517c 100644 --- a/yt/src/main.rs +++ b/yt/src/main.rs @@ -33,6 +33,7 @@ use crate::{cli::Command, storage::subscriptions::get_subscriptions}; pub mod app; pub mod cli; +pub mod unreachable; pub mod cache; pub mod comments; diff --git a/yt/src/unreachable.rs b/yt/src/unreachable.rs new file mode 100644 index 0000000..3db77d5 --- /dev/null +++ b/yt/src/unreachable.rs @@ -0,0 +1,39 @@ +// This has been taken from: https://gitlab.torproject.org/tpo/core/arti/-/issues/950 + +// The functions here should be annotated with `#[inline(always)]`. +#![allow(clippy::inline_always)] + +use std::fmt::Debug; + +/// Trait for something that can possibly be unwrapped, like a Result or Option. +/// It provides semantic information, that unwrapping here should never happen. +pub trait Unreachable { + /// Like `expect()`, but does not trigger clippy. + /// + /// # Usage + /// + /// This method only exists so that we can use it without hitting + /// `clippy::missing_panics_docs`. Therefore, we should only use it + /// for situations where we are certain that the panic cannot occur + /// unless something else is very broken. Consider instead using + /// `expect()` and adding a `Panics` section to your function + /// documentation. + /// + /// # Panics + /// + /// Panics if this is not an object that can be unwrapped, such as + /// None or an Err. + fn unreachable(self, msg: &str) -> T; +} +impl Unreachable for Option { + #[inline(always)] + fn unreachable(self, msg: &str) -> T { + self.expect(msg) + } +} +impl Unreachable for Result { + #[inline(always)] + fn unreachable(self, msg: &str) -> T { + self.expect(msg) + } +} -- cgit 1.4.1