-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support ? for Option and Result (#950)
* support ? for Option * support Result based on Chris's suggestion
- Loading branch information
Showing
7 changed files
with
234 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
use crate::prelude::*; | ||
use core::ops::Try; | ||
use core::ops::ControlFlow; | ||
use core::ops::FromResidual; | ||
use core::convert::Infallible; | ||
|
||
verus!{ | ||
|
||
#[verifier(external_type_specification)] | ||
#[verifier::accept_recursive_types(B)] | ||
#[verifier::reject_recursive_types_in_ground_variants(C)] | ||
pub struct ExControlFlow<B, C>(ControlFlow<B, C>); | ||
|
||
#[verifier(external_type_specification)] | ||
#[verifier(external_body)] | ||
pub struct ExInfallible(Infallible); | ||
|
||
|
||
#[verifier::external_fn_specification] | ||
pub fn ex_result_branch<T, E>(result: Result<T, E>) -> (cf: ControlFlow<<Result<T, E> as Try>::Residual, <Result<T, E> as Try>::Output>) | ||
ensures | ||
cf === match result { | ||
Ok(v) => ControlFlow::Continue(v), | ||
Err(e) => ControlFlow::Break(Err(e)), | ||
}, | ||
{ | ||
result.branch() | ||
} | ||
|
||
#[verifier::external_fn_specification] | ||
pub fn ex_option_branch<T>(option: Option<T>) -> (cf: ControlFlow<<Option<T> as Try>::Residual, <Option<T> as Try>::Output>) | ||
ensures | ||
cf === match option { | ||
Some(v) => ControlFlow::Continue(v), | ||
None => ControlFlow::Break(None), | ||
}, | ||
{ | ||
option.branch() | ||
} | ||
|
||
#[verifier::external_fn_specification] | ||
pub fn ex_option_from_residual<T>(option: Option<Infallible>) -> (option2: Option<T>) | ||
ensures | ||
option.is_none(), | ||
option2.is_none(), | ||
{ | ||
Option::from_residual(option) | ||
} | ||
|
||
pub spec fn spec_from<S, T>(value: T, ret: S) -> bool; | ||
|
||
#[verifier::broadcast_forall] | ||
#[verifier::external_body] | ||
pub proof fn spec_from_blanket_identity<T>(t: T, s: T) | ||
ensures | ||
spec_from::<T, T>(t, s) ==> t == s | ||
{ | ||
} | ||
|
||
#[verifier::external_fn_specification] | ||
pub fn ex_result_from_residual<T, E, F: From<E>>(result: Result<Infallible, E>) | ||
-> (result2: Result<T, F>) | ||
ensures | ||
match (result, result2) { | ||
(Err(e), Err(e2)) => spec_from::<F, E>(e, e2), | ||
_ => false, | ||
}, | ||
{ | ||
Result::from_residual(result) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters