Implement Index for nsStyleAutoArray. r?heycam draft
authorHiroyuki Ikezoe <hiikezoe@mozilla-japan.org>
Fri, 06 Jan 2017 19:06:53 +0900
changeset 456840 1fd28a9fecb37f0c09fe2fc51a471f0cd8f6b7dd
parent 456839 d72bfa93b092b6c54d999dde9b429503574c5da6
child 456841 8f8c6a629ca276d9c8a80afd66a09fc4959ac99f
push id40619
push userhiikezoe@mozilla-japan.org
push dateFri, 06 Jan 2017 10:07:41 +0000
reviewersheycam
milestone53.0a1
Implement Index for nsStyleAutoArray. r?heycam MozReview-Commit-ID: CKHULr1840z
servo/components/style/gecko_bindings/sugar/ns_style_auto_array.rs
--- 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())
     }