style: Ensure that derived types are right for optimized-away implementations.

We have this optimization where, for non-generic structs, we generate just a
clone / move as the ToComputedValue / ToResolvedValue implementation.

This moves the optimization a bit further down, and refines it so that we still
generate all the relevant where clauses that make it sound, that is, that all
the ToComputedValue implementations of the fields return the same type.

Otherwise this wouldn't be sound and the type would need to become generic.

We add an escape hatch (no_field_bound) for fields that need to be cloned but
which don't implement the trait. This is right now only for the RefPtr<> in the
shared font-family list, and a piece of code in PaintWorklet which looks kinda
fishy, and probably should be fixed (but we don't ship it in Firefox and there's
a pre-existing FIXME for servo, so I punted on it for now).

The other thing this patch does is adding a bunch of ToComputedValue /
ToResolvedValue implementations that are trivial and were missing.

Differential Revision: https://phabricator.services.mozilla.com/D67913
This commit is contained in:
Emilio Cobos Álvarez 2020-03-26 13:04:20 +00:00
parent 0514059618
commit 7d438cd816
18 changed files with 295 additions and 146 deletions

View file

@ -6,7 +6,9 @@
//! there are used values.
use crate::properties::ComputedValues;
use crate::ArcSlice;
use cssparser;
use servo_arc::Arc;
use smallvec::SmallVec;
mod color;
@ -79,6 +81,7 @@ trivial_to_resolved_value!(computed::url::ComputedImageUrl);
#[cfg(feature = "servo")]
trivial_to_resolved_value!(html5ever::Prefix);
trivial_to_resolved_value!(computed::LengthPercentage);
trivial_to_resolved_value!(style_traits::values::specified::AllowedNumericType);
impl<A, B> ToResolvedValue for (A, B)
where
@ -214,3 +217,43 @@ where
Self::from(Box::from_resolved_value(resolved.into_box()))
}
}
// NOTE(emilio): This is implementable more generically, but it's unlikely what
// you want there, as it forces you to have an extra allocation.
//
// We could do that if needed, ideally with specialization for the case where
// ResolvedValue = T. But we don't need it for now.
impl<T> ToResolvedValue for Arc<T>
where
T: ToResolvedValue<ResolvedValue = T>,
{
type ResolvedValue = Self;
#[inline]
fn to_resolved_value(self, _: &Context) -> Self {
self
}
#[inline]
fn from_resolved_value(resolved: Self) -> Self {
resolved
}
}
// Same caveat as above applies.
impl<T> ToResolvedValue for ArcSlice<T>
where
T: ToResolvedValue<ResolvedValue = T>,
{
type ResolvedValue = Self;
#[inline]
fn to_resolved_value(self, _: &Context) -> Self {
self
}
#[inline]
fn from_resolved_value(resolved: Self) -> Self {
resolved
}
}