Design By Contract for Rust
Annotate functions and methods with "contracts", using invariants, pre-conditions and post-conditions.
Design by contract is a popular method to augment code with formal interface specifications. These specifications are used to increase the correctness of the code by checking them as assertions at runtime.
Attributes
This crate exposes the requires
, ensures
and invariant
attributes.
requires
are checked before a function/method is executed.ensures
are checked after a function/method ran to completioninvariant
s are checked both before and after a function/method ran.
Additionally, all those attributes have versions with different "modes". See the Modes section below.
For trait
s and trait impl
s the contract_trait
attribute can be used.
More specific information can be found in the crate documentation.
Pseudo-functions and operators
old()
function
One unique feature that this crate provides is an old()
pseudo-function which
allows to perform checks using a value of a parameter before the function call
happened. This is only available in ensures
attributes.
->
operator
For more complex functions it can be useful to express behaviour using logical implication. Because Rust does not feature an operator for implication, this crate adds this operator for use in attributes.
This operator is right-associative.
Note: Because of the design of syn
, it is tricky to add custom operators
to be parsed, so this crate performs a rewrite of the TokenStream
instead.
The rewrite works by separating the expression into a part that's left of the
->
operator and the rest on the right side. This means that
if a -> b { c } else { d }
will not generate the expected code.
Explicit grouping using parenthesis or curly-brackets can be used to avoid this.
Modes
All the attributes (requires, ensures, invariant) have debug_*
and test_*
versions.
-
debug_requires
/debug_ensures
/debug_invariant
usedebug_assert!
internally rather thanassert!
-
test_requires
/test_ensures
/test_invariant
guard theassert!
with anif cfg!(test)
. This should mostly be used for stating equivalence to "slow but obviously correct" alternative implementations or checks.For example, a merge-sort implementation might look like this
Set-up
To install the latest version, add contracts
to the dependency section of the
Cargo.toml
file.
[dependencies]
contracts = "0.6.3"
To then bring all procedural macros into scope, you can add use contracts::*;
in all files you plan to use the contract attributes.
Alternatively use the "old-style" of importing macros to have them available project-wide.
extern crate contracts;
Configuration
This crate exposes a number of feature flags to configure the assertion behavior.
disable_contracts
- disables all checks and assertions.override_debug
- changes all contracts (excepttest_
ones) intodebug_*
versionsoverride_log
- changes all contracts (excepttest_
ones) into alog::error!()
call if the condition is violated. No abortion happens.mirai_assertions
- instead of regular assert! style macros, emit macros used by the MIRAI static analyzer. For more documentation of this usage, head to the MIRAI repo.
TODOs
- implement more contracts for traits.
- add a static analyzer à la SPARK for whole-projects using the contracts to make static assertions.