Implement Index for nsStyleAutoArray. r?heycam
draft
Implement Index for nsStyleAutoArray. r?heycam
MozReview-Commit-ID: CKHULr1840z
--- a/servo/components/style/gecko_bindings/sugar/ns_style_auto_array.rs
+++ b/servo/components/style/gecko_bindings/sugar/ns_style_auto_array.rs
@@ -1,16 +1,30 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
use gecko_bindings::structs::nsStyleAutoArray;
use std::iter::{once, Chain, Once, IntoIterator};
+use std::ops::Index;
use std::slice::{Iter, IterMut};
+impl<T> Index<usize> for nsStyleAutoArray<T> {
+ type Output = T;
+ fn index(&self, index: usize) -> &T {
+ if index > self.len() {
+ panic!("out of range")
+ }
+ match index {
+ 0 => &self.mFirstElement,
+ _ => &self.mOtherElements[index - 1],
+ }
+ }
+}
+
impl<T> nsStyleAutoArray<T> {
pub fn iter_mut(&mut self) -> Chain<Once<&mut T>, IterMut<T>> {
once(&mut self.mFirstElement).chain(self.mOtherElements.iter_mut())
}
pub fn iter(&self) -> Chain<Once<&T>, Iter<T>> {
once(&self.mFirstElement).chain(self.mOtherElements.iter())
}