Struct creusot_contracts::logic::FSet
source · pub struct FSet<T: ?Sized>(/* private fields */);
Expand description
A finite set type usable in pearlite and ghost!
blocks.
If you need an infinite set, see Set
.
§Ghost
Since std::collections::HashSet
and std::collections::BTreeSet
have finite
capacity, this could cause some issues in ghost code:
ghost! {
let mut set = HashSet::new();
for _ in 0..=usize::MAX as u128 + 1 {
set.insert(0); // cannot fail, since we are in a ghost block
}
proof_assert!(set.len() <= usize::MAX@); // by definition
proof_assert!(set.len() > usize::MAX@); // uh-oh
}
This type is designed for this use-case, with no restriction on the capacity.
Implementations§
source§impl<T: ?Sized> FSet<T>
impl<T: ?Sized> FSet<T>
sourcepub fn insert(self, e: T) -> Self
pub fn insert(self, e: T) -> Self
Returns a new set, where e
has been added if it was not present.
logic
Self::add(e, self)
sourcepub fn remove(self, e: T) -> Self
pub fn remove(self, e: T) -> Self
Returns a new set, where e
is no longer present.
logic
Self::rem(e, self)
sourcepub fn union(self, other: Self) -> Self
pub fn union(self, other: Self) -> Self
Returns a new set, which is the union of self
and other
.
An element is in the result if it is in self
or if it is in other
.
logic ⚠
sourcepub fn is_subset(self, other: Self) -> bool
pub fn is_subset(self, other: Self) -> bool
Returns true
if every element of self
is in other
.
logic ⚠
sourcepub fn is_superset(self, other: Self) -> bool
pub fn is_superset(self, other: Self) -> bool
Returns true
if every element of other
is in self
.
logic
Self::is_subset(other, self)
sourcepub fn len(self) -> Int
pub fn len(self) -> Int
Returns the number of elements in the set, also called its length.
logic ⚠
source§impl<T: ?Sized> FSet<T>
impl<T: ?Sized> FSet<T>
Ghost definitions
sourcepub fn new() -> GhostBox<Self>
pub fn new() -> GhostBox<Self>
Create a new, empty set on the ghost heap.
pure
ensures
result.is_empty()
sourcepub fn len_ghost(&self) -> Int
pub fn len_ghost(&self) -> Int
Returns the number of elements in the set.
If you need to get the length in pearlite, consider using len
.
§Example
use creusot_contracts::{logic::FSet, *};
let mut set = FSet::new();
ghost! {
let len1 = set.len_ghost();
set.insert_ghost(1);
set.insert_ghost(2);
set.insert_ghost(1);
let len2 = set.len_ghost();
proof_assert!(len1 == 0);
proof_assert!(len2 == 2);
};
pure
ensures
result == self.len()
sourcepub fn contains_ghost(&self, value: &T) -> bool
pub fn contains_ghost(&self, value: &T) -> bool
Returns true if the set contains the specified value.
§Example
use creusot_contracts::{logic::FSet, *};
let mut set = FSet::new();
ghost! {
set.insert_ghost(1);
let (b1, b2) = (set.contains_ghost(&1), set.contains_ghost(&2));
proof_assert!(b1);
proof_assert!(!b2);
};
pure
ensures
result == self.contains(*value)
sourcepub fn insert_ghost(&mut self, value: T) -> boolwhere
T: Sized,
pub fn insert_ghost(&mut self, value: T) -> boolwhere
T: Sized,
Adds a value to the set.
Returns whether the value was newly inserted. That is:
- If the set did not previously contain this value,
true
is returned. - If the set already contained this value,
false
is returned, and the set is not modified: original value is not replaced, and the value passed as argument is dropped.
§Example
use creusot_contracts::{logic::FSet, *};
let mut set = FSet::new();
ghost! {
let res1 = set.insert_ghost(42);
proof_assert!(res1);
proof_assert!(set.contains(42i32));
let res2 = set.insert_ghost(41);
let res3 = set.insert_ghost(42);
proof_assert!(res2);
proof_assert!(!res3);
proof_assert!(set.len() == 2);
};
pure
ensures
^self == (*self).insert(value)
ensures
result == !(*self).contains(value)
sourcepub fn insert_ghost_unsized(&mut self, value: Box<T>) -> bool
pub fn insert_ghost_unsized(&mut self, value: Box<T>) -> bool
Same as Self::insert_ghost
, but for unsized values.
pure
ensures
^self == (*self).insert(*value)
ensures
result == !(*self).contains(*value)
sourcepub fn remove_ghost(&mut self, value: &T) -> bool
pub fn remove_ghost(&mut self, value: &T) -> bool
Removes a value from the set. Returns whether the value was present in the set.
§Example
use creusot_contracts::{logic::FSet, *};
let mut set = FSet::new();
let res = ghost! {
set.insert_ghost(1);
let res1 = set.remove_ghost(&1);
let res2 = set.remove_ghost(&1);
proof_assert!(res1 && !res2);
};
pure
ensures
^self == (*self).remove(*value)
ensures
result == (*self).contains(*value)
Trait Implementations§
impl<T: Clone + Copy> Copy for FSet<T>
Auto Trait Implementations§
impl<T> Freeze for FSet<T>where
T: ?Sized,
impl<T> !RefUnwindSafe for FSet<T>
impl<T> !Send for FSet<T>
impl<T> !Sync for FSet<T>
impl<T> !Unpin for FSet<T>
impl<T> !UnwindSafe for FSet<T>
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§default unsafe fn clone_to_uninit(&self, dst: *mut T)
default unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)source§impl<T> CloneToUninit for Twhere
T: Copy,
impl<T> CloneToUninit for Twhere
T: Copy,
source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)source§impl<T> MakeSized for Twhere
T: ?Sized,
impl<T> MakeSized for Twhere
T: ?Sized,
source§fn make_sized(&self) -> Box<T>
fn make_sized(&self) -> Box<T>
logic ⚠
ensures
*result == *self