Macro polonius_the_crab::polonius
source · [−]macro_rules! polonius {
(
|$var:ident $(,)?| -> $Ret:ty
$body:block
$(,)?
) => { ... };
}
Expand description
Convenient entry-point to this crate’s logic.
- See the top-level docs for more info.
Usage
use ::polonius_the_crab::prelude::*;
let mut a_mut_binding: &mut _ = // …
// the lifetime placeholder has to be
// named `'polonius` !!
// vvvvvvvvv
let x = polonius!(|a_mut_binding| -> SomeRetType<'polonius> {
let some_dependent_type = stuff(a_mut_binding);
if some_cond() {
polonius_return!(some_dependent_type);
}
if some_other_cond() {
exit_polonius!(42);
unreachable!();
}
42
});
assert_eq!(x, 42);
stuff(a_mut_binding) // macro gave it back
// …
Generic parameters
They now Just Work™.
use ::polonius_the_crab::prelude::*;
fn get_or_insert<'map, 'v, K, V : ?Sized> (
mut map: &'map mut ::std::collections::HashMap<K, &'v V>,
key: &'_ K,
fallback_value: &'v V,
) -> &'map &'v V
where
K : ::core::hash::Hash + Eq + Clone,
V : ::core::fmt::Debug,
{
polonius!(|map| -> &'polonius &'v V {
if let Some(v) = map.get(key) {
dbg!(v); // Even though `Debug` is not provided to the signature, it is available to the body.
polonius_return!(v);
}
});
map.insert(key.clone(), fallback_value);
&map[key]
}