use RawMessage;
use std::mem::size_of;
use std::intrinsics::TypeId;
pub struct CloneMessage {
pub hash: u64,
pub payload: RawMessage,
}
impl Clone for CloneMessage {
fn clone(&self) -> CloneMessage {
CloneMessage {
hash: self.hash,
payload: self.payload.clone(),
}
}
}
impl CloneMessage {
pub fn new<T: Send + Clone + 'static>(t: T) -> CloneMessage {
let tyid = TypeId::of::<T>();
let hash = tyid.hash();
let mut rmsg = RawMessage::new(size_of::<T>());
rmsg.writestruct(0, t);
CloneMessage {
hash: hash,
payload: rmsg
}
}
pub fn is_type<T: Send + 'static>(&self) -> bool {
let tyid = TypeId::of::<T>();
let hash = tyid.hash();
if hash != self.hash {
return false;
}
return true;
}
pub fn get_payload<T: 'static>(self) -> T {
let rawmsg = self.payload;
let tyid = TypeId::of::<T>();
let hash = tyid.hash();
if hash != self.hash {
panic!("clone message was not correct type");
}
let t: T = unsafe { rawmsg.readstructunsafe(0) };
t
}
}