Skip to content

Commit

Permalink
Spec for Vec::extend_from_slice (#1439)
Browse files Browse the repository at this point in the history
  • Loading branch information
jaylorch authored Feb 10, 2025
1 parent a2cb2dd commit ee55922
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
20 changes: 20 additions & 0 deletions source/rust_verify/example/std_test/vec_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use std::vec::Vec;

use vstd::pervasive::runtime_assert;
use vstd::prelude::*;

verus! {

fn vec_extend_slice_test() {
let mut a: Vec<u32> = vec![1, 2];
let b: Vec<u32> = vec![3, 4];
a.extend_from_slice(b.as_slice());
runtime_assert(a.len() == 4);
runtime_assert(a[0] == 1);
runtime_assert(a[1] == 2);
runtime_assert(a[2] == 3);
runtime_assert(a[3] == 4);
runtime_assert(b.len() == 2);
}

} // verus!
15 changes: 15 additions & 0 deletions source/vstd/std_specs/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,21 @@ pub assume_specification<T, A: Allocator>[ Vec::<T, A>::append ](
other@ == Seq::<T>::empty(),
;

pub assume_specification<T: core::clone::Clone, A: Allocator>[ Vec::<T, A>::extend_from_slice ](
vec: &mut Vec<T, A>,
other: &[T],
)
ensures
vec@.len() == old(vec)@.len() + other@.len(),
forall|i: int|
#![trigger vec@[i]]
0 <= i < vec@.len() ==> if i < old(vec)@.len() {
vec@[i] == old(vec)@[i]
} else {
cloned::<T>(other@[i - old(vec)@.len()], vec@[i])
},
;

/*
// TODO find a way to support this
// This is difficult because of the SliceIndex trait
Expand Down

0 comments on commit ee55922

Please sign in to comment.