nyar_wasm/wasi_values/
arithmetic.rs

1use super::*;
2
3impl Hash for WasiValue {
4    fn hash<H: Hasher>(&self, state: &mut H) {
5        match self {
6            Self::Boolean(v) => {
7                "Boolean".hash(state);
8                v.hash(state)
9            }
10            Self::Integer8(v) => {
11                "Integer8".hash(state);
12                v.hash(state)
13            }
14            Self::Integer16(v) => {
15                "Integer16".hash(state);
16                v.hash(state)
17            }
18            Self::Integer32(v) => {
19                "Integer32".hash(state);
20                v.hash(state)
21            }
22            Self::Integer64(v) => {
23                "Integer64".hash(state);
24                v.hash(state)
25            }
26            Self::Unsigned8(v) => {
27                "Unsigned8".hash(state);
28                v.hash(state)
29            }
30            Self::Unsigned16(v) => {
31                "Unsigned16".hash(state);
32                v.hash(state)
33            }
34            Self::Unsigned32(v) => {
35                "Unsigned32".hash(state);
36                v.hash(state)
37            }
38            Self::Unsigned64(v) => {
39                "Unsigned64".hash(state);
40                v.hash(state)
41            }
42            Self::Float32(v) => {
43                "Float32".hash(state);
44                v.to_le_bytes().hash(state)
45            }
46            Self::Float64(v) => {
47                "Float64".hash(state);
48                v.to_le_bytes().hash(state)
49            }
50            Self::DynamicArray(v) => {
51                "DynamicArray".hash(state);
52                v.hash(state)
53            }
54        }
55    }
56}
57
58impl Eq for WasiValue {}
59
60impl PartialEq<Self> for WasiValue {
61    fn eq(&self, other: &Self) -> bool {
62        match (self, other) {
63            (Self::Boolean(lhs), Self::Boolean(rhs)) => lhs.eq(rhs),
64            (Self::Integer8(lhs), Self::Integer8(rhs)) => lhs.eq(rhs),
65            (Self::Integer16(lhs), Self::Integer16(rhs)) => lhs.eq(rhs),
66            (Self::Integer32(lhs), Self::Integer32(rhs)) => lhs.eq(rhs),
67            (Self::Integer64(lhs), Self::Integer64(rhs)) => lhs.eq(rhs),
68            (Self::Unsigned8(lhs), Self::Unsigned8(rhs)) => lhs.eq(rhs),
69            (Self::Unsigned16(lhs), Self::Unsigned16(rhs)) => lhs.eq(rhs),
70            (Self::Unsigned32(lhs), Self::Unsigned32(rhs)) => lhs.eq(rhs),
71            (Self::Unsigned64(lhs), Self::Unsigned64(rhs)) => lhs.eq(rhs),
72            // (Self::Float32(lhs), Self::Float32(rhs)) => lhs.eq(rhs),
73            // (Self::Float64(lhs), Self::Float64(rhs)) => lhs.eq(rhs),
74            // (Self::DynamicArray(lhs), Self::DynamicArray(rhs)) => lhs.eq(rhs),
75            _ => false,
76        }
77    }
78}
79
80impl Ord for WasiValue {
81    fn cmp(&self, other: &Self) -> Ordering {
82        todo!()
83    }
84}
85
86impl PartialOrd<Self> for WasiValue {
87    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
88        match (self, other) {
89            (Self::Boolean(lhs), Self::Boolean(rhs)) => lhs.partial_cmp(rhs),
90            (Self::Integer8(lhs), Self::Integer8(rhs)) => lhs.partial_cmp(rhs),
91            (Self::Integer16(lhs), Self::Integer16(rhs)) => lhs.partial_cmp(rhs),
92            (Self::Integer32(lhs), Self::Integer32(rhs)) => lhs.partial_cmp(rhs),
93            (Self::Integer64(lhs), Self::Integer64(rhs)) => lhs.partial_cmp(rhs),
94            (Self::Unsigned8(lhs), Self::Unsigned8(rhs)) => lhs.partial_cmp(rhs),
95            (Self::Unsigned16(lhs), Self::Unsigned16(rhs)) => lhs.partial_cmp(rhs),
96            (Self::Unsigned32(lhs), Self::Unsigned32(rhs)) => lhs.partial_cmp(rhs),
97            (Self::Unsigned64(lhs), Self::Unsigned64(rhs)) => lhs.partial_cmp(rhs),
98            // (Self::Float32(lhs), Self::Float32(rhs)) => lhs.eq(rhs),
99            // (Self::Float64(lhs), Self::Float64(rhs)) => lhs.eq(rhs),
100            // (Self::DynamicArray(lhs), Self::DynamicArray(rhs)) => lhs.eq(rhs),
101            _ => None,
102        }
103    }
104}
105
106impl ToWasiType for WasiValue {
107    fn to_wasi_type(&self) -> WasiType {
108        match self {
109            Self::Boolean(_) => WasiType::Boolean,
110            Self::Integer8(_) => WasiType::Integer8 { signed: true },
111            Self::Integer16(_) => WasiType::Integer8 { signed: true },
112            Self::Integer32(_) => WasiType::Integer8 { signed: true },
113            Self::Integer64(_) => WasiType::Integer8 { signed: true },
114            Self::Unsigned8(_) => WasiType::Integer8 { signed: false },
115            Self::Unsigned16(_) => WasiType::Integer8 { signed: false },
116            Self::Unsigned32(_) => WasiType::Integer8 { signed: false },
117            Self::Unsigned64(_) => WasiType::Integer8 { signed: false },
118            Self::Float32(_) => WasiType::Float32,
119            Self::Float64(_) => WasiType::Float64,
120            Self::DynamicArray(v) => v.to_wasi_type(),
121        }
122    }
123}