Created
December 12, 2018 14:35
-
-
Save rust-play/9cd516bc621002515c9ca1855783ec0e to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::marker::PhantomData; | |
struct Normal; | |
struct Auction; | |
struct Stop; | |
enum Mode { | |
Normal(Normal), | |
Auction(Auction), | |
Stop(Stop), | |
} | |
struct MatcherState { | |
} | |
struct Service { | |
mode: Mode, | |
state: MatcherState, | |
} | |
impl Service { | |
fn place_order(&mut self) { | |
match self.mode { | |
Mode::Normal(Normal) => Matcher::<Normal>::new(&mut self.state).place_order(), | |
Mode::Auction(Auction) => Matcher::<Auction>::new(&mut self.state).place_order(), | |
Mode::Stop(Stop) => Matcher::<Stop>::new(&mut self.state).place_order(), | |
} | |
} | |
fn cancel_order(&mut self) {} | |
// ... | |
} | |
struct Matcher<T> { | |
_p: PhantomData<T>, | |
} | |
impl<T> Matcher<T> { | |
fn new(state: &mut MatcherState) -> Matcher<T> { | |
Matcher { _p: PhantomData } | |
} | |
} | |
impl Matcher<Normal> { | |
fn place_order(&mut self) { | |
} | |
fn cancel_order(&mut self) { | |
} | |
} | |
impl Matcher<Auction> { | |
fn place_order(&mut self) { | |
} | |
fn cancel_order(&mut self) { | |
} | |
} | |
impl Matcher<Stop> { | |
fn place_order(&mut self) { | |
} | |
fn cancel_order(&mut self) { | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment