ad_astra/exports/
boolean.rs1use std::cmp::Ordering;
36
37use crate::{
38 export,
39 exports::utils::transparent_upcast,
40 runtime::{
41 ops::{
42 ScriptAnd,
43 ScriptAssign,
44 ScriptClone,
45 ScriptConcat,
46 ScriptDebug,
47 ScriptDefault,
48 ScriptDisplay,
49 ScriptHash,
50 ScriptOr,
51 ScriptOrd,
52 ScriptPartialEq,
53 ScriptPartialOrd,
54 },
55 Arg,
56 Cell,
57 Downcast,
58 Origin,
59 Provider,
60 RuntimeResult,
61 ScriptType,
62 TypeHint,
63 __intrinsics::canonicals::{script_assign, script_concat},
64 },
65};
66
67#[export(include)]
69type BoolType = bool;
70
71impl<'a> Downcast<'a> for BoolType {
72 fn downcast(origin: Origin, provider: Provider<'a>) -> RuntimeResult<Self> {
73 let mut type_match = provider.type_match();
74
75 if type_match.is::<BoolType>() {
76 return provider.to_owned().take::<BoolType>(origin);
77 }
78
79 return Err(type_match.mismatch(origin));
80 }
81
82 #[inline(always)]
83 fn hint() -> TypeHint {
84 TypeHint::Type(BoolType::type_meta())
85 }
86}
87
88impl<'a> Downcast<'a> for &'a BoolType {
89 fn downcast(origin: Origin, provider: Provider<'a>) -> RuntimeResult<Self> {
90 let mut type_match = provider.type_match();
91
92 if type_match.is::<BoolType>() {
93 return provider.to_borrowed(&origin)?.borrow_ref(origin);
94 }
95
96 return Err(type_match.mismatch(origin));
97 }
98
99 #[inline(always)]
100 fn hint() -> TypeHint {
101 TypeHint::Type(BoolType::type_meta())
102 }
103}
104
105impl<'a> Downcast<'a> for &'a mut BoolType {
106 fn downcast(origin: Origin, provider: Provider<'a>) -> RuntimeResult<Self> {
107 let mut type_match = provider.type_match();
108
109 if type_match.is::<BoolType>() {
110 return provider.to_borrowed(&origin)?.borrow_mut(origin);
111 }
112
113 return Err(type_match.mismatch(origin));
114 }
115
116 #[inline(always)]
117 fn hint() -> TypeHint {
118 TypeHint::Type(BoolType::type_meta())
119 }
120}
121
122transparent_upcast!(BoolType);
123
124#[export(include)]
125impl ScriptAssign for BoolType {
126 type RHS = Self;
127
128 fn script_assign(_origin: Origin, lhs: Arg, rhs: Arg) -> RuntimeResult<()> {
129 script_assign::<Self>(lhs, rhs)
130 }
131}
132
133#[export(include)]
134impl ScriptConcat for BoolType {
135 type Result = Self;
136
137 fn script_concat(origin: Origin, items: &mut [Arg]) -> RuntimeResult<Cell> {
138 script_concat::<Self>(origin, items)
139 }
140}
141
142#[export(include)]
143impl ScriptClone for BoolType {}
144
145#[export(include)]
146impl ScriptDebug for BoolType {}
147
148#[export(include)]
149impl ScriptDisplay for BoolType {}
150
151#[export(include)]
152impl ScriptPartialEq for BoolType {
153 type RHS = BoolType;
154
155 fn script_eq(_origin: Origin, mut lhs: Arg, mut rhs: Arg) -> RuntimeResult<bool> {
156 let lhs = lhs.data.borrow_ref::<BoolType>(lhs.origin)?;
157 let rhs = rhs.data.borrow_ref::<BoolType>(rhs.origin)?;
158
159 Ok(lhs == rhs)
160 }
161}
162
163#[export(include)]
164impl ScriptDefault for BoolType {
165 fn script_default(origin: Origin) -> RuntimeResult<Cell> {
166 Cell::give(origin, BoolType::default())
167 }
168}
169
170#[export(include)]
171impl ScriptPartialOrd for BoolType {
172 type RHS = BoolType;
173
174 fn script_partial_cmp(
175 _origin: Origin,
176 mut lhs: Arg,
177 mut rhs: Arg,
178 ) -> RuntimeResult<Option<Ordering>> {
179 let lhs = lhs.data.borrow_ref::<BoolType>(lhs.origin)?;
180 let rhs = rhs.data.borrow_ref::<BoolType>(rhs.origin)?;
181
182 Ok(lhs.partial_cmp(rhs))
183 }
184}
185
186#[export(include)]
187impl ScriptOrd for BoolType {
188 fn script_cmp(_origin: Origin, mut lhs: Arg, mut rhs: Arg) -> RuntimeResult<Ordering> {
189 let lhs = lhs.data.borrow_ref::<BoolType>(lhs.origin)?;
190 let rhs = rhs.data.borrow_ref::<BoolType>(rhs.origin)?;
191
192 Ok(lhs.cmp(rhs))
193 }
194}
195
196#[export(include)]
197impl ScriptHash for BoolType {}
198
199#[export(include)]
200impl ScriptAnd for BoolType {
201 type RHS = BoolType;
202 type Result = BoolType;
203
204 fn script_and(origin: Origin, lhs: Arg, rhs: Arg) -> RuntimeResult<Cell> {
205 let lhs = lhs.data.take::<BoolType>(lhs.origin)?;
206 let rhs = rhs.data.take::<BoolType>(rhs.origin)?;
207
208 Cell::give(origin, lhs && rhs)
209 }
210}
211
212#[export(include)]
213impl ScriptOr for BoolType {
214 type RHS = BoolType;
215 type Result = BoolType;
216
217 fn script_or(origin: Origin, lhs: Arg, rhs: Arg) -> RuntimeResult<Cell> {
218 let lhs = lhs.data.take::<BoolType>(lhs.origin)?;
219 let rhs = rhs.data.take::<BoolType>(rhs.origin)?;
220
221 Cell::give(origin, lhs || rhs)
222 }
223}