“Last year, someone called take_off
mid-flight, and we ended up performing air donuts over Siberia. Never. Again.”
“It was kind of cool, though.” Prancer muttered.
Santa whirled. “Cool? COOL? I had to explain to a reindeer rescue hotline why Blitzen was tangled in GPS satellites! No more cowboy coding. We’re doing this with the type state pattern.”
Bernard hesitated. “Uh, what’s wrong with just adding a check for is_flying
?”
Santa's glare could have melted the North Pole. “Oh, brilliant idea, Bernard. Let’s just trust runtime checks! No. The sleigh will know its state: parked, flying, or landed. You call take_off
while flying? The compiler won’t even let you. No crashes, no ISS collisions, no lawsuits.”
Blitzen snorted. “Sounds over-engineered.”
"BLITZEN, do you want to be the first reindeer to test the emergency eject system? Get to work. I want this sleigh state-safe by tonight!"
We don't want to end up with a sleigh in the wrong state again and we certainly don't want the take_off
method to be available when the sleigh is already flying.
And Santa doesn't want any runtime checks, so it must all be checked at compile time.
Here's what you need to do:
Sleigh
struct to represent the state of the sleigh.Sleigh
struct must have 3 states Empty
, Ready
and Flying
.There are a few methods we want to define for each state:
new()
an associated function that creates a new Sleigh
in the Empty
state.load()
a method that transitions the Sleigh
from Empty
to Ready
.take_off()
a method that transitions the Sleigh
from Ready
to Flying
.unload()
a method that transitions the Sleigh
from Ready
to Empty
.land()
a method that transitions the Sleigh
from Flying
to Ready
.If you're stuck or need a starting point, here are some hints to help you along the way!
Create three structs for each state Empty
, Ready
and Flying
.
You can create a generic struct that represents the state of the sleigh. e.g.
Use PhantomData
to ensure that the state is used in the struct without consuming any memory.
Implement the methods for each state. e.g.
josephcopenhaver
use std::marker::PhantomData; // 1. We have 3 states:// - Empty// - Ready// - Flyingpub struct Empty;pub struct Ready;pub struct Flying;// 2. Finish the Sleigh struct definitionpub struct Sleigh<State> { _state: PhantomData<State>,}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 } }}// 3. Write the Sleigh Implementations for all states
Ljungg
use std::marker::PhantomData;pub struct Empty;pub struct Ready;pub struct Flying;// 2. Finish the Seligh struct definitionpub struct Sleigh<State> { _state: PhantomData<State>,}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;// 1. We have 3 states:// - Empty// - Ready// - Flyingpub struct Empty;pub struct Ready;pub struct Flying;// 2. Finish the Seligh struct definitionpub struct Sleigh<State> { _state: PhantomData<State>}// 3. Write the Sleigh Implementations for all statesimpl 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 } }}
A1-exe
use std::marker::PhantomData;// 1. We have 3 states:// - Empty// - Ready// - Flyingpub struct Empty;pub struct Ready;pub struct Flying;// 2. Finish the Seligh struct definitionpub struct Sleigh<State> { _state: PhantomData<State>}// 3. Write the Sleigh Implementations for all statesimpl 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;// 1. We have 3 states:// - Empty// - Ready// - Flyingpub struct Empty;pub struct Ready;pub struct Flying;// 2. Finish the Seligh struct definitionpub struct Sleigh<State> { _state: PhantomData<State>,}// 3. Write the Sleigh Implementations for all statesimpl 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 } }}
rjensen
use std::marker::PhantomData;// 1. We have 3 states:// - Empty// - Ready// - Flyingpub struct Empty;pub struct Ready;pub struct Flying;// 2. Finish the Seligh struct definitionpub struct Sleigh<State> { _state: PhantomData<State>,}// 3. Write the Sleigh Implementations for all statesimpl 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;// 1. We have 3 states:// - Empty// - Ready// - Flyingpub struct Empty;pub struct Ready;pub struct Flying;// 2. Finish the Sleigh struct definitionpub struct Sleigh<State> { _state: PhantomData<State>}// 3. Write the Sleigh Implementations for all statesimpl 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
pub struct Empty;pub struct Ready;pub struct Flying;// 2. Finish the Seligh struct definitionpub struct Sleigh<T> { pub _state: std::marker::PhantomData<T>,}// 3. Write the Sleigh Implementations for all statesimpl Sleigh<Empty> { pub fn new() -> Sleigh<Empty> { Sleigh { _state: std::marker::PhantomData, } } pub fn load(self) -> Sleigh<Ready> { Sleigh { _state: std::marker::PhantomData, } }}impl Sleigh<Ready> { pub fn take_off(self) -> Sleigh<Flying> { Sleigh { _state: std::marker::PhantomData, } } pub fn unload(self) -> Sleigh<Empty> { Sleigh { _state: std::marker::PhantomData, } }}impl Sleigh<Flying> { pub fn land(self) -> Sleigh<Ready> { Sleigh { _state: std::marker::PhantomData, } }}
sweet2honey
use std::marker::PhantomData;// 1. We have 3 states:// - Empty// - Ready// - Flyingpub struct Empty;pub struct Ready;pub struct Flying;// 2. Finish the Sleigh struct definitionpub struct Sleigh<State> { // State must be used pub _state: PhantomData<State>,}// 3. Write the Sleigh Implementations for all statesimpl Sleigh<Empty> { pub fn new() -> Sleigh<Empty> { Self { _state: PhantomData } } pub fn load(self) -> Sleigh<Ready> { Sleigh { _state: PhantomData } }}impl Sleigh<Ready> { pub fn unload(self) -> Sleigh<Empty> { Sleigh { _state: PhantomData } } pub fn take_off(self) -> Sleigh<Flying> { Sleigh { _state: PhantomData } }}impl Sleigh<Flying> { pub fn land(self) -> Sleigh<Ready> { Sleigh { _state: PhantomData } }}
pagyeman
use std::marker::PhantomData;// 1. We have 3 states:// - Empty// - Ready// - Flyingpub struct Empty;pub struct Ready;pub struct Flying;// 2. Finish the Sleigh struct definitionpub struct Sleigh<State> { // State must be used pub _state: PhantomData<State>,}// 3. Write the Sleigh Implementations for all statesimpl Sleigh<Empty> { pub fn new() -> Sleigh<Empty> { Self { _state: PhantomData } } pub fn load(self) -> Sleigh<Ready> { Sleigh { _state: PhantomData } }}impl Sleigh<Ready> { pub fn unload(self) -> Sleigh<Empty> { Sleigh { _state: PhantomData } } pub fn take_off(self) -> Sleigh<Flying> { Sleigh { _state: PhantomData } }}impl Sleigh<Flying> { pub fn land(self) -> Sleigh<Ready> { Sleigh { _state: PhantomData } }}
kapaseker
pub struct Empty;pub struct Ready;pub struct Flying;// 2. Finish the Seligh struct definitionpub struct Sleigh<T> { pub _state: std::marker::PhantomData<T>,}// 3. Write the Sleigh Implementations for all statesimpl Sleigh<Empty> { pub fn new() -> Sleigh<Empty> { Sleigh { _state: std::marker::PhantomData, } } pub fn load(self) -> Sleigh<Ready> { Sleigh { _state: std::marker::PhantomData, } }}impl Sleigh<Ready> { pub fn take_off(self) -> Sleigh<Flying> { Sleigh { _state: std::marker::PhantomData, } } pub fn unload(self) -> Sleigh<Empty> { Sleigh { _state: std::marker::PhantomData, } }}impl Sleigh<Flying> { pub fn land(self) -> Sleigh<Ready> { Sleigh { _state: std::marker::PhantomData, } }}
sarub0b0
use std::marker::PhantomData;// 1. We have 3 states:pub struct Empty;pub struct Ready;pub struct Flying;// 2. Finish the Seligh struct definitionpub struct Sleigh<S> { _state: PhantomData<S>,}// 3. Write the Sleigh Implementations for all statesimpl 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 } }}
MGehrmann
use std::marker::PhantomData;// 1. We have 3 states:// - Empty// - Ready// - Flyingpub struct Empty; pub struct Ready; pub struct Flying;// 2. Finish the Seligh struct definitionpub struct Sleigh<State> { state: PhantomData<State>}// 3. Write the Sleigh Implementations for all statesimpl Sleigh<Empty> { pub fn new() -> Self { Self { state: PhantomData } } pub fn load(self) -> Sleigh<Ready> { Sleigh { state: PhantomData } }}impl Sleigh<Flying> { pub fn land(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 } }}
aaron-otis
use std::marker::PhantomData;// 1. We have 3 states:// - Empty// - Ready// - Flyingpub struct Empty;pub struct Ready;pub struct Flying;// 2. Finish the Seligh struct definitionpub struct Sleigh<State> { state: PhantomData<State>,}// 3. Write the Sleigh Implementations for all statesimpl 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 } }}
KushnerykPavel
use std::marker::PhantomData;// 1. We have 3 states:// - Emptypub struct Empty;// - Readypub struct Ready;// - Flyingpub struct Flying;// 2. Finish the Seligh struct definitionpub struct Sleigh<State> { _state: PhantomData<State>,}// 3. Write the Sleigh Implementations for all statesimpl Sleigh<Empty> { pub fn new() -> Self { Self { _state: PhantomData} } pub fn load(self) -> Sleigh<Ready> { Sleigh { _state: PhantomData::<Ready>, } }}impl Sleigh<Ready> { pub fn take_off(self) -> Sleigh<Flying> { Sleigh { _state: PhantomData::<Flying>} } pub fn unload(self) -> Sleigh<Empty> { Sleigh { _state: PhantomData::<Empty>, } }}impl Sleigh<Flying> { pub fn land(self) -> Sleigh<Ready> { Sleigh { _state: PhantomData::<Ready>, } }}
eguefif
use std::marker::PhantomData;// 1. We have 3 states:// - Empty// - Ready// - Flyingpub struct Empty;pub struct Ready;pub struct Flying;// 2. Finish the Seligh struct definitionpub struct Sleigh<State> { _state: PhantomData<State>,}// 3. Write the Sleigh Implementations for all statesimpl Sleigh<Empty> { pub fn new() -> Sleigh<Empty> { Sleigh { _state: PhantomData, } } pub fn load(self) -> Sleigh<Ready> { Sleigh { _state: PhantomData::<Ready>, } }}impl Sleigh<Flying> { pub fn land(self) -> Sleigh<Ready> { Sleigh { _state: PhantomData::<Ready>, } }}impl Sleigh<Ready> { pub fn take_off(self) -> Sleigh<Flying> { Sleigh { _state: PhantomData::<Flying>, } } pub fn unload(self) -> Sleigh<Empty> { Sleigh { _state: PhantomData::<Empty>, } }}
habu1010
use std::marker::PhantomData;// 1. We have 3 states:// - Empty// - Ready// - Flyingpub struct Empty;pub struct Ready;pub struct Flying;// 2. Finish the Seligh struct definitionpub struct Sleigh<State> { _state: PhantomData<State>,}// 3. Write the Sleigh Implementations for all statesimpl 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 } }}
CianciuStyles
use std::marker::PhantomData;// 1. We have 3 states:// - Emptypub struct Empty;// - Readypub struct Ready;// - Flyingpub struct Flying;// 2. Finish the Seligh struct definitionpub struct Sleigh<State> { state: PhantomData<State>}// 3. Write the Sleigh Implementations for all statesimpl 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
pub struct Empty;pub struct Ready;pub struct Flying;pub struct Sleigh<T> { pub _state: std::marker::PhantomData<T>,}impl Sleigh<Empty> { pub fn new() -> Sleigh<Empty> { Sleigh { _state: std::marker::PhantomData, } } pub fn load(self) -> Sleigh<Ready> { Sleigh { _state: std::marker::PhantomData, } }}impl Sleigh<Ready> { pub fn take_off(self) -> Sleigh<Flying> { Sleigh { _state: std::marker::PhantomData, } } pub fn unload(self) -> Sleigh<Empty> { Sleigh { _state: std::marker::PhantomData, } }}impl Sleigh<Flying> { pub fn land(self) -> Sleigh<Ready> { Sleigh { _state: std::marker::PhantomData, } }}fn test() { let sleigh1 = Sleigh::new().load().take_off();}
Kyawkhaing444
use std::marker::PhantomData;// 1. We have 3 states:// - Empty// - Ready// - Flyingpub struct Empty; pub struct Ready; pub struct Flying;// 2. Finish the Seligh struct definitionpub struct Sleigh<State> { state: PhantomData<State>}// 3. Write the Sleigh Implementations for all statesimpl Sleigh<Empty> { pub fn new() -> Self { Self { state: PhantomData } } pub fn load(self) -> Sleigh<Ready> { Sleigh { state: PhantomData } }}impl Sleigh<Flying> { pub fn land(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 } }}
AliShenanigans
use std::marker::PhantomData;// 1. We have 3 states:// - Empty// - Ready// - Flyingpub struct Empty;pub struct Ready;pub struct Flying;// 2. Finish the Seligh struct definitionpub struct Sleigh<State> { state: PhantomData<State>}// 3. Write the Sleigh Implementations for all statesimpl 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;// 1. We have 3 states:// - Empty// - Ready// - Flyingpub struct Empty;pub struct Ready;pub struct Flying;// 2. Finish the Seligh struct definitionpub struct Sleigh<State> { state: PhantomData<State>}// 3. Write the Sleigh Implementations for all statesimpl 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} }}
cloki0610
use std::marker::PhantomData;// 1. We have 3 states:// - Emptypub struct Empty;// - Readypub struct Ready;// - Flyingpub struct Flying;// 2. Finish the Seligh struct definitionpub struct Sleigh <State> { _state: PhantomData<State>,}// 3. Write the Sleigh Implementations for all statesimpl 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 } }}
creativecoder
use std::marker::PhantomData;// 1. We have 3 states:// - Empty// - Ready// - Flyingpub struct Empty;pub struct Ready;pub struct Flying;// 2. Finish the Sleigh struct definitionpub struct Sleigh<State> { _state: PhantomData<State>,}// 3. Write the Sleigh Implementations for all statesimpl 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 } }}
wendko
// We don't want to end up with a sleigh in the wrong state again and we certainly don't want the take_off method to be available when the sleigh is already flying.// And Santa doesn't want any runtime checks, so it must all be checked at compile time.// Here's what you need to do:// Finish the Sleigh struct to represent the state of the sleigh.// The Sleigh struct must have 3 states Empty, Ready and Flying.// 1. We have 3 states:// - Empty// - Ready// - Flyinguse std::{marker::PhantomData};pub struct Empty;pub struct Ready;pub struct Flying;pub struct Sleigh<State> { _state: PhantomData<State>,}// Empty State// new() an associated function that creates a new Sleigh in the Empty state.// load() a method that transitions the Sleigh from Empty to Ready.impl Sleigh<Empty> { pub fn new() -> Self { Self { _state: PhantomData } } pub fn load(self) -> Sleigh<Ready> { Sleigh { _state: PhantomData } }}// Ready State// take_off() a method that transitions the Sleigh from Ready to Flying.// unload() a method that transitions the Sleigh from Ready to Empty.impl Sleigh<Ready> { pub fn unload(self) -> Sleigh<Empty> { Sleigh { _state: PhantomData } } pub fn take_off(self) -> Sleigh<Flying> { Sleigh { _state: PhantomData } }}// Flying State// land() a method that transitions the Sleigh from Flying to Ready.impl Sleigh<Flying> { pub fn land(self) -> Sleigh<Ready> { Sleigh { _state: PhantomData } }}
Ankit8848
// 1. We have 3 states:// - Empty// - Ready// - Flyinguse std::marker::PhantomData;pub struct Empty;pub struct Ready;pub struct Flying;// 2. Finish the Seligh struct definitionpub struct Sleigh<State> { state: PhantomData<State>,}// 3. Write the Sleigh Implementations for all statesimpl 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, } }}
DominicD
use std::marker::PhantomData;// 1. We have 3 states:// - Empty// - Ready// - Flyingpub struct Empty;pub struct Ready;pub struct Flying;// 2. Finish the Seligh struct definitionpub struct Sleigh<T>{ phantom: PhantomData<T>,}impl Sleigh<Empty> { pub fn new() -> Self { Sleigh{ phantom: PhantomData } } pub fn load(self) -> Sleigh<Ready> { Sleigh{ phantom: PhantomData } }}impl Sleigh<Ready> { pub fn take_off(self) -> Sleigh<Flying> { Sleigh{ phantom: PhantomData } } pub fn unload(self) -> Sleigh<Empty> { Sleigh{ phantom: PhantomData } }}impl Sleigh<Flying> { pub fn land(self) -> Sleigh<Ready> { Sleigh{ phantom: PhantomData } } }// 3. Write the Sleigh Implementations for all states
arm01846
// 1. We have 3 states:// - Empty// - Ready// - Flyinguse std::marker::PhantomData;pub struct Empty;pub struct Ready;pub struct Flying;// 2. Finish the Seligh struct definitionpub struct Sleigh<State> { state: PhantomData<State>,}// 3. Write the Sleigh Implementations for all statesimpl 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, } }}
joanne-cmd
use std::marker::PhantomData;pub struct Empty;pub struct Ready;pub struct Flying;// 2. Finish the Seligh struct definitionpub struct Sleigh<State> { _state: PhantomData<State>,}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} }}// 3. Write the Sleigh Implementations for all state
rony0000013
// 1. We have 3 states:// - Empty// - Ready// - Flyinguse std::marker::PhantomData;pub struct Empty;pub struct Ready;pub struct Flying;// 2. Finish the Seligh struct definitionpub struct Sleigh<State> { _state: PhantomData<State>,}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} }}// 3. Write the Sleigh Implementations for all states
vaclav0411
use std::marker::PhantomData;pub struct Empty;pub struct Ready;pub struct Flying;// 2. Finish the Seligh struct definitionpub struct Sleigh<State> { _state: PhantomData<State>,}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} }}// 3. Write the Sleigh Implementations for all states
wamimi
use std::marker::PhantomData;// Define the state typespub struct Empty;pub struct Ready;pub struct Flying;// Define the Sleigh with generic statepub struct Sleigh<State> { _state: PhantomData<State>,}// Implementation for Empty stateimpl Sleigh<Empty> { // Associated function to create new sleigh in Empty state pub fn new() -> Self { Self { _state: PhantomData, } } // Method to transition from Empty to Ready pub fn load(self) -> Sleigh<Ready> { Sleigh { _state: PhantomData, } }}// Implementation for Ready stateimpl Sleigh<Ready> { // Method to transition from Ready to Flying pub fn take_off(self) -> Sleigh<Flying> { Sleigh { _state: PhantomData, } } // Method to transition from Ready to Empty pub fn unload(self) -> Sleigh<Empty> { Sleigh { _state: PhantomData, } }}// Implementation for Flying stateimpl Sleigh<Flying> { // Method to transition from Flying to Ready pub fn land(self) -> Sleigh<Ready> { Sleigh { _state: PhantomData, } }}
tamanishi
use std::marker::PhantomData;// 1. We have 3 states:// - Empty// - Ready// - Flyingpub struct Empty;pub struct Ready;pub struct Flying;// 2. Finish the Seligh struct definitionpub struct Sleigh<State> { _state: PhantomData<State>,}// 3. Write the Sleigh Implementations for all statesimpl 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 Sleigh {// pub fn new() -> Self {// Self {// state: State::Empty,// }// }// pub fn load(&mut self) {// self.state = State::Ready;// }// pub fn take_off(&mut self) {// self.state = State::Flying;// }// pub fn unload(&mut self) {// self.state = State::Empty;// }// pub fn land(&mut self) {// self.state = State::Ready;// }// }
miscer
use std::marker::PhantomData;// 1. We have 3 states:pub struct Empty;pub struct Ready;pub struct Flying;// 2. Finish the Seligh struct definitionpub struct Sleigh<State> { _state: PhantomData<State>}// 3. Write the Sleigh Implementations for all statesimpl 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;// 1. We have 3 states:// - Empty// - Ready// - Flyingpub struct Empty;pub struct Ready;pub struct Flying;// 2. Finish the Seligh struct definitionpub struct Sleigh<T> { state: PhantomData<T>,}// 3. Write the Sleigh Implementations for all statesimpl 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, } }}
gmvar
use std::marker::PhantomData;// 1. We have 3 states:// - Empty// - Ready// - Flyingpub struct Empty;pub struct Ready;pub struct Flying;// 2. Finish the Seligh struct definitionpub struct Sleigh<State> { _state: PhantomData<State>,}// 3. Write the Sleigh Implementations for all statesimpl 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
// 1. We have 3 states:// - Empty// - Ready// - Flyingpub trait SleighState {}pub struct Empty;impl SleighState for Empty {}pub struct Ready;impl SleighState for Ready {}pub struct Flying;impl SleighState for Flying {}use std::marker::PhantomData;// 2. Finish the Sleigh struct definitionpub struct Sleigh<S: SleighState> { state: PhantomData<S>}// 3. Write the Sleigh Implementations for all statesimpl 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;// 1. We have 3 states:// - Empty// - Ready// - Flyingpub struct Empty;pub struct Ready;pub struct Flying;// 2. Finish the Seligh struct definitionpub struct Sleigh<State> { _state: PhantomData<State>,}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, } }}
Burnus
use std::marker::PhantomData;pub struct Empty;pub struct Ready;pub struct Flying;pub struct Sleigh<State> { _phantom_data: PhantomData<State>}impl Sleigh<Empty> { pub fn new() -> Sleigh<Empty> { Sleigh { _phantom_data: PhantomData::<Empty> } } pub fn load(self) -> Sleigh<Ready> { Sleigh { _phantom_data: PhantomData::<Ready> } }}impl Sleigh<Ready> { pub fn take_off(self) -> Sleigh<Flying> { Sleigh { _phantom_data: PhantomData::<Flying> } } pub fn unload(self) -> Sleigh<Empty> { Sleigh { _phantom_data: PhantomData::<Empty> } }}impl Sleigh<Flying> { pub fn land(self) -> Sleigh<Ready> { Sleigh { _phantom_data: PhantomData::<Ready> } }}
KLcpb
// 1. We have 3 states:// - Empty// - Ready// - Flyinguse std::marker::PhantomData;pub struct Empty;pub struct Ready;pub struct Flying;// 2. Finish the Seligh struct definitionpub struct Sleigh<T> { _state: PhantomData<T>,}// 3. Write the Sleigh Implementations for all statesimpl 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 } }}
tonisk
use std::marker::PhantomData;// 1. We have 3 states:// - Empty// - Ready// - Flying// 2. Finish the Seligh struct definitionpub enum State { Empty, Ready, Flying,}pub struct Empty;pub struct Ready;pub struct Flying;pub struct Sleigh<T> { _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 } }}// 3. Write the Sleigh Implementations for all states
jayber
// 1. We have 3 states:// - Empty// - Ready// - Flyinguse std::marker::PhantomData;pub struct Empty;pub struct Ready;pub struct Flying;// 2. Finish the Seligh struct definitionpub struct Sleigh<T> { _state: PhantomData<T>,}// 3. Write the Sleigh Implementations for all statesimpl 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
// 1. We have 3 states:// - Empty// - Ready// - Flyinguse std::marker::PhantomData;pub struct Empty;pub struct Ready;pub struct Flying;// 2. Finish the Seligh struct definitionpub struct Sleigh<State = Empty> { _state: PhantomData<State>}// 3. Write the Sleigh Implementations for all statesimpl Sleigh<Empty> { pub fn new() -> Self { Self { _state: PhantomData } } pub fn load( self ) -> Sleigh<Ready> { Sleigh { _state: PhantomData::<Ready> } }}impl Sleigh<Ready> { pub fn take_off( self ) -> Sleigh<Flying> { Sleigh { _state: PhantomData::<Flying> } } pub fn unload( self ) -> Sleigh<Empty> { Sleigh { _state: PhantomData::<Empty> } }}impl Sleigh<Flying> { pub fn land( self ) -> Sleigh<Ready> { Sleigh { _state: PhantomData::<Ready> } }}
kometen
use std::marker::PhantomData;// 1. We have 3 states:// - Empty// - Ready// - Flyingpub struct Empty;pub struct Ready;pub struct Flying;// 2. Finish the Sleigh struct definitionpub struct Sleigh<State> { state: PhantomData<State>,}// 3. Write the Sleigh Implementations for all statesimpl Sleigh<Empty> { pub fn new() -> Self { Self { state: PhantomData } } pub fn load(self) -> Sleigh<Ready> { Sleigh { state: PhantomData } } pub fn unload(self) -> Sleigh<Empty> { 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<Empty> { Sleigh { state: PhantomData } }}
zkar0927
// 1. We have 3 states:// - Empty// - Ready// - Flyingpub struct Empty;pub struct Ready;pub struct Flying;// 2. Finish the Seligh struct definitionpub struct Sleigh<State> { _state: State,}// 3. Write the Sleigh Implementations for all states//impl Sleigh<Empty> { pub fn new() -> Sleigh<Empty> { Sleigh::<Empty> { _state: Empty {} } } pub fn load(self) -> Sleigh<Ready> { Sleigh::<Ready> { _state: Ready {} } }}impl Sleigh<Ready> { pub fn take_off(self) -> Sleigh<Flying> { Sleigh::<Flying> { _state: Flying {} } } pub fn unload(self) -> Sleigh<Empty> { Sleigh::<Empty> { _state: Empty {} } }}impl Sleigh<Flying> { pub fn land(self) -> Sleigh<Ready> { Sleigh::<Ready> { _state: Ready {} } }}
lulingar
use std::marker::PhantomData;// 1. We have 3 states:pub struct Empty; pub struct Ready; pub struct Flying; pub trait State {}impl State for Empty {}impl State for Ready {}impl State for 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, } }}// 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() }}
wishkus
use std::marker::PhantomData;// 1. We have 3 states:// - Empty// - Ready// - Flyingpub struct Empty;pub struct Ready;pub struct Flying;// 2. Finish the Seligh struct definitionpub struct Sleigh<State> { _state: PhantomData<State>}// 3. Write the Sleigh Implementations for all statesimpl 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} }}
Stephan-Lindner
use std::marker::PhantomData;pub struct Empty;pub struct Ready;pub struct Flying;pub struct Sleigh<State> { _marker: PhantomData<State>,}impl Sleigh<Empty> { pub fn new() -> Self { Sleigh { _marker: PhantomData, } } pub fn load(&self) -> Sleigh<Ready> { Sleigh{ _marker: PhantomData, } }}impl Sleigh<Ready> { pub fn take_off(&self) -> Sleigh<Flying> { Sleigh{ _marker: PhantomData, } } pub fn unload(&self) -> Sleigh<Empty> { Sleigh{ _marker: PhantomData, } }}impl Sleigh<Flying> { pub fn land(&self) -> Sleigh<Ready> { Sleigh{ _marker: PhantomData, } }}
Stephan-Lindner
use std::marker::PhantomData;pub struct Empty {}pub struct Ready {}pub struct Flying {}trait State {}pub struct Sleigh<State> { _marker: PhantomData<State>,}impl Sleigh<Empty> { pub fn new() -> Self { Sleigh { _marker: PhantomData, } } pub fn load(&self) -> Sleigh<Ready> { Sleigh{ _marker: PhantomData, } }}impl Sleigh<Ready> { pub fn take_off(&self) -> Sleigh<Flying> { Sleigh{ _marker: PhantomData, } } pub fn unload(&self) -> Sleigh<Empty> { Sleigh{ _marker: PhantomData, } }}impl Sleigh<Flying> { pub fn land(&self) -> Sleigh<Ready> { Sleigh{ _marker: PhantomData, } }}
chriswmann
use std::marker::PhantomData;// 1. We have 3 states:// - Empty// - Ready// - Flyingpub struct Empty;pub struct Ready;pub struct Flying;// 2. Finish the Seligh struct definitionpub struct Sleigh<State> { _state: PhantomData<State>,}// 3. Write the Sleigh Implementations for all statesimpl 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 } }}