Convert Vec values into boxed slices

This commit is contained in:
Nazım Can Altınova 2017-07-18 11:10:47 -07:00
parent 744cf0ee44
commit 1ab86a46b6
4 changed files with 36 additions and 30 deletions

View file

@ -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,
@ -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);
}
},

View file

@ -278,34 +278,35 @@
}
% 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)
@ -313,7 +314,7 @@
let template_rows = TrackList {
list_type: TrackListType::Normal,
values: values,
line_names: line_names,
line_names: line_names.into_boxed_slice(),
auto_repeat: None,
};

View file

@ -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>>,
}
@ -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());
}
@ -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.
@ -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>>,
}
@ -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>,
}
@ -650,7 +651,7 @@ impl Parse for LineNameList {
}
Ok(LineNameList {
names: line_names,
names: line_names.into_boxed_slice(),
fill_idx: fill_idx,
})
}

View file

@ -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![];
@ -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())
})
}
@ -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 {
@ -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 {
@ -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))
@ -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;
@ -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 {
@ -237,7 +238,8 @@ 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 => (),
@ -245,10 +247,11 @@ impl Parse for TrackList<LengthOrPercentage> {
// 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);
}
@ -260,7 +263,7 @@ impl Parse for TrackList<LengthOrPercentage> {
return Err(StyleParseError::UnspecifiedError.into())
}
names.push(current_names);
names.push(current_names.into_boxed_slice());
break
}
}
@ -268,7 +271,7 @@ impl Parse for TrackList<LengthOrPercentage> {
Ok(TrackList {
list_type: list_type,
values: values,
line_names: names,
line_names: names.into_boxed_slice(),
auto_repeat: auto_repeat,
})
}