当我尝试将二进制数据反序列化为错误类型时,为什么我不会从 bincode 中收到错误消息?
use bincode; // 1.3.1
use serde::{Deserialize, Serialize}; // { version = "1.0", features = ["derive"] }
#[derive(Serialize, Deserialize, Copy, Clone, Debug)]
pub struct Ping {
pub pinger_id: usize,
pub useless_field: usize,
pub i_need_one_more: usize,
}
#[derive(Serialize, Deserialize, Copy, Clone, Debug)]
pub struct Heartbeat {
pub term: usize,
pub node_id: usize,
}
#[derive(Serialize, Deserialize, Clone, Debug)]
pub enum Message {
Heartbeat(Heartbeat),
Ping(Ping),
}
fn main() {
let rpc_message_bin = bincode::serialize(&Ping {
pinger_id: 0,
useless_field: 1,
i_need_one_more: 2,
})
.unwrap();
let m: Message = bincode::deserialize(&rpc_message_bin).unwrap();
println!("{:#?}", m);
}
我期待得到一个Message::Ping
,但我得到:
Heartbeat (
Heartbeat {
term: 4294967296,
node_id: 8589934592,
},
)