rune/runtime/
range_to_inclusive.rs1use 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#[rune(constructor, item = ::std::ops)]
45pub struct RangeToInclusive {
46 #[rune(get, set)]
48 pub end: Value,
49}
50
51impl RangeToInclusive {
52 pub fn new(end: Value) -> Self {
54 Self { end }
55 }
56
57 #[rune::function(keep, protocol = PARTIAL_EQ)]
72 pub fn partial_eq(&self, other: &Self) -> Result<bool, VmError> {
73 self.partial_eq_with(other, &mut EnvProtocolCaller)
74 }
75
76 pub(crate) fn partial_eq_with(
77 &self,
78 b: &Self,
79 caller: &mut dyn ProtocolCaller,
80 ) -> Result<bool, VmError> {
81 Value::partial_eq_with(&self.end, &b.end, caller)
82 }
83
84 #[rune::function(keep, protocol = EQ)]
96 pub fn eq(&self, other: &Self) -> Result<bool, VmError> {
97 self.eq_with(other, &mut EnvProtocolCaller)
98 }
99
100 pub(crate) fn eq_with(
101 &self,
102 b: &Self,
103 caller: &mut dyn ProtocolCaller,
104 ) -> Result<bool, VmError> {
105 Value::eq_with(&self.end, &b.end, caller)
106 }
107
108 #[rune::function(keep, protocol = PARTIAL_CMP)]
119 pub fn partial_cmp(&self, other: &Self) -> Result<Option<Ordering>, VmError> {
120 self.partial_cmp_with(other, &mut EnvProtocolCaller)
121 }
122
123 pub(crate) fn partial_cmp_with(
124 &self,
125 b: &Self,
126 caller: &mut dyn ProtocolCaller,
127 ) -> Result<Option<Ordering>, VmError> {
128 Value::partial_cmp_with(&self.end, &b.end, caller)
129 }
130
131 #[rune::function(keep, protocol = CMP)]
143 pub fn cmp(&self, other: &Self) -> Result<Ordering, VmError> {
144 self.cmp_with(other, &mut EnvProtocolCaller)
145 }
146
147 pub(crate) fn cmp_with(
148 &self,
149 b: &Self,
150 caller: &mut dyn ProtocolCaller,
151 ) -> Result<Ordering, VmError> {
152 Value::cmp_with(&self.end, &b.end, caller)
153 }
154
155 #[rune::function(keep)]
172 pub(crate) fn contains(&self, value: Value) -> Result<bool, VmError> {
173 self.contains_with(value, &mut EnvProtocolCaller)
174 }
175
176 pub(crate) fn contains_with(
177 &self,
178 value: Value,
179 caller: &mut dyn ProtocolCaller,
180 ) -> Result<bool, VmError> {
181 Ok(matches!(
182 Value::partial_cmp_with(&self.end, &value, caller)?,
183 Some(Ordering::Greater | Ordering::Equal)
184 ))
185 }
186}
187
188impl fmt::Debug for RangeToInclusive {
189 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
190 write!(f, "..={:?}", self.end)
191 }
192}
193
194impl<Idx> ToValue for ops::RangeToInclusive<Idx>
195where
196 Idx: ToValue,
197{
198 fn to_value(self) -> Result<Value, RuntimeError> {
199 let end = self.end.to_value()?;
200 Ok(Value::new(RangeToInclusive::new(end))?)
201 }
202}
203
204impl<Idx> FromValue for ops::RangeToInclusive<Idx>
205where
206 Idx: FromValue,
207{
208 #[inline]
209 fn from_value(value: Value) -> Result<Self, RuntimeError> {
210 let range = value.downcast::<RangeToInclusive>()?;
211 let end = Idx::from_value(range.end)?;
212 Ok(ops::RangeToInclusive { end })
213 }
214}