use std::mem::size_of;
use std::intrinsics::TypeId;
use std::sync::Arc;
use std::sync::Mutex;
use rawmessage::RawMessage;
pub struct SyncMessage {
pub hash: u64,
pub valid: Arc<Mutex<bool>>,
pub payload: RawMessage,
}
unsafe impl Send for SyncMessage { }
impl SyncMessage {
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!("sync message was not correct type");
}
let t: T = unsafe { rawmsg.readstructunsafe(0) };
t
}
#[inline]
pub fn internal_clone(&self, key: uint) -> SyncMessage {
if key != 0x879 {
panic!("You used an internal function. They key is 0x879.")
}
SyncMessage {
hash: self.hash,
valid: self.valid.clone(),
payload: self.payload.clone(),
}
}
pub fn takeasvalid(&self) -> bool {
let mut lock = self.valid.lock();
if *lock {
*lock = false;
true
} else {
false
}
}
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 new<T: Send + 'static>(t: T) -> SyncMessage {
let tyid = TypeId::of::<T>();
let hash = tyid.hash();
let mut rmsg = RawMessage::new(size_of::<T>());
rmsg.writestruct(0, t);
SyncMessage {
hash: hash,
valid: Arc::new(Mutex::new(true)),
payload: rmsg
}
}
}