rune/runtime/
range_to.rs
1use core::cmp::Ordering;
2use core::fmt;
3use core::ops;
4
5use crate as rune;
6use crate::alloc::clone::TryClone;
7use crate::runtime::{
8 EnvProtocolCaller, FromValue, ProtocolCaller, RuntimeError, ToValue, Value, VmResult,
9};
10use crate::Any;
11
12#[derive(Any, Clone, TryClone)]
45#[try_clone(crate)]
46#[rune(crate)]
47#[rune(constructor, item = ::std::ops)]
48pub struct RangeTo {
49 #[rune(get, set)]
51 pub end: Value,
52}
53
54impl RangeTo {
55 pub fn new(end: Value) -> Self {
57 Self { end }
58 }
59
60 #[rune::function(keep, protocol = PARTIAL_EQ)]
75 pub fn partial_eq(&self, other: &Self) -> VmResult<bool> {
76 self.partial_eq_with(other, &mut EnvProtocolCaller)
77 }
78
79 pub(crate) fn partial_eq_with(
80 &self,
81 b: &Self,
82 caller: &mut dyn ProtocolCaller,
83 ) -> VmResult<bool> {
84 Value::partial_eq_with(&self.end, &b.end, caller)
85 }
86
87 #[rune::function(keep, protocol = EQ)]
99 pub fn eq(&self, other: &Self) -> VmResult<bool> {
100 self.eq_with(other, &mut EnvProtocolCaller)
101 }
102
103 pub(crate) fn eq_with(&self, b: &Self, caller: &mut dyn ProtocolCaller) -> VmResult<bool> {
104 Value::eq_with(&self.end, &b.end, caller)
105 }
106
107 #[rune::function(keep, protocol = PARTIAL_CMP)]
118 pub fn partial_cmp(&self, other: &Self) -> VmResult<Option<Ordering>> {
119 self.partial_cmp_with(other, &mut EnvProtocolCaller)
120 }
121
122 pub(crate) fn partial_cmp_with(
123 &self,
124 b: &Self,
125 caller: &mut dyn ProtocolCaller,
126 ) -> VmResult<Option<Ordering>> {
127 Value::partial_cmp_with(&self.end, &b.end, caller)
128 }
129
130 #[rune::function(keep, protocol = CMP)]
142 pub fn cmp(&self, other: &Self) -> VmResult<Ordering> {
143 self.cmp_with(other, &mut EnvProtocolCaller)
144 }
145
146 pub(crate) fn cmp_with(&self, b: &Self, caller: &mut dyn ProtocolCaller) -> VmResult<Ordering> {
147 Value::cmp_with(&self.end, &b.end, caller)
148 }
149
150 #[rune::function(keep)]
167 pub(crate) fn contains(&self, value: Value) -> VmResult<bool> {
168 self.contains_with(value, &mut EnvProtocolCaller)
169 }
170
171 pub(crate) fn contains_with(
172 &self,
173 value: Value,
174 caller: &mut dyn ProtocolCaller,
175 ) -> VmResult<bool> {
176 VmResult::Ok(matches!(
177 vm_try!(Value::partial_cmp_with(&self.end, &value, caller)),
178 Some(Ordering::Greater)
179 ))
180 }
181}
182
183impl fmt::Debug for RangeTo {
184 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
185 write!(f, "..{:?}", self.end)
186 }
187}
188
189impl<Idx> ToValue for ops::RangeTo<Idx>
190where
191 Idx: ToValue,
192{
193 fn to_value(self) -> Result<Value, RuntimeError> {
194 let end = self.end.to_value()?;
195 Ok(Value::new(RangeTo::new(end))?)
196 }
197}
198
199impl<Idx> FromValue for ops::RangeTo<Idx>
200where
201 Idx: FromValue,
202{
203 #[inline]
204 fn from_value(value: Value) -> Result<Self, RuntimeError> {
205 let range = value.downcast::<RangeTo>()?;
206 let end = Idx::from_value(range.end)?;
207 Ok(ops::RangeTo { end })
208 }
209}