summaryrefslogtreecommitdiffstats
path: root/servo/components/style/gecko_bindings/sugar/ns_style_auto_array.rs
blob: b5772a6c77ad0ef9198e42f42886642840b65374 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/* 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 https://mozilla.org/MPL/2.0/. */

//! Rust helpers for Gecko's `nsStyleAutoArray`.

use crate::gecko_bindings::bindings::Gecko_EnsureStyleAnimationArrayLength;
use crate::gecko_bindings::bindings::Gecko_EnsureStyleScrollTimelineArrayLength;
use crate::gecko_bindings::bindings::Gecko_EnsureStyleTransitionArrayLength;
use crate::gecko_bindings::bindings::Gecko_EnsureStyleViewTimelineArrayLength;
use crate::gecko_bindings::structs::nsStyleAutoArray;
use crate::gecko_bindings::structs::{StyleAnimation, StyleTransition};
use crate::gecko_bindings::structs::{StyleScrollTimeline, StyleViewTimeline};
use std::iter::{once, Chain, IntoIterator, Once};
use std::ops::{Index, IndexMut};
use std::slice::{Iter, IterMut};

impl<T> Index<usize> for nsStyleAutoArray<T> {
    type Output = T;
    fn index(&self, index: usize) -> &T {
        match index {
            0 => &self.mFirstElement,
            _ => &self.mOtherElements[index - 1],
        }
    }
}

impl<T> IndexMut<usize> for nsStyleAutoArray<T> {
    fn index_mut(&mut self, index: usize) -> &mut T {
        match index {
            0 => &mut self.mFirstElement,
            _ => &mut self.mOtherElements[index - 1],
        }
    }
}

impl<T> nsStyleAutoArray<T> {
    /// Mutably iterate over the array elements.
    pub fn iter_mut(&mut self) -> Chain<Once<&mut T>, IterMut<T>> {
        once(&mut self.mFirstElement).chain(self.mOtherElements.iter_mut())
    }

    /// Iterate over the array elements.
    pub fn iter(&self) -> Chain<Once<&T>, Iter<T>> {
        once(&self.mFirstElement).chain(self.mOtherElements.iter())
    }

    /// Returns the length of the array.
    ///
    /// Note that often structs containing autoarrays will have additional
    /// member fields that contain the length, which must be kept in sync.
    pub fn len(&self) -> usize {
        1 + self.mOtherElements.len()
    }
}

impl nsStyleAutoArray<StyleAnimation> {
    /// Ensures that the array has length at least the given length.
    pub fn ensure_len(&mut self, len: usize) {
        unsafe {
            Gecko_EnsureStyleAnimationArrayLength(
                self as *mut nsStyleAutoArray<StyleAnimation> as *mut _,
                len,
            );
        }
    }
}

impl nsStyleAutoArray<StyleTransition> {
    /// Ensures that the array has length at least the given length.
    pub fn ensure_len(&mut self, len: usize) {
        unsafe {
            Gecko_EnsureStyleTransitionArrayLength(
                self as *mut nsStyleAutoArray<StyleTransition> as *mut _,
                len,
            );
        }
    }
}

impl nsStyleAutoArray<StyleViewTimeline> {
    /// Ensures that the array has length at least the given length.
    pub fn ensure_len(&mut self, len: usize) {
        unsafe {
            Gecko_EnsureStyleViewTimelineArrayLength(
                self as *mut nsStyleAutoArray<StyleViewTimeline> as *mut _,
                len,
            );
        }
    }
}

impl nsStyleAutoArray<StyleScrollTimeline> {
    /// Ensures that the array has length at least the given length.
    pub fn ensure_len(&mut self, len: usize) {
        unsafe {
            Gecko_EnsureStyleScrollTimelineArrayLength(
                self as *mut nsStyleAutoArray<StyleScrollTimeline> as *mut _,
                len,
            );
        }
    }
}

impl<'a, T> IntoIterator for &'a mut nsStyleAutoArray<T> {
    type Item = &'a mut T;
    type IntoIter = Chain<Once<&'a mut T>, IterMut<'a, T>>;
    fn into_iter(self) -> Self::IntoIter {
        self.iter_mut()
    }
}