pub enum Number {
Integer(i64),
Real(NotNan<f64>),
}Expand description
Subset of ExprKind that covers number-type expression values.
Variants§
Implementations§
Source§impl Number
impl Number
Sourcepub fn real(r: f64) -> Number
pub fn real(r: f64) -> Number
§Panics
This function will panic if r is NaN.
TODO: Change this function to take NotNan instead, so the caller doesn’t have to
worry about panics.
Examples found in repository?
examples/wstp.rs (line 224)
202fn total(args: Vec<Expr>) -> Expr {
203 let mut total = Number::Integer(0);
204
205 for (index, arg) in args.into_iter().enumerate() {
206 let number = match arg.try_as_number() {
207 Some(number) => number,
208 None => panic!(
209 "expected argument at position {} to be a number, got {}",
210 // Add +1 to display using WL 1-based indexing.
211 index + 1,
212 arg
213 ),
214 };
215
216 use Number::{Integer, Real};
217
218 total = match (total, number) {
219 // If the sum and new term are integers, use integers.
220 (Integer(total), Integer(term)) => Integer(total + term),
221 // Otherwise, if the either the total or new term are machine real numbers,
222 // use floating point numbers.
223 (Integer(int), Real(real)) | (Real(real), Integer(int)) => {
224 Number::real(int as f64 + *real)
225 },
226 (Real(total), Real(term)) => Real(total + term),
227 }
228 }
229
230 Expr::number(total)
231}Trait Implementations§
Source§impl PartialOrd for Number
impl PartialOrd for Number
impl Copy for Number
impl Eq for Number
impl StructuralPartialEq for Number
Auto Trait Implementations§
impl Freeze for Number
impl RefUnwindSafe for Number
impl Send for Number
impl Sync for Number
impl Unpin for Number
impl UnwindSafe for Number
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more