aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJosh Matthews <josh@joshmatthews.net>2017-05-26 13:46:13 -0400
committerJosh Matthews <josh@joshmatthews.net>2017-09-25 16:10:58 -0400
commit16166d66731d7040a91ddbed6ffd05d4fc64c97b (patch)
treeb1c20d92de7a3cc5a87e2d1c6ebfefa6a872c84d
parentb169689f32db6d497b04f3a5b173cba4acd33e93 (diff)
downloadservo-16166d66731d7040a91ddbed6ffd05d4fc64c97b.tar.gz
servo-16166d66731d7040a91ddbed6ffd05d4fc64c97b.zip
Derive the Default trait for dictionaries containing GC values.
-rw-r--r--components/script/dom/bindings/codegen/CodegenRust.py19
-rw-r--r--components/script/dom/bindings/num.rs7
-rw-r--r--components/script/dom/bindings/str.rs5
-rw-r--r--components/script/dom/bindings/trace.rs6
-rw-r--r--components/script/dom/event.rs9
-rw-r--r--components/script/dom/extendableevent.rs10
6 files changed, 49 insertions, 7 deletions
diff --git a/components/script/dom/bindings/codegen/CodegenRust.py b/components/script/dom/bindings/codegen/CodegenRust.py
index 5b2b987c233..aa58afe1913 100644
--- a/components/script/dom/bindings/codegen/CodegenRust.py
+++ b/components/script/dom/bindings/codegen/CodegenRust.py
@@ -4028,12 +4028,18 @@ impl super::%s {
}
}
+impl Default for super::%s {
+ fn default() -> super::%s {
+ pairs[0].1
+ }
+}
+
impl ToJSValConvertible for super::%s {
unsafe fn to_jsval(&self, cx: *mut JSContext, rval: MutableHandleValue) {
pairs[*self as usize].0.to_jsval(cx, rval);
}
}
- """ % (ident, pairs, ident, ident)
+ """ % (ident, pairs, ident, ident, ident, ident)
self.cgRoot = CGList([
CGGeneric(decl),
CGNamespace.build([ident + "Values"],
@@ -6038,17 +6044,22 @@ class CGDictionary(CGThing):
(self.makeMemberName(m[0].identifier.name), self.getMemberType(m))
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(
- "#[derive(JSTraceable)]\n"
+ "#[derive(${derive})]\n"
"${mustRoot}" +
"pub struct ${selfName} {\n" +
"${inheritance}" +
"\n".join(memberDecls) + "\n" +
"}").substitute({"selfName": self.makeClassName(d),
"inheritance": inheritance,
- "mustRoot": mustRoot}))
+ "mustRoot": mustRoot,
+ "derive": ', '.join(derive)}))
def impl(self):
d = self.dictionary
diff --git a/components/script/dom/bindings/num.rs b/components/script/dom/bindings/num.rs
index c7d24f4fb83..f7604cfab20 100644
--- a/components/script/dom/bindings/num.rs
+++ b/components/script/dom/bindings/num.rs
@@ -6,6 +6,7 @@
use heapsize::HeapSizeOf;
use num_traits::Float;
+use std::default::Default;
use std::ops::Deref;
/// Encapsulates the IDL restricted float type.
@@ -45,3 +46,9 @@ impl<T: Float + HeapSizeOf> HeapSizeOf for Finite<T> {
(**self).heap_size_of_children()
}
}
+
+impl<T: Float + Default> Default for Finite<T> {
+ fn default() -> Finite<T> {
+ Finite::wrap(T::default())
+ }
+}
diff --git a/components/script/dom/bindings/str.rs b/components/script/dom/bindings/str.rs
index ea0b4f529b0..b665b64da8a 100644
--- a/components/script/dom/bindings/str.rs
+++ b/components/script/dom/bindings/str.rs
@@ -9,6 +9,7 @@ use html5ever::{LocalName, Namespace};
use servo_atoms::Atom;
use std::ascii::AsciiExt;
use std::borrow::{Borrow, Cow, ToOwned};
+use std::default::Default;
use std::fmt;
use std::hash::{Hash, Hasher};
use std::marker::PhantomData;
@@ -18,7 +19,7 @@ use std::str;
use std::str::{Bytes, FromStr};
/// 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>);
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
/// points with the replacement character.
-#[derive(Clone, HeapSizeOf)]
+#[derive(Clone, Default, HeapSizeOf)]
pub struct USVString(pub String);
diff --git a/components/script/dom/bindings/trace.rs b/components/script/dom/bindings/trace.rs
index 797ee356bd8..cdb2fd425ac 100644
--- a/components/script/dom/bindings/trace.rs
+++ b/components/script/dom/bindings/trace.rs
@@ -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> {
type Target = T;
fn deref(&self) -> &T {
diff --git a/components/script/dom/event.rs b/components/script/dom/event.rs
index c9ef22bc6c1..cead9f74df9 100644
--- a/components/script/dom/event.rs
+++ b/components/script/dom/event.rs
@@ -522,3 +522,12 @@ fn inner_invoke(window: Option<&Window>,
// Step 3.
found
}
+
+impl Default for EventBinding::EventInit {
+ fn default() -> EventBinding::EventInit {
+ EventBinding::EventInit {
+ bubbles: false,
+ cancelable: false,
+ }
+ }
+}
diff --git a/components/script/dom/extendableevent.rs b/components/script/dom/extendableevent.rs
index 8cc88706fb1..553ed7a1fe4 100644
--- a/components/script/dom/extendableevent.rs
+++ b/components/script/dom/extendableevent.rs
@@ -2,7 +2,7 @@
* 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/. */
-use dom::bindings::codegen::Bindings::EventBinding::EventMethods;
+use dom::bindings::codegen::Bindings::EventBinding::{self, EventMethods};
use dom::bindings::codegen::Bindings::ExtendableEventBinding;
use dom::bindings::error::{Error, ErrorResult, Fallible};
use dom::bindings::inheritance::Castable;
@@ -67,3 +67,11 @@ impl ExtendableEvent {
self.event.IsTrusted()
}
}
+
+impl Default for ExtendableEventBinding::ExtendableEventInit {
+ fn default() -> ExtendableEventBinding::ExtendableEventInit {
+ ExtendableEventBinding::ExtendableEventInit {
+ parent: EventBinding::EventInit::default(),
+ }
+ }
+}