#![allow(unused_imports)] use super::super::prelude::*; use core::option::Option; verus! { ////// Add is_variant-style spec functions pub trait OptionAdditionalFns: Sized { #[cfg_attr(not(verus_verify_core), deprecated = "is_Variant is deprecated - use `->` or `matches` instead: https://verus-lang.github.io/verus/guide/datatypes_enum.html")] #[allow(non_snake_case)] spec fn is_Some(&self) -> bool; #[cfg_attr(not(verus_verify_core), deprecated = "get_Variant is deprecated - use `->` or `matches` instead: https://verus-lang.github.io/verus/guide/datatypes_enum.html")] #[allow(non_snake_case)] spec fn get_Some_0(&self) -> T; #[cfg_attr(not(verus_verify_core), deprecated = "is_Variant is deprecated - use `->` or `matches` instead: https://verus-lang.github.io/verus/guide/datatypes_enum.html")] #[allow(non_snake_case)] spec fn is_None(&self) -> bool; #[allow(non_snake_case)] spec fn arrow_Some_0(&self) -> T; #[allow(non_snake_case)] spec fn arrow_0(&self) -> T; #[allow(deprecated)] proof fn tracked_unwrap(tracked self) -> (tracked t: T) requires self.is_Some(), ensures t == self->0, ; #[allow(deprecated)] proof fn tracked_expect(tracked self, msg: &str) -> (tracked t: T) requires self.is_Some(), ensures t == self->0, ; #[allow(deprecated)] proof fn tracked_borrow(tracked &self) -> (tracked t: &T) requires self.is_Some(), ensures t == self->0, ; #[allow(deprecated)] #[verifier::tracked_take_option_primitive] proof fn tracked_take(tracked &mut self) -> (tracked t: T) requires old(self).is_Some(), ensures t == old(self)->0, final(self).is_None(), ; } impl OptionAdditionalFns for Option { #[verifier::inline] open spec fn is_Some(&self) -> bool { is_variant(self, "Some") } #[verifier::inline] open spec fn get_Some_0(&self) -> T { get_variant_field(self, "Some", "0") } #[verifier::inline] open spec fn is_None(&self) -> bool { is_variant(self, "None") } #[verifier::inline] open spec fn arrow_Some_0(&self) -> T { get_variant_field(self, "Some", "0") } #[verifier::inline] open spec fn arrow_0(&self) -> T { get_variant_field(self, "Some", "0") } proof fn tracked_unwrap(tracked self) -> (tracked t: T) { match self { Option::Some(t) => t, Option::None => proof_from_false(), } } proof fn tracked_expect(tracked self, msg: &str) -> (tracked t: T) { match self { Option::Some(t) => t, Option::None => proof_from_false(), } } proof fn tracked_borrow(tracked &self) -> (tracked t: &T) { match self { Option::Some(t) => t, Option::None => proof_from_false(), } } /// Similar to `Option::take` #[verifier::tracked_take_option_primitive] axiom fn tracked_take(tracked &mut self) -> (tracked t: T); } ////// Specs for std methods // is_some #[verifier::inline] pub open spec fn is_some(option: &Option) -> bool { is_variant(option, "Some") } #[verifier::when_used_as_spec(is_some)] pub assume_specification[ Option::::is_some ](option: &Option) -> (b: bool) ensures b == is_some(option), no_unwind ; // is_none #[verifier::inline] pub open spec fn is_none(option: &Option) -> bool { is_variant(option, "None") } #[verifier::when_used_as_spec(is_none)] pub assume_specification[ Option::::is_none ](option: &Option) -> (b: bool) ensures b == is_none(option), no_unwind ; // as_ref pub assume_specification[ Option::::as_ref ](option: &Option) -> (a: Option<&T>) ensures a is Some <==> option is Some, a is Some ==> option->0 == a->0, no_unwind ; // unwrap #[verifier::inline] pub open spec fn spec_unwrap(option: Option) -> T recommends option is Some, { option->0 } #[verifier::when_used_as_spec(spec_unwrap)] pub assume_specification[ Option::::unwrap ](option: Option) -> (t: T) requires option is Some, ensures t == spec_unwrap(option), ; // unwrap_or #[verifier::inline] pub open spec fn spec_unwrap_or(option: Option, default: T) -> T { match option { Some(t) => t, None => default, } } #[verifier::when_used_as_spec(spec_unwrap_or)] pub assume_specification[ Option::::unwrap_or ](option: Option, default: T) -> (t: T) ensures t == spec_unwrap_or(option, default), no_unwind ; // expect #[verifier::inline] pub open spec fn spec_expect(option: Option, msg: &str) -> T recommends option is Some, { option->0 } #[verifier::when_used_as_spec(spec_expect)] pub assume_specification[ Option::::expect ](option: Option, msg: &str) -> (t: T) requires option is Some, ensures t == spec_expect(option, msg), ; // take pub assume_specification[ Option::::take ](option: &mut Option) -> (t: Option) ensures t == *old(option), *final(option) is None, no_unwind ; // map pub assume_specification U>[ Option::::map ](a: Option, f: F) -> (ret: Option) requires a.is_some() ==> f.requires((a.unwrap(),)), ensures ret.is_some() == a.is_some(), ret.is_some() ==> f.ensures((a.unwrap(),), ret.unwrap()), ; // cloned pub assume_specification<'a, T: Clone>[ Option::<&'a T>::cloned ](opt: Option<&'a T>) -> (res: Option) ensures opt.is_none() ==> res.is_none(), opt.is_some() ==> res.is_some() && cloned::(*opt.unwrap(), res.unwrap()), ; // copied pub assume_specification<'a, T: Copy>[ Option::<&'a T>::copied ](opt: Option<&'a T>) -> (res: Option) ensures res == match opt { Some(x) => Some(*x), None => None::, }, ; // and_then pub assume_specification Option>[ Option::::and_then ]( option: Option, f: F, ) -> (res: Option) requires option.is_some() ==> f.requires((option.unwrap(),)), ensures option.is_none() ==> res.is_none(), option.is_some() ==> f.ensures((option.unwrap(),), res), ; // ok_or_else pub assume_specification E>[ Option::::ok_or_else ]( option: Option, err: F, ) -> (res: Result) requires option.is_none() ==> err.requires(()), ensures option.is_some() ==> res == Ok::(option.unwrap()), option.is_none() ==> { &&& res.is_err() &&& err.ensures((), res->Err_0) }, ; // unwrap_or_default pub assume_specification[ Option::::unwrap_or_default ]( option: Option, ) -> (res: T) ensures option.is_some() ==> res == option.unwrap(), option.is_none() ==> T::default.ensures((), res), ; // unwrap_or_else pub assume_specification T>[ Option::::unwrap_or_else ]( option: Option, f: F, ) -> (res: T) requires option.is_none() ==> f.requires(()), ensures option.is_some() ==> res == option.unwrap(), option.is_none() ==> f.ensures((), res), ; // clone pub assume_specification[ as Clone>::clone ](opt: &Option) -> (res: Option< T, >) ensures opt.is_none() ==> res.is_none(), opt.is_some() ==> res.is_some() && cloned::(opt.unwrap(), res.unwrap()), ; // PartialEq and Eq impl super::cmp::PartialEqSpecImpl for Option { open spec fn obeys_eq_spec() -> bool { T::obeys_eq_spec() } open spec fn eq_spec(&self, other: &Option) -> bool { match (self, other) { (None, None) => true, (Some(x), Some(y)) => x.eq_spec(y), _ => false, } } } pub assume_specification[ as PartialEq>::eq ]( x: &Option, y: &Option, ) -> bool ; // PartialOrd and Ord impl super::cmp::PartialOrdSpecImpl for Option { open spec fn obeys_partial_cmp_spec() -> bool { T::obeys_partial_cmp_spec() } open spec fn partial_cmp_spec(&self, other: &Option) -> Option { match (self, other) { (None, None) => Some(core::cmp::Ordering::Equal), (None, Some(_)) => Some(core::cmp::Ordering::Less), (Some(_), None) => Some(core::cmp::Ordering::Greater), (Some(x), Some(y)) => x.partial_cmp_spec(y), } } } pub assume_specification[ as PartialOrd>::partial_cmp ]( x: &Option, y: &Option, ) -> Option ; impl super::cmp::OrdSpecImpl for Option { open spec fn obeys_cmp_spec() -> bool { T::obeys_cmp_spec() } open spec fn cmp_spec(&self, other: &Option) -> core::cmp::Ordering { match (self, other) { (None, None) => core::cmp::Ordering::Equal, (None, Some(_)) => core::cmp::Ordering::Less, (Some(_), None) => core::cmp::Ordering::Greater, (Some(x), Some(y)) => x.cmp_spec(y), } } } pub assume_specification[ as Ord>::cmp ]( x: &Option, y: &Option, ) -> core::cmp::Ordering ; // ok_or #[verifier::inline] pub open spec fn spec_ok_or(option: Option, err: E) -> Result { match option { Some(t) => Ok(t), None => Err(err), } } #[verifier::when_used_as_spec(spec_ok_or)] pub assume_specification[ Option::ok_or ](option: Option, err: E) -> (res: Result) ensures res == spec_ok_or(option, err), ; #[doc(hidden)] pub assume_specification[ Option::as_mut ](option: &mut Option) -> (res: Option<&mut T>) ensures (match *old(option) { None => final(option).is_none() && res.is_none(), Some(r) => final(option).is_some() && res.is_some() && *res.unwrap() == r && *final(res.unwrap()) == final(option).unwrap(), }), ; pub assume_specification[ Option::as_slice ](option: &Option) -> (res: &[T]) ensures res@ == (match *option { Some(x) => seq![x], None => seq![], }), ; #[doc(hidden)] pub assume_specification[ Option::as_mut_slice ](option: &mut Option) -> (res: &mut [T]) ensures res@ == (match *old(option) { Some(x) => seq![x], None => seq![], }), final(res)@.len() == res@.len(), // TODO this should be broadcast for all `&mut [T]` final(option)@ == (match *old(option) { Some(_) => Some(final(res)@[0]), None => None, }), ; #[doc(hidden)] pub assume_specification[ Option::insert ](option: &mut Option, value: T) -> (res: &mut T) ensures *res == value, *final(option) == Some(*final(res)), ; #[doc(hidden)] pub assume_specification[ Option::get_or_insert ](option: &mut Option, value: T) -> (res: &mut T) ensures *res == (match *old(option) { Some(x) => x, None => value, }), *final(option) == Some(*final(res)), ; } // verus!