Skip to main content

verus_proof_idempotent_filter

Macro verus_proof_idempotent_filter 

Source
macro_rules! verus_proof_idempotent_filter {
    (
        item = $itemty:ty
        $(, captures = |$($cap:ident : $capty:ty),* $(,)?|)?
        , captures_mut = |$mcap:ident : $mcapty:ty $(,)?|
        $(, proof = |$pstate:ident, $px:ident| { $($proof_body:tt)* })?
        , __target = { $(move)? |$titem:ident $(: $titemty:ty)?| $target_body:expr }
        , __captures = [$($fv:ident),* $(,)?] $(,)?
    ) => { ... };
    (
        $($rest:tt)*
    ) => { ... };
}
Expand description

Fulfills an idempotent = ... proof parameter for a predicate (shape |item| -> bool where item is received by reference, e.g. for filter or partition) that may mutate a captured singleton reference (from Singleton::by_mut), with a Verus-checked proof of idempotence.

What idempotence means for a predicate: processing an item twice in a row, starting from the same captured state, must (1) leave the captured state in the same final value as processing it once, and (2) make the same decision both times as processing it once. Condition (2) is required for partition, where an element rejected on retry lands in the other output. (For filter alone, dropping a retried element that was retained the first time would be harmless; this macro conservatively rejects it as well.)

The mutated state is the mutable capture, declared (with the type behind the reference) in the required captures_mut = |state: S| clause; additional read-only captures can be declared in a captures = |...| clause. The macro checks at compile time that every capture is declared. The item = ... type must be written exactly as the closure receives it (a reference like &u32).

An optional proof = |state, x| { ... } clause supplies a proof script to help the SMT solver, with ghost bindings for the initial captured state and the item.

The proof is gated on cfg(verus_keep_ghost), so it only exists when the crate is compiled by the Verus driver (cargo verus verify). Under normal compilation the macro just produces a VerusIdempotentProof marker.

§Example

ⓘ
let max_mut = max_seen.by_mut();
stream.filter(q!(
    |x| {
        if *x > *max_mut { *max_mut = *x; }
        *x > 1 // the decision must not depend on whether `x` was seen before!
    },
    idempotent = verus_proof_idempotent_filter!(
        item = &u32,
        captures_mut = |max_mut: u32|
    )
));