use std::marker::PhantomData;
use std::sync::Arc;
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct HNil;
#[derive(Debug)]
pub struct HCons<H, T>(pub Arc<H>, pub T);
impl<H, T: Clone> Clone for HCons<H, T> {
fn clone(&self) -> Self {
Self(Arc::clone(&self.0), self.1.clone())
}
}
impl<H, T> HCons<H, T> {
pub fn new(head: H, tail: T) -> Self {
Self(Arc::new(head), tail)
}
}
#[derive(Debug)]
pub struct Here;
#[derive(Debug)]
pub struct There<I>(PhantomData<I>);
pub trait HList: Clone + Send + Sync + 'static {
const LEN: usize;
}
impl HList for HNil {
const LEN: usize = 0;
}
impl<H: Send + Sync + 'static, T: HList> HList for HCons<H, T> {
const LEN: usize = 1 + T::LEN;
}
#[diagnostic::on_unimplemented(
message = "capability `{T}` is not in this effect's environment",
label = "`{T}` is required here",
note = "add `{T}` to the effect's `caps!(..)`, or remove it with `.provide(..)` / `.provide_env(..)` before running"
)]
pub trait Selector<T, I> {
fn get(&self) -> &Arc<T>;
}
impl<T, Tl> Selector<T, Here> for HCons<T, Tl> {
fn get(&self) -> &Arc<T> {
&self.0
}
}
#[diagnostic::do_not_recommend]
impl<T, H, Tl: Selector<T, I>, I> Selector<T, There<I>> for HCons<H, Tl> {
fn get(&self) -> &Arc<T> {
self.1.get()
}
}
#[diagnostic::on_unimplemented(
message = "`{T}` is not required by this effect, so it cannot be provided",
label = "this effect's environment `{Self}` has no `{T}`",
note = "only capabilities in the effect's `R` can be provided; drop this `.provide(..)`"
)]
pub trait Plucker<T, I>: Sized {
type Rest: HList;
fn plant(t: Arc<T>, rest: Self::Rest) -> Self;
fn pluck(self) -> (Arc<T>, Self::Rest);
}
impl<T: Send + Sync + 'static, Tl: HList> Plucker<T, Here> for HCons<T, Tl> {
type Rest = Tl;
fn plant(t: Arc<T>, rest: Tl) -> Self {
Self(t, rest)
}
fn pluck(self) -> (Arc<T>, Tl) {
(self.0, self.1)
}
}
#[diagnostic::do_not_recommend]
impl<T, H: Send + Sync + 'static, Tl: Plucker<T, I>, I> Plucker<T, There<I>> for HCons<H, Tl> {
type Rest = HCons<H, Tl::Rest>;
fn plant(t: Arc<T>, rest: Self::Rest) -> Self {
Self(rest.0, Tl::plant(t, rest.1))
}
fn pluck(self) -> (Arc<T>, Self::Rest) {
let (t, rest) = self.1.pluck();
(t, HCons(self.0, rest))
}
}
#[diagnostic::on_unimplemented(
message = "`{Sub}` is not a part of this effect's environment `{Self}`",
label = "cannot provide `{Sub}` here",
note = "every capability given to `.provide_some(..)` must be in the effect's `R`; provide the others separately or drop them"
)]
pub trait Sculptor<Sub, Is>: Sized {
type Rest: HList;
fn plant(sub: Sub, rest: Self::Rest) -> Self;
}
impl<S: HList> Sculptor<HNil, HNil> for S {
type Rest = S;
fn plant(_: HNil, rest: S) -> S {
rest
}
}
#[diagnostic::do_not_recommend]
impl<S, H, T, I, Is> Sculptor<HCons<H, T>, HCons<I, Is>> for S
where
S: Plucker<H, I>,
S::Rest: Sculptor<T, Is>,
{
type Rest = <S::Rest as Sculptor<T, Is>>::Rest;
fn plant(sub: HCons<H, T>, rest: Self::Rest) -> Self {
S::plant(sub.0, <S::Rest as Sculptor<T, Is>>::plant(sub.1, rest))
}
}
#[diagnostic::on_unimplemented(
message = "the environment `{Self}` does not contain every capability of `{Sub}`",
label = "cannot narrow `{Self}` to `{Sub}`",
note = "a `~` bind, `.narrow()` or `.provide_env(..)` needs every capability of `{Sub}` in `{Self}`; add the missing ones to the caller's `caps!(..)`"
)]
pub trait Embed<Sub, Is> {
fn project(&self) -> Sub;
}
impl<S> Embed<HNil, HNil> for S {
fn project(&self) -> HNil {
HNil
}
}
#[diagnostic::do_not_recommend]
impl<S, H, T, I, Is> Embed<HCons<H, T>, HCons<I, Is>> for S
where
S: Selector<H, I> + Embed<T, Is>,
{
fn project(&self) -> HCons<H, T> {
HCons(
Arc::clone(Selector::<H, I>::get(self)),
Embed::<T, Is>::project(self),
)
}
}
pub trait Concat<Rhs> {
type Out: HList;
fn concat(self, rhs: Rhs) -> Self::Out;
}
impl<Rhs: HList> Concat<Rhs> for HNil {
type Out = Rhs;
fn concat(self, rhs: Rhs) -> Rhs {
rhs
}
}
impl<H: Send + Sync + 'static, T: Concat<Rhs>, Rhs> Concat<Rhs> for HCons<H, T> {
type Out = HCons<H, T::Out>;
fn concat(self, rhs: Rhs) -> Self::Out {
HCons(self.0, self.1.concat(rhs))
}
}
#[doc(hidden)]
#[macro_export]
macro_rules! __hlist {
() => { $crate::HNil };
($head:expr $(, $rest:expr)* $(,)?) => {
$crate::HCons::new($head, $crate::__hlist!($($rest),*))
};
}
#[doc(inline)]
pub use crate::__hlist as hlist;
#[cfg(test)]
mod tests {
use super::*;
struct A(u8);
struct B(&'static str);
struct C;
fn get<T, I, L: Selector<T, I>>(l: &L) -> &Arc<T> {
l.get()
}
#[test]
fn select_pluck_sculpt_embed_concat() {
let env = hlist![A(1), B("b"), C];
assert_eq!(<HCons<A, HCons<B, HCons<C, HNil>>> as HList>::LEN, 3);
assert_eq!(get::<A, _, _>(&env).0, 1);
assert_eq!(get::<B, _, _>(&env).0, "b");
let (b, rest): (Arc<B>, HCons<A, HCons<C, HNil>>) = Plucker::<B, _>::pluck(env.clone());
assert_eq!(b.0, "b");
let back: HCons<A, HCons<B, HCons<C, HNil>>> = Plucker::<B, _>::plant(b, rest);
assert_eq!(get::<A, _, _>(&back).0, 1);
let sub: HCons<C, HCons<A, HNil>> = Embed::<_, _>::project(&env);
assert_eq!(get::<A, _, _>(&sub).0, 1);
let rest = HCons::new(B("x"), HNil);
let whole: HCons<A, HCons<B, HCons<C, HNil>>> =
Sculptor::<HCons<A, HCons<C, HNil>>, _>::plant(hlist![A(3), C], rest);
assert_eq!(get::<B, _, _>(&whole).0, "x");
assert_eq!(get::<A, _, _>(&whole).0, 3);
let cat = hlist![A(2)].concat(hlist![C]);
let _: &HCons<A, HCons<C, HNil>> = &cat;
}
}