Merge all keyword arms in Clone for PropertyDeclaration 🐉🐲

We further partition the variants to retrieve all properties that are
defined as simple keywords, and we clone them by copying the value
as a smaller enum made of only the keyword variants.
This commit is contained in:
Anthony Ramine 2018-02-07 14:08:17 +01:00
parent cf4f89c36e
commit 8b9b3617a2

View file

@ -218,19 +218,20 @@ pub mod animated_properties {
"name": property.camel_case, "name": property.camel_case,
"type": ty, "type": ty,
"doc": "`" + property.name + "`", "doc": "`" + property.name + "`",
"keyword": bool(property.keyword) and not property.is_vector,
}) })
groups = {} groups = {}
keyfunc = lambda x: x["type"] keyfunc = lambda x: x["type"]
sortkeys = {} sortkeys = {}
for ty, group in groupby(sorted(variants, key=keyfunc), keyfunc): for ty, group in groupby(sorted(variants, key=keyfunc), keyfunc):
group = [v["name"] for v in group] group = list(group)
groups[ty] = group groups[ty] = group
for v in group: for v in group:
if len(group) == 1: if len(group) == 1:
sortkeys[v] = (1, v, "") sortkeys[v["name"]] = (not v["keyword"], 1, v["name"], "")
else: else:
sortkeys[v] = (len(group), ty, v) sortkeys[v["name"]] = (not v["keyword"], len(group), ty, v["name"])
variants.sort(key=lambda x: sortkeys[x["name"]]) variants.sort(key=lambda x: sortkeys[x["name"]])
# It is extremely important to sort the `data.longhands` array here so # It is extremely important to sort the `data.longhands` array here so
@ -250,21 +251,24 @@ pub mod animated_properties {
"name": "CSSWideKeyword", "name": "CSSWideKeyword",
"type": "WideKeywordDeclaration", "type": "WideKeywordDeclaration",
"doc": "A CSS-wide keyword.", "doc": "A CSS-wide keyword.",
"keyword": False,
}, },
{ {
"name": "WithVariables", "name": "WithVariables",
"type": "VariableDeclaration", "type": "VariableDeclaration",
"doc": "An unparsed declaration.", "doc": "An unparsed declaration.",
"keyword": False,
}, },
{ {
"name": "Custom", "name": "Custom",
"type": "CustomDeclaration", "type": "CustomDeclaration",
"doc": "A custom property declaration.", "doc": "A custom property declaration.",
"keyword": False,
}, },
] ]
for v in extra: for v in extra:
variants.append(v) variants.append(v)
groups[v["type"]] = [v["name"]] groups[v["type"]] = [v]
%> %>
/// Servo's representation for a property declaration. /// Servo's representation for a property declaration.
@ -287,14 +291,39 @@ impl Clone for PropertyDeclaration {
fn clone(&self) -> Self { fn clone(&self) -> Self {
use self::PropertyDeclaration::*; use self::PropertyDeclaration::*;
<%
[keywords, others] = [list(g) for _, g in groupby(variants, key=lambda x: not x["keyword"])]
%>
match *self { match *self {
% for ty, variants in groups.iteritems(): ${" | ".join("{}(..)".format(v["name"]) for v in keywords)} => {
% if len(variants) == 1: #[derive(Clone, Copy)]
${variants[0]}(ref value) => { #[repr(u16)]
${variants[0]}(value.clone()) enum Keywords {
% for v in keywords:
_${v["name"]}(${v["type"]}),
% endfor
}
unsafe {
let mut out = mem::uninitialized();
ptr::write(
&mut out as *mut _ as *mut Keywords,
*(self as *const _ as *const Keywords),
);
out
}
}
% for ty, vs in groupby(others, key=lambda x: x["type"]):
<%
vs = list(vs)
%>
% if len(vs) == 1:
${vs[0]["name"]}(ref value) => {
${vs[0]["name"]}(value.clone())
} }
% else: % else:
${" | ".join("{}(ref value)".format(v) for v in variants)} => { ${" | ".join("{}(ref value)".format(v["name"]) for v in vs)} => {
unsafe { unsafe {
let mut out = ManuallyDrop::new(mem::uninitialized()); let mut out = ManuallyDrop::new(mem::uninitialized());
ptr::write( ptr::write(
@ -327,8 +356,8 @@ impl PartialEq for PropertyDeclaration {
return false; return false;
} }
match *self { match *self {
% for ty, variants in groups.iteritems(): % for ty, vs in groupby(variants, key=lambda x: x["type"]):
${" | ".join("{}(ref this)".format(v) for v in variants)} => { ${" | ".join("{}(ref this)".format(v["name"]) for v in vs)} => {
let other_repr = let other_repr =
&*(other as *const _ as *const PropertyDeclarationVariantRepr<${ty}>); &*(other as *const _ as *const PropertyDeclarationVariantRepr<${ty}>);
*this == other_repr.value *this == other_repr.value
@ -346,8 +375,8 @@ impl MallocSizeOf for PropertyDeclaration {
use self::PropertyDeclaration::*; use self::PropertyDeclaration::*;
match *self { match *self {
% for ty, variants in groups.iteritems(): % for ty, vs in groupby(variants, key=lambda x: x["type"]):
${" | ".join("{}(ref value)".format(v) for v in variants)} => { ${" | ".join("{}(ref value)".format(v["name"]) for v in vs)} => {
value.size_of(ops) value.size_of(ops)
} }
% endfor % endfor
@ -365,8 +394,8 @@ impl PropertyDeclaration {
let mut dest = CssWriter::new(dest); let mut dest = CssWriter::new(dest);
match *self { match *self {
% for ty, variants in groups.iteritems(): % for ty, vs in groupby(variants, key=lambda x: x["type"]):
${" | ".join("{}(ref value)".format(v) for v in variants)} => { ${" | ".join("{}(ref value)".format(v["name"]) for v in vs)} => {
value.to_css(&mut dest) value.to_css(&mut dest)
} }
% endfor % endfor