1use core::cmp::Ordering;
2use core::fmt;
3use core::ops;
4
5use crate as rune;
6use crate::alloc::clone::TryClone;
7use crate::Any;
8
9use super::{EnvProtocolCaller, FromValue, ProtocolCaller, RuntimeError, ToValue, Value, VmError};
10
11#[derive(Any, Clone, TryClone)]
44#[try_clone(crate)]
45#[rune(crate)]
46#[rune(constructor, item = ::std::ops)]
47pub struct RangeTo {
48 #[rune(get, set)]
50 pub end: Value,
51}
52
53impl RangeTo {
54 pub fn new(end: Value) -> Self {
56 Self { end }
57 }
58
59 #[rune::function(keep, protocol = PARTIAL_EQ)]
74 pub fn partial_eq(&self, other: &Self) -> Result<bool, VmError> {
75 self.partial_eq_with(other, &mut EnvProtocolCaller)
76 }
77
78 pub(crate) fn partial_eq_with(
79 &self,
80 b: &Self,
81 caller: &mut dyn ProtocolCaller,
82 ) -> Result<bool, VmError> {
83 Value::partial_eq_with(&self.end, &b.end, caller)
84 }
85
86 #[rune::function(keep, protocol = EQ)]
98 pub fn eq(&self, other: &Self) -> Result<bool, VmError> {
99 self.eq_with(other, &mut EnvProtocolCaller)
100 }
101
102 pub(crate) fn eq_with(
103 &self,
104 b: &Self,
105 caller: &mut dyn ProtocolCaller,
106 ) -> Result<bool, VmError> {
107 Value::eq_with(&self.end, &b.end, caller)
108 }
109
110 #[rune::function(keep, protocol = PARTIAL_CMP)]
121 pub fn partial_cmp(&self, other: &Self) -> Result<Option<Ordering>, VmError> {
122 self.partial_cmp_with(other, &mut EnvProtocolCaller)
123 }
124
125 pub(crate) fn partial_cmp_with(
126 &self,
127 b: &Self,
128 caller: &mut dyn ProtocolCaller,
129 ) -> Result<Option<Ordering>, VmError> {
130 Value::partial_cmp_with(&self.end, &b.end, caller)
131 }
132
133 #[rune::function(keep, protocol = CMP)]
145 pub fn cmp(&self, other: &Self) -> Result<Ordering, VmError> {
146 self.cmp_with(other, &mut EnvProtocolCaller)
147 }
148
149 pub(crate) fn cmp_with(
150 &self,
151 b: &Self,
152 caller: &mut dyn ProtocolCaller,
153 ) -> Result<Ordering, VmError> {
154 Value::cmp_with(&self.end, &b.end, caller)
155 }
156
157 #[rune::function(keep)]
174 pub(crate) fn contains(&self, value: Value) -> Result<bool, VmError> {
175 self.contains_with(value, &mut EnvProtocolCaller)
176 }
177
178 pub(crate) fn contains_with(
179 &self,
180 value: Value,
181 caller: &mut dyn ProtocolCaller,
182 ) -> Result<bool, VmError> {
183 Ok(matches!(
184 Value::partial_cmp_with(&self.end, &value, caller)?,
185 Some(Ordering::Greater)
186 ))
187 }
188}
189
190impl fmt::Debug for RangeTo {
191 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
192 write!(f, "..{:?}", self.end)
193 }
194}
195
196impl<Idx> ToValue for ops::RangeTo<Idx>
197where
198 Idx: ToValue,
199{
200 fn to_value(self) -> Result<Value, RuntimeError> {
201 let end = self.end.to_value()?;
202 Ok(Value::new(RangeTo::new(end))?)
203 }
204}
205
206impl<Idx> FromValue for ops::RangeTo<Idx>
207where
208 Idx: FromValue,
209{
210 #[inline]
211 fn from_value(value: Value) -> Result<Self, RuntimeError> {
212 let range = value.downcast::<RangeTo>()?;
213 let end = Idx::from_value(range.end)?;
214 Ok(ops::RangeTo { end })
215 }
216}