lldb/
typelist.rs

1// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
2// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
3// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
4// option. This file may not be copied, modified, or distributed
5// except according to those terms.
6
7use crate::{sys, SBType};
8
9/// A list of [types].
10///
11/// [types]: SBType
12pub struct SBTypeList {
13    /// The underlying raw `SBTypeListRef`.
14    pub raw: sys::SBTypeListRef,
15}
16
17impl SBTypeList {
18    /// Construct a new `SBTypeList`.
19    pub(crate) fn wrap(raw: sys::SBTypeListRef) -> SBTypeList {
20        SBTypeList { raw }
21    }
22
23    #[allow(missing_docs)]
24    pub fn append(&self, t: &SBType) {
25        unsafe { sys::SBTypeListAppend(self.raw, t.raw) };
26    }
27
28    /// Is this type list empty?
29    pub fn is_empty(&self) -> bool {
30        unsafe { sys::SBTypeListGetSize(self.raw) == 0 }
31    }
32
33    /// Iterate over this type list.
34    pub fn iter(&self) -> SBTypeListIter {
35        SBTypeListIter {
36            type_list: self,
37            idx: 0,
38        }
39    }
40}
41
42impl Clone for SBTypeList {
43    fn clone(&self) -> SBTypeList {
44        SBTypeList {
45            raw: unsafe { sys::CloneSBTypeList(self.raw) },
46        }
47    }
48}
49
50impl Drop for SBTypeList {
51    fn drop(&mut self) {
52        unsafe { sys::DisposeSBTypeList(self.raw) };
53    }
54}
55
56impl<'d> IntoIterator for &'d SBTypeList {
57    type IntoIter = SBTypeListIter<'d>;
58    type Item = SBType;
59    fn into_iter(self) -> Self::IntoIter {
60        self.iter()
61    }
62}
63
64unsafe impl Send for SBTypeList {}
65unsafe impl Sync for SBTypeList {}
66
67/// An iterator over the [types] in an [`SBTypeList`].
68///
69/// [types]: SBType
70pub struct SBTypeListIter<'d> {
71    type_list: &'d SBTypeList,
72    idx: usize,
73}
74
75impl Iterator for SBTypeListIter<'_> {
76    type Item = SBType;
77
78    fn next(&mut self) -> Option<SBType> {
79        if self.idx < unsafe { sys::SBTypeListGetSize(self.type_list.raw) as usize } {
80            let r = SBType::wrap(unsafe {
81                sys::SBTypeListGetTypeAtIndex(self.type_list.raw, self.idx as u32)
82            });
83            self.idx += 1;
84            Some(r)
85        } else {
86            None
87        }
88    }
89
90    fn size_hint(&self) -> (usize, Option<usize>) {
91        let sz = unsafe { sys::SBTypeListGetSize(self.type_list.raw) } as usize;
92        (sz - self.idx, Some(sz))
93    }
94}
95
96impl ExactSizeIterator for SBTypeListIter<'_> {}