use crate::dsl::prelude::RudaPrimitive;
use crate::dsl::frontend::assign::expand_no_check;
use crate::dsl::prelude::*;
use ruda_core::ir::Operation;
use ruda_kernel_macros::intrinsic;
#[derive(Clone, Copy)]
pub struct RuntimeCell<T: RudaType> {
#[allow(unused)]
value: T,
}
pub struct RuntimeCellExpand<T: RudaType> {
value: <T as crate::dsl::prelude::RudaType>::ExpandType,
}
impl<T: RudaType> Clone for RuntimeCellExpand<T> {
fn clone(&self) -> Self {
Self {
value: self.value.clone(),
}
}
}
impl<T: RudaType> crate::dsl::prelude::RudaType for RuntimeCell<T> {
type ExpandType = RuntimeCellExpand<T>;
}
impl<T: RudaType> crate::dsl::prelude::IntoMut for RuntimeCellExpand<T> {
fn into_mut(self, _scope: &mut crate::dsl::prelude::Scope) -> Self {
Self {
value: self.value.clone(),
}
}
}
impl<T: RudaType> crate::dsl::prelude::RudaDebug for RuntimeCellExpand<T> {}
#[ruda]
impl<T: RudaPrimitive> RuntimeCell<T> {
#[allow(unused_variables)]
pub fn new(init: T) -> Self {
intrinsic!(|scope| {
let value = init_expand(scope, init.expand, true, Operation::Copy);
RuntimeCellExpand {
value: value.into(),
}
})
}
#[allow(unused_variables)]
pub fn store(&self, value: T) {
intrinsic!(|scope| {
expand_no_check(scope, value, self.value);
})
}
pub fn read(&self) -> T {
intrinsic!(|scope| {
let value = init_expand(scope, self.value.expand, false, Operation::Copy);
value.into()
})
}
pub fn consume(self) -> T {
intrinsic!(|scope| { self.value })
}
}
#[ruda]
impl<T: RudaIndexMut> RuntimeCell<T> {
#[allow(unused_variables)]
pub fn store_at(&mut self, index: <T as RudaIndex>::Idx, value: <T as RudaIndex>::Output) {
intrinsic!(|scope| { self.value.expand_index_mut(scope, index, value) })
}
}
#[ruda]
impl<T: RudaIndex> RuntimeCell<T> {
#[allow(unused_variables)]
pub fn read_at(&self, index: T::Idx) -> T::Output {
intrinsic!(|scope| { self.value.expand_index(scope, index) })
}
}