I have a simple struct that defines some kind of message to be sent over network.
struct Message {
message_type: u32,
user_id: u32,
message: Vec<u8>,
}
And somewhere else I want to serialize it into a simple sequence of bytes. So I defined iterator for the bytes of the message like this:
impl Message {
fn iter(&self) -> std::iter::Chain<std::iter::Chain<std::slice::Iter<'_, u8>, std::slice::Iter<'_, u8>>, std::slice::Iter<'_, u8>> {
self.message_type
.to_be_bytes()
.iter()
.chain(self.user_id.to_be_bytes().iter())
.chain(self.message.iter())
}
fn data(&self) -> Vec<u8> {
self.iter().cloned().collect()
}
}
Yes, the type keeps growing with more chained iterators, which is kind of a shame
But I get 2 compiler errors when I try to run it
cannot return value referencing temporary value
returns a value referencing data owned by the current function. rustc(E0515)
Guess I'm not versed enough in the rust's ownership system