script_bindings/
num.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5//! The `Finite<T>` struct.
6
7use std::default::Default;
8use std::ops::Deref;
9
10use malloc_size_of::{MallocSizeOf, MallocSizeOfOps};
11use num_traits::Float;
12
13/// Encapsulates the IDL restricted float type.
14#[derive(Clone, Copy, Debug, Eq, JSTraceable, PartialEq)]
15pub struct Finite<T: Float>(T);
16
17impl<T: Float> Finite<T> {
18    /// Create a new `Finite<T: Float>` safely.
19    pub fn new(value: T) -> Option<Finite<T>> {
20        if value.is_finite() {
21            Some(Finite(value))
22        } else {
23            None
24        }
25    }
26
27    /// Create a new `Finite<T: Float>`.
28    #[inline]
29    pub fn wrap(value: T) -> Finite<T> {
30        assert!(
31            value.is_finite(),
32            "Finite<T> doesn't encapsulate unrestricted value."
33        );
34        Finite(value)
35    }
36}
37
38impl<T: Float> Deref for Finite<T> {
39    type Target = T;
40
41    fn deref(&self) -> &T {
42        let Finite(value) = self;
43        value
44    }
45}
46
47impl<T: Float + MallocSizeOf> MallocSizeOf for Finite<T> {
48    fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize {
49        (**self).size_of(ops)
50    }
51}
52
53impl<T: Float + Default> Default for Finite<T> {
54    fn default() -> Finite<T> {
55        Finite::wrap(T::default())
56    }
57}