rust


Why do I get an error about non-exhaustive patterns?


I'm writing a chat server using mio (0.5.1), following this tutorial. With it, I have written the following Handler:
const SERVER_TOKEN: Token = Token(0);
impl Handler for WebSocketServer {
type Timeout = usize;
type Message = ();
fn ready(&mut self, event_loop: &mut EventLoop<WebSocketServer>,
token: Token, events: EventSet)
{
match token {
SERVER_TOKEN => {
let client_socket = match self.socket.accept() {
Err(e) => {
println!("Accept error: {}", e);
return;
},
Ok(None) => unreachable!("Accept has returned 'None'"),
Ok(Some((sock, addr))) => sock,
};
self.token_counter += 1;
let new_token = Token(self.token_counter);
self.clients.insert(new_token, client_socket);
event_loop.register(&self.clients[&new_token],
new_token, EventSet::readable(),
PollOpt::edge() | PollOpt::oneshot()).unwrap();
}
}
}
}
However, in trying to compile it I get an error:
error[E0004]: non-exhaustive patterns: `Token(_)` not covered
--> src/main.rs:23:15
|
23 | match token {
| ^^^^^ pattern `Token(_)` not covered
error: aborting due to previous error
Although I understand that this should mean that my match does not cover all possible cases, I do not see how that is true.
Why am I getting this error, and how may I fix it?
Here SERVER_TOKEN is a constant equal to Token(0), so let's simplify the match:
match token {
Token(0) => { },
}
While you know that you won't get any other tokens, the compiler doesn't know, and the type allows other values like Token(1). You can add a case to cover the other cases, and just panic. I like to print out what was matched if it ever does happen as a debug aid.
match token {
SERVER_TOKEN => { },
x => panic!("Unexpected invalid token {:?}", x),
}
This assumes that Token implements Debug.

Related Links

Conditionally derive based on feature flag
Possible to pass a '&str' or '&String' to function in Rust using 'Into'?
Why does a mutable reference to a dropped object still count as a mutable reference?
How to use 'on_send' method of Nickel response?
How to build an executable that depends on curl for x86_64-unknown-linux-musl
How can I create a struct with a HashMap where the keys the same type as the container
Issues constraining implementation lifetimes on type without lifetime parameter
How to use recursive associated types in functions?
Right way to share a reference between closures
Borrow checker and function arguments in Rust, correct or over zealous?
Most idiomatic way to create a default struct
How to return an Iterator from a Map [duplicate]
Do all primitive types implement the Copy trait?
Comparison of two floats in Rust to arbitrary level of precision
Is there a convenient way to represent x86 instructions in a struct or other language feature?
Why isn't `regex!` a wrapper for `Regex::new` to offer the same regex matching speed?

Categories

HOME
openshift
freeradius
subdomain
greasemonkey
joomla3.2
phpseclib
cisco
eddystone
yeoman-generator-angular
documentum
cname
spring-amqp
responsivevoice
createjs
dhcp
webstore
sql-injection
complexity-theory
django-rq
xbox360
temperature
web-deployment
codefluent
renderscript
spam
dashdb
amazon-sns
acl
substring
quartz.net
python-imageio
sendmessage
ninject
fusion
objectmapper
shapeless
cloudera-manager
serversocket
spring-rabbitmq
rails-postgresql
dreamfactory
nsuserdefaults
ms-dos
mv
strapi
mars-simulator
linq.js
flowchart
yoast
minikube
dbcc
ipp-protocol
sony-future-lab-n
supertest
comm
winston
pen
mongodb-aggregation
dtd
fitbit
ternary-operator
jsf-2.3
p6spy
nvda
groovyfx
jemdoc
openmrs
ui4j
place
org-babel
shell-extensions
lifetime
iplimage
rpg
csslint
windows-taskbar
lync-2010
particle-swarm
illuminate-container
xsd2code
quickgraph
statechart
sly-scroller
struts2-json-plugin
stretch
voice-recording
cakeyframeanimation
delphi-prism
simile
pyjamas
solandra
remote-working

Resources

Encrypt Message