oo_bindgen/model/
iterator.rs

1use std::rc::Rc;
2
3use crate::model::*;
4
5#[derive(Debug, Clone)]
6#[non_exhaustive]
7pub enum IteratorItemType {
8    Primitive(Primitive),
9    Struct(UniversalOr<FunctionReturnStructField>),
10}
11
12impl From<UniversalOr<FunctionReturnStructField>> for IteratorItemType {
13    fn from(x: UniversalOr<FunctionReturnStructField>) -> Self {
14        IteratorItemType::Struct(x)
15    }
16}
17
18impl From<UniversalStructHandle> for IteratorItemType {
19    fn from(x: UniversalStructHandle) -> Self {
20        Self::Struct(UniversalOr::Universal(x))
21    }
22}
23
24impl From<FunctionReturnStructHandle> for IteratorItemType {
25    fn from(x: FunctionReturnStructHandle) -> Self {
26        Self::Struct(UniversalOr::Specific(x))
27    }
28}
29
30impl From<Primitive> for IteratorItemType {
31    fn from(x: Primitive) -> Self {
32        Self::Primitive(x)
33    }
34}
35
36impl IteratorItemType {
37    pub(crate) fn get_function_return_value(&self) -> FunctionReturnValue {
38        match self {
39            IteratorItemType::Struct(x) => FunctionReturnValue::StructRef(x.typed_declaration()),
40            IteratorItemType::Primitive(x) => {
41                FunctionReturnValue::PrimitiveRef(PrimitiveRef::new(*x))
42            }
43        }
44    }
45}
46
47#[derive(Debug)]
48pub struct AbstractIterator<D>
49where
50    D: DocReference,
51{
52    /// underlying Rust iterator may have an associated lifetime annotation
53    pub(crate) has_lifetime_annotation: bool,
54    /// function used to retrieve the next value
55    /// it takes the `iter_class` and returns a pointer to the `iter_type`
56    pub(crate) next_function: Handle<Function<D>>,
57    /// opaque c struct type for the iterator
58    pub(crate) iter_class: ClassDeclarationHandle,
59    /// type of the value returned as a possibly null pointer
60    pub(crate) item_type: IteratorItemType,
61    /// library settings
62    pub(crate) settings: Rc<LibrarySettings>,
63}
64
65impl AbstractIterator<Unvalidated> {
66    pub(crate) fn validate(
67        &self,
68        lib: &LibraryFields,
69    ) -> BindResult<Handle<AbstractIterator<Validated>>> {
70        Ok(Handle::new(AbstractIterator {
71            has_lifetime_annotation: self.has_lifetime_annotation,
72            next_function: self.next_function.validate(lib)?,
73            iter_class: self.iter_class.clone(),
74            item_type: self.item_type.clone(),
75            settings: self.settings.clone(),
76        }))
77    }
78}
79
80impl<D> AbstractIterator<D>
81where
82    D: DocReference,
83{
84    pub(crate) fn new(
85        has_lifetime_annotation: bool,
86        iter_class: ClassDeclarationHandle,
87        next_function: Handle<Function<D>>,
88        item_type: IteratorItemType,
89        settings: Rc<LibrarySettings>,
90    ) -> AbstractIterator<D> {
91        AbstractIterator {
92            has_lifetime_annotation,
93            next_function,
94            iter_class,
95            item_type,
96            settings,
97        }
98    }
99
100    pub(crate) fn name(&self) -> &Name {
101        &self.iter_class.name
102    }
103}
104
105pub type AbstractIteratorHandle = Handle<AbstractIterator<Unvalidated>>;