style: Improve #[derive(Parse)].

I want to do this so that I can get rid of Either<>. The reasons for getting rid
of either are multiple:

 * It doesn't generate as nice C++ code using cbindgen.
 * It isn't that nice to use either from Rust.
 * cbindgen has bugs with zero-sized types.

I started using this for ColorOrAuto and a few others, for now.

Differential Revision: https://phabricator.services.mozilla.com/D19844
This commit is contained in:
Emilio Cobos Álvarez 2019-02-14 20:45:10 +01:00
parent 6118e4d993
commit 73d5b82f9f
24 changed files with 238 additions and 362 deletions

View file

@ -165,6 +165,7 @@ impl<LengthPercentage> MaxSize<LengthPercentage> {
Copy,
Debug,
MallocSizeOf,
Parse,
PartialEq,
SpecifiedValueInfo,
ToAnimatedValue,
@ -174,29 +175,17 @@ impl<LengthPercentage> MaxSize<LengthPercentage> {
)]
#[repr(C, u8)]
pub enum GenericLengthOrNumber<L, N> {
/// A number.
///
/// NOTE: Numbers need to be before lengths, in order to parse them
/// first, since `0` should be a number, not the `0px` length.
Number(N),
/// A length.
Length(L),
/// A number.
Number(N),
}
pub use self::GenericLengthOrNumber as LengthOrNumber;
impl<L: Parse, N: Parse> Parse for LengthOrNumber<L, N> {
fn parse<'i, 't>(
context: &ParserContext,
input: &mut Parser<'i, 't>,
) -> Result<Self, ParseError<'i>> {
if let Ok(number) = input.try(|i| N::parse(context, i)) {
// Numbers need to be parsed first because `0` must be recognised
// as the number `0` and not the length `0px`.
return Ok(LengthOrNumber::Number(number));
}
Ok(LengthOrNumber::Length(L::parse(context, input)?))
}
}
impl<L, N> LengthOrNumber<L, N> {
/// Returns `0`.
pub fn zero() -> Self