mirror of
https://github.com/mozilla/gecko-dev.git
synced 2025-11-12 22:28:59 +02:00
<!-- Please describe your changes on the following line: --> --- <!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `__` with appropriate data: --> - [x] `./mach build -d` does not report any errors - [x] `./mach test-tidy` does not report any errors - [ ] These changes fix #__ (github issue number if applicable). <!-- Either: --> - [ ] There are tests for these changes OR - [ ] These changes do not require tests because _____ <!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. --> Source-Repo: https://github.com/servo/servo Source-Revision: c6e15f8ef244a0f3859489ca7f1c7bcc80181468
47 lines
1.2 KiB
Rust
47 lines
1.2 KiB
Rust
/* 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 http://mozilla.org/MPL/2.0/. */
|
|
|
|
//! The `Finite<T>` struct.
|
|
|
|
use heapsize::HeapSizeOf;
|
|
use num_traits::Float;
|
|
use std::ops::Deref;
|
|
|
|
/// Encapsulates the IDL restricted float type.
|
|
#[derive(JSTraceable, Clone, Copy, Eq, 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(ref value) = self;
|
|
value
|
|
}
|
|
}
|
|
|
|
impl<T: Float + HeapSizeOf> HeapSizeOf for Finite<T> {
|
|
fn heap_size_of_children(&self) -> usize {
|
|
(**self).heap_size_of_children()
|
|
}
|
|
}
|