Created
December 11, 2014 17:59
-
-
Save nicolasbrugneaux/e2370719d97e3ee2c234 to your computer and use it in GitHub Desktop.
compile error
This file contains hidden or 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::io::net; | |
use std::io::net::ip::{Port, SocketAddr}; | |
use std::io::TcpStream; | |
use std::kinds::marker::NoCopy; | |
enum Client { | |
Connected(ConnectedClient), | |
Disconnected(DisconnectedClient) | |
} | |
struct DisconnectedClient { | |
seq: int, | |
_no_copy: NoCopy | |
} | |
struct ConnectedClient { | |
seq: int, | |
socket: String //Socket later | |
} | |
impl Client { | |
fn new() -> Client { | |
Client::Disconnected(DisconnectedClient::new()) | |
} | |
fn disconnect(&self) -> Client { | |
match self { | |
&Client::Disconnected(ref DisconnectedClient) => self, | |
&Client::Connected(ref c) => Client::Disconnected(c.disconnect()) | |
} | |
} | |
fn connect(&self) -> Client { | |
match self { | |
&Client::Disconnected(ref c) => Client::Connected(c.connect()), | |
&Client::Connected(ref ConnectedClient) => self | |
} | |
} | |
} | |
impl DisconnectedClient { | |
fn new() -> DisconnectedClient { | |
DisconnectedClient { seq: 1i, _no_copy: NoCopy } | |
} | |
fn connect(&self) -> ConnectedClient { | |
ConnectedClient { seq: self.seq, socket: "socket_soon".to_string() } | |
} | |
} | |
impl ConnectedClient { | |
fn disconnect(&self) -> DisconnectedClient { | |
// self.socket will be dropped here | |
DisconnectedClient { seq: self.seq, _no_copy: NoCopy } | |
// Ok("Disconnected") | |
} | |
} | |
struct Bot { | |
host: String, | |
port: u16, | |
name: String, | |
cli: Client | |
} | |
impl Bot { | |
fn new(host: String, port: u16, name: String) -> Bot { | |
let mut cli = Client::new(); | |
Bot { host: host, port: port, name: name, cli: cli } | |
} | |
fn is_connected(&self) -> bool { | |
match self.cli { | |
Client::Disconnected(ref DisconnectedClient) => false, | |
Client::Connected(ref ConnectedClient) => true | |
} | |
} | |
fn connect(&mut self) { | |
println!("should be disconnected: {}", self.is_connected()); | |
println!("trying to connect to: {}", self.host); | |
self.cli.connect(); | |
println!("should be connected, state: {}", self.is_connected()); | |
self.cli.disconnect(); | |
println!("should be disconnected, state: {}", self.is_connected()); | |
} | |
} | |
fn main() { | |
let mut rusty: Bot = Bot::new("192.168.3.254".to_string(), 6667, | |
"Rusty".to_string() ); | |
rusty.connect(); | |
} | |
///////////////////////////////////////////////////////////////////////////// | |
❯ cargo run | |
Compiling Rusty v0.0.1 (file:///Users/nicolasb/workspace/experiments/rust/rusty) | |
src/rusty.rs:27:9: 30:10 error: match arms have incompatible types: expected `&Client`, found `Client` (expected &-ptr, found enum Client) | |
src/rusty.rs:27 match self { | |
src/rusty.rs:28 &Client::Disconnected(ref DisconnectedClient) => self, | |
src/rusty.rs:29 &Client::Connected(ref c) => Client::Disconnected(c.disconnect()) | |
src/rusty.rs:30 } | |
src/rusty.rs:29:42: 29:78 note: match arm with an incompatible type | |
src/rusty.rs:29 &Client::Connected(ref c) => Client::Disconnected(c.disconnect()) | |
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
src/rusty.rs:34:9: 37:10 error: match arms have incompatible types: expected `Client`, found `&Client` (expected enum Client, found &-ptr) | |
src/rusty.rs:34 match self { | |
src/rusty.rs:35 &Client::Disconnected(ref c) => Client::Connected(c.connect()), | |
src/rusty.rs:36 &Client::Connected(ref ConnectedClient) => self | |
src/rusty.rs:37 } | |
src/rusty.rs:36:56: 36:60 note: match arm with an incompatible type | |
src/rusty.rs:36 &Client::Connected(ref ConnectedClient) => self | |
^~~~ | |
error: aborting due to 2 previous errors | |
Could not compile `Rusty`. | |
To learn more, run the command again with --verbose. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment