Skip to main content

ruda_kernel/dsl/frontend/
list.rs

1use core::ops::{Deref, DerefMut};
2
3use super::{RudaType, NativeExpand};
4use crate::dsl::{prelude::*, unexpanded};
5use ruda_core::ir::{Scope, VectorSize};
6
7/// Type from which we can read values in ruda functions.
8/// For a mutable version, see [`ListMut`].
9#[allow(clippy::len_without_is_empty)]
10#[ruda(self_type = "ref", expand_base_traits = "SliceOperatorExpand<T>")]
11pub trait List<T: RudaPrimitive>: SliceOperator<T> + Vectorized + Deref<Target = [T]> {
12    #[allow(unused)]
13    fn read(&self, index: usize) -> T {
14        unexpanded!()
15    }
16
17    #[allow(unused)]
18    fn read_unchecked(&self, index: usize) -> T {
19        unexpanded!()
20    }
21
22    #[allow(unused)]
23    fn len(&self) -> usize {
24        unexpanded!();
25    }
26}
27
28/// Type for which we can read and write values in ruda functions.
29/// For an immutable version, see [List].
30#[ruda(self_type = "ref", expand_base_traits = "SliceMutOperatorExpand<T>")]
31pub trait ListMut<T: RudaPrimitive>:
32    List<T> + SliceMutOperator<T> + DerefMut<Target = [T]>
33{
34    #[allow(unused)]
35    fn write(&self, index: usize, value: T) {
36        unexpanded!()
37    }
38}
39
40// Automatic implementation for references to List.
41impl<'a, T: RudaPrimitive, L: List<T>> List<T> for &'a L
42where
43    &'a L: RudaType<ExpandType = L::ExpandType>,
44    &'a L: Deref<Target = [T]>,
45{
46    fn read(&self, index: usize) -> T {
47        L::read(self, index)
48    }
49
50    fn __expand_read(
51        scope: &mut Scope,
52        this: Self::ExpandType,
53        index: NativeExpand<usize>,
54    ) -> <T as RudaType>::ExpandType {
55        L::__expand_read(scope, this, index)
56    }
57}
58
59// Automatic implementation for mutable references to List.
60impl<'a, T: RudaPrimitive, L: List<T>> List<T> for &'a mut L
61where
62    &'a mut L: RudaType<ExpandType = L::ExpandType>,
63    &'a mut L: Deref<Target = [T]>,
64{
65    fn read(&self, index: usize) -> T {
66        L::read(self, index)
67    }
68
69    fn __expand_read(
70        scope: &mut Scope,
71        this: Self::ExpandType,
72        index: NativeExpand<usize>,
73    ) -> <T as RudaType>::ExpandType {
74        L::__expand_read(scope, this, index)
75    }
76}
77
78// Automatic implementation for references to ListMut.
79impl<'a, T: RudaPrimitive, L: ListMut<T>> ListMut<T> for &'a L
80where
81    &'a L: RudaType<ExpandType = L::ExpandType>,
82    &'a L: DerefMut<Target = [T]>,
83{
84    fn write(&self, index: usize, value: T) {
85        L::write(self, index, value);
86    }
87
88    fn __expand_write(
89        scope: &mut Scope,
90        this: Self::ExpandType,
91        index: NativeExpand<usize>,
92        value: T::ExpandType,
93    ) {
94        L::__expand_write(scope, this, index, value);
95    }
96}
97
98// Automatic implementation for mutable references to ListMut.
99impl<'a, T: RudaPrimitive, L: ListMut<T>> ListMut<T> for &'a mut L
100where
101    &'a mut L: RudaType<ExpandType = L::ExpandType>,
102    &'a mut L: DerefMut<Target = [T]>,
103{
104    fn write(&self, index: usize, value: T) {
105        L::write(self, index, value);
106    }
107
108    fn __expand_write(
109        scope: &mut Scope,
110        this: Self::ExpandType,
111        index: NativeExpand<usize>,
112        value: T::ExpandType,
113    ) {
114        L::__expand_write(scope, this, index, value);
115    }
116}
117
118pub trait Vectorized: RudaType<ExpandType: VectorizedExpand> {
119    fn vector_size(&self) -> VectorSize {
120        unexpanded!()
121    }
122    fn __expand_vector_size(_scope: &mut Scope, this: Self::ExpandType) -> VectorSize {
123        this.vector_size()
124    }
125}
126
127pub trait VectorizedExpand {
128    fn vector_size(&self) -> VectorSize;
129    fn __expand_vector_size_method(&self, _scope: &mut Scope) -> VectorSize {
130        self.vector_size()
131    }
132}
133
134impl<'a, L: Vectorized> Vectorized for &'a L where &'a L: RudaType<ExpandType: VectorizedExpand> {}
135impl<'a, L: Vectorized> Vectorized for &'a mut L where
136    &'a mut L: RudaType<ExpandType: VectorizedExpand>
137{
138}