diff options
author | Josh Matthews <josh@joshmatthews.net> | 2025-03-29 04:11:27 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2025-03-29 08:11:27 +0000 |
commit | c30ad5a30e10b9aabe5842624db24b9846c3d5d4 (patch) | |
tree | c879104dc5ef15647d975da14a6b703d4099df4d /components/script_bindings/num.rs | |
parent | 2c9411095255f804a43c2abc05c5aaa2a0becca7 (diff) | |
download | servo-c30ad5a30e10b9aabe5842624db24b9846c3d5d4.tar.gz servo-c30ad5a30e10b9aabe5842624db24b9846c3d5d4.zip |
Miscellaneous script splitting preparation changes (#36216)
* script: Move num module to script_bindings.
Signed-off-by: Josh Matthews <josh@joshmatthews.net>
* script: Make JS reflector creation generic over DOM trait.
Signed-off-by: Josh Matthews <josh@joshmatthews.net>
* script: Move bindings-specific lock to script_bindings.
Signed-off-by: Josh Matthews <josh@joshmatthews.net>
* script: Move DOM proto array code to script_bindings.
Signed-off-by: Josh Matthews <josh@joshmatthews.net>
* script: Move finalizer implementations to script_bindings.
Signed-off-by: Josh Matthews <josh@joshmatthews.net>
* script: Move some error routines to script_bindings.
Signed-off-by: Josh Matthews <josh@joshmatthews.net>
* script: Move some DOM interface conversion routines to script_bindings.
Signed-off-by: Josh Matthews <josh@joshmatthews.net>
* script: Make is_array_like generic over DOM trait.
Signed-off-by: Josh Matthews <josh@joshmatthews.net>
* script: Use generic interfaces for conditional exposure functions.
Signed-off-by: Josh Matthews <josh@joshmatthews.net>
* script: Move a bunch of routines used by codegen to script_bindings.
Signed-off-by: Josh Matthews <josh@joshmatthews.net>
* Formatting.
Signed-off-by: Josh Matthews <josh@joshmatthews.net>
* Fix clippy warnings.
Signed-off-by: Josh Matthews <josh@joshmatthews.net>
---------
Signed-off-by: Josh Matthews <josh@joshmatthews.net>
Diffstat (limited to 'components/script_bindings/num.rs')
-rw-r--r-- | components/script_bindings/num.rs | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/components/script_bindings/num.rs b/components/script_bindings/num.rs new file mode 100644 index 00000000000..58b12f6cb7f --- /dev/null +++ b/components/script_bindings/num.rs @@ -0,0 +1,57 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ + +//! The `Finite<T>` struct. + +use std::default::Default; +use std::ops::Deref; + +use malloc_size_of::{MallocSizeOf, MallocSizeOfOps}; +use num_traits::Float; + +/// Encapsulates the IDL restricted float type. +#[derive(Clone, Copy, Eq, JSTraceable, PartialEq)] +pub struct Finite<T: Float>(T); + +impl<T: Float> Finite<T> { + /// Create a new `Finite<T: Float>` safely. + pub fn new(value: T) -> Option<Finite<T>> { + if value.is_finite() { + Some(Finite(value)) + } else { + None + } + } + + /// Create a new `Finite<T: Float>`. + #[inline] + pub fn wrap(value: T) -> Finite<T> { + assert!( + value.is_finite(), + "Finite<T> doesn't encapsulate unrestricted value." + ); + Finite(value) + } +} + +impl<T: Float> Deref for Finite<T> { + type Target = T; + + fn deref(&self) -> &T { + let Finite(value) = self; + value + } +} + +impl<T: Float + MallocSizeOf> MallocSizeOf for Finite<T> { + fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize { + (**self).size_of(ops) + } +} + +impl<T: Float + Default> Default for Finite<T> { + fn default() -> Finite<T> { + Finite::wrap(T::default()) + } +} |