Crate typestates_comparison
source ·Expand description
Comparison of different implementations of typestates.
There are three primary ways of implementing typestates:
- individual types,
- generic type with phantom types,
- generic type with real types.
The last approach can also be implemented with the help of the
typestate
crate.
In this library we compare the three approaches by implementing the following system.
- An entity gets created with an ID.
- We can add a key
a
orb
to the entity, but not both. - After we’ve added
a
orb
, we can addc
. - The entity is now ready to be used, i.e. printed.
Examples
Individual types
use typestates_comparison::individual_types::*;
let e = Initial::new(42);
let e = e.add_a(3.14);
let e = e.add_c('a');
assert_eq!(e.format(), format!("id:42 a:3.14 c:a"));
Generic type with phantom types
use typestates_comparison::phantom_types::*;
let e = Entity::new(42);
let e = e.add_a(3.14);
let e = e.add_c('a');
assert_eq!(e.format(), format!("id:42 a:3.14 c:a"));
Generic type with real types
use typestates_comparison::real_types::*;
let e = Entity::new(42);
let e = e.add_a(3.14);
let e = e.add_c('a');
assert_eq!(e.format(), format!("id:42 a:3.14 c:a"));
Generic type with real types (typestate
crate)
use typestates_comparison::typestate_crate::*;
let e = Entity::new(42);
let e = e.add_a(3.14);
let e = e.add_c('a');
assert_eq!(e.format(), format!("id:42 a:3.14 c:a"));
Modules
- Every state is implemented as a separate type.
- Every state is implemented as a phantom type of a generic type.
- Every state is implemented as a type parameter of a generic type.
- Implemented with the help of the
typestate
crate.