#![no_std]
use core::ops::{Deref, DerefMut};
use core::{iter::once, ops::Index};
extern crate alloc;
use alloc::boxed::Box;
use alloc::vec;
use alloc::vec::Vec;
use arena_traits::Arena;
use either::Either;
pub trait Func {
type Block;
type Blocks: Arena<Self::Block, Output: Block<Self>>;
type BRef<'a>: Deref<Target = Self::Blocks> where Self: 'a;
type BMut<'a>: DerefMut<Target = Self::Blocks> where Self: 'a;
fn blocks<'a>(&'a self) -> Self::BRef<'a>;
fn blocks_mut<'a>(&'a mut self) -> Self::BMut<'a>;
fn entry(&self) -> Self::Block;
}
pub type BlockI<F> = <<F as Func>::Blocks as Index<<F as Func>::Block>>::Output;
pub type TermI<F> = <BlockI<F> as Block<F>>::Terminator;
pub type TargetI<F> = <TermI<F> as Term<F>>::Target;
pub trait Block<F: Func<Blocks: Arena<F::Block, Output = Self>> + ?Sized> {
type Terminator: Term<F>;
fn term(&self) -> &Self::Terminator;
fn term_mut(&mut self) -> &mut Self::Terminator;
}
pub trait Target<F: Func + ?Sized>: Term<F, Target = Self> {
fn block(&self) -> F::Block;
type BMut<'a>: DerefMut<Target = F::Block> where Self: 'a;
fn block_mut<'a>(&'a mut self) -> Self::BMut<'a>;
}
pub trait Term<F: Func + ?Sized> {
type Target: Target<F>;
fn targets<'a>(&'a self) -> Box<dyn Iterator<Item = &'a Self::Target> + 'a>
where
F: 'a;
fn targets_mut<'a>(&'a mut self) -> Box<dyn Iterator<Item = &'a mut Self::Target> + 'a>
where
F: 'a;
}
impl<F: Func + ?Sized, T: Target<F>, A: Term<F, Target = T>, B: Term<F, Target = T>> Term<F>
for Either<A, B>
{
type Target = T;
fn targets<'a>(&'a self) -> Box<(dyn Iterator<Item = &'a T> + 'a)>
where
F: 'a,
{
match self {
Either::Left(a) => a.targets(),
Either::Right(b) => b.targets(),
}
}
fn targets_mut<'a>(&'a mut self) -> Box<(dyn Iterator<Item = &'a mut T> + 'a)>
where
F: 'a,
{
match self {
Either::Left(a) => a.targets_mut(),
Either::Right(b) => b.targets_mut(),
}
}
}