chiark / gitweb /
want: Provide new facilities and use them in one place
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Fri, 2 Apr 2021 12:41:29 +0000 (13:41 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Fri, 2 Apr 2021 15:16:29 +0000 (16:16 +0100)
Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
src/hidden.rs
src/prelude.rs
src/utils.rs

index 7f262023fdd3c062a07bbda8d3da8b5654adf376..c4c5674115fca426fedc409270a40198d160515a 100644 (file)
@@ -649,7 +649,7 @@ fn recalculate_occultation_general<
       let occ = occultation(goccults, occid);
       if_chain!{
         if occ.notches.is_empty();
-        if let Some(ilk) = ipc.occilk.as_ref();            // expected, really
+        if let Some(ilk) = want!( Some = ipc.occilk.as_ref() );
         let ilk = ilk.borrow();
         if let Some(ilk) = ioccults.ilks.get(ilk);         // expected, really
         if let Ok::<_,IE>(bbox) = ilk.p_occ.bbox_approx(); // expected, really
index 24eb944301e6158b1fb39ca0cc8c8545532ee42a..2ca7c4dc74f4d05662aef66a5e7a2686f0d4ceb9 100644 (file)
@@ -113,6 +113,7 @@ pub use crate::ensure_eq;
 pub use crate::from_instance_lock_error;
 pub use crate::matches_doesnot;
 pub use crate::trace_dbg;
+pub use crate::{want, want_let, want_failed_internal};
 
 pub use crate::accounts::loaded_acl::{self, EffectiveACL, LoadedAcl, PermSet};
 pub use crate::accounts::*;
index 4cc9404ae635c70aad60f09d6ffa5f3cfbb9bd57..4ca64a882d1b83e5d8c2a10b1f178f3e9dffc7bc 100644 (file)
@@ -428,4 +428,92 @@ macro_rules! trace_dbg {
       trace!("{}", buf);
     }
   }
+
+}
+
+#[macro_export]
+macro_rules! want_failed_internal {
+  { $variant:ident($binding:pat) = $input:expr, $x:expr, $($d:expr),* } => {
+    InternalLogicError::new({
+      #[allow(unused_mut)]
+      let mut s = format!("wanted {}({}) = {}, but got {:?}",
+                         stringify!($variant), stringify!($binding),
+                          stringify!($input), $x);
+      $(
+        write!(&mut s, " {}={:?}", stringify!($d), &$d);
+      )*
+      s
+    }).tolerate()
+  }
 }
+
+#[macro_export]
+macro_rules! want {
+  { $variant:ident = $input:expr,
+    else ?($($d:expr),*)
+  } => (
+    match $input {
+      $variant(y) => Some(y),
+      x => {
+        want_failed_internal!{ $variant(_)=$input, x, $($d:expr),* }
+        None
+      },
+    };
+  );
+  { $variant:ident = $input:expr } => {
+    want!( $variant = $input,
+           else ?() )
+  };
+}
+
+#[macro_export]
+macro_rules! want_let {
+  { $variant:ident($binding:pat) = $input:expr;
+    else ?($($d:expr),*) $($otherwise:tt)*
+  } => {
+    let $binding = match $input {
+      $variant(y) => y,
+      x => {
+        want_failed_internal!{ $variant($binding)=$input, x, $($d:expr),* }
+        $($otherwise)*
+      },
+    };
+  };
+  { $variant:ident($binding:pat) = $input:expr;
+    else $($otherwise:tt)*
+  } => {
+    want_let!{ $variant($binding) = $input; ?(); $($otherwise:tt)* }
+  };
+}
+
+/*
+#[macro_export]
+macro_rules! want {
+  { $variant:ident($binding:pat) = $input:expr; else $($otherwise:tt)* } => {
+    want
+
+    let $binding = match $input {
+      $variant(y) => y,
+      x => {
+        error!("internal error: wanted {}({}) = {}, but got {:?}",
+               stringify!($variant), stringify!($binding),
+               stringify!($input), x);
+        { $($otherwise)* }
+      },
+    };
+  }
+  { $variant:ident($binding:pat) = $input:expr, $(xdbg:extra),*;
+    else $($otherwise:tt)* }
+  => {
+    let $binding = match $input {
+      $variant(y) => y,
+      x => {
+        error!("internal error: wanted {}({}) = {}, but got {:?}",
+               stringify!($variant), stringify!($binding),
+               stringify!($input), x);
+        { $($otherwise)* }
+      },
+    };
+  }
+}
+*/