From: Ian Jackson Date: Fri, 2 Apr 2021 12:41:29 +0000 (+0100) Subject: want: Provide new facilities and use them in one place X-Git-Tag: otter-0.5.0~321 X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=commitdiff_plain;h=70d24341b26ddfbf151a45e9b7202aa770943f9b;p=otter.git want: Provide new facilities and use them in one place Signed-off-by: Ian Jackson --- diff --git a/src/hidden.rs b/src/hidden.rs index 7f262023..c4c56741 100644 --- a/src/hidden.rs +++ b/src/hidden.rs @@ -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 diff --git a/src/prelude.rs b/src/prelude.rs index 24eb9443..2ca7c4dc 100644 --- a/src/prelude.rs +++ b/src/prelude.rs @@ -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::*; diff --git a/src/utils.rs b/src/utils.rs index 4cc9404a..4ca64a88 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -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)* } + }, + }; + } +} +*/