Skip to content

Instantly share code, notes, and snippets.

@rust-play
Created December 12, 2018 14:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rust-play/9cd516bc621002515c9ca1855783ec0e to your computer and use it in GitHub Desktop.
Save rust-play/9cd516bc621002515c9ca1855783ec0e to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
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