Skip to content

Instantly share code, notes, and snippets.

@rust-play
Created October 3, 2018 20:41
Show Gist options
  • Save rust-play/46e04c8d44a10e49eb88a4d76185537a to your computer and use it in GitHub Desktop.
Save rust-play/46e04c8d44a10e49eb88a4d76185537a to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
#[derive(Debug)]
enum Bar {
A,
B
}
impl Bar {
fn as_ref(&self) -> Bar {
match self {
Bar::A => Bar::A,
Bar::B => Bar::B
}
}
}
#[derive(Debug)]
enum Message<T> {
Quit,
Move { x: i32, y: i32 },
WriteString(T),
ChangeColor(i32, i32, i32)
}
impl Message<String> {
fn as_ref(&self) -> Message<&String> {
match self {
Message::Quit => Message::Quit,
Message::Move { x: x, y: y } => Message::Move { x: *x, y: *y },
Message::WriteString(ref string) => Message::WriteString(string),
Message::ChangeColor(r, g, b) => Message::ChangeColor(*r, *g, *b)
}
}
}
impl<'a> Message<&'a String> {
fn as_ref(&self) -> Self {
match self {
Message::Quit => Message::Quit,
Message::Move { x: x, y: y } => Message::Move { x: *x, y: *y },
Message::WriteString(string) => Message::WriteString(string),
Message::ChangeColor(r, g, b) => Message::ChangeColor(*r, *g, *b)
}
}
}
fn main() {
let b = Bar::B {};
let b = b.as_ref();
println!("{:?}", b);
let s = "s".to_string();
let m = &Message::WriteString(s);
println!("{:?}", m.as_ref())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment