about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorBenedikt Peetz <benedikt.peetz@b-peetz.de>2024-10-14 18:04:26 +0200
committerBenedikt Peetz <benedikt.peetz@b-peetz.de>2024-10-14 18:04:26 +0200
commitbdf00337daf1ed8b343168ea2496352a7788fe25 (patch)
tree6ae608987bd6104eb5ba39d0c86617a78676ada4
parentrefactor(treewide): Combine the separate crates in one workspace (diff)
downloadyt-bdf00337daf1ed8b343168ea2496352a7788fe25.tar.gz
yt-bdf00337daf1ed8b343168ea2496352a7788fe25.zip
feat(unreachable): Init trait
Diffstat (limited to '')
-rw-r--r--yt/src/main.rs1
-rw-r--r--yt/src/unreachable.rs39
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)
+    }
+}