use std::rc::Rc;
use crate::model::*;
#[derive(Debug, Clone)]
#[non_exhaustive]
pub enum IteratorItemType {
Primitive(Primitive),
Struct(UniversalOr<FunctionReturnStructField>),
}
impl From<UniversalOr<FunctionReturnStructField>> for IteratorItemType {
fn from(x: UniversalOr<FunctionReturnStructField>) -> Self {
IteratorItemType::Struct(x)
}
}
impl From<UniversalStructHandle> for IteratorItemType {
fn from(x: UniversalStructHandle) -> Self {
Self::Struct(UniversalOr::Universal(x))
}
}
impl From<FunctionReturnStructHandle> for IteratorItemType {
fn from(x: FunctionReturnStructHandle) -> Self {
Self::Struct(UniversalOr::Specific(x))
}
}
impl From<Primitive> for IteratorItemType {
fn from(x: Primitive) -> Self {
Self::Primitive(x)
}
}
impl IteratorItemType {
pub(crate) fn get_function_return_value(&self) -> FunctionReturnValue {
match self {
IteratorItemType::Struct(x) => FunctionReturnValue::StructRef(x.typed_declaration()),
IteratorItemType::Primitive(x) => {
FunctionReturnValue::PrimitiveRef(PrimitiveRef::new(*x))
}
}
}
}
#[derive(Debug)]
pub struct AbstractIterator<D>
where
D: DocReference,
{
pub(crate) has_lifetime_annotation: bool,
pub(crate) next_function: Handle<Function<D>>,
pub(crate) iter_class: ClassDeclarationHandle,
pub(crate) item_type: IteratorItemType,
pub(crate) settings: Rc<LibrarySettings>,
}
impl AbstractIterator<Unvalidated> {
pub(crate) fn validate(
&self,
lib: &LibraryFields,
) -> BindResult<Handle<AbstractIterator<Validated>>> {
Ok(Handle::new(AbstractIterator {
has_lifetime_annotation: self.has_lifetime_annotation,
next_function: self.next_function.validate(lib)?,
iter_class: self.iter_class.clone(),
item_type: self.item_type.clone(),
settings: self.settings.clone(),
}))
}
}
impl<D> AbstractIterator<D>
where
D: DocReference,
{
pub(crate) fn new(
has_lifetime_annotation: bool,
iter_class: ClassDeclarationHandle,
next_function: Handle<Function<D>>,
item_type: IteratorItemType,
settings: Rc<LibrarySettings>,
) -> AbstractIterator<D> {
AbstractIterator {
has_lifetime_annotation,
next_function,
iter_class,
item_type,
settings,
}
}
pub(crate) fn name(&self) -> &Name {
&self.iter_class.name
}
}
pub type AbstractIteratorHandle = Handle<AbstractIterator<Unvalidated>>;