“IS THE SLEIGH PARKED, READY, OR FLYING? WHY DOESN’T ANYONE KNOW?”
“Uh… shouldn’t you know? Didn't we use the type state pattern?” Bernard spoke up.
Santa's face twitched. "Of course, we used the type state pattern! But does anyone have a quick way to check the state right now? There are some sleighs in the air, it's hard to keep track on all of them."
Blitzen, lounging in a corner, snorted. “Sounds like y'all need to add a status()
method.”
Prancer’s ears perked up. “Yeah! A single method we can call to figure out the state, no matter which one it’s in.”
Santa stroked his beard, muttering. “Fine. Add it. And make sure it’s compile-time safe. If I get a wrong answer, someone’s spending Christmas hand-delivering packets in binary.”
It's time to give Santa what he wants: a single status()
method that works in all states of the sleigh (Empty
, Ready
, and Flying
), returning the current state as a string.
Here's the plan:
status()
method: It should be callable on the Sleigh
regardless of its state."Empty"
when the sleigh is empty."Ready"
when the sleigh is loaded and ready to fly."Flying"
when the sleigh is in the air.Sleigh
struct should have a trait bound, not just a generic <T>
.Here's how Santa wants to the the API:
let sleigh = Sleigh::new();
assert_eq!(sleigh.status(), "Empty");
sleigh.load();
assert_eq!(sleigh.status(), "Ready");
sleigh.take_off();
assert_eq!(sleigh.status(), "Flying");
If you’re unsure where to start, take a look at these tips:
Use a trait to define shared behavior across all states. For example:
pub trait State {
fn status() -> &'static str;
}
Implement the trait for each state. e.g.
impl State for Empty {
fn status() -> &'static str {
"Empty"
}
}
Implement a method for all types that implement the State
trait. e.g.
impl<T: State> Sleigh<T> {
pub fn status(&self) -> &'static str {
T::status()
}
}
use std::marker::PhantomData;pub struct Empty;pub struct Ready;pub struct Flying;// TODO: Define the `status` method for all statespub trait State { fn status() -> &'static str;}pub struct Sleigh<T> where T : State { // This is only public for testing purposes // In real-world scenarios, this should be private pub state: PhantomData<T>}impl State for Empty { fn status() -> &'static str { "Empty" }}impl State for Ready { fn status() -> &'static str { "Ready" }}impl State for Flying { fn status() -> &'static str { "Flying" }}impl Sleigh<Empty> { pub fn new() -> Self { Self { state: PhantomData } } pub fn load(self) -> Sleigh<Ready> { Sleigh { state: PhantomData } }}// Add status method for all statesimpl<T> Sleigh<T>where T: State{ pub fn status(&self) -> &'static str { T::status() }}impl Sleigh<Ready> { pub fn take_off(self) -> Sleigh<Flying> { Sleigh { state: PhantomData } } pub fn unload(self) -> Sleigh<Empty> { Sleigh { state: PhantomData } }}impl Sleigh<Flying> { pub fn land(self) -> Sleigh<Ready> { Sleigh { state: PhantomData } }}
use std::marker::PhantomData;pub struct Empty;pub struct Ready;pub struct Flying;// TODO: Define the `status` method for all statespub trait State { fn status() -> &'static str;}pub struct Sleigh<T: State> { // This is only public for testing purposes // In real-world scenarios, this should be private pub state: PhantomData<T>,}impl<T: State> Sleigh<T> { pub fn status(&self) -> &'static str { T::status() }}impl State for Empty { fn status() -> &'static str { "Empty" }}impl State for Ready { fn status() -> &'static str { "Ready" }}impl State for Flying { fn status() -> &'static str { "Flying" }}impl Sleigh<Empty> { pub fn new() -> Self { Self { state: PhantomData } } pub fn load(self) -> Sleigh<Ready> { Sleigh { state: PhantomData } }}impl Sleigh<Ready> { pub fn take_off(self) -> Sleigh<Flying> { Sleigh { state: PhantomData } } pub fn unload(self) -> Sleigh<Empty> { Sleigh { state: PhantomData } }}impl Sleigh<Flying> { pub fn land(self) -> Sleigh<Ready> { Sleigh { state: PhantomData } }}
use std::marker::PhantomData;pub struct Empty;pub struct Ready;pub struct Flying;// TODO: Define the `status` method for all statespub trait State { fn status() -> &'static str;}impl State for Empty { fn status() -> &'static str { "Empty" }}impl State for Ready { fn status() -> &'static str { "Ready" }}impl State for Flying { fn status() -> &'static str { "Flying" }}impl<T: State> Sleigh<T> { pub fn status(&self) -> &'static str { T::status() }}pub struct Sleigh<T: State> { // This is only public for testing purposes // In real-world scenarios, this should be private pub state: PhantomData<T>,}impl Sleigh<Empty> { pub fn new() -> Self { Self { state: PhantomData } } pub fn load(self) -> Sleigh<Ready> { Sleigh { state: PhantomData } }}impl Sleigh<Ready> { pub fn take_off(self) -> Sleigh<Flying> { Sleigh { state: PhantomData } } pub fn unload(self) -> Sleigh<Empty> { Sleigh { state: PhantomData } }}impl Sleigh<Flying> { pub fn land(self) -> Sleigh<Ready> { Sleigh { state: PhantomData } }}
use std::marker::PhantomData;pub struct Empty;pub struct Ready;pub struct Flying;// TODO: Define the `status` method for all statespub trait State { fn status() -> &'static str;}impl State for Empty { fn status() -> &'static str { "Empty" }}impl State for Ready { fn status() -> &'static str { "Ready" }}impl State for Flying { fn status() -> &'static str { "Flying" }}pub struct Sleigh<T: State> { // This is only public for testing purposes // In real-world scenarios, this should be private pub state: PhantomData<T>,}impl Sleigh<Empty> { pub fn new() -> Self { Self { state: PhantomData } } pub fn load(self) -> Sleigh<Ready> { Sleigh { state: PhantomData } }}impl Sleigh<Ready> { pub fn take_off(self) -> Sleigh<Flying> { Sleigh { state: PhantomData } } pub fn unload(self) -> Sleigh<Empty> { Sleigh { state: PhantomData } }}impl Sleigh<Flying> { pub fn land(self) -> Sleigh<Ready> { Sleigh { state: PhantomData } }}impl<T: State> Sleigh<T> { pub fn status(&self) -> &'static str { T::status() }}
use std::marker::PhantomData;pub struct Empty;pub struct Ready;pub struct Flying;// TODO: Define the `status` method for all statespub trait State { fn status() -> &'static str;}pub struct Sleigh<T>where T: State,{ // This is only public for testing purposes // In real-world scenarios, this should be private pub state: PhantomData<T>,}impl<T> Sleigh<T>where T: State,{ pub fn status(&self) -> &str { T::status() }}impl Sleigh<Empty> { pub fn new() -> Self { Self { state: PhantomData } } pub fn load(self) -> Sleigh<Ready> { Sleigh { state: PhantomData } }}impl State for Empty { fn status() -> &'static str { "Empty" }}impl Sleigh<Ready> { pub fn take_off(self) -> Sleigh<Flying> { Sleigh { state: PhantomData } } pub fn unload(self) -> Sleigh<Empty> { Sleigh { state: PhantomData } }}impl State for Ready { fn status() -> &'static str { "Ready" }}impl Sleigh<Flying> { pub fn land(self) -> Sleigh<Ready> { Sleigh { state: PhantomData } }}impl State for Flying { fn status() -> &'static str { "Flying" }}
use std::marker::PhantomData;pub struct Empty;pub struct Ready;pub struct Flying;// TODO: Define the `status` method for all statespub struct Sleigh<T: Status> { // This is only public for testing purposes // In real-world scenarios, this should be private pub state: PhantomData<T>,}pub trait Status { fn status() -> &'static str;}impl Sleigh<Empty> { pub fn new() -> Self { Self { state: PhantomData } } pub fn load(self) -> Sleigh<Ready> { Sleigh { state: PhantomData } }}impl Sleigh<Ready> { pub fn take_off(self) -> Sleigh<Flying> { Sleigh { state: PhantomData } } pub fn unload(self) -> Sleigh<Empty> { Sleigh { state: PhantomData } }}impl Sleigh<Flying> { pub fn land(self) -> Sleigh<Ready> { Sleigh { state: PhantomData } }}impl Status for Flying { fn status() -> &'static str { "Flying" }}impl Status for Empty { fn status() -> &'static str { "Empty" }}impl Status for Ready { fn status() -> &'static str { "Ready" }}impl<T: Status> Sleigh<T> { pub fn status(&self) -> &'static str { T::status() }}
use std::marker::PhantomData;pub struct Empty;pub struct Ready;pub struct Flying;pub trait State { fn status() -> &'static str;}pub struct Sleigh<T: State> { // This is only public for testing purposes // In real-world scenarios, this should be private pub state: PhantomData<T>,}impl Sleigh<Empty> { pub fn new() -> Self { Self { state: PhantomData } } pub fn load(self) -> Sleigh<Ready> { Sleigh { state: PhantomData } }}impl State for Empty { fn status() -> &'static str { "Empty" }}impl Sleigh<Ready> { pub fn take_off(self) -> Sleigh<Flying> { Sleigh { state: PhantomData } } pub fn unload(self) -> Sleigh<Empty> { Sleigh { state: PhantomData } }}impl State for Ready { fn status() -> &'static str { "Ready" }}impl Sleigh<Flying> { pub fn land(self) -> Sleigh<Ready> { Sleigh { state: PhantomData } }}impl State for Flying { fn status() -> &'static str { "Flying" }}impl<T: State> Sleigh<T> { pub fn status(&self) -> &'static str { T::status() }}
use std::marker::PhantomData;pub struct Empty;pub struct Ready;pub struct Flying;pub struct Sleigh<T: State> { pub state: PhantomData<T>,}pub trait State { fn status() -> &'static str;}impl State for Empty { fn status() -> &'static str { "Empty" }}impl State for Ready { fn status() -> &'static str { "Ready" }}impl State for Flying { fn status() -> &'static str { "Flying" }}impl<T: State> Sleigh<T> { pub fn status(&self) -> &'static str { T::status() }}impl Sleigh<Empty> { pub fn new() -> Self { Self { state: PhantomData } } pub fn load(self) -> Sleigh<Ready> { Sleigh { state: PhantomData } }}impl Sleigh<Ready> { pub fn take_off(self) -> Sleigh<Flying> { Sleigh { state: PhantomData } } pub fn unload(self) -> Sleigh<Empty> { Sleigh { state: PhantomData } }}impl Sleigh<Flying> { pub fn land(self) -> Sleigh<Ready> { Sleigh { state: PhantomData } }}
use std::marker::PhantomData;pub struct Empty;pub struct Ready;pub struct Flying;pub struct Sleigh<T: State> { pub state: PhantomData<T>,}pub trait State { fn status() -> &'static str;}impl State for Empty { fn status() -> &'static str { "Empty" }}impl State for Ready { fn status() -> &'static str { "Ready" }}impl State for Flying { fn status() -> &'static str { "Flying" }}impl<T: State> Sleigh<T> { pub fn status(&self) -> &'static str { T::status() }}impl Sleigh<Empty> { pub fn new() -> Self { Self { state: PhantomData } } pub fn load(self) -> Sleigh<Ready> { Sleigh { state: PhantomData } }}impl Sleigh<Ready> { pub fn take_off(self) -> Sleigh<Flying> { Sleigh { state: PhantomData } } pub fn unload(self) -> Sleigh<Empty> { Sleigh { state: PhantomData } }}impl Sleigh<Flying> { pub fn land(self) -> Sleigh<Ready> { Sleigh { state: PhantomData } }}
use std::marker::PhantomData;pub struct Empty;pub struct Ready;pub struct Flying;// TODO: Define the `status` method for all statespub trait State { fn status() -> &'static str;}impl State for Empty { fn status() -> &'static str { "Empty" }}impl State for Ready { fn status() -> &'static str { "Ready" }}impl State for Flying { fn status() -> &'static str { "Flying" }}pub struct Sleigh<T>where T: State,{ // This is only public for testing purposes // In real-world scenarios, this should be private pub state: PhantomData<T>,}impl<T> Sleigh<T>where T: State,{ pub fn status(&self) -> &'static str { T::status() }}impl Sleigh<Empty> { pub fn new() -> Self { Self { state: PhantomData } } pub fn load(self) -> Sleigh<Ready> { Sleigh { state: PhantomData } }}impl Sleigh<Ready> { pub fn take_off(self) -> Sleigh<Flying> { Sleigh { state: PhantomData } } pub fn unload(self) -> Sleigh<Empty> { Sleigh { state: PhantomData } }}impl Sleigh<Flying> { pub fn land(self) -> Sleigh<Ready> { Sleigh { state: PhantomData } }}#[cfg(test)]mod tests { use super::*; #[test] fn test_status() { let sleigh = Sleigh::new(); assert_eq!(sleigh.status(), "Empty"); let sleigh = sleigh.load(); assert_eq!(sleigh.status(), "Ready"); let sleigh = sleigh.take_off(); assert_eq!(sleigh.status(), "Flying"); }}
use std::marker::PhantomData;pub struct Empty;pub struct Ready;pub struct Flying;// TODO: Define the `status` method for all statespub trait State { fn status() -> &'static str;}impl State for Empty { fn status() -> &'static str { "Empty" }}impl State for Ready { fn status() -> &'static str { "Ready" }}impl State for Flying { fn status() -> &'static str { "Flying" }}impl<T: State> Sleigh<T> { pub fn status(&self) -> &'static str { T::status() }}pub struct Sleigh<T: State> { // This is only public for testing purposes // In real-world scenarios, this should be private pub state: PhantomData<T>,}impl Sleigh<Empty> { pub fn new() -> Self { Self { state: PhantomData } } pub fn load(self) -> Sleigh<Ready> { Sleigh { state: PhantomData } }}impl Sleigh<Ready> { pub fn take_off(self) -> Sleigh<Flying> { Sleigh { state: PhantomData } } pub fn unload(self) -> Sleigh<Empty> { Sleigh { state: PhantomData } }}impl Sleigh<Flying> { pub fn land(self) -> Sleigh<Ready> { Sleigh { state: PhantomData } }}
use std::marker::PhantomData;pub struct Empty;pub struct Ready;pub struct Flying;pub trait State { fn status() -> &'static str;}impl State for Empty { fn status() -> &'static str { "Empty" }}impl State for Ready { fn status() -> &'static str { "Ready" }}impl State for Flying { fn status() -> &'static str { "Flying" }}// TODO: Define the `status` method for all statespub struct Sleigh<T: State> { // This is only public for testing purposes // In real-world scenarios, this should be private pub state: PhantomData<T>,}impl<T: State> Sleigh<T> { pub fn status(&self) -> &'static str { T::status() }}impl Sleigh<Empty> { pub fn new() -> Self { Self { state: PhantomData } } pub fn load(self) -> Sleigh<Ready> { Sleigh { state: PhantomData } }}impl Sleigh<Ready> { pub fn take_off(self) -> Sleigh<Flying> { Sleigh { state: PhantomData } } pub fn unload(self) -> Sleigh<Empty> { Sleigh { state: PhantomData } }}impl Sleigh<Flying> { pub fn land(self) -> Sleigh<Ready> { Sleigh { state: PhantomData } }}
use std::marker::PhantomData;pub struct Empty;pub struct Ready;pub struct Flying;pub trait State {}impl State for Empty {}impl State for Ready {}impl State for Flying {}// TODO: Define the `status` method for all statespub struct Sleigh<T: State> { // This is only public for testing purposes // In real-world scenarios, this should be private pub state: PhantomData<T>,}impl Sleigh<Empty> { pub fn new() -> Self { Self { state: PhantomData } } pub fn load(self) -> Sleigh<Ready> { Sleigh { state: PhantomData } } pub fn status(&self) -> String { String::from("Empty") }}impl Sleigh<Ready> { pub fn take_off(self) -> Sleigh<Flying> { Sleigh { state: PhantomData } } pub fn unload(self) -> Sleigh<Empty> { Sleigh { state: PhantomData } } pub fn status(&self) -> String { String::from("Ready") }}impl Sleigh<Flying> { pub fn land(self) -> Sleigh<Ready> { Sleigh { state: PhantomData } } pub fn status(&self) -> String { String::from("Flying") }}
use std::marker::PhantomData;pub struct Empty;pub struct Ready;pub struct Flying;// TODO: Define the `status` method for all statespub struct Sleigh<T: State> { // This is only public for testing purposes // In real-world scenarios, this should be private pub state: PhantomData<T>,}impl<T: State> Sleigh<T> { pub fn status(&self) -> &'static str { T::status() }}pub trait State { fn status() -> &'static str;}impl Sleigh<Empty> { pub fn new() -> Self { Self { state: PhantomData } } pub fn load(self) -> Sleigh<Ready> { Sleigh { state: PhantomData } }}impl State for Empty { fn status() -> &'static str { "Empty" }}impl Sleigh<Ready> { pub fn take_off(self) -> Sleigh<Flying> { Sleigh { state: PhantomData } } pub fn unload(self) -> Sleigh<Empty> { Sleigh { state: PhantomData } }}impl State for Ready { fn status() -> &'static str { "Ready" }}impl Sleigh<Flying> { pub fn land(self) -> Sleigh<Ready> { Sleigh { state: PhantomData } }}impl State for Flying { fn status() -> &'static str { "Flying" }}
use std::marker::PhantomData;pub struct Empty;pub struct Ready;pub struct Flying;pub struct Sleigh<T>where T: State,{ // This is only public for testing purposes // In real-world scenarios, this should be private pub state: PhantomData<T>,}pub trait State { fn status() -> &'static str;}macro_rules! impl_state { ($($state:ident),+) => { $( impl State for $state { fn status() -> &'static str { stringify!($state) } } )+ };}impl_state!(Empty, Ready, Flying);impl<T: State> Sleigh<T> { pub fn status(&self) -> &'static str { T::status() }}impl Sleigh<Empty> { pub fn new() -> Self { Self { state: PhantomData } } pub fn load(self) -> Sleigh<Ready> { Sleigh { state: PhantomData } }}impl Sleigh<Ready> { pub fn take_off(self) -> Sleigh<Flying> { Sleigh { state: PhantomData } } pub fn unload(self) -> Sleigh<Empty> { Sleigh { state: PhantomData } }}impl Sleigh<Flying> { pub fn land(self) -> Sleigh<Ready> { Sleigh { state: PhantomData } }}
use std::marker::PhantomData;pub struct Empty;pub struct Ready;pub struct Flying;// TODO: Define the `status` method for all statespub trait Status { fn status(&self) -> &'static str;}impl Status for Empty { fn status(&self) -> &'static str { "Empty" }}impl Status for Ready { fn status(&self) -> &'static str { "Ready" }}impl Status for Flying { fn status(&self) -> &'static str { "Flying" }}pub struct Sleigh<T: Status> { // This is only public for testing purposes // In real-world scenarios, this should be private pub state: T,}impl Sleigh<Empty> { pub fn new() -> Self { Self { state: Empty } } pub fn load(self) -> Sleigh<Ready> { Sleigh { state: Ready } } pub fn status(&self) -> &'static str { self.state.status() }}impl Sleigh<Ready> { pub fn take_off(self) -> Sleigh<Flying> { Sleigh { state: Flying } } pub fn unload(self) -> Sleigh<Empty> { Sleigh { state: Empty } } pub fn status(&self) -> &'static str { self.state.status() }}impl Sleigh<Flying> { pub fn land(self) -> Sleigh<Ready> { Sleigh { state: Ready } } pub fn status(&self) -> &'static str { self.state.status() }}fn main() {}
use std::marker::PhantomData;pub struct Empty;pub struct Ready;pub struct Flying;// TODO: Define the `status` method for all statespub trait Status { fn status() -> &'static str;}macro_rules! sleigh_status { ($($state:ident),+) => { $( impl Status for $state { fn status() -> &'static str { stringify!($state) } })* };}sleigh_status!(Empty, Ready, Flying);pub struct Sleigh<T: Status> { // This is only public for testing purposes // In real-world scenarios, this should be private pub state: PhantomData<T>,}impl<T: Status> Sleigh<T> { pub fn status(&self) -> &'static str { T::status() }}impl Sleigh<Empty> { pub fn new() -> Self { Self { state: PhantomData } } pub fn load(self) -> Sleigh<Ready> { Sleigh { state: PhantomData } }}impl Sleigh<Ready> { pub fn take_off(self) -> Sleigh<Flying> { Sleigh { state: PhantomData } } pub fn unload(self) -> Sleigh<Empty> { Sleigh { state: PhantomData } }}impl Sleigh<Flying> { pub fn land(self) -> Sleigh<Ready> { Sleigh { state: PhantomData } }}
pub struct Empty;pub struct Ready;pub struct Flying;// TODO: Define the `status` method for all statespub trait State { fn show(&self) -> &str; }impl State for Empty { fn show(&self) -> &str { "Empty" } }impl State for Ready { fn show(&self) -> &str { "Ready" } }impl State for Flying { fn show(&self) -> &str { "Flying" } }pub struct Sleigh<T: State> { // This is only public for testing purposes // In real-world scenarios, this should be private pub state: T,}impl<T: State> Sleigh<T> { pub fn status(&self) -> &str { self.state.show() } }impl Sleigh<Empty> { pub fn new() -> Self { Self { state: Empty } } pub fn load(self) -> Sleigh<Ready> { Sleigh { state: Ready } }}impl Sleigh<Ready> { pub fn take_off(self) -> Sleigh<Flying> { Sleigh { state: Flying } } pub fn unload(self) -> Sleigh<Empty> { Sleigh { state: Empty } }}impl Sleigh<Flying> { pub fn land(self) -> Sleigh<Ready> { Sleigh { state: Ready } }}
// 1. We have 3 states:// - Empty// - Ready// - Flyinguse std::marker::PhantomData;pub trait State { const STATUS: &'static str;}pub struct Empty;impl State for Empty { const STATUS: &'static str = "Empty";}pub struct Ready;impl State for Ready { const STATUS: &'static str = "Ready";}pub struct Flying;impl State for Flying { const STATUS: &'static str = "Flying";}// 2. Finish the Seligh struct definitionpub struct Sleigh<S: State> { state: PhantomData<S>,}impl<S: State> Sleigh<S> { pub fn status(&self) -> &str { S::STATUS }}impl Sleigh<Empty> { pub fn new() -> Self { Sleigh { state: PhantomData } } pub fn load(self) -> Sleigh<Ready> { Sleigh { state: PhantomData } }}impl Sleigh<Ready> { pub fn take_off(self) -> Sleigh<Flying> { Sleigh { state: PhantomData } } pub fn unload(self) -> Sleigh<Empty> { Sleigh { state: PhantomData } }}impl Sleigh<Flying> { pub fn land(self) -> Sleigh<Ready> { Sleigh { state: PhantomData } }}
use std::marker::PhantomData;pub struct Empty;pub struct Ready;pub struct Flying;// TODO: Define the `status` method for all statespub trait State { fn status() -> &'static str;}impl State for Empty { fn status() -> &'static str { "Empty" }}impl State for Ready { fn status() -> &'static str { "Ready" }}impl State for Flying { fn status() -> &'static str { "Flying" }}impl<T: State> Sleigh<T> { pub fn status(&self) -> &'static str { T::status() }}pub struct Sleigh<T: State> { // This is only public for testing purposes // In real-world scenarios, this should be private pub state: PhantomData<T>,}impl Sleigh<Empty> { pub fn new() -> Self { Self { state: PhantomData } } pub fn load(self) -> Sleigh<Ready> { Sleigh { state: PhantomData } }}impl Sleigh<Ready> { pub fn take_off(self) -> Sleigh<Flying> { Sleigh { state: PhantomData } } pub fn unload(self) -> Sleigh<Empty> { Sleigh { state: PhantomData } }}impl Sleigh<Flying> { pub fn land(self) -> Sleigh<Ready> { Sleigh { state: PhantomData } }}
use std::marker::PhantomData;pub struct Empty;pub struct Ready;pub struct Flying;// TODO: Define the `status` method for all statespub trait State { fn status() -> &'static str;}impl State for Empty { fn status() -> &'static str { "Empty" }}impl State for Ready { fn status() -> &'static str { "Ready" }}impl State for Flying { fn status() -> &'static str { "Flying" }}impl<T: State> Sleigh<T> { pub fn status(&self) -> &'static str { T::status() }}pub struct Sleigh<T: State> { // This is only public for testing purposes // In real-world scenarios, this should be private pub state: PhantomData<T>,}impl Sleigh<Empty> { pub fn new() -> Self { Self { state: PhantomData } } pub fn load(self) -> Sleigh<Ready> { Sleigh { state: PhantomData } }}impl Sleigh<Ready> { pub fn take_off(self) -> Sleigh<Flying> { Sleigh { state: PhantomData } } pub fn unload(self) -> Sleigh<Empty> { Sleigh { state: PhantomData } }}impl Sleigh<Flying> { pub fn land(self) -> Sleigh<Ready> { Sleigh { state: PhantomData } }}
pub struct Empty;pub struct Ready;pub struct Flying;// States should know their own namespub trait NamedState { // fn get_name(&self) -> String; // Better (after checking the hints) - NamedState structs don't need to be instantiated to report their names fn get_name() -> String;}impl NamedState for Empty { // fn get_name(&self) -> String { fn get_name() -> String { "Empty".to_string() }}impl NamedState for Ready { // fn get_name(&self) -> String { fn get_name() -> String { "Ready".to_string() }}impl NamedState for Flying { // fn get_name(&self) -> String { fn get_name() -> String { "Flying".to_string() }}pub struct Sleigh<T: NamedState> { // This is only public for testing purposes // In real-world scenarios, this should be private pub state: T,}// If we want to restrict T to a subset of types, can group those types using a trait// There's no way to do <T: Empty || Ready || Flying>impl<T: NamedState> Sleigh<T>{ pub fn status(&self) -> String { // self.state.get_name() T::get_name() }}impl Sleigh<Empty> { pub fn new() -> Self { Self { state: Empty } } pub fn load(self) -> Sleigh<Ready> { Sleigh { state: Ready } }}impl Sleigh<Ready> { pub fn take_off(self) -> Sleigh<Flying> { Sleigh { state: Flying } } pub fn unload(self) -> Sleigh<Empty> { Sleigh { state: Empty } }}impl Sleigh<Flying> { pub fn land(self) -> Sleigh<Ready> { Sleigh { state: Ready } }}
use std::marker::PhantomData;pub struct Empty;pub struct Ready;pub struct Flying;// TODO: Define the `status` method for all statespub trait State { fn status() -> &'static str;}pub struct Sleigh<T: State> { // This is only public for testing purposes // In real-world scenarios, this should be private pub state: PhantomData<T>,}impl Sleigh<Empty> { pub fn new() -> Self { Self { state: PhantomData } } pub fn load(self) -> Sleigh<Ready> { Sleigh { state: PhantomData } }}impl<T: State> Sleigh<T> { pub fn status(&self) -> &'static str { T::status() }}impl State for Empty { fn status() -> &'static str { "Empty" }}impl Sleigh<Ready> { pub fn take_off(self) -> Sleigh<Flying> { Sleigh { state: PhantomData } } pub fn unload(self) -> Sleigh<Empty> { Sleigh { state: PhantomData } }}impl State for Ready { fn status() -> &'static str { "Ready" }}impl Sleigh<Flying> { pub fn land(self) -> Sleigh<Ready> { Sleigh { state: PhantomData } }}impl State for Flying { fn status() -> &'static str { "Flying" }}
use std::marker::PhantomData;pub struct Empty;pub struct Ready;pub struct Flying;// TODO: Define the `status` method for all statespub trait State { fn status() -> &'static str;}impl<T: State> Sleigh<T> { pub fn status(&self) -> &'static str { T::status() }}impl State for Empty { fn status() -> &'static str { "Empty" }}impl State for Ready { fn status() -> &'static str { "Ready" }}impl State for Flying { fn status() -> &'static str { "Flying" }}pub struct Sleigh<T: State> { // This is only public for testing purposes // In real-world scenarios, this should be private pub state: PhantomData<T>,}impl Sleigh<Empty> { pub fn new() -> Self { Self { state: PhantomData } } pub fn load(self) -> Sleigh<Ready> { Sleigh { state: PhantomData } }}impl Sleigh<Ready> { pub fn take_off(self) -> Sleigh<Flying> { Sleigh { state: PhantomData } } pub fn unload(self) -> Sleigh<Empty> { Sleigh { state: PhantomData } }}impl Sleigh<Flying> { pub fn land(self) -> Sleigh<Ready> { Sleigh { state: PhantomData } }}
use std::marker::PhantomData;pub struct Empty;pub struct Ready;pub struct Flying;pub trait State { fn status() -> &'static str;}impl State for Empty { fn status() -> &'static str { "Empty" }}impl State for Ready { fn status() -> &'static str { "Ready" }}impl State for Flying { fn status() -> &'static str { "Flying" }}// TODO: Define the `status` method for all statespub struct Sleigh<T: State> { // This is only public for testing purposes // In real-world scenarios, this should be private pub state: PhantomData<T>,}impl <T: State> Sleigh<T> { pub fn status(&self) -> &'static str { T::status() }}impl Sleigh<Empty> { pub fn new() -> Self { Self { state: PhantomData } } pub fn load(self) -> Sleigh<Ready> { Sleigh { state: PhantomData } }}impl Sleigh<Ready> { pub fn take_off(self) -> Sleigh<Flying> { Sleigh { state: PhantomData } } pub fn unload(self) -> Sleigh<Empty> { Sleigh { state: PhantomData } }}impl Sleigh<Flying> { pub fn land(self) -> Sleigh<Ready> { Sleigh { state: PhantomData } }}
use std::marker::PhantomData;pub struct Empty;pub struct Ready;pub struct Flying;// TODO: Define the `status` method for all states//pub trait State { fn status() -> &'static str;}impl State for Empty { fn status() -> &'static str { "Empty" }}impl State for Ready { fn status() -> &'static str { "Ready" }}impl State for Flying { fn status() -> &'static str { "Flying" }}pub struct Sleigh<T: State> { // This is only public for testing purposes // In real-world scenarios, this should be private pub state: PhantomData<T>,}impl<T: State> Sleigh<T> { pub fn status(&self) -> &'static str { T::status() }}impl Sleigh<Empty> { pub fn new() -> Self { Self { state: PhantomData } } pub fn load(self) -> Sleigh<Ready> { Sleigh { state: PhantomData } }}impl Sleigh<Ready> { pub fn take_off(self) -> Sleigh<Flying> { Sleigh { state: PhantomData } } pub fn unload(self) -> Sleigh<Empty> { Sleigh { state: PhantomData } }}impl Sleigh<Flying> { pub fn land(self) -> Sleigh<Ready> { Sleigh { state: PhantomData } }}
use std::marker::PhantomData;pub struct Empty;pub struct Ready;pub struct Flying;// TODO: Define the `status` method for all states//pub trait State { fn status() -> &'static str;}impl State for Empty { fn status() -> &'static str { "Empty" }}impl State for Ready { fn status() -> &'static str { "Ready" }}impl State for Flying { fn status() -> &'static str { "Flying" }}pub struct Sleigh<T: State> { // This is only public for testing purposes // In real-world scenarios, this should be private pub state: PhantomData<T>,}impl<T: State> Sleigh<T> { pub fn status(&self) -> &'static str { T::status() }}impl Sleigh<Empty> { pub fn new() -> Self { Self { state: PhantomData } } pub fn load(self) -> Sleigh<Ready> { Sleigh { state: PhantomData } }}impl Sleigh<Ready> { pub fn take_off(self) -> Sleigh<Flying> { Sleigh { state: PhantomData } } pub fn unload(self) -> Sleigh<Empty> { Sleigh { state: PhantomData } }}impl Sleigh<Flying> { pub fn land(self) -> Sleigh<Ready> { Sleigh { state: PhantomData } }}
use std::marker::PhantomData;pub struct Empty;pub struct Ready;pub struct Flying;// TODO: Define the `status` method for all statespub trait State { fn status() -> &'static str;}impl State for Empty { fn status() -> &'static str { "Empty" }}impl State for Ready { fn status() -> &'static str { "Ready" }}impl State for Flying { fn status() -> &'static str { "Flying" }}pub struct Sleigh<T: State> { // This is only public for testing purposes // In real-world scenarios, this should be private pub state: PhantomData<T>,}impl<T: State> Sleigh<T> { pub fn status(&self) -> &'static str { T::status() }}impl Sleigh<Empty> { pub fn new() -> Self { Self { state: PhantomData } } pub fn load(self) -> Sleigh<Ready> { Sleigh { state: PhantomData } }}impl Sleigh<Ready> { pub fn take_off(self) -> Sleigh<Flying> { Sleigh { state: PhantomData } } pub fn unload(self) -> Sleigh<Empty> { Sleigh { state: PhantomData } }}impl Sleigh<Flying> { pub fn land(self) -> Sleigh<Ready> { Sleigh { state: PhantomData } }}
use std::marker::PhantomData;pub struct Empty;pub struct Ready;pub struct Flying;// TODO: Define the `status` method for all statespub struct Sleigh<T: State> { // This is only public for testing purposes // In real-world scenarios, this should be private pub state: PhantomData<T>,}pub trait State { fn status() -> &'static str;}impl State for Empty { fn status() -> &'static str { "Empty" }}impl State for Ready { fn status() -> &'static str { "Ready" }}impl State for Flying { fn status() -> &'static str { "Flying" }}impl<T: State> Sleigh<T> { pub fn status(&self) -> &'static str { T::status() }}impl Sleigh<Empty> { pub fn new() -> Self { Self { state: PhantomData } } pub fn load(self) -> Sleigh<Ready> { Sleigh { state: PhantomData } }}impl Sleigh<Ready> { pub fn take_off(self) -> Sleigh<Flying> { Sleigh { state: PhantomData } } pub fn unload(self) -> Sleigh<Empty> { Sleigh { state: PhantomData } }}impl Sleigh<Flying> { pub fn land(self) -> Sleigh<Ready> { Sleigh { state: PhantomData } }}
use std::marker::PhantomData;pub struct Empty;pub struct Ready;pub struct Flying;// TODO: Define the `status` method for all statespub trait State { fn status() -> &'static str;}impl State for Empty { fn status() -> &'static str { "Empty" }} impl State for Ready { fn status() -> &'static str { "Ready" }} impl State for Flying { fn status() -> &'static str { "Flying" }} impl<T: State> Sleigh<T> { pub fn status(&self) -> &'static str { T::status() }}pub struct Sleigh<T: State> { // This is only public for testing purposes // In real-world scenarios, this should be private pub state: PhantomData<T>,}impl Sleigh<Empty> { pub fn new() -> Self { Self { state: PhantomData } } pub fn load(self) -> Sleigh<Ready> { Sleigh { state: PhantomData } }}impl Sleigh<Ready> { pub fn take_off(self) -> Sleigh<Flying> { Sleigh { state: PhantomData } } pub fn unload(self) -> Sleigh<Empty> { Sleigh { state: PhantomData } }}impl Sleigh<Flying> { pub fn land(self) -> Sleigh<Ready> { Sleigh { state: PhantomData } }}
use std::marker::PhantomData;pub struct Empty;pub struct Ready;pub struct Flying;// TODO: Define the `status` method for all statespub trait SleighStatus { fn status() -> &'static str;}impl SleighStatus for Empty { fn status() -> &'static str { "Empty" }}impl SleighStatus for Ready { fn status() -> &'static str { "Ready" }}impl SleighStatus for Flying { fn status() -> &'static str { "Flying" }}pub struct Sleigh<T: SleighStatus> { // This is only public for testing purposes // In real-world scenarios, this should be private pub state: PhantomData<T>,}impl<T: SleighStatus> Sleigh<T> { pub fn status(&self) -> &'static str { T::status() }}impl Sleigh<Empty> { pub fn new() -> Self { Self { state: PhantomData } } pub fn load(self) -> Sleigh<Ready> { Sleigh { state: PhantomData } }}impl Default for Sleigh<Empty> { fn default() -> Self { Self::new() }}impl Sleigh<Ready> { pub fn take_off(self) -> Sleigh<Flying> { Sleigh { state: PhantomData } } pub fn unload(self) -> Sleigh<Empty> { Sleigh { state: PhantomData } }}impl Sleigh<Flying> { pub fn land(self) -> Sleigh<Ready> { Sleigh { state: PhantomData } }}
use std::marker::PhantomData;pub struct Empty;pub struct Ready;pub struct Flying;pub trait State { fn status() -> &'static str;}impl State for Empty { fn status() -> &'static str { "Empty" }}impl State for Ready { fn status() -> &'static str { "Ready" }}impl State for Flying { fn status() -> &'static str { "Flying" }}// TODO: Define the `status` method for all statespub struct Sleigh<T: State> { // This is only public for testing purposes // In real-world scenarios, this should be private pub state: PhantomData<T>,}impl Sleigh<Empty> { pub fn new() -> Self { Self { state: PhantomData } } pub fn load(self) -> Sleigh<Ready> { Sleigh { state: PhantomData } }}impl Sleigh<Ready> { pub fn take_off(self) -> Sleigh<Flying> { Sleigh { state: PhantomData } } pub fn unload(self) -> Sleigh<Empty> { Sleigh { state: PhantomData } }}impl Sleigh<Flying> { pub fn land(self) -> Sleigh<Ready> { Sleigh { state: PhantomData } }}impl<T: State> Sleigh<T> { pub fn status(&self) -> &'static str { T::status() }}
use std::marker::PhantomData;pub struct Empty;pub struct Ready;pub struct Flying;// TODO: Define the `status` method for all statespub trait State { fn status() -> &'static str;}impl State for Empty { fn status() -> &'static str { "Empty" }}impl State for Ready { fn status() -> &'static str { "Ready" }}impl State for Flying { fn status() -> &'static str { "Flying" }}pub struct Sleigh<T: State> { // This is only public for testing purposes // In real-world scenarios, this should be private pub state: PhantomData<T>,}impl Sleigh<Empty> { pub fn new() -> Self { Self { state: PhantomData } } pub fn load(self) -> Sleigh<Ready> { Sleigh { state: PhantomData } }}impl Sleigh<Ready> { pub fn take_off(self) -> Sleigh<Flying> { Sleigh { state: PhantomData } } pub fn unload(self) -> Sleigh<Empty> { Sleigh { state: PhantomData } }}impl Sleigh<Flying> { pub fn land(self) -> Sleigh<Ready> { Sleigh { state: PhantomData } }}impl<T:State> Sleigh<T> { pub fn status(&self) -> &'static str { T::status() }}
use std::marker::PhantomData;pub struct Empty;pub struct Ready;pub struct Flying;// TODO: Define the `status` method for all statespub trait State { fn status() -> &'static str;}impl State for Empty { fn status() -> &'static str { "Empty" }}impl State for Ready { fn status() -> &'static str { "Ready" }}impl State for Flying { fn status() -> &'static str { "Flying" }}pub struct Sleigh<T> where T: State{ // This is only public for testing purposes // In real-world scenarios, this should be private pub state: PhantomData<T>,}impl <T: State> Sleigh<T> { pub fn status(&self) -> &'static str { T::status() }}impl Sleigh<Empty> { pub fn new() -> Self { Self { state: PhantomData } } pub fn load(self) -> Sleigh<Ready> { Sleigh { state: PhantomData } }}impl Sleigh<Ready> { pub fn take_off(self) -> Sleigh<Flying> { Sleigh { state: PhantomData } } pub fn unload(self) -> Sleigh<Empty> { Sleigh { state: PhantomData } }}impl Sleigh<Flying> { pub fn land(self) -> Sleigh<Ready> { Sleigh { state: PhantomData } }}
use std::marker::PhantomData;pub struct Empty;pub struct Ready;pub struct Flying;pub trait State { fn status() -> &'static str;}impl State for Empty { fn status() -> &'static str { "Empty" }}impl State for Ready { fn status() -> &'static str { "Ready" }}impl State for Flying { fn status() -> &'static str { "Flying" }}pub struct Sleigh<T>where T: State,{ pub state: PhantomData<T>,}impl<T: State> Sleigh<T> { pub fn status(&self) -> &'static str { T::status() }}impl Sleigh<Empty> { pub fn new() -> Sleigh<Empty> { Sleigh { state: PhantomData } } pub fn load(self) -> Sleigh<Ready> { Sleigh { state: PhantomData } }}impl Sleigh<Ready> { pub fn take_off(self) -> Sleigh<Flying> { Sleigh { state: PhantomData } } pub fn unload(self) -> Sleigh<Empty> { Sleigh { state: PhantomData } }}impl Sleigh<Flying> { pub fn land(self) -> Sleigh<Ready> { Sleigh { state: PhantomData } }}
use std::marker::PhantomData;pub struct Empty;pub struct Ready;pub struct Flying;// TODO: Define the `status` method for all statespub trait State { fn status() -> &'static str;}impl State for Empty { fn status() -> &'static str { "Empty" }}impl State for Ready { fn status() -> &'static str { "Ready" }}impl State for Flying { fn status() -> &'static str { "Flying" }}pub struct Sleigh<T>where T: State { // This is only public for testing purposes // In real-world scenarios, this should be private pub state: PhantomData<T>,}impl<T: State> Sleigh<T> { pub fn status(&self) -> &'static str { T::status() }}impl Sleigh<Empty> { pub fn new() -> Self { Self { state: PhantomData } } pub fn load(self) -> Sleigh<Ready> { Sleigh { state: PhantomData } }}impl Sleigh<Ready> { pub fn take_off(self) -> Sleigh<Flying> { Sleigh { state: PhantomData } } pub fn unload(self) -> Sleigh<Empty> { Sleigh { state: PhantomData } }}impl Sleigh<Flying> { pub fn land(self) -> Sleigh<Ready> { Sleigh { state: PhantomData } }}
use std::marker::PhantomData;pub struct Empty;pub struct Ready;pub struct Flying;pub trait Status{ fn name()->&'static str;}impl Status for Empty{ fn name()->&'static str{"Empty"}}impl Status for Ready{ fn name()->&'static str{"Ready"}}impl Status for Flying{ fn name()->&'static str{"Flying"}}// TODO: Define the `status` method for all statespub struct Sleigh<T:Status> { // This is only public for testing purposes // In real-world scenarios, this should be private pub state: PhantomData<T>,}impl<T:Status> Sleigh<T>{ pub fn status(&self) -> &'static str{ T::name() }}impl Sleigh<Empty> { pub fn new() -> Self { Self { state: PhantomData } } pub fn load(self) -> Sleigh<Ready> { Sleigh { state: PhantomData } }}impl Sleigh<Ready> { pub fn take_off(self) -> Sleigh<Flying> { Sleigh { state: PhantomData } } pub fn unload(self) -> Sleigh<Empty> { Sleigh { state: PhantomData } }}impl Sleigh<Flying> { pub fn land(self) -> Sleigh<Ready> { Sleigh { state: PhantomData } }}
use std::marker::PhantomData;pub struct Empty;pub struct Ready;pub struct Flying;pub trait State { fn status() -> &'static str;}impl State for Empty { fn status() -> &'static str { "Empty" }}impl State for Ready { fn status() -> &'static str { "Ready" }}impl State for Flying { fn status() -> &'static str { "Flying" }}#[derive(Debug)]pub struct Sleigh<T: State> { pub state: PhantomData<T>,}impl Sleigh<Empty> { pub fn new() -> Self { Self { state: PhantomData } } pub fn load(self) -> Sleigh<Ready> { Sleigh { state: PhantomData } }}impl Sleigh<Ready> { pub fn take_off(self) -> Sleigh<Flying> { Sleigh { state: PhantomData } } pub fn unload(self) -> Sleigh<Empty> { Sleigh { state: PhantomData } }}impl Sleigh<Flying> { pub fn land(self) -> Sleigh<Ready> { Sleigh { state: PhantomData } }}impl <T: State> Sleigh<T> { pub fn status(&self) -> &'static str { T::status() }}
use std::marker::PhantomData;pub struct Empty;pub struct Ready;pub struct Flying;pub trait State { fn status() -> &'static str;}impl State for Empty { fn status() -> &'static str { "Empty" }}impl State for Ready { fn status() -> &'static str { "Ready" }}impl State for Flying { fn status() -> &'static str { "Flying" }}#[derive(Debug)]pub struct Sleigh<T: State> { pub state: PhantomData<T>,}impl Sleigh<Empty> { pub fn new() -> Self { Self { state: PhantomData } } pub fn load(self) -> Sleigh<Ready> { Sleigh { state: PhantomData } }}impl Sleigh<Ready> { pub fn take_off(self) -> Sleigh<Flying> { Sleigh { state: PhantomData } } pub fn unload(self) -> Sleigh<Empty> { Sleigh { state: PhantomData } }}impl Sleigh<Flying> { pub fn land(self) -> Sleigh<Ready> { Sleigh { state: PhantomData } }}impl <T: State> Sleigh<T> { pub fn status(&self) -> &'static str { T::status() }}
use std::marker::PhantomData;pub struct Empty;pub struct Ready;pub struct Flying;// TODO: Define the `status` method for all statespub struct Sleigh<T>whereT: State{ // This is only public for testing purposes // In real-world scenarios, this should be private pub state: PhantomData<T>,}impl<T> Sleigh<T>where T: State,{ pub fn status(&self) -> &'static str { T::status() }}pub trait State { fn status() -> &'static str;}impl Sleigh<Empty> { pub fn new() -> Self { Self { state: PhantomData } } pub fn load(self) -> Sleigh<Ready> { Sleigh { state: PhantomData } }}impl State for Empty { fn status() -> &'static str { "Empty" }}impl Sleigh<Ready> { pub fn take_off(self) -> Sleigh<Flying> { Sleigh { state: PhantomData } } pub fn unload(self) -> Sleigh<Empty> { Sleigh { state: PhantomData } }}impl State for Ready { fn status() -> &'static str { "Ready" }}impl Sleigh<Flying> { pub fn land(self) -> Sleigh<Ready> { Sleigh { state: PhantomData } }}impl State for Flying { fn status() -> &'static str { "Flying" }}
use std::marker::PhantomData;pub struct Empty;pub struct Ready;pub struct Flying;pub struct Sleigh<T:SleighState> { // This is only public for testing purposes // In real-world scenarios, this should be private pub state: PhantomData<T>,}impl Sleigh<Empty> { pub fn new() -> Self { Self { state: PhantomData } } pub fn load(self) -> Sleigh<Ready> { Sleigh { state: PhantomData } }}impl Sleigh<Ready> { pub fn take_off(self) -> Sleigh<Flying> { Sleigh { state: PhantomData } } pub fn unload(self) -> Sleigh<Empty> { Sleigh { state: PhantomData } }}impl Sleigh<Flying> { pub fn land(self) -> Sleigh<Ready> { Sleigh { state: PhantomData } }}pub trait SleighState { fn status() -> &'static str;}impl SleighState for Empty { fn status() -> &'static str { "Empty" }}impl SleighState for Ready { fn status() -> &'static str { "Ready" }}impl SleighState for Flying { fn status() -> &'static str { "Flying" }}impl<T: SleighState> Sleigh<T> { pub fn status(&self) -> &'static str { T::status() }}
use std::marker::PhantomData;pub struct Empty;pub struct Ready;pub struct Flying;// TODO: Define the `status` method for all statespub struct Sleigh<T: SleighState> { // This is only public for testing purposes // In real-world scenarios, this should be private pub state: PhantomData<T>,}impl Sleigh<Empty> { pub fn new() -> Self { Self { state: PhantomData } } pub fn load(self) -> Sleigh<Ready> { Sleigh { state: PhantomData } }}impl Sleigh<Ready> { pub fn take_off(self) -> Sleigh<Flying> { Sleigh { state: PhantomData } } pub fn unload(self) -> Sleigh<Empty> { Sleigh { state: PhantomData } }}impl Sleigh<Flying> { pub fn land(self) -> Sleigh<Ready> { Sleigh { state: PhantomData } }}pub trait SleighState { fn status() -> &'static str;}impl SleighState for Empty { fn status() -> &'static str { "Empty" }}impl SleighState for Flying { fn status() -> &'static str { "Flying" }}impl SleighState for Ready { fn status() -> &'static str { "Ready" }}impl<T: SleighState> Sleigh<T> { pub fn status(&self) -> &'static str { T::status() }}
use std::marker::PhantomData;pub struct Empty;pub struct Ready;pub struct Flying;// TODO: Define the `status` method for all statespub struct Sleigh<T: State> { // This is only public for testing purposes // In real-world scenarios, this should be private pub state: PhantomData<T>,}pub trait State { fn status() -> &'static str;}impl State for Empty { fn status() -> &'static str { "Empty" }}impl State for Ready { fn status() -> &'static str { "Ready" }}impl State for Flying { fn status() -> &'static str { "Flying" }}impl<T: State> Sleigh<T> { pub fn status(&self) -> &'static str { T::status() }}impl Sleigh<Empty> { pub fn new() -> Self { Self { state: PhantomData } } pub fn load(self) -> Sleigh<Ready> { Sleigh { state: PhantomData } }}impl Sleigh<Ready> { pub fn take_off(self) -> Sleigh<Flying> { Sleigh { state: PhantomData } } pub fn unload(self) -> Sleigh<Empty> { Sleigh { state: PhantomData } }}impl Sleigh<Flying> { pub fn land(self) -> Sleigh<Ready> { Sleigh { state: PhantomData } }}
use std::marker::PhantomData;pub struct Empty;pub struct Ready;pub struct Flying;// TODO: Define the `status` method for all statespub trait State { fn status() -> &'static str;}impl State for Empty { fn status() -> &'static str { "Empty" }}impl State for Ready { fn status() -> &'static str { "Ready" }}impl State for Flying { fn status() -> &'static str { "Flying" }}pub struct Sleigh<T: State> { // This is only public for testing purposes // In real-world scenarios, this should be private pub state: PhantomData<T>,}impl<T: State> Sleigh<T> { pub fn status (&self) -> &'static str { T::status() } }impl Sleigh<Empty> { pub fn new() -> Self { Self { state: PhantomData } } pub fn load(self) -> Sleigh<Ready> { Sleigh { state: PhantomData } }}impl Sleigh<Ready> { pub fn take_off(self) -> Sleigh<Flying> { Sleigh { state: PhantomData } } pub fn unload(self) -> Sleigh<Empty> { Sleigh { state: PhantomData } }}impl Sleigh<Flying> { pub fn land(self) -> Sleigh<Ready> { Sleigh { state: PhantomData } }}
use std::marker::PhantomData;pub struct Empty;impl State for Empty { fn status() -> &'static str { "Empty" }}pub struct Ready;impl State for Ready { fn status() -> &'static str { "Ready" }}pub struct Flying;impl State for Flying { fn status() -> &'static str { "Flying" }}// TODO: Define the `status` method for all statespub trait State { fn status() -> &'static str;}impl<T: State> Sleigh<T> { pub fn status(&self) -> &'static str { T::status() }}pub struct Sleigh<T: State> { // This is only public for testing purposes // In real-world scenarios, this should be private pub state: PhantomData<T>,}impl Sleigh<Empty> { pub fn new() -> Self { Self { state: PhantomData } } pub fn load(self) -> Sleigh<Ready> { Sleigh { state: PhantomData } }}impl Sleigh<Ready> { pub fn take_off(self) -> Sleigh<Flying> { Sleigh { state: PhantomData } } pub fn unload(self) -> Sleigh<Empty> { Sleigh { state: PhantomData } }}impl Sleigh<Flying> { pub fn land(self) -> Sleigh<Ready> { Sleigh { state: PhantomData } }}
use std::marker::PhantomData;pub struct Empty;pub struct Ready;pub struct Flying;// TODO: Define the `status` method for all statespub struct Sleigh<T: State> { // This is only public for testing purposes // In real-world scenarios, this should be private pub state: PhantomData<T>,}impl<T: State> Sleigh<T> { pub fn status(&self) -> &'static str { T::status() }}impl Sleigh<Empty> { pub fn new() -> Self { Self { state: PhantomData } } pub fn load(self) -> Sleigh<Ready> { Sleigh { state: PhantomData } }}impl Sleigh<Ready> { pub fn take_off(self) -> Sleigh<Flying> { Sleigh { state: PhantomData } } pub fn unload(self) -> Sleigh<Empty> { Sleigh { state: PhantomData } }}impl Sleigh<Flying> { pub fn land(self) -> Sleigh<Ready> { Sleigh { state: PhantomData } }}pub trait State { fn status() -> &'static str;}impl State for Empty { fn status() -> &'static str { "Empty" }}impl State for Ready { fn status() -> &'static str { "Ready" }}impl State for Flying { fn status() -> &'static str { "Flying" }}
use std::marker::PhantomData;pub struct Empty;pub struct Ready;pub struct Flying;// TODO: Define the `status` method for all statespub trait State { fn status() -> &'static str;}pub struct Sleigh<T: State> { // This is only public for testing purposes // In real-world scenarios, this should be private pub state: PhantomData<T>,}impl Sleigh<Empty> { pub fn new() -> Self { Self { state: PhantomData } } pub fn load(self) -> Sleigh<Ready> { Sleigh { state: PhantomData } }}impl Sleigh<Ready> { pub fn take_off(self) -> Sleigh<Flying> { Sleigh { state: PhantomData } } pub fn unload(self) -> Sleigh<Empty> { Sleigh { state: PhantomData } }}impl Sleigh<Flying> { pub fn land(self) -> Sleigh<Ready> { Sleigh { state: PhantomData } }}impl State for Empty { fn status() -> &'static str { "Empty" }}impl State for Ready { fn status() -> &'static str { "Ready" }}impl State for Flying { fn status() -> &'static str { "Flying" }}impl<T: State> Sleigh<T> { pub fn status(&self) -> &'static str { T::status() }}
use std::marker::PhantomData;pub struct Empty;pub struct Ready;pub struct Flying;// TODO: Define the `status` method for all statespub trait Status { fn status(&self) -> String ;}impl Status for Sleigh<Empty> { fn status(&self) -> String { "Empty".to_string() }}impl Status for Sleigh<Ready> { fn status(&self) -> String { "Ready".to_string() }}impl Status for Sleigh<Flying> { fn status(&self) -> String { "Flying".to_string() }}pub struct Sleigh<T> where Sleigh<T>: Status{ // This is only public for testing purposes // In real-world scenarios, this should be private pub state: PhantomData<T>,}impl Sleigh<Empty> { pub fn new() -> Self { Self { state: PhantomData } } pub fn load(self) -> Sleigh<Ready> { Sleigh { state: PhantomData } }}impl Sleigh<Ready> { pub fn take_off(self) -> Sleigh<Flying> { Sleigh { state: PhantomData } } pub fn unload(self) -> Sleigh<Empty> { Sleigh { state: PhantomData } }}impl Sleigh<Flying> { pub fn land(self) -> Sleigh<Ready> { Sleigh { state: PhantomData } }}
use std::marker::PhantomData;pub struct Empty;pub struct Ready;pub struct Flying;pub trait State { fn status() -> &'static str;}pub struct Sleigh<T: State> { pub state: PhantomData<T>,}impl<T: State> Sleigh<T> { pub fn status(&self) -> &'static str { T::status() }}impl State for Empty { fn status() -> &'static str { "Empty" }}impl Sleigh<Empty> { pub fn new() -> Self { Self { state: PhantomData } } pub fn load(self) -> Sleigh<Ready> { Sleigh { state: PhantomData } }}impl State for Ready { fn status() -> &'static str { "Ready" }}impl Sleigh<Ready> { pub fn take_off(self) -> Sleigh<Flying> { Sleigh { state: PhantomData } } pub fn unload(self) -> Sleigh<Empty> { Sleigh { state: PhantomData } }}impl State for Flying { fn status() -> &'static str { "Flying" }}impl Sleigh<Flying> { pub fn land(self) -> Sleigh<Ready> { Sleigh { state: PhantomData } }}
use std::marker::PhantomData;pub struct Empty;pub struct Ready;pub struct Flying;// TODO: Define the `status` method for all statespub trait State { fn status() -> &'static str;}pub struct Sleigh<T: State> { // This is only public for testing purposes // In real-world scenarios, this should be private pub state: PhantomData<T>,}impl Sleigh<Empty> { pub fn new() -> Self { Self { state: PhantomData } } pub fn load(self) -> Sleigh<Ready> { Sleigh { state: PhantomData } }}impl State for Empty { fn status() -> &'static str { "Empty" }}impl Sleigh<Ready> { pub fn take_off(self) -> Sleigh<Flying> { Sleigh { state: PhantomData } } pub fn unload(self) -> Sleigh<Empty> { Sleigh { state: PhantomData } }}impl State for Ready { fn status() -> &'static str { "Ready" }}impl Sleigh<Flying> { pub fn land(self) -> Sleigh<Ready> { Sleigh { state: PhantomData } }}impl<T: State> Sleigh<T> { pub fn status(&self) -> &'static str { T::status() }}impl State for Flying { fn status() -> &'static str { "Flying" }}