rune/runtime/
control_flow.rs1use core::ops;
2
3use crate as rune;
4use crate::alloc;
5use crate::alloc::clone::TryClone;
6use crate::alloc::fmt::TryWrite;
7use crate::Any;
8
9use super::{
10 EnvProtocolCaller, Formatter, FromValue, ProtocolCaller, RuntimeError, ToValue, Value, VmError,
11};
12
13#[derive(Debug, Clone, TryClone, Any)]
27#[try_clone(crate)]
28#[rune(item = ::std::ops)]
29pub enum ControlFlow {
30 #[rune(constructor)]
32 Continue(#[rune(get, set, dismantle)] Value),
33 #[rune(constructor)]
35 Break(#[rune(get, set, dismantle)] Value),
36}
37
38impl ControlFlow {
39 #[rune::function(keep, protocol = PARTIAL_EQ)]
60 pub(crate) fn partial_eq(&self, other: &Self) -> Result<bool, VmError> {
61 Self::partial_eq_with(self, other, &mut EnvProtocolCaller)
62 }
63
64 pub(crate) fn partial_eq_with(
65 &self,
66 other: &Self,
67 caller: &mut dyn ProtocolCaller,
68 ) -> Result<bool, VmError> {
69 match (self, other) {
70 (ControlFlow::Continue(a), ControlFlow::Continue(b)) => {
71 Value::partial_eq_with(a, b, caller)
72 }
73 (ControlFlow::Break(a), ControlFlow::Break(b)) => Value::partial_eq_with(a, b, caller),
74 _ => Ok(false),
75 }
76 }
77
78 #[rune::function(keep, protocol = EQ)]
99 pub(crate) fn eq(&self, other: &ControlFlow) -> Result<bool, VmError> {
100 self.eq_with(other, &mut EnvProtocolCaller)
101 }
102
103 pub(crate) fn eq_with(
104 &self,
105 other: &ControlFlow,
106 caller: &mut dyn ProtocolCaller,
107 ) -> Result<bool, VmError> {
108 match (self, other) {
109 (ControlFlow::Continue(a), ControlFlow::Continue(b)) => Value::eq_with(a, b, caller),
110 (ControlFlow::Break(a), ControlFlow::Break(b)) => Value::eq_with(a, b, caller),
111 _ => Ok(false),
112 }
113 }
114
115 #[rune::function(keep, protocol = DEBUG_FMT)]
125 pub(crate) fn debug_fmt(&self, f: &mut Formatter) -> Result<(), VmError> {
126 Self::debug_fmt_with(self, f, &mut EnvProtocolCaller)
127 }
128
129 pub(crate) fn debug_fmt_with(
130 &self,
131 f: &mut Formatter,
132 caller: &mut dyn ProtocolCaller,
133 ) -> Result<(), VmError> {
134 match self {
135 ControlFlow::Continue(value) => {
136 write!(f, "Continue(")?;
137 Value::debug_fmt_with(value, f, caller)?;
138 write!(f, ")")?;
139 }
140 ControlFlow::Break(value) => {
141 write!(f, "Break(")?;
142 Value::debug_fmt_with(value, f, caller)?;
143 write!(f, ")")?;
144 }
145 }
146
147 Ok(())
148 }
149
150 #[rune::function(keep, protocol = CLONE)]
163 pub(crate) fn clone(&self) -> alloc::Result<Self> {
164 self.try_clone()
165 }
166}
167
168impl<B, C> ToValue for ops::ControlFlow<B, C>
169where
170 B: ToValue,
171 C: ToValue,
172{
173 #[inline]
174 fn to_value(self) -> Result<Value, RuntimeError> {
175 let value = match self {
176 ops::ControlFlow::Continue(value) => ControlFlow::Continue(C::to_value(value)?),
177 ops::ControlFlow::Break(value) => ControlFlow::Break(B::to_value(value)?),
178 };
179
180 Ok(Value::try_from(value)?)
181 }
182}
183
184impl<B, C> FromValue for ops::ControlFlow<B, C>
185where
186 B: FromValue,
187 C: FromValue,
188{
189 #[inline]
190 fn from_value(value: Value) -> Result<Self, RuntimeError> {
191 Ok(match &*value.borrow_ref::<ControlFlow>()? {
192 ControlFlow::Continue(value) => {
193 ops::ControlFlow::Continue(C::from_value(value.clone())?)
194 }
195 ControlFlow::Break(value) => ops::ControlFlow::Break(B::from_value(value.clone())?),
196 })
197 }
198}