Add iterators for vector types

MozReview-Commit-ID: I7oOpYhVP5S
This commit is contained in:
Manish Goregaokar 2017-05-02 21:58:21 -07:00 committed by Manish Goregaokar
parent 36f26148e6
commit c85aae4abd
3 changed files with 43 additions and 3 deletions

View file

@ -100,6 +100,35 @@ impl<'a> Context<'a> {
pub fn mutate_style(&mut self) -> &mut StyleBuilder<'a> { &mut self.style }
}
/// An iterator over a slice of computed values
pub struct ComputedVecIter<'a, 'cx, 'cx_a: 'cx, S: ToComputedValue + 'a> {
cx: &'cx Context<'cx_a>,
values: &'a [S],
}
impl<'a, 'cx, 'cx_a: 'cx, S: ToComputedValue + 'a> ComputedVecIter<'a, 'cx, 'cx_a, S> {
/// Construct an iterator from a slice of specified values and a context
pub fn new(cx: &'cx Context<'cx_a>, values: &'a [S]) -> Self {
ComputedVecIter {
cx: cx,
values: values,
}
}
}
impl<'a, 'cx, 'cx_a: 'cx, S: ToComputedValue + 'a> Iterator for ComputedVecIter<'a, 'cx, 'cx_a, S> {
type Item = S::ComputedValue;
fn next(&mut self) -> Option<Self::Item> {
if let Some((next, rest)) = self.values.split_first() {
let ret = next.to_computed_value(self.cx);
self.values = rest;
Some(ret)
} else {
None
}
}
}
/// A trait to represent the conversion between computed and specified values.
pub trait ToComputedValue {
/// The computed value type we're going to be converted to.