diff options
-rw-r--r-- | yt/src/main.rs | 1 | ||||
-rw-r--r-- | yt/src/unreachable.rs | 39 |
2 files changed, 40 insertions, 0 deletions
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<T> { + /// 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<T> Unreachable<T> for Option<T> { + #[inline(always)] + fn unreachable(self, msg: &str) -> T { + self.expect(msg) + } +} +impl<T, E: Debug> Unreachable<T> for Result<T, E> { + #[inline(always)] + fn unreachable(self, msg: &str) -> T { + self.expect(msg) + } +} |