teaching-material
teaching-material copied to clipboard
add typestates assignment
Rework cool example from the RfR
book by
- Task 1:starting with a toy example like
struct Grounded;
struct Launched;
// and so on
struct Rocket<Stage = Grounded> {
stage: std::marker::PhantomData<Stage>,
}
impl Default for Rocket<Grounded> {}
impl Rocket<Grounded> {
pub fn launch(self) -> Rocket<Launched> { }
}
impl Rocket<Launched> {
pub fn accelerate(&mut self) { }
pub fn decelerate(&mut self) { }
}
impl<Stage> Rocket<Stage> {
pub fn color(&self) -> Color { }
pub fn weight(&self) -> Kilograms { }
}
-
Task 2: working up to the
Mailbox
assignment code. See also state type parameters. -
Task 3: See the use of the
typestate
crate to avoid the boilerplate of the typestate pattern