strafe-client/lib/common/src/instruction.rs

81 lines
2.0 KiB
Rust
Raw Normal View History

2024-01-29 16:15:49 -08:00
use crate::integer::Time;
2025-01-15 21:41:44 -08:00
#[derive(Clone,Debug)]
2025-01-07 22:36:21 -08:00
pub struct TimedInstruction<I,T>{
pub time:Time<T>,
2024-01-29 16:15:49 -08:00
pub instruction:I,
}
2025-01-15 03:22:19 -08:00
impl<I,T> TimedInstruction<I,T>{
2025-01-15 03:26:43 -08:00
#[inline]
2025-01-15 03:22:19 -08:00
pub fn set_time<TimeInner>(self,new_time:Time<TimeInner>)->TimedInstruction<I,TimeInner>{
TimedInstruction{
time:new_time,
instruction:self.instruction,
}
}
}
2024-01-29 16:15:49 -08:00
/// Ensure all emitted instructions are processed before consuming external instructions
pub trait InstructionEmitter<I>{
2025-01-07 22:36:21 -08:00
type TimeInner;
fn next_instruction(&self,time_limit:Time<Self::TimeInner>)->Option<TimedInstruction<I,Self::TimeInner>>;
2024-01-29 16:15:49 -08:00
}
/// Apply an atomic state update
pub trait InstructionConsumer<I>{
2025-01-07 22:36:21 -08:00
type TimeInner;
fn process_instruction(&mut self,instruction:TimedInstruction<I,Self::TimeInner>);
2024-01-29 16:15:49 -08:00
}
/// If the object produces its own instructions, allow exhaustively feeding them back in
pub trait InstructionFeedback<I,T>:InstructionEmitter<I,TimeInner=T>+InstructionConsumer<I,TimeInner=T>
where
Time<T>:Copy,
{
2025-01-15 03:26:43 -08:00
#[inline]
fn process_exhaustive(&mut self,time_limit:Time<T>){
while let Some(instruction)=self.next_instruction(time_limit){
self.process_instruction(instruction);
}
}
}
impl<I,T,X> InstructionFeedback<I,T> for X
where
Time<T>:Copy,
X:InstructionEmitter<I,TimeInner=T>+InstructionConsumer<I,TimeInner=T>,
{}
2024-01-29 16:15:49 -08:00
//PROPER PRIVATE FIELDS!!!
2025-01-07 22:36:21 -08:00
pub struct InstructionCollector<I,T>{
time:Time<T>,
2024-01-29 16:15:49 -08:00
instruction:Option<I>,
}
2025-01-07 22:36:21 -08:00
impl<I,T> InstructionCollector<I,T>
where Time<T>:Copy+PartialOrd,
{
2025-01-15 03:26:43 -08:00
#[inline]
2025-01-07 22:36:21 -08:00
pub const fn new(time:Time<T>)->Self{
2024-01-29 16:15:49 -08:00
Self{
time,
instruction:None
}
}
#[inline]
2025-01-07 22:36:21 -08:00
pub const fn time(&self)->Time<T>{
2024-01-29 16:15:49 -08:00
self.time
}
2025-01-07 22:36:21 -08:00
pub fn collect(&mut self,instruction:Option<TimedInstruction<I,T>>){
2025-01-15 03:26:43 -08:00
if let Some(ins)=instruction{
if ins.time<self.time{
self.time=ins.time;
self.instruction=Some(ins.instruction);
}
2024-01-29 16:15:49 -08:00
}
}
2025-01-07 22:36:21 -08:00
pub fn instruction(self)->Option<TimedInstruction<I,T>>{
2024-01-29 16:15:49 -08:00
//STEAL INSTRUCTION AND DESTROY INSTRUCTIONCOLLECTOR
2025-01-15 03:26:43 -08:00
self.instruction.map(|instruction|TimedInstruction{
time:self.time,
instruction
})
2024-01-29 16:15:49 -08:00
}
2024-08-02 13:45:10 -07:00
}