forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support to min_specialization for arbitrary / invariant (rust-lan…
…g#1029) * Add support to mim_specialization for arbitrary / invariant We would like to enable users to provide custom implementations of Invariant and Arbitrary. Use feature `min_specialization` to allow that. Note that users will also need to enable that same feature in their code.
- Loading branch information
Showing
3 changed files
with
44 additions
and
1 deletion.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 OR MIT | ||
// | ||
// Check that users can implement Invariant and Arbitrary to the same struct. | ||
#![cfg_attr(kani, feature(min_specialization))] | ||
|
||
extern crate kani; | ||
use kani::{Arbitrary, Invariant}; | ||
|
||
// Dummy wrappar that keeps track of the vector size. | ||
struct VecWrapper { | ||
has_data: bool, | ||
data: Vec<u8>, | ||
} | ||
|
||
impl VecWrapper { | ||
fn new() -> Self { | ||
VecWrapper { has_data: false, data: Vec::new() } | ||
} | ||
|
||
fn from(buf: &[u8]) -> Self { | ||
VecWrapper { has_data: true, data: Vec::from(buf) } | ||
} | ||
} | ||
|
||
unsafe impl Invariant for VecWrapper { | ||
fn is_valid(&self) -> bool { | ||
self.has_data ^ self.data.is_empty() | ||
} | ||
} | ||
|
||
impl Arbitrary for VecWrapper { | ||
fn any() -> Self { | ||
if kani::any() { VecWrapper::new() } else { VecWrapper::from(&[kani::any(), kani::any()]) } | ||
} | ||
} | ||
|
||
#[kani::proof] | ||
fn check() { | ||
let wrap: VecWrapper = kani::any(); | ||
assert!(wrap.is_valid()); | ||
} |