Skip to content

Commit

Permalink
Convert Vec values into boxed slices
Browse files Browse the repository at this point in the history
  • Loading branch information
canova committed Jul 19, 2017
1 parent 744cf0e commit 1ab86a4
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 30 deletions.
7 changes: 4 additions & 3 deletions components/style/properties/gecko.mako.rs
Expand Up @@ -1469,7 +1469,7 @@ fn static_assert() {
&mut ${self_grid}.mRepeatAutoLineNameListAfter, 0);
}
},
GridTemplateComponent::Subgrid(mut list) => {
GridTemplateComponent::Subgrid(list) => {
${self_grid}.set_mIsSubgrid(true);
let names_length = match list.fill_idx {
Some(_) => list.names.len() - 1,
Expand All @@ -1486,14 +1486,15 @@ fn static_assert() {
&mut ${self_grid}.mRepeatAutoLineNameListAfter, 0);
}

let mut names = list.names.into_vec();
if let Some(idx) = list.fill_idx {
${self_grid}.set_mIsAutoFill(true);
${self_grid}.mRepeatAutoIndex = idx as i16;
set_line_names(&list.names.swap_remove(idx as usize),
set_line_names(&names.swap_remove(idx as usize),
&mut ${self_grid}.mRepeatAutoLineNameListBefore);
}

for (servo_names, gecko_names) in list.names.iter().zip(${self_grid}.mLineNameLists.iter_mut()) {
for (servo_names, gecko_names) in names.iter().zip(${self_grid}.mLineNameLists.iter_mut()) {
set_line_names(servo_names, gecko_names);
}
},
Expand Down
17 changes: 9 additions & 8 deletions components/style/properties/shorthand/position.mako.rs
Expand Up @@ -278,42 +278,43 @@
}
% endfor

let first_line_names = input.try(parse_line_names).unwrap_or(vec![]);
let first_line_names = input.try(parse_line_names).unwrap_or(vec![].into_boxed_slice());
if let Ok(s) = input.try(Parser::expect_string) {
let mut strings = vec![];
let mut values = vec![];
let mut line_names = vec![];
let mut names = first_line_names;
let mut names = first_line_names.into_vec();
let mut string = s.into_owned().into_boxed_str();
loop {
line_names.push(names);
line_names.push(names.into_boxed_slice());
strings.push(string);
let size = input.try(|i| TrackSize::parse(context, i)).unwrap_or_default();
values.push(size);
names = input.try(parse_line_names).unwrap_or(vec![]);
names = input.try(parse_line_names).unwrap_or(vec![].into_boxed_slice()).into_vec();
if let Ok(v) = input.try(parse_line_names) {
names.extend(v);
names.extend(v.into_vec());
}

string = match input.try(Parser::expect_string) {
Ok(s) => s.into_owned().into_boxed_str(),
_ => { // only the named area determines whether we should bail out
line_names.push(names);
line_names.push(names.into_boxed_slice());
break
},
};
}

if line_names.len() == values.len() {
line_names.push(vec![]); // should be one longer than track sizes
// should be one longer than track sizes
line_names.push(vec![].into_boxed_slice());
}

let template_areas = TemplateAreas::from_vec(strings)
.map_err(|()| StyleParseError::UnspecifiedError)?;
let template_rows = TrackList {
list_type: TrackListType::Normal,
values: values,
line_names: line_names,
line_names: line_names.into_boxed_slice(),
auto_repeat: None,
};

Expand Down
15 changes: 8 additions & 7 deletions components/style/values/generics/grid.rs
Expand Up @@ -386,7 +386,7 @@ pub struct TrackRepeat<L> {
/// If there's no `<line-names>`, then it's represented by an empty vector.
/// For N `<track-size>` values, there will be N+1 `<line-names>`, and so this vector's
/// length is always one value more than that of the `<track-size>`.
pub line_names: Vec<Vec<CustomIdent>>,
pub line_names: Box<[Box<[CustomIdent]>]>,
/// `<track-size>` values.
pub track_sizes: Vec<TrackSize<L>>,
}
Expand Down Expand Up @@ -447,7 +447,8 @@ impl<L: Clone> TrackRepeat<L> {
let mut names_iter = self.line_names.iter();
for (size, names) in self.track_sizes.iter().zip(&mut names_iter) {
prev_names.extend_from_slice(&names);
line_names.push(mem::replace(&mut prev_names, vec![]));
let vec = mem::replace(&mut prev_names, vec![]);
line_names.push(vec.into_boxed_slice());
track_sizes.push(size.clone());
}

Expand All @@ -456,11 +457,11 @@ impl<L: Clone> TrackRepeat<L> {
}
}

line_names.push(prev_names);
line_names.push(prev_names.into_boxed_slice());
TrackRepeat {
count: self.count,
track_sizes: track_sizes,
line_names: line_names,
line_names: line_names.into_boxed_slice(),
}

} else { // if it's auto-fit/auto-fill, then it's left to the layout.
Expand Down Expand Up @@ -541,7 +542,7 @@ pub struct TrackList<T> {
/// If there's no `<line-names>`, then it's represented by an empty vector.
/// For N values, there will be N+1 `<line-names>`, and so this vector's
/// length is always one value more than that of the `<track-size>`.
pub line_names: Vec<Vec<CustomIdent>>,
pub line_names: Box<[Box<[CustomIdent]>]>,
/// `<auto-repeat>` value. There can only be one `<auto-repeat>` in a TrackList.
pub auto_repeat: Option<TrackRepeat<T>>,
}
Expand Down Expand Up @@ -598,7 +599,7 @@ impl<T: ToCss> ToCss for TrackList<T> {
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
pub struct LineNameList {
/// The optional `<line-name-list>`
pub names: Vec<Vec<CustomIdent>>,
pub names: Box<[Box<[CustomIdent]>]>,
/// Indicates the line name that requires `auto-fill`
pub fill_idx: Option<u32>,
}
Expand Down Expand Up @@ -650,7 +651,7 @@ impl Parse for LineNameList {
}

Ok(LineNameList {
names: line_names,
names: line_names.into_boxed_slice(),
fill_idx: fill_idx,
})
}
Expand Down
27 changes: 15 additions & 12 deletions components/style/values/specified/grid.rs
Expand Up @@ -81,7 +81,7 @@ impl Parse for TrackSize<LengthOrPercentage> {
/// Parse the grid line names into a vector of owned strings.
///
/// https://drafts.csswg.org/css-grid/#typedef-line-names
pub fn parse_line_names<'i, 't>(input: &mut Parser<'i, 't>) -> Result<Vec<CustomIdent>, ParseError<'i>> {
pub fn parse_line_names<'i, 't>(input: &mut Parser<'i, 't>) -> Result<Box<[CustomIdent]>, ParseError<'i>> {
input.expect_square_bracket_block()?;
input.parse_nested_block(|input| {
let mut values = vec![];
Expand All @@ -90,7 +90,7 @@ pub fn parse_line_names<'i, 't>(input: &mut Parser<'i, 't>) -> Result<Vec<Custom
values.push(ident);
}

Ok(values)
Ok(values.into_boxed_slice())
})
}

Expand Down Expand Up @@ -128,7 +128,7 @@ impl TrackRepeat<LengthOrPercentage> {
let mut current_names;

loop {
current_names = input.try(parse_line_names).unwrap_or(vec![]);
current_names = input.try(parse_line_names).unwrap_or(vec![].into_boxed_slice());
if let Ok(track_size) = input.try(|i| TrackSize::parse(context, i)) {
if !track_size.is_fixed() {
if is_auto {
Expand All @@ -150,7 +150,7 @@ impl TrackRepeat<LengthOrPercentage> {
// one `TrackSize`. But in current version of the spec, this is deprecated
// but we are adding this for gecko parity. We should remove this when
// gecko implements new spec.
names.push(input.try(parse_line_names).unwrap_or(vec![]));
names.push(input.try(parse_line_names).unwrap_or(vec![].into_boxed_slice()));
break
}
} else {
Expand All @@ -167,7 +167,7 @@ impl TrackRepeat<LengthOrPercentage> {
let repeat = TrackRepeat {
count: count,
track_sizes: values,
line_names: names,
line_names: names.into_boxed_slice(),
};

Ok((repeat, repeat_type))
Expand Down Expand Up @@ -205,7 +205,7 @@ impl Parse for TrackList<LengthOrPercentage> {
let mut atleast_one_not_fixed = false;

loop {
current_names.append(&mut input.try(parse_line_names).unwrap_or(vec![]));
current_names.extend_from_slice(&mut input.try(parse_line_names).unwrap_or(vec![].into_boxed_slice()));
if let Ok(track_size) = input.try(|i| TrackSize::parse(context, i)) {
if !track_size.is_fixed() {
atleast_one_not_fixed = true;
Expand All @@ -215,7 +215,8 @@ impl Parse for TrackList<LengthOrPercentage> {
}
}

names.push(mem::replace(&mut current_names, vec![]));
let vec = mem::replace(&mut current_names, vec![]);
names.push(vec.into_boxed_slice());
values.push(track_size);
} else if let Ok((repeat, type_)) = input.try(|i| TrackRepeat::parse_with_repeat_type(context, i)) {
if list_type == TrackListType::Explicit {
Expand All @@ -237,18 +238,20 @@ impl Parse for TrackList<LengthOrPercentage> {

list_type = TrackListType::Auto(values.len() as u16);
auto_repeat = Some(repeat);
names.push(mem::replace(&mut current_names, vec![]));
let vec = mem::replace(&mut current_names, vec![]);
names.push(vec.into_boxed_slice());
continue
},
RepeatType::Fixed => (),
}

// If the repeat count is numeric, we axpand and merge the values.
let mut repeat = repeat.expand();
let mut repeat_names_iter = repeat.line_names.drain(..);
let mut repeat_names_iter = repeat.line_names.iter();
for (size, repeat_names) in repeat.track_sizes.drain(..).zip(&mut repeat_names_iter) {
current_names.extend_from_slice(&repeat_names);
names.push(mem::replace(&mut current_names, vec![]));
let vec = mem::replace(&mut current_names, vec![]);
names.push(vec.into_boxed_slice());
values.push(size);
}

Expand All @@ -260,15 +263,15 @@ impl Parse for TrackList<LengthOrPercentage> {
return Err(StyleParseError::UnspecifiedError.into())
}

names.push(current_names);
names.push(current_names.into_boxed_slice());
break
}
}

Ok(TrackList {
list_type: list_type,
values: values,
line_names: names,
line_names: names.into_boxed_slice(),
auto_repeat: auto_repeat,
})
}
Expand Down

0 comments on commit 1ab86a4

Please sign in to comment.