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

60 lines
1.3 KiB
Rust
Raw Normal View History

2024-01-30 00:15:49 +00:00
use crate::integer::Time;
#[derive(Debug)]
2025-01-08 06:36:21 +00:00
pub struct TimedInstruction<I,T>{
pub time:Time<T>,
2024-01-30 00:15:49 +00:00
pub instruction:I,
}
2025-01-08 06:36:21 +00:00
pub trait InstructionEmitter{
type Instruction;
type TimeInner;
fn next_instruction(&self,time_limit:Time<Self::TimeInner>)->Option<TimedInstruction<Self::Instruction,Self::TimeInner>>;
2024-01-30 00:15:49 +00:00
}
2025-01-08 06:36:21 +00:00
pub trait InstructionConsumer{
type Instruction;
type TimeInner;
fn process_instruction(&mut self, instruction:TimedInstruction<Self::Instruction,Self::TimeInner>);
2024-01-30 00:15:49 +00:00
}
//PROPER PRIVATE FIELDS!!!
2025-01-08 06:36:21 +00:00
pub struct InstructionCollector<I,T>{
time:Time<T>,
2024-01-30 00:15:49 +00:00
instruction:Option<I>,
}
2025-01-08 06:36:21 +00:00
impl<I,T> InstructionCollector<I,T>
where Time<T>:Copy+PartialOrd,
{
pub const fn new(time:Time<T>)->Self{
2024-01-30 00:15:49 +00:00
Self{
time,
instruction:None
}
}
#[inline]
2025-01-08 06:36:21 +00:00
pub const fn time(&self)->Time<T>{
2024-01-30 00:15:49 +00:00
self.time
}
2025-01-08 06:36:21 +00:00
pub fn collect(&mut self,instruction:Option<TimedInstruction<I,T>>){
2024-01-30 00:15:49 +00:00
match instruction{
Some(unwrap_instruction)=>{
if unwrap_instruction.time<self.time {
self.time=unwrap_instruction.time;
self.instruction=Some(unwrap_instruction.instruction);
}
},
None=>(),
}
}
2025-01-08 06:36:21 +00:00
pub fn instruction(self)->Option<TimedInstruction<I,T>>{
2024-01-30 00:15:49 +00:00
//STEAL INSTRUCTION AND DESTROY INSTRUCTIONCOLLECTOR
match self.instruction{
Some(instruction)=>Some(TimedInstruction{
time:self.time,
instruction
}),
None=>None,
}
}
2024-08-02 20:45:10 +00:00
}