Updates for recent versions of Rust.

* Added lots of dyns
* Removed unused whitespace.
* Removed rust-toolchain because stable rust compiles the project.
* Fixed Doc-tests with ignore keyword.
This commit is contained in:
Sebastian Imlay 2019-10-04 14:52:33 -07:00
parent f15cf258af
commit 5695ec9b94
23 changed files with 170 additions and 171 deletions

View file

@ -51,10 +51,10 @@ impl Color {
#[inline]
pub fn new(red: u8, green: u8, blue: u8, alpha: u8) -> Self {
Color {
red: red,
green: green,
blue: blue,
alpha: alpha,
red,
green,
blue,
alpha,
}
}
@ -81,7 +81,7 @@ impl Color {
pub fn alpha_f32(&self) -> f32 {
self.alpha as f32 / 255.0
}
/// Parse a <color> value, per CSS Color Module Level 3.
///
/// FIXME(#2) Deprecated CSS2 System Colors are not supported yet.
@ -501,9 +501,9 @@ pub fn parse_color_keyword(ident: &str) -> Result<Color, ()> {
#[inline]
fn from_hex(c: u8) -> Result<u8, ()> {
match c {
b'0'...b'9' => Ok(c - b'0'),
b'a'...b'f' => Ok(c - b'a' + 10),
b'A'...b'F' => Ok(c - b'A' + 10),
b'0'..=b'9' => Ok(c - b'0'),
b'a'..=b'f' => Ok(c - b'a' + 10),
b'A'..=b'F' => Ok(c - b'A' + 10),
_ => Err(()),
}
}
@ -663,5 +663,5 @@ where
let red = clamp_unit_f32(hue_to_rgb(m1, m2, hue_times_3 + 1.));
let green = clamp_unit_f32(hue_to_rgb(m1, m2, hue_times_3));
let blue = clamp_unit_f32(hue_to_rgb(m1, m2, hue_times_3 - 1.));
return Ok((red, green, blue, uses_commas));
Ok((red, green, blue, uses_commas))
}

View file

@ -54,7 +54,7 @@ struct FlexLine {
}
impl Stretch {
pub(crate) fn compute(&mut self, root: Node, size: Size<Number>) -> Result<(), Box<Any>> {
pub(crate) fn compute(&mut self, root: Node, size: Size<Number>) -> Result<(), Box<dyn Any>> {
let style = self.style[&root];
let has_root_min_max = style.min_size.width.is_defined()
|| style.min_size.height.is_defined()
@ -133,7 +133,7 @@ impl Stretch {
node_size: Size<Number>,
parent_size: Size<Number>,
perform_layout: bool,
) -> Result<ComputeResult, Box<Any>> {
) -> Result<ComputeResult, Box<dyn Any>> {
*self.is_dirty.get_mut(node).unwrap() = false;
// First we check if we have a result for the given input
@ -277,7 +277,7 @@ impl Stretch {
// TODO - this does not follow spec. See commented out code below
// 3. Determine the flex base size and hypothetical main size of each item:
flex_items.iter_mut().try_for_each(|child| -> Result<(), Box<Any>> {
flex_items.iter_mut().try_for_each(|child| -> Result<(), Box<dyn Any>> {
let child_style = self.style[&child.node];
// A. If the item has a definite used flex basis, thats the flex base size.
@ -364,7 +364,7 @@ impl Stretch {
// The hypothetical main size is the items flex base size clamped according to its
// used min and max main sizes (and flooring the content box size at zero).
flex_items.iter_mut().try_for_each(|child| -> Result<(), Box<Any>> {
flex_items.iter_mut().try_for_each(|child| -> Result<(), Box<dyn Any>> {
child.inner_flex_basis = child.flex_basis - child.padding.main(dir) - child.border.main(dir);
// TODO - not really spec abiding but needs to be done somewhere. probably somewhere else though.
@ -441,7 +441,7 @@ impl Stretch {
//
// 9.7. Resolving Flexible Lengths
flex_lines.iter_mut().try_for_each(|line| -> Result<(), Box<Any>> {
flex_lines.iter_mut().try_for_each(|line| -> Result<(), Box<dyn Any>> {
// 1. Determine the used flex factor. Sum the outer hypothetical main sizes of all
// items on the line. If the sum is less than the flex containers inner main size,
// use the flex grow factor for the rest of this algorithm; otherwise, use the
@ -458,7 +458,7 @@ impl Stretch {
// - If using the flex shrink factor: any item that has a flex base size
// smaller than its hypothetical main size
line.items.iter_mut().try_for_each(|child| -> Result<(), Box<Any>> {
line.items.iter_mut().try_for_each(|child| -> Result<(), Box<dyn Any>> {
// TODO - This is not found by reading the spec. Maybe this can be done in some other place
// instead. This was found by trail and error fixing tests to align with webkit output.
if node_inner_size.main(dir).is_undefined() && is_row {
@ -614,7 +614,7 @@ impl Stretch {
// items target main size was made smaller by this, its a max violation.
// If the items target main size was made larger by this, its a min violation.
let total_violation = unfrozen.iter_mut().try_fold(0.0, |acc, child| -> Result<f32, Box<Any>> {
let total_violation = unfrozen.iter_mut().try_fold(0.0, |acc, child| -> Result<f32, Box<dyn Any>> {
// TODO - not really spec abiding but needs to be done somewhere. probably somewhere else though.
// The following logic was developed not from the spec but by trail and error looking into how
// webkit handled various scenarios. Can probably be solved better by passing in
@ -691,7 +691,7 @@ impl Stretch {
// used main size and the available space, treating auto as fit-content.
flex_lines.iter_mut().try_for_each(|line| {
line.items.iter_mut().try_for_each(|child| -> Result<(), Box<Any>> {
line.items.iter_mut().try_for_each(|child| -> Result<(), Box<dyn Any>> {
let child_cross =
child.size.cross(dir).maybe_max(child.min_size.cross(dir)).maybe_min(child.max_size.cross(dir));
@ -737,7 +737,7 @@ impl Stretch {
if has_baseline_child {
flex_lines.iter_mut().try_for_each(|line| {
line.items.iter_mut().try_for_each(|child| -> Result<(), Box<Any>> {
line.items.iter_mut().try_for_each(|child| -> Result<(), Box<dyn Any>> {
let result = self.compute_internal(
child.node,
Size {
@ -1148,12 +1148,12 @@ impl Stretch {
let mut lines: Vec<Vec<result::Layout>> = vec![];
let mut total_offset_cross = padding_border.cross_start(dir);
let layout_line = |line: &mut FlexLine| -> Result<(), Box<Any>> {
let layout_line = |line: &mut FlexLine| -> Result<(), Box<dyn Any>> {
let mut children: Vec<result::Layout> = vec![];
let mut total_offset_main = padding_border.main_start(dir);
let line_offset_cross = line.offset_cross;
let layout_item = |child: &mut FlexItem| -> Result<(), Box<Any>> {
let layout_item = |child: &mut FlexItem| -> Result<(), Box<dyn Any>> {
let result = self.compute_internal(
child.node,
child.target_size.map(|s| s.to_number()),

View file

@ -15,7 +15,7 @@ use core::any::Any;
#[derive(Debug)]
pub enum Error {
InvalidNode(node::Node),
Measure(Box<Any>),
Measure(Box<dyn Any>),
}
impl std::fmt::Display for Error {

View file

@ -16,7 +16,7 @@ use crate::stretch::result::{Cache, Layout};
use crate::stretch::style::*;
use crate::stretch::Error;
type MeasureFunc = Box<Fn(Size<Number>) -> Result<Size<f32>, Box<Any>> + Send + Sync + 'static>;
type MeasureFunc = Box<dyn Fn(Size<Number>) -> Result<Size<f32>, Box<dyn Any>> + Send + Sync + 'static>;
lazy_static! {
/// Global stretch instance id allocator.

View file

@ -67,7 +67,7 @@ impl<'i> QualifiedRuleParser<'i> for RuleParser {
let styles = DeclarationListParser::new(input, StyleParser {}).collect::<Vec<_>>();
Ok(Rule {
key: key,
key,
styles: styles.into_iter().filter_map(|decl| {
if !decl.is_ok() {
eprintln!("{:?}", decl);
@ -119,7 +119,7 @@ impl<'i> DeclarationParser<'i> for StyleParser {
"space-around" => Styles::AlignContent(AlignContent::SpaceAround),
_ => { return Err(s.new_unexpected_token_error(t.clone())); }
}},
"align-items" => { let s = input.current_source_location(); let t = input.next()?; match ident(&t) {
"flex-start" => Styles::AlignItems(AlignItems::FlexStart),
"flex-end" => Styles::AlignItems(AlignItems::FlexEnd),
@ -128,7 +128,7 @@ impl<'i> DeclarationParser<'i> for StyleParser {
"stretch" => Styles::AlignItems(AlignItems::Stretch),
_ => { return Err(s.new_unexpected_token_error(t.clone())); }
}},
"align_self" => { let s = input.current_source_location(); let t = input.next()?; match ident(&t) {
"auto" => Styles::AlignSelf(AlignSelf::Auto),
"flex-start" => Styles::AlignSelf(AlignSelf::FlexStart),
@ -149,14 +149,14 @@ impl<'i> DeclarationParser<'i> for StyleParser {
}},
"background-color" => Styles::BackgroundColor(Color::parse(input)?),
// Border values~
"border-color" => Styles::BorderColor(Color::parse(input)?),
"border-top-color" => Styles::BorderTopColor(Color::parse(input)?),
"border-bottom-color" => Styles::BorderBottomColor(Color::parse(input)?),
"border-left-color" => Styles::BorderLeftColor(Color::parse(input)?),
"border-right-color" => Styles::BorderRightColor(Color::parse(input)?),
"bottom" => Styles::Bottom(parse_floaty_mcfloatface_value(input)?),
"color" => Styles::TextColor(Color::parse(input)?),
@ -173,11 +173,11 @@ impl<'i> DeclarationParser<'i> for StyleParser {
"none" => Styles::Display(Display::None),
_ => { return Err(s.new_unexpected_token_error(t.clone())); }
}},
"end" => Styles::End(parse_floaty_mcfloatface_value(input)?),
"flex-basis" => Styles::FlexBasis(parse_floaty_mcfloatface_value(input)?),
"flex-direction" => { let s = input.current_source_location(); let t = input.next()?; match ident(&t) {
"row" => Styles::FlexDirection(FlexDirection::Row),
"row-reverse" => Styles::FlexDirection(FlexDirection::RowReverse),
@ -188,17 +188,17 @@ impl<'i> DeclarationParser<'i> for StyleParser {
"flex-grow" => Styles::FlexGrow(parse_floaty_mcfloatface_value(input)?),
"flex-shrink" => Styles::FlexShrink(parse_floaty_mcfloatface_value(input)?),
"flex-wrap" => { let s = input.current_source_location(); let t = input.next()?; match ident(&t) {
"no-wrap" => Styles::FlexWrap(FlexWrap::NoWrap),
"wrap" => Styles::FlexWrap(FlexWrap::Wrap),
"wrap-reverse" => Styles::FlexWrap(FlexWrap::WrapReverse),
_ => { return Err(s.new_unexpected_token_error(t.clone())); }
}},
//FontFamily(FontFamily),
"font-size" => Styles::FontSize(parse_floaty_mcfloatface_value(input)?),
"font-style" => { let s = input.current_source_location(); let t = input.next()?; match ident(&t) {
"normal" => Styles::FontStyle(FontStyle::Normal),
"italic" => Styles::FontStyle(FontStyle::Italic),
@ -211,7 +211,7 @@ impl<'i> DeclarationParser<'i> for StyleParser {
"bold" => Styles::FontWeight(FontWeight::Bold),
_ => { return Err(s.new_unexpected_token_error(t.clone())); }
}},
"height" => Styles::Height(parse_floaty_mcfloatface_value(input)?),
"justify-content" => { let s = input.current_source_location(); let t = input.next()?; match ident(&t) {
@ -223,7 +223,7 @@ impl<'i> DeclarationParser<'i> for StyleParser {
"space-evenly" => Styles::JustifyContent(JustifyContent::SpaceEvenly),
_ => { return Err(s.new_unexpected_token_error(t.clone())); }
}},
"left" => Styles::Left(parse_floaty_mcfloatface_value(input)?),
"line-height" => Styles::FontLineHeight(parse_floaty_mcfloatface_value(input)?),
@ -236,35 +236,35 @@ impl<'i> DeclarationParser<'i> for StyleParser {
"max-height" => Styles::MaxHeight(parse_floaty_mcfloatface_value(input)?),
"max-width" => Styles::MaxWidth(parse_floaty_mcfloatface_value(input)?),
"min-height" => Styles::MinHeight(parse_floaty_mcfloatface_value(input)?),
"min-width" => Styles::MinWidth(parse_floaty_mcfloatface_value(input)?),
"opacity" => Styles::Opacity(parse_floaty_mcfloatface_value(input)?),
"overflow" => { let s = input.current_source_location(); let t = input.next()?; match ident(&t) {
"visible" => Styles::Overflow(Overflow::Visible),
"hidden" => Styles::Overflow(Overflow::Hidden),
"scroll" => Styles::Overflow(Overflow::Scroll),
_ => { return Err(s.new_unexpected_token_error(t.clone())); }
}},
"padding-bottom" => Styles::PaddingBottom(parse_floaty_mcfloatface_value(input)?),
"padding-end" => Styles::PaddingEnd(parse_floaty_mcfloatface_value(input)?),
"padding-left" => Styles::PaddingLeft(parse_floaty_mcfloatface_value(input)?),
"padding-right" => Styles::PaddingRight(parse_floaty_mcfloatface_value(input)?),
"padding-start" => Styles::PaddingStart(parse_floaty_mcfloatface_value(input)?),
"padding-top" => Styles::PaddingTop(parse_floaty_mcfloatface_value(input)?),
"position" => { let s = input.current_source_location(); let t = input.next()?; match ident(&t) {
"absolute" => Styles::PositionType(PositionType::Absolute),
"relative" => Styles::PositionType(PositionType::Relative),
_ => { return Err(s.new_unexpected_token_error(t.clone())); }
}},
"right" => Styles::Right(parse_floaty_mcfloatface_value(input)?),
"start" => Styles::Start(parse_floaty_mcfloatface_value(input)?),
"text-align" => { let s = input.current_source_location(); let t = input.next()?; match ident(&t) {
"auto" => Styles::TextAlignment(TextAlignment::Auto),
"left" => Styles::TextAlignment(TextAlignment::Left),
@ -273,14 +273,14 @@ impl<'i> DeclarationParser<'i> for StyleParser {
"justify" => Styles::TextAlignment(TextAlignment::Justify),
_ => { return Err(s.new_unexpected_token_error(t.clone())); }
}},
"text-decoration-color" => Styles::TextDecorationColor(Color::parse(input)?),
"text-shadow-color" => Styles::TextShadowColor(Color::parse(input)?),
"tint-color" => Styles::TintColor(Color::parse(input)?),
"top" => Styles::Top(parse_floaty_mcfloatface_value(input)?),
"width" => Styles::Width(parse_floaty_mcfloatface_value(input)?),
t => {
let location = input.current_source_location();
return Err(location.new_unexpected_token_error(Token::Ident(t.to_string().into())));
@ -298,7 +298,7 @@ fn parse_floaty_mcfloatface_value<'i, 't>(input: &mut Parser<'i, 't>) -> Result<
let token = input.next()?;
match token {
Token::Number { value, .. } => Ok(*value),
Token::Number { value, .. } => Ok(*value),
_ => Err(location.new_basic_unexpected_token_error(token.clone()))
}
}