tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

ns_style_auto_array.rs (3698B)


      1 /* This Source Code Form is subject to the terms of the Mozilla Public
      2 * License, v. 2.0. If a copy of the MPL was not distributed with this
      3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
      4 
      5 //! Rust helpers for Gecko's `nsStyleAutoArray`.
      6 
      7 use crate::gecko_bindings::bindings::Gecko_EnsureStyleAnimationArrayLength;
      8 use crate::gecko_bindings::bindings::Gecko_EnsureStyleScrollTimelineArrayLength;
      9 use crate::gecko_bindings::bindings::Gecko_EnsureStyleTransitionArrayLength;
     10 use crate::gecko_bindings::bindings::Gecko_EnsureStyleViewTimelineArrayLength;
     11 use crate::gecko_bindings::structs::nsStyleAutoArray;
     12 use crate::gecko_bindings::structs::{StyleAnimation, StyleTransition};
     13 use crate::gecko_bindings::structs::{StyleScrollTimeline, StyleViewTimeline};
     14 use std::iter::{once, Chain, Once};
     15 use std::ops::{Index, IndexMut};
     16 use std::slice::{Iter, IterMut};
     17 
     18 impl<T> Index<usize> for nsStyleAutoArray<T> {
     19    type Output = T;
     20    fn index(&self, index: usize) -> &T {
     21        match index {
     22            0 => &self.mFirstElement,
     23            _ => &self.mOtherElements[index - 1],
     24        }
     25    }
     26 }
     27 
     28 impl<T> IndexMut<usize> for nsStyleAutoArray<T> {
     29    fn index_mut(&mut self, index: usize) -> &mut T {
     30        match index {
     31            0 => &mut self.mFirstElement,
     32            _ => &mut self.mOtherElements[index - 1],
     33        }
     34    }
     35 }
     36 
     37 impl<T> nsStyleAutoArray<T> {
     38    /// Mutably iterate over the array elements.
     39    pub fn iter_mut(&mut self) -> Chain<Once<&mut T>, IterMut<'_, T>> {
     40        once(&mut self.mFirstElement).chain(self.mOtherElements.iter_mut())
     41    }
     42 
     43    /// Iterate over the array elements.
     44    pub fn iter(&self) -> Chain<Once<&T>, Iter<'_, T>> {
     45        once(&self.mFirstElement).chain(self.mOtherElements.iter())
     46    }
     47 
     48    /// Returns the length of the array.
     49    ///
     50    /// Note that often structs containing autoarrays will have additional
     51    /// member fields that contain the length, which must be kept in sync.
     52    pub fn len(&self) -> usize {
     53        1 + self.mOtherElements.len()
     54    }
     55 }
     56 
     57 impl nsStyleAutoArray<StyleAnimation> {
     58    /// Ensures that the array has length at least the given length.
     59    pub fn ensure_len(&mut self, len: usize) {
     60        unsafe {
     61            Gecko_EnsureStyleAnimationArrayLength(
     62                self as *mut nsStyleAutoArray<StyleAnimation> as *mut _,
     63                len,
     64            );
     65        }
     66    }
     67 }
     68 
     69 impl nsStyleAutoArray<StyleTransition> {
     70    /// Ensures that the array has length at least the given length.
     71    pub fn ensure_len(&mut self, len: usize) {
     72        unsafe {
     73            Gecko_EnsureStyleTransitionArrayLength(
     74                self as *mut nsStyleAutoArray<StyleTransition> as *mut _,
     75                len,
     76            );
     77        }
     78    }
     79 }
     80 
     81 impl nsStyleAutoArray<StyleViewTimeline> {
     82    /// Ensures that the array has length at least the given length.
     83    pub fn ensure_len(&mut self, len: usize) {
     84        unsafe {
     85            Gecko_EnsureStyleViewTimelineArrayLength(
     86                self as *mut nsStyleAutoArray<StyleViewTimeline> as *mut _,
     87                len,
     88            );
     89        }
     90    }
     91 }
     92 
     93 impl nsStyleAutoArray<StyleScrollTimeline> {
     94    /// Ensures that the array has length at least the given length.
     95    pub fn ensure_len(&mut self, len: usize) {
     96        unsafe {
     97            Gecko_EnsureStyleScrollTimelineArrayLength(
     98                self as *mut nsStyleAutoArray<StyleScrollTimeline> as *mut _,
     99                len,
    100            );
    101        }
    102    }
    103 }
    104 
    105 impl<'a, T> IntoIterator for &'a mut nsStyleAutoArray<T> {
    106    type Item = &'a mut T;
    107    type IntoIter = Chain<Once<&'a mut T>, IterMut<'a, T>>;
    108    fn into_iter(self) -> Self::IntoIter {
    109        self.iter_mut()
    110    }
    111 }