use crate::format::{ConstArg, GenericArgWord, TyBoundEntry, TyDefId, TyId};
use super::TyArenaView;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum GenericArgView {
Type(TyId),
Const(ConstArg),
Lifetime(u32),
}
impl GenericArgView {
#[inline]
pub const fn decode(raw: u32) -> Self {
let w = GenericArgWord(raw);
match w.tag_bits() {
GenericArgWord::TAG_TYPE => Self::Type(TyId(w.payload())),
GenericArgWord::TAG_LIFETIME => Self::Lifetime(w.payload()),
_ => Self::Const(GenericArgWord::decode_const(raw)),
}
}
}
#[derive(Debug, Clone, Copy)]
pub struct GenericArgIter<'a> {
pub(crate) words: &'a [u32],
}
impl<'a> GenericArgIter<'a> {
pub const EMPTY: Self = Self { words: &[] };
#[inline]
pub const fn from_words(words: &'a [u32]) -> Self {
Self { words }
}
#[inline]
pub fn len(&self) -> usize {
self.words.len()
}
#[inline]
pub fn is_empty(&self) -> bool {
self.words.is_empty()
}
#[inline]
pub fn get(&self, idx: usize) -> Option<GenericArgView> {
self.words.get(idx).map(|&w| GenericArgView::decode(u32::from_le(w)))
}
}
impl<'a> Iterator for GenericArgIter<'a> {
type Item = GenericArgView;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
let (&first, rest) = self.words.split_first()?;
self.words = rest;
Some(GenericArgView::decode(u32::from_le(first)))
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
(self.words.len(), Some(self.words.len()))
}
}
impl ExactSizeIterator for GenericArgIter<'_> {}
#[derive(Debug, Clone, Copy)]
pub struct TyListIter<'a> {
pub(crate) words: &'a [u32],
}
impl<'a> TyListIter<'a> {
pub const EMPTY: Self = Self { words: &[] };
#[inline]
pub const fn from_words(words: &'a [u32]) -> Self {
Self { words }
}
#[inline]
pub fn len(&self) -> usize {
self.words.len()
}
#[inline]
pub fn is_empty(&self) -> bool {
self.words.is_empty()
}
#[inline]
pub fn get(&self, idx: usize) -> Option<TyId> {
self.words.get(idx).map(|&w| TyId(u32::from_le(w)))
}
}
impl<'a> Iterator for TyListIter<'a> {
type Item = TyId;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
let (&first, rest) = self.words.split_first()?;
self.words = rest;
Some(TyId(u32::from_le(first)))
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
(self.words.len(), Some(self.words.len()))
}
}
impl ExactSizeIterator for TyListIter<'_> {}
#[derive(Debug, Clone, Copy)]
pub struct U32ListIter<'a> {
words: &'a [u32],
}
impl<'a> U32ListIter<'a> {
pub const EMPTY: Self = Self { words: &[] };
#[inline]
pub const fn from_words(words: &'a [u32]) -> Self {
Self { words }
}
#[inline]
pub fn len(&self) -> usize {
self.words.len()
}
#[inline]
pub fn is_empty(&self) -> bool {
self.words.is_empty()
}
#[inline]
pub fn get(&self, idx: usize) -> Option<u32> {
self.words.get(idx).map(|&w| u32::from_le(w))
}
}
impl<'a> Iterator for U32ListIter<'a> {
type Item = u32;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
let (&first, rest) = self.words.split_first()?;
self.words = rest;
Some(u32::from_le(first))
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
(self.words.len(), Some(self.words.len()))
}
}
impl ExactSizeIterator for U32ListIter<'_> {}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct AssocBindingView<'a> {
pub name: &'a str,
pub ty: TyId,
}
#[derive(Debug, Clone, Copy)]
pub struct AssocBindingIter<'a> {
pub(crate) arena: TyArenaView<'a>,
pub(crate) words: &'a [u32],
}
impl<'a> Iterator for AssocBindingIter<'a> {
type Item = AssocBindingView<'a>;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
if self.words.len() < 2 {
self.words = &[];
return None;
}
let name_off = u32::from_le(self.words[0]);
let ty = TyId(u32::from_le(self.words[1]));
self.words = &self.words[2..];
Some(AssocBindingView { name: self.arena.str_at(name_off), ty })
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
let n = self.words.len() / 2;
(n, Some(n))
}
}
impl ExactSizeIterator for AssocBindingIter<'_> {}
#[derive(Debug, Clone, Copy)]
pub struct TyBoundView<'a> {
pub trait_def: TyDefId,
pub is_maybe: bool,
pub flags: u32,
pub args: GenericArgIter<'a>,
pub assocs: AssocBindingIter<'a>,
}
#[derive(Debug, Clone, Copy)]
pub struct TyBoundIter<'a> {
pub(crate) arena: TyArenaView<'a>,
pub(crate) bounds: &'a [TyBoundEntry],
}
impl<'a> TyBoundIter<'a> {
#[inline]
pub fn len(&self) -> usize {
self.bounds.len()
}
#[inline]
pub fn is_empty(&self) -> bool {
self.bounds.is_empty()
}
}
impl<'a> Iterator for TyBoundIter<'a> {
type Item = TyBoundView<'a>;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
let (&raw, rest) = self.bounds.split_first()?;
self.bounds = rest;
Some(self.arena.decode_bound(raw.from_le()))
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
(self.bounds.len(), Some(self.bounds.len()))
}
}
impl ExactSizeIterator for TyBoundIter<'_> {}