“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:
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:
Implement the trait for each state. e.g.
Implement a method for all types that implement the State
trait. e.g.
josephcopenhaver
use std::marker::PhantomData;pub trait State { fn status() -> &'static str;}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 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 } }}
Ljungg
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" }}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 } }}
A1-exe
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 } }}
thescooby
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 } }}
jayber
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 } }}
rjensen
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 } }}
FlorianGD
use std::marker::PhantomData;pub struct Empty;pub struct Ready;pub struct Flying;pub trait Statusable { fn status() -> &'static str;}impl Statusable for Empty { fn status() -> &'static str { "Empty" }}impl Statusable for Ready { fn status() -> &'static str { "Ready" }}impl Statusable for Flying { fn status() -> &'static str { "Flying" }}// TODO: Define the `status` method for all statesimpl<T: Statusable> Sleigh<T> { pub fn status(&self) -> &'static str { T::status() }}pub struct Sleigh<T: Statusable> { // 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 } }}
FlorianGD
use std::marker::PhantomData;pub struct Empty;pub struct Ready;pub struct Flying;pub trait Statusable { fn status() -> &'static str;}impl Statusable for Empty { fn status() -> &'static str { "Empty" }}impl Statusable for Ready { fn status() -> &'static str { "Ready" }}impl Statusable for Flying { fn status() -> &'static str { "Flying" }}// TODO: Define the `status` method for all statespub struct Sleigh<T: Statusable> { // 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 { Empty::status().to_string() }}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 { Ready::status().to_string() }}impl Sleigh<Flying> { pub fn land(self) -> Sleigh<Ready> { Sleigh { state: PhantomData } } pub fn status(&self) -> String { Flying::status().to_string() }}
mei28
use std::marker::PhantomData;pub struct Empty;pub struct Ready;pub struct Flying;// TODO: Define the `status` method for all statespub struct Sleigh<T:Stateful> { // This is only public for testing purposes // In real-world scenarios, this should be private pub state: PhantomData<T>,}pub trait Stateful { fn status() -> &'static str;}impl Stateful for Empty { fn status() -> &'static str { "Empty" }}impl Stateful for Ready { fn status() -> &'static str { "Ready" }}impl Stateful for Flying { fn status() -> &'static str { "Flying" }}impl<T: Stateful> 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 } }}
mei28
use std::marker::PhantomData;pub struct Empty;pub struct Ready;pub struct Flying;// TODO: Define the `status` method for all statespub struct Sleigh<T:Stateful> { // This is only public for testing purposes // In real-world scenarios, this should be private pub state: PhantomData<T>,}pub trait Stateful { fn status() -> &'static str;}impl Stateful for Empty { fn status() -> &'static str { "Empty" }}impl Stateful for Ready { fn status() -> &'static str { "Ready" }}impl Stateful for Flying { fn status() -> &'static str { "Flying" }}impl<T: Stateful> 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 } }}
sweet2honey
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 } }}
pagyeman
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 } }}
kapaseker
use std::marker::PhantomData;pub struct Empty;pub struct Ready;pub struct Flying;// TODO: Define the `status` method for all statespub struct Sleigh<T:Stateful> { // This is only public for testing purposes // In real-world scenarios, this should be private pub state: PhantomData<T>,}pub trait Stateful { fn status() -> &'static str;}impl Stateful for Empty { fn status() -> &'static str { "Empty" }}impl Stateful for Ready { fn status() -> &'static str { "Ready" }}impl Stateful for Flying { fn status() -> &'static str { "Flying" }}impl<T: Stateful> 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 } }}
sarub0b0
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 } }}
eguefif
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;}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 Status for Empty { fn status() -> &'static str{ "Empty" }}impl Status for Flying { fn status() -> &'static str{ "Flying" }}impl Sleigh<Ready> { pub fn take_off(self) -> Sleigh<Flying> { Sleigh { state: PhantomData } } pub fn unload(self) -> Sleigh<Empty> { Sleigh { state: PhantomData } }}impl Status for Ready { fn status() -> &'static str{ "Ready" }}impl Sleigh<Flying> { pub fn land(self) -> Sleigh<Ready> { Sleigh { state: PhantomData } }}
MGehrmann
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 } }}
aaron-otis
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 } }}
KushnerykPavel
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 } }}
habu1010
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 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 } }}
Fedott
use std::marker::PhantomData;pub struct Empty;pub struct Ready;pub struct Flying;// TODO: Define the `status` method for all statestrait Status { fn status() -> &'static str;}impl Status for Empty { fn status() -> &'static str { "Empty" }}impl Status for Ready { fn status() -> &'static str { "Ready" }}impl Status for Flying { fn status() -> &'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: 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 } }}
hafihaf123
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 } }}
titoeb
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 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<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" }}fn main() { println!("{}", Sleigh::new().status()); println!("{}", Sleigh::new().load().status()); println!("{}", Sleigh::new().load().take_off().status());}
CianciuStyles
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 } }}
Kyawkhaing444
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 } }}
gmvar
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 } }}
creativecoder
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) -> &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 } }}
strachan
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 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() }}
cloki0610
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 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() }}
wendko
use std::marker::PhantomData;// 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:// Define the status() method: It should be callable on the Sleigh regardless of its state.// Return a string indicating the sleigh’s 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.pub struct Empty;pub struct Ready;pub struct Flying;// trait to define shared behavior between structspub trait State { fn status() -> &'static str;}// impl trait for each struct (trait bounds)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 a Sleigh for all trait boundsimpl<T: State> Sleigh<T> { pub fn status(&self) -> &'static str { T::status() }}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 } }}
Ankit8848
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 } }}
DominicD
use std::marker::PhantomData;pub struct Empty;pub struct Ready;pub struct Flying;// TODO: Define the `status` method for all statespub trait Marker { fn status() -> &'static str;}//impl Marker for () {}impl Marker for Empty { fn status() -> &'static str { "Empty" }}impl Marker for Ready { fn status() -> &'static str { "Ready" }}impl Marker for Flying { fn status() -> &'static str { "Flying" }}pub struct Sleigh<T: Marker> { // This is only public for testing purposes // In real-world scenarios, this should be private pub state: PhantomData<T>,}impl<T: Marker> 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 } }}
arm01846
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 } }}
frankstolle
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 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 } }}
joanne-cmd
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 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 } }}
rony0000013
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 } }}
vaclav0411
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 } }}
wamimi
use std::marker::PhantomData;// Define the State traitpub trait State { fn status() -> &'static str;}pub struct Empty;pub struct Ready;pub struct Flying;// Implement State trait for each stateimpl 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" }}// Update Sleigh to require State traitpub struct Sleigh<T: State> { pub state: PhantomData<T>,}// Implement status method for all statesimpl<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 } }}
tamanishi
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 } }}
miscer
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: 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 } }}
Nismirno
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 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 } }}
wlabranche
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 } }}
joshvon44
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() }}
Burnus
use std::marker::PhantomData;pub struct Empty;pub struct Ready;pub struct Flying;pub trait Status { fn status() -> &'static str;}impl Status for Empty { fn status() -> &'static str { &"Empty" }}impl Status for Ready { fn status() -> &'static str { &"Ready" }}impl Status for Flying { fn status() -> &'static str { &"Flying" }}pub struct Sleigh<T: Status> { 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 } }}
KLcpb
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 } }}
kometen
use std::marker::PhantomData;pub struct Empty;pub struct Ready;pub struct Flying;pub trait State { fn status() -> &'static str;}// TODO: Define the `status` method for all statesimpl 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() }}
tonisk
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() -> String;}impl State for Empty { fn status() -> String { "Empty".into() }}impl State for Ready { fn status() -> String { "Ready".into() }}impl State for Flying { fn status() -> String { "Flying".into() }}impl<T: State> Sleigh<T> { pub fn status(&self) -> String { 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 } }}
chriswmann
use std::marker::PhantomData;pub struct Empty;pub struct Ready;pub struct Flying;// TODO: Define the `status` method for all statestrait Status { fn status() -> &'static str;}impl Status for Empty { fn status() -> &'static str { "Empty" }}impl Status for Ready { fn status() -> &'static str { "Ready" }}impl Status for Flying { fn status() -> &'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: 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 } }}
GermanS
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 } }}
lulingar
use std::marker::PhantomData;// 1. We have 3 states: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" }}// 2. Finish the Sleigh struct definitionpub struct Sleigh<S: State> { _state: PhantomData<S>,}impl<S: State> Sleigh<S> { fn transition() -> Sleigh<S> { Sleigh { _state: PhantomData, } } pub fn status(&self) -> &'static str { S::status() }}// 3. Write the Sleigh Implementations for all statesimpl Sleigh<Empty> { pub fn new() -> Sleigh<Empty> { Sleigh::transition() } pub fn load(self) -> Sleigh<Ready> { Sleigh::transition() }}impl Sleigh<Ready> { pub fn take_off(self) -> Sleigh<Flying> { Sleigh::transition() } pub fn unload(self) -> Sleigh<Empty> { Sleigh::transition() }}impl Sleigh<Flying> { pub fn land(self) -> Sleigh<Ready> { Sleigh::transition() }}
steveluscher
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: 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 } }}