use crate::{
global::RC,
vm::alpha::io::{Channel, InputChannel},
};
use nar_dev_utils::RefCount;
use navm::cmd::Cmd;
use std::collections::VecDeque;
#[derive(Debug, Clone, Default)]
pub struct ChannelIn {
cached_inputs: VecDeque<Cmd>,
}
impl ChannelIn {
pub fn new() -> Self {
Self::default()
}
pub fn put(&mut self, cmd: Cmd) {
self.cached_inputs.push_back(cmd);
}
#[inline]
pub fn put_rc(this: &mut RC<Self>, cmd: Cmd) {
this.mut_().put(cmd);
}
pub fn fetch(&mut self) -> Option<Cmd> {
self.cached_inputs.pop_front()
}
}
impl Channel for ChannelIn {
fn need_remove(&self) -> bool {
false
}
}
impl InputChannel for ChannelIn {
fn next_input(&mut self) -> (bool, Vec<Cmd>) {
let cmd = self.fetch();
(cmd.is_some(), cmd.into_iter().collect())
}
}
impl Channel for RC<ChannelIn> {
fn need_remove(&self) -> bool {
self.get_().need_remove()
}
}
impl InputChannel for RC<ChannelIn> {
fn next_input(&mut self) -> (bool, Vec<Cmd>) {
self.mut_().next_input()
}
}