use core::cmp::Ordering; use core::{mem, ptr}; use instant::Instant; #[doc(hidden)] #[repr(C)] pub struct Node where T: 'static, { #[cfg(feature = "timer-queue")] baseline: Instant, payload: T, } #[doc(hidden)] pub struct Slot where T: 'static, { node: &'static mut Node, } impl Slot { #[cfg(feature = "timer-queue")] pub fn write(self, bl: Instant, data: T) -> Payload { self.node.baseline = bl; unsafe { ptr::write(&mut self.node.payload, data) } Payload { node: self.node } } #[cfg(not(feature = "timer-queue"))] pub fn write(self, data: T) -> Payload { unsafe { ptr::write(&mut self.node.payload, data) } Payload { node: self.node } } } impl Into> for &'static mut Node { fn into(self) -> Slot { Slot { node: self } } } #[doc(hidden)] pub struct Payload where T: 'static, { node: &'static mut Node, } impl Payload { #[cfg(feature = "timer-queue")] pub fn read(self) -> (Instant, T, Slot) { let data = unsafe { ptr::read(&self.node.payload) }; (self.node.baseline, data, Slot { node: self.node }) } #[cfg(not(feature = "timer-queue"))] pub fn read(self) -> (T, Slot) { let data = unsafe { ptr::read(&self.node.payload) }; (data, Slot { node: self.node }) } pub fn tag(self, tag: A) -> TaggedPayload where A: Copy, { TaggedPayload { tag, payload: unsafe { mem::transmute(self) }, } } } #[doc(hidden)] pub struct TaggedPayload where A: Copy, { tag: A, payload: Payload, } impl TaggedPayload where A: Copy, { pub unsafe fn coerce(self) -> Payload { mem::transmute(self.payload) } #[cfg(feature = "timer-queue")] pub fn baseline(&self) -> Instant { self.payload.node.baseline } pub fn tag(&self) -> A { self.tag } pub fn retag(self, tag: B) -> TaggedPayload where B: Copy, { TaggedPayload { tag, payload: self.payload, } } } #[cfg(feature = "timer-queue")] impl Eq for TaggedPayload where T: Copy, { } #[cfg(feature = "timer-queue")] impl Ord for TaggedPayload where T: Copy, { fn cmp(&self, rhs: &Self) -> Ordering { self.payload.node.cmp(&rhs.payload.node) } } #[cfg(feature = "timer-queue")] impl PartialEq for TaggedPayload where T: Copy, { fn eq(&self, rhs: &Self) -> bool { self.payload.node.eq(&rhs.payload.node) } } #[cfg(feature = "timer-queue")] impl PartialOrd for TaggedPayload where T: Copy, { fn partial_cmp(&self, rhs: &Self) -> Option { Some(self.cmp(rhs)) } }