Derive the Default trait for dictionaries containing GC values.

This commit is contained in:
Josh Matthews 2017-05-26 13:46:13 -04:00
parent b169689f32
commit 16166d6673
6 changed files with 49 additions and 7 deletions

View file

@ -4028,12 +4028,18 @@ impl super::%s {
} }
} }
impl Default for super::%s {
fn default() -> super::%s {
pairs[0].1
}
}
impl ToJSValConvertible for super::%s { impl ToJSValConvertible for super::%s {
unsafe fn to_jsval(&self, cx: *mut JSContext, rval: MutableHandleValue) { unsafe fn to_jsval(&self, cx: *mut JSContext, rval: MutableHandleValue) {
pairs[*self as usize].0.to_jsval(cx, rval); pairs[*self as usize].0.to_jsval(cx, rval);
} }
} }
""" % (ident, pairs, ident, ident) """ % (ident, pairs, ident, ident, ident, ident)
self.cgRoot = CGList([ self.cgRoot = CGList([
CGGeneric(decl), CGGeneric(decl),
CGNamespace.build([ident + "Values"], CGNamespace.build([ident + "Values"],
@ -6038,17 +6044,22 @@ class CGDictionary(CGThing):
(self.makeMemberName(m[0].identifier.name), self.getMemberType(m)) (self.makeMemberName(m[0].identifier.name), self.getMemberType(m))
for m in self.memberInfo] for m in self.memberInfo]
mustRoot = "#[must_root]\n" if self.membersNeedTracing() else "" derive = ["JSTraceable"]
mustRoot = ""
if self.membersNeedTracing():
mustRoot = "#[must_root]\n"
derive += ["Default"]
return (string.Template( return (string.Template(
"#[derive(JSTraceable)]\n" "#[derive(${derive})]\n"
"${mustRoot}" + "${mustRoot}" +
"pub struct ${selfName} {\n" + "pub struct ${selfName} {\n" +
"${inheritance}" + "${inheritance}" +
"\n".join(memberDecls) + "\n" + "\n".join(memberDecls) + "\n" +
"}").substitute({"selfName": self.makeClassName(d), "}").substitute({"selfName": self.makeClassName(d),
"inheritance": inheritance, "inheritance": inheritance,
"mustRoot": mustRoot})) "mustRoot": mustRoot,
"derive": ', '.join(derive)}))
def impl(self): def impl(self):
d = self.dictionary d = self.dictionary

View file

@ -6,6 +6,7 @@
use heapsize::HeapSizeOf; use heapsize::HeapSizeOf;
use num_traits::Float; use num_traits::Float;
use std::default::Default;
use std::ops::Deref; use std::ops::Deref;
/// Encapsulates the IDL restricted float type. /// Encapsulates the IDL restricted float type.
@ -45,3 +46,9 @@ impl<T: Float + HeapSizeOf> HeapSizeOf for Finite<T> {
(**self).heap_size_of_children() (**self).heap_size_of_children()
} }
} }
impl<T: Float + Default> Default for Finite<T> {
fn default() -> Finite<T> {
Finite::wrap(T::default())
}
}

View file

@ -9,6 +9,7 @@ use html5ever::{LocalName, Namespace};
use servo_atoms::Atom; use servo_atoms::Atom;
use std::ascii::AsciiExt; use std::ascii::AsciiExt;
use std::borrow::{Borrow, Cow, ToOwned}; use std::borrow::{Borrow, Cow, ToOwned};
use std::default::Default;
use std::fmt; use std::fmt;
use std::hash::{Hash, Hasher}; use std::hash::{Hash, Hasher};
use std::marker::PhantomData; use std::marker::PhantomData;
@ -18,7 +19,7 @@ use std::str;
use std::str::{Bytes, FromStr}; use std::str::{Bytes, FromStr};
/// Encapsulates the IDL `ByteString` type. /// Encapsulates the IDL `ByteString` type.
#[derive(Clone, Debug, Eq, HeapSizeOf, JSTraceable, PartialEq)] #[derive(Clone, Debug, Default, Eq, HeapSizeOf, JSTraceable, PartialEq)]
pub struct ByteString(Vec<u8>); pub struct ByteString(Vec<u8>);
impl ByteString { impl ByteString {
@ -77,7 +78,7 @@ impl ops::Deref for ByteString {
/// A string that is constructed from a UCS-2 buffer by replacing invalid code /// A string that is constructed from a UCS-2 buffer by replacing invalid code
/// points with the replacement character. /// points with the replacement character.
#[derive(Clone, HeapSizeOf)] #[derive(Clone, Default, HeapSizeOf)]
pub struct USVString(pub String); pub struct USVString(pub String);

View file

@ -769,6 +769,12 @@ impl<T: JSTraceable + 'static> RootedTraceableBox<T> {
} }
} }
impl<T: JSTraceable + Default> Default for RootedTraceableBox<T> {
fn default() -> RootedTraceableBox<T> {
RootedTraceableBox::new(T::default())
}
}
impl<T: JSTraceable> Deref for RootedTraceableBox<T> { impl<T: JSTraceable> Deref for RootedTraceableBox<T> {
type Target = T; type Target = T;
fn deref(&self) -> &T { fn deref(&self) -> &T {

View file

@ -522,3 +522,12 @@ fn inner_invoke(window: Option<&Window>,
// Step 3. // Step 3.
found found
} }
impl Default for EventBinding::EventInit {
fn default() -> EventBinding::EventInit {
EventBinding::EventInit {
bubbles: false,
cancelable: false,
}
}
}

View file

@ -2,7 +2,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this * 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/. */ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
use dom::bindings::codegen::Bindings::EventBinding::EventMethods; use dom::bindings::codegen::Bindings::EventBinding::{self, EventMethods};
use dom::bindings::codegen::Bindings::ExtendableEventBinding; use dom::bindings::codegen::Bindings::ExtendableEventBinding;
use dom::bindings::error::{Error, ErrorResult, Fallible}; use dom::bindings::error::{Error, ErrorResult, Fallible};
use dom::bindings::inheritance::Castable; use dom::bindings::inheritance::Castable;
@ -67,3 +67,11 @@ impl ExtendableEvent {
self.event.IsTrusted() self.event.IsTrusted()
} }
} }
impl Default for ExtendableEventBinding::ExtendableEventInit {
fn default() -> ExtendableEventBinding::ExtendableEventInit {
ExtendableEventBinding::ExtendableEventInit {
parent: EventBinding::EventInit::default(),
}
}
}