|
| 1 | +// check-pass |
| 2 | +#![feature(inline_const, generic_const_exprs)] |
| 3 | +#![allow(incomplete_features)] |
| 4 | +use std::marker::PhantomData; |
| 5 | + |
| 6 | +pub struct Equal<const T: usize, const R: usize>(); |
| 7 | +pub trait True {} |
| 8 | +impl<const T: usize> True for Equal<T, T> {} |
| 9 | + |
| 10 | +// replacement for generativity |
| 11 | +pub struct Id<'id>(PhantomData<fn(&'id ()) -> &'id ()>); |
| 12 | +pub struct Guard<'id>(Id<'id>); |
| 13 | +fn make_guard<'id>(i: &'id Id<'id>) -> Guard<'id> { |
| 14 | + Guard(Id(PhantomData)) |
| 15 | +} |
| 16 | + |
| 17 | +impl<'id> Into<Id<'id>> for Guard<'id> { |
| 18 | + fn into(self) -> Id<'id> { |
| 19 | + self.0 |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +pub struct Arena<'life> { |
| 24 | + bytes: *mut [u8], |
| 25 | + //bitmap: RefCell<RoaringBitmap>, |
| 26 | + _token: PhantomData<Id<'life>>, |
| 27 | +} |
| 28 | + |
| 29 | +#[repr(transparent)] |
| 30 | +pub struct Item<'life, T> { |
| 31 | + data: T, |
| 32 | + _phantom: PhantomData<Id<'life>>, |
| 33 | +} |
| 34 | + |
| 35 | +#[repr(transparent)] |
| 36 | +pub struct Token<'life, 'borrow, 'compact, 'reborrow, T> |
| 37 | +where |
| 38 | + 'life: 'reborrow, |
| 39 | + T: Tokenize<'life, 'borrow, 'compact, 'reborrow>, |
| 40 | +{ |
| 41 | + //ptr: *mut <T as Tokenize>::Tokenized, |
| 42 | + ptr: core::ptr::NonNull<T::Tokenized>, |
| 43 | + _phantom: PhantomData<Id<'life>>, |
| 44 | + _compact: PhantomData<&'borrow Guard<'compact>>, |
| 45 | + _result: PhantomData<&'reborrow T::Untokenized>, |
| 46 | +} |
| 47 | + |
| 48 | +impl<'life> Arena<'life> { |
| 49 | + pub fn tokenize<'before, 'compact, 'borrow, 'reborrow, T, U>( |
| 50 | + &self, |
| 51 | + guard: &'borrow Guard<'compact>, |
| 52 | + item: Item<'life, &'before mut T>, |
| 53 | + ) -> Token<'life, 'borrow, 'compact, 'reborrow, U> |
| 54 | + where |
| 55 | + T: Tokenize<'life, 'borrow, 'compact, 'reborrow, Untokenized = U>, |
| 56 | + T::Untokenized: Tokenize<'life, 'borrow, 'compact, 'reborrow>, |
| 57 | + Equal<{ core::mem::size_of::<T>() }, { core::mem::size_of::<U>() }>: True, |
| 58 | + 'compact: 'borrow, |
| 59 | + 'life: 'reborrow, |
| 60 | + 'life: 'compact, |
| 61 | + 'life: 'borrow, |
| 62 | + // 'borrow: 'before ?? |
| 63 | + { |
| 64 | + let dst = item.data as *mut T as *mut T::Tokenized; |
| 65 | + Token { |
| 66 | + ptr: core::ptr::NonNull::new(dst as *mut _).unwrap(), |
| 67 | + _phantom: PhantomData, |
| 68 | + _compact: PhantomData, |
| 69 | + _result: PhantomData, |
| 70 | + } |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +pub trait Tokenize<'life, 'borrow, 'compact, 'reborrow> |
| 75 | +where |
| 76 | + 'compact: 'borrow, |
| 77 | + 'life: 'reborrow, |
| 78 | + 'life: 'borrow, |
| 79 | + 'life: 'compact, |
| 80 | +{ |
| 81 | + type Tokenized; |
| 82 | + type Untokenized; |
| 83 | + const TO: fn(&Arena<'life>, &'borrow Guard<'compact>, Self) -> Self::Tokenized; |
| 84 | + const FROM: fn(&'reborrow Arena<'life>, Self::Tokenized) -> Self::Untokenized; |
| 85 | +} |
| 86 | + |
| 87 | +macro_rules! tokenize { |
| 88 | + ($to:expr, $from:expr) => { |
| 89 | + const TO: fn(&Arena<'life>, &'borrow Guard<'compact>, Self) -> Self::Tokenized = $to; |
| 90 | + const FROM: fn(&'reborrow Arena<'life>, Self::Tokenized) -> Self::Untokenized = $from; |
| 91 | + }; |
| 92 | +} |
| 93 | + |
| 94 | +struct Foo<'life, 'borrow>(Option<Item<'life, &'borrow mut Bar>>); |
| 95 | +struct TokenFoo<'life, 'borrow, 'compact, 'reborrow>( |
| 96 | + Option<Token<'life, 'borrow, 'compact, 'reborrow, Bar>>, |
| 97 | +); |
| 98 | +struct Bar(u8); |
| 99 | + |
| 100 | +impl<'life, 'before, 'borrow, 'compact, 'reborrow> Tokenize<'life, 'borrow, 'compact, 'reborrow> |
| 101 | + for Foo<'life, 'before> |
| 102 | +where |
| 103 | + 'compact: 'borrow, |
| 104 | + 'life: 'reborrow, |
| 105 | + 'life: 'borrow, |
| 106 | + 'life: 'compact, |
| 107 | +{ |
| 108 | + type Tokenized = TokenFoo<'life, 'borrow, 'compact, 'reborrow>; |
| 109 | + type Untokenized = Foo<'life, 'reborrow>; |
| 110 | + tokenize!(foo_to, foo_from); |
| 111 | +} |
| 112 | + |
| 113 | +impl<'life, 'borrow, 'compact, 'reborrow> Tokenize<'life, 'borrow, 'compact, 'reborrow> for Bar |
| 114 | +where |
| 115 | + 'compact: 'borrow, |
| 116 | + 'life: 'reborrow, |
| 117 | + 'life: 'borrow, |
| 118 | + 'life: 'compact, |
| 119 | +{ |
| 120 | + type Tokenized = Bar; |
| 121 | + type Untokenized = Bar; |
| 122 | + tokenize!(bar_to, bar_from); |
| 123 | +} |
| 124 | + |
| 125 | +fn bar_to<'life, 'borrow, 'compact>( |
| 126 | + arena: &Arena<'life>, |
| 127 | + guard: &'borrow Guard<'compact>, |
| 128 | + s: Bar, |
| 129 | +) -> Bar { |
| 130 | + s |
| 131 | +} |
| 132 | +fn bar_from<'life, 'reborrow>(arena: &'reborrow Arena<'life>, s: Bar) -> Bar { |
| 133 | + s |
| 134 | +} |
| 135 | + |
| 136 | +fn foo_to<'life, 'borrow, 'compact, 'reborrow, 'before>( |
| 137 | + arena: &'before Arena<'life>, |
| 138 | + guard: &'borrow Guard<'compact>, |
| 139 | + s: Foo<'life, 'before>, |
| 140 | +) -> TokenFoo<'life, 'borrow, 'compact, 'reborrow> { |
| 141 | + let Foo(bar) = s; |
| 142 | + TokenFoo(bar.map(|bar| arena.tokenize(guard, bar))) |
| 143 | +} |
| 144 | +fn foo_from<'life, 'borrow, 'compact, 'reborrow>( |
| 145 | + arena: &'reborrow Arena<'life>, |
| 146 | + s: TokenFoo<'life, 'borrow, 'compact, 'reborrow>, |
| 147 | +) -> Foo<'life, 'reborrow> { |
| 148 | + Foo(s.0.map(|bar| panic!())) |
| 149 | +} |
| 150 | + |
| 151 | +fn main() {} |
0 commit comments