I've got code which is serializing structs, sending them over the wire, and then deserializing them, and then having to do different things based on the type of the struct. I'm using a match, but keep running into "unreachable pattern" with a catchall pattern at the end.
Here's a simplified version of my code:
use serde::{Serialize, Deserialize, de::DeserializeOwned};
use serde_json;
#[derive(Serialize, Deserialize)]
struct Foo {
a: i32,
b: u8,
}
#[derive(Serialize, Deserialize)]
struct Bar {
c: i32,
d: u8,
}
fn send<S: Serialize>(s: &S) -> Result<String, serde_json::Error> {
serde_json::to_string(s)
}
fn recv<D: DeserializeOwned>(json: &str) -> Result<D, serde_json::Error> {
serde_json::from_str(json)
}
fn main() {
let f = Foo { a: 1, b: 2 };
let json = send(&f).unwrap();
let deserialized = recv(&json).unwrap();
match deserialized {
Foo { a, b } => println!("a = {}, b = {}", a, b),
_ => println!("who knows?"),
};
}
When I compile this, I get this error:
warning: unreachable pattern
--> src/main.rs:30:9
|
29 | Foo { a, b } => println!("a = {}, b = {}", a, b),
| ------------ matches any value
30 | _ => println!("who knows?"),
| ^ no value can reach this
|
= note: `#[warn(unreachable_patterns)]` on by default
When I make f
a Bar
instead of a Foo
, the unwrap()
on the let deserialized =
line panics because it thinks it's supposed to be a Foo
, which means that the compiler's inferring the type of the object based on the match pattern (or, at least it seems like it is).
What I'm trying to do here is to, basically, deserialize a message into it's original message type, and then, if it's a message I'm expecting at that point, do something with it, otherwise throw an error. I would have thought this would be the proper place to use match
, but clearly I'm mistaken. How would one go about doing what I'm trying to do?
Much appreciation for any help with this.
-Jack