1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
//! A collection of built in protocols between walkers and visitors.
//!
//! Treaty has not set data model. Instead, this module contains a set of basic protocols
//! walkers and visitors can use to exchange information.
//!
//! | Rust Type (`T`) | Protocol |
//! |-----------|----------|
//! | `bool`    | [`dyn Value<'_, OwnedStatic<T>, _>`][visitor::Value] |
//! | `i8`, `i16`, `i32`, `i64`, `i128`, `isize`    | [`dyn Value<'_, OwnedStatic<T>, _>`][visitor::Value] |
//! | `u8`, `u16`, `u32`, `u64`, `u128`, `usize`    | [`dyn Value<'_, OwnedStatic<T>, _>`][visitor::Value] |
//! | `f32`, `f64` | [`dyn Value<'_, OwnedStatic<T>, _>`][visitor::Value] |
//! | `char` | [`dyn Value<'_, OwnedStatic<T>, _>`][visitor::Value] |
//! | `String` | [`dyn Value<'_, OwnedStatic<T>, _>`][visitor::Value] |
//! | `Vec<u8>` | [`dyn Value<'_, OwnedStatic<T>, _>`][visitor::Value] |
//! | `&'ctx str` | [`dyn Value<'ctx, BorrowedStatic<'ctx, T>, _>`][visitor::Value] |
//! | `&'ctx [u8]` | [`dyn Value<'ctx, BorrowedStatic<'ctx, T>, _>`][visitor::Value] |
//!
//!
//!
//! Interface for interfaces.
//!
//! ## Design
//! The design of protocols is based on an idea found in the
//! [`gdbstub`](https://docs.rs/gdbstub/latest/gdbstub/target/ext/index.html) crate.
//! This idea is of so called inlinable dyn extension traits.
//! However, in the form given in `gdbstub` they can't be used for arbitrary interfaces.
//! The main trait still needs to know about all the possible protocols.
//! That is where this module comes in.
//!
//! This module implements a technique we name dynamic inlinable dyn extension traits (DIDETs).
//! DIDETs adds one more layer to IDETs. Instead of a trait that knows all the possible protocols,
//! we have a single trait [`Implementer`] that allows looking up an extension trait
//! using a type ID. This may seem like it defeats the purpose of IDETs, that being to
//! make them inlinable. However, it turns out LLVM (the optimizer) is able to see
//! through this style of runtime reflection. As such, we still gain the benefits of
//! IDETs but with more flexability.
//! Protocols can now be defined in *any* crate and used between arbitrary crates.
//!
//! A protocol is a special trait that can participate as a DIDET. The only thing needed
//! for a protocol is an associated trait object. Because we need to use the
//! [`TypeId`][core::any::TypeId] of a protocol to perform reflection, we can't just use
//! the trait object itself as the protocol type. Instead an uninhabited type is used
//! as a marker for the trait.
//!
//! We then "implement" a protocol for a type by using [`Implementation`]. This provides
//! a mapping from `T` to the protocol's trait object.
//! By itself, [`Implementation`] is not enough for DIDET. A type also needs to implement
//! [`Implementer`] which allows looking up a particular [`Implementation`] trait object
//! from a [`ProtocolId`].
//!
//! The implementation of DIDETs defined by this module allows [`Implementer`] to be object safe.
//! This is done via the help of the [`AnyImpl`] type. This is not required for the core
//! idea of DIDETs.

pub mod visitor;
pub mod walker;

use core::ops::{Deref, DerefMut};

use effectful::{bound::{IsSend, IsSync}, environment::Environment};

use crate::any::AnyTrait;

pub struct DynVisitor<'a, 'ctx, Env: Environment>(pub &'a mut (dyn AnyTrait<'ctx, Env> + 'a));

impl<'a, 'ctx, Env: Environment> DynVisitor<'a, 'ctx, Env> {
    pub fn cast<'b>(&'b mut self) -> DynVisitor<'b, 'ctx, Env> {
        DynVisitor(&mut *self.0)
    }
}

impl<'a, 'ctx, Env: Environment> Deref for DynVisitor<'a, 'ctx, Env> {
    type Target = dyn AnyTrait<'ctx, Env> + 'a;

    fn deref(&self) -> &Self::Target {
        &*self.0
    }
}

impl<'a, 'ctx, Env: Environment> DerefMut for DynVisitor<'a, 'ctx, Env> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut *self.0
    }
}

pub trait AsVisitor<'ctx, Env: Environment> {
    fn as_visitor<'a>(&'a mut self) -> DynVisitor<'a, 'ctx, Env>
    where
        'ctx: 'a;
}

impl<'b, 'ctx, Env: Environment> AsVisitor<'ctx, Env> for DynVisitor<'b, 'ctx, Env> {
    fn as_visitor<'a>(&'a mut self) -> DynVisitor<'a, 'ctx, Env>
    where
        'ctx: 'a,
    {
        self.cast()
    }
}

pub struct DynWalker<'a, 'ctx, Env: Environment>(pub &'a mut (dyn AnyTrait<'ctx, Env> + 'a));

unsafe impl<'a, 'ctx, E: Environment> IsSend<E::NeedSend> for DynWalker<'a, 'ctx, E> {}

unsafe impl<'a, 'ctx, E: Environment> IsSync<E::NeedSync> for DynWalker<'a, 'ctx, E> {}

impl<'a, 'ctx, Env: Environment> DynWalker<'a, 'ctx, Env> {
    pub fn cast<'b>(&'b mut self) -> DynWalker<'b, 'ctx, Env> {
        DynWalker(&mut *self.0)
    }
}

impl<'a, 'ctx, Env: Environment> Deref for DynWalker<'a, 'ctx, Env> {
    type Target = dyn AnyTrait<'ctx, Env> + 'a;

    fn deref(&self) -> &Self::Target {
        &*self.0
    }
}

impl<'a, 'ctx, Env: Environment> DerefMut for DynWalker<'a, 'ctx, Env> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut *self.0
    }
}