Problems: * the Heap::new constructor is memory-unsafe with any value other than Undefined/Null * this means that moving dictionaries containing Heap values (ie. any/object) is memory-unsafe * unions containing GC pointers are similarly unsafe Solutions: - dictionaries containing GC pointers are now wrapped in RootedTraceableBox (even inside other dictionaries) - instead of using Heap::new, dictionaries containing GC pointers are now initialized to a default value (by deriving Default) and mutated one field at a time - dictionaries containing GC pointers are marked #[must_root] - FromJSVal for dictionaries containing GC pointers now returns RootedTraceableBox<Dictionary> - unions wrap their variants that require rooting in RootedTraceableBox Rather than attempting to derive Default for all dictionaries, we only do so for the dictionaries that require it. Because some dictionaries that require it inherit from dictionaries that do not, we need to write manual implementations for those parent dictionaries. This is a better option than having to figure out a default value for types like `Root<T>`, which would be required for deriving Default for all dictionaries. I would still like to come up with an automated test for this, but I figured I would get eyes on this first. --- - [X] `./mach build -d` does not report any errors - [X] `./mach test-tidy` does not report any errors - [X] These changes fix #16952 - [ ] There are tests for these changes Source-Repo: https://github.com/servo/servo Source-Revision: 532dee36c10b7dd5d33e560b55cf65c7243ef1d3
55 lines
1.3 KiB
Rust
55 lines
1.3 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::default::Default;
|
|
use std::ops::Deref;
|
|
|
|
/// 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(ref value) = self;
|
|
value
|
|
}
|
|
}
|
|
|
|
impl<T: Float + HeapSizeOf> HeapSizeOf for Finite<T> {
|
|
fn heap_size_of_children(&self) -> usize {
|
|
(**self).heap_size_of_children()
|
|
}
|
|
}
|
|
|
|
impl<T: Float + Default> Default for Finite<T> {
|
|
fn default() -> Finite<T> {
|
|
Finite::wrap(T::default())
|
|
}
|
|
}
|