supply 0.1.0

Provider API for arbitrary number of lifetimes.
Documentation
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
//! `'static` tags for types.
//!
//! Each type may have many tags, but each tag names on type.
//! The types named may contain an arbitrary number of lifetimes.
//!
//! Types that should be tags should implement the [`WithLt`] trait.
//! Types that are tagged by a specific tag should implement the [`Tagged`] trait.
//! Bounds for tags/tagged types should use [`Reify`] or [`ReifySized`].

mod tag_type_id;
mod tagged;

use core::marker::PhantomData;

pub use tag_type_id::TagTypeId;

use crate::lt_list::{EmptyLt, LifetimesOf, LtListStructure, ReifyLt, Subset};
use crate::{Lt0, Lt1, LtList};

/// Types with an associated `'static` tag.
///
/// All [`WithLt`] implementers are required to also implement this trait.
/// As a result [`Self::Tag`] may not reify to `Self` if `Self` is a tag.
/// Additionally, all implementers of this trait implement [`Reify`].
pub trait Tagged {
    /// The tag type.
    ///
    /// This can be used with [`Reify`] to construct a type with lifetimes.
    type Tag: ?Sized + Tag + Tagged<Tag = Self::Tag> + 'static;
}

/// [`Any`](core::any::Any) like trait with support for arbitrary number of lifetimes.
///
/// This trait is implemented for everything that can.
pub trait Anything<L: LtList = Lt0>: anything_seal::Sealed<L> {
    fn self_tag_id(&self) -> TagTypeId<L>;

    fn tag_id() -> TagTypeId<L>
    where
        Self: Sized;
}

pub trait AnythingExt<L: LtList>: Anything<L> {
    fn is<T: Reify<L>>(&self) -> bool {
        self.self_tag_id()
            .is_same(&TagTypeId::<L>::of::<T::Tag, T::Lifetimes>())
    }

    fn downcast_ref<T: ReifySized<L>>(&self) -> Option<&T::Reified> {
        if self.is::<T>() {
            Some(unsafe { &*(self as *const _ as *const T::Reified) })
        } else {
            None
        }
    }

    fn downcast_mut<T: ReifySized<L>>(&mut self) -> Option<&mut T::Reified> {
        if self.is::<T>() {
            Some(unsafe { &mut *(self as *mut _ as *mut T::Reified) })
        } else {
            None
        }
    }

    fn downcast_move<T: ReifySized<L>>(self) -> Result<T::Reified, Self>
    where
        Self: Sized,
    {
        if self.is::<T>() {
            // can't use `Option` trick here like with `Any`
            let this = core::mem::MaybeUninit::new(self);
            Ok(unsafe { core::mem::transmute_copy(&this) })
        } else {
            Err(self)
        }
    }
}

mod anything_seal {
    use super::*;

    pub trait Sealed<L: LtList> {}

    impl<L: LtList, T: ?Sized + Reify<L, Reified = T>> Sealed<L> for T {}
}

// The Reified = T here must be there for the downcast to be valid.
impl<L: LtList, T: ?Sized + Reify<L, Reified = T>> Anything<L> for T {
    fn self_tag_id(&self) -> TagTypeId<L> {
        TagTypeId::of::<T::Tag, T::Lifetimes>()
    }

    fn tag_id() -> TagTypeId<L>
    where
        Self: Sized,
    {
        TagTypeId::of::<T::Tag, T::Lifetimes>()
    }
}

impl<L: LtList, T: ?Sized + Anything<L>> AnythingExt<L> for T {}

pub trait Tag {
    /// The structure of the lifetimes the tag needs.
    ///
    /// This will be used by the blanket [`Reify`] implementation to strip
    /// the needed lifetimes from a lifetime list to reify the tag.
    type LifetimesTag: LtListStructure + 'static;
}

/// [`Tagged`] but reified with some lifetimes `L`.
///
/// The lifetimes in `L` are used to get the reified type and the lifetime list for
/// the reified type. The lifetimes are chosen via [`Tag::LifetimesTag`].
///
/// If you need a sized [`Self::Reified`] then bound on [`ReifySized`] instead.
///
/// This trait **cannot** be manually implemented. To have it be implemented on a type,
/// implement [`Tagged`] and this trait will automatically be implemented when possible.
pub trait Reify<L: LtList>: reify_seal::Sealed<L> {
    /// The type that was tagged, with it's lifetimes.
    type Reified: ?Sized;

    /// The lifetime list used to reify the type.
    ///
    /// This is some subset of lifetimes from `L`.
    type Lifetimes: LtList + Subset<L>;

    /// The tag for [`Self::Reified`].
    ///
    /// This is known to reify using [`WithLt`] into [`Self::Reified`].
    ///
    /// This is taken directly from [`Tagged::Tag`].
    type Tag: ?Sized
        + Tagged<Tag = Self::Tag>
        + Tag<LifetimesTag = Self::LifetimesTag>
        + WithLt<Self::Lifetimes, Reified = Self::Reified>
        + 'static;

    /// The structure of lifetimes for [`Reify::Lifetimes`].
    ///
    /// This is known to reify from the lifetimes in [`Reify::Lifetimes`].
    ///
    /// This is taken directly from [`Tag::LifetimesTag`].
    type LifetimesTag: ReifyLt<Self::Lifetimes, Lifetimes = Self::Lifetimes>
        + ReifyLt<L, Lifetimes = Self::Lifetimes>
        + 'static;
}

pub trait ReifySized<L: LtList>: Reify<L, Reified = Self::SizedReified> {
    type SizedReified;
}

impl<L: LtList, T: ?Sized + Reify<L>> ReifySized<L> for T
where
    T::Reified: Sized,
{
    type SizedReified = T::Reified;
}

impl<T, L> Reify<L> for T
where
    T: ?Sized + Tagged,
    L: LtList,
    LifetimesTagOf<T::Tag>: ReifyLt<L>,
    LifetimesTagOf<T::Tag>: ReifyLt<
        LifetimesOf<LifetimesTagOf<T::Tag>, L>,
        Lifetimes = LifetimesOf<LifetimesTagOf<T::Tag>, L>,
    >,
    T::Tag: WithLt<LifetimesOf<LifetimesTagOf<T::Tag>, L>>,
{
    type Reified = ReifiedOf<T::Tag, LifetimesOf<LifetimesTagOf<T::Tag>, L>>;

    type Lifetimes = LifetimesOf<LifetimesTagOf<T::Tag>, L>;

    type Tag = T::Tag;

    type LifetimesTag = LifetimesTagOf<T::Tag>;
}

mod reify_seal {
    use super::*;

    pub trait Sealed<L: LtList> {}

    impl<L, T> Sealed<L> for T
    where
        T: ?Sized + Tagged,
        L: LtList,
        LifetimesTagOf<T::Tag>: ReifyLt<L>,
        LifetimesTagOf<T::Tag>: ReifyLt<
            LifetimesOf<LifetimesTagOf<T::Tag>, L>,
            Lifetimes = LifetimesOf<LifetimesTagOf<T::Tag>, L>,
        >,
        T::Tag: WithLt<LifetimesOf<LifetimesTagOf<T::Tag>, L>>,
    {
    }
}

/// Get the [`Tagged::Tag`] of a type.
pub type TagOf<T> = <T as Tagged>::Tag;

pub type LifetimesTagOf<T> = <T as Tag>::LifetimesTag;

// pub type SizedReifiedOf<L, T> = <T as Tag<L>>::SizedReified;

pub type ReifiedOf<T, L> = <T as WithLt<L>>::Reified;

// /// A `'static` type tag.
// ///
// /// The difference between this trait and [`MaybeSizedTag`] is
// /// [`Self::SizedReified`] (and by extension [`MaybeSizedTag::Reified`])
// /// is known to implement [`Sized`].
// ///
// /// If you need to implement a custom tag, then implement [`MaybeSizedTag`] instead.
// /// The type will then automatically implement this trait also.
// pub trait Tag<L: LtList>: MaybeSizedTag<L, Reified = Self::SizedReified> {
//     /// Same type as [`MaybeSizedTag::Reified`].
//     type SizedReified;
// }

/// Add `L` lifetimes to get a new type.
///
/// This is designed for use on tags that are `'static`. Tags should implement this trait.
/// However, bounds should usually be for ...
pub trait WithLt<L: LtList>: Tag + Tagged<Tag = Self> + 'static {
    /// The type when given lifetimes from the `L` lifetime list.
    ///
    /// As example if `Self = Ref<str>` then `Reified = &'a str` when `L = Lt1<'a>`.
    /// The `'a` lifetime is "added" to the type.
    type Reified: ?Sized;
}

// Auto impl Tag when possible.
// impl<T: ?Sized + MaybeSizedTag<L>, L: LtList> Tag<L> for T
// where
//     T::Reified: Sized,
// {
//     type SizedReified = T::Reified;
// }

/// A tag configurable for any type.
#[allow(clippy::type_complexity)]
pub struct DynTag<LT, T: ?Sized>(PhantomData<(fn() -> *const T, LT)>);

pub trait TagDef<L: LtList>: 'static {
    type T: ?Sized;
}

// impl<L: LtList, T: ?Sized> TagDef<L> for dyn MaybeSizedTag<L, Reified = T> {
//     type T = T;
// }

// impl<L: LtList, Def: ?Sized + MaybeSizedTag<L>> MaybeSizedTag<L> for DynTag<Def> {
//     type Reified = Def::Reified;
// }

impl<Def: ?Sized + TagDef<L>, L: LtList, LT: ReifyLt<L, Lifetimes = L> + 'static> WithLt<L>
    for DynTag<LT, Def>
{
    type Reified = Def::T;
}

impl<Def: ?Sized + 'static, LT: LtListStructure + 'static> Tagged for DynTag<LT, Def> {
    type Tag = Self;
}

impl<Def: ?Sized + 'static, LT: LtListStructure + 'static> Tag for DynTag<LT, Def> {
    type LifetimesTag = LT;
}

/// Tag for `'static` type `T`.
///
/// The type `T` can be non-`Sized`.
pub type Static<T> = tag_for!(T);

/// Tag that adds a needed lifetime.
///
/// Tags like [`Ref`] and [`Mut`] use the first lifetime of the
/// provided `L` lifetime list. However this sharing is not always wanted.
pub struct AddLt<T: ?Sized>(PhantomData<fn() -> *const T>);

impl<T: ?Sized + WithLt<L::Tail>, L: LtList> WithLt<L> for AddLt<T> {
    type Reified = T::Reified;
}

impl<T: ?Sized + Tagged> Tagged for AddLt<T> {
    type Tag = AddLt<T::Tag>;
}

impl<T: ?Sized + Tag> Tag for AddLt<T> {
    type LifetimesTag = EmptyLt<T::LifetimesTag>;
}

/// Tag for `&T` that uses the first lifetime given in `L`.
pub struct Ref<T: ?Sized>(PhantomData<fn() -> T>);

impl<'r, T: ?Sized + WithLt<L>, L: LtList<Head = Lt1<'r>>> WithLt<L> for Ref<T>
where
    T::Reified: 'r,
{
    type Reified = &'r T::Reified;
}

impl<T: ?Sized + Tagged> Tagged for Ref<T> {
    type Tag = Ref<T::Tag>;
}

impl<T: ?Sized + Tag> Tag for Ref<T> {
    type LifetimesTag = T::LifetimesTag;
}

/// Tag for `&mut T` that uses the first lifetime given in `L`.
pub struct Mut<T: ?Sized>(PhantomData<fn() -> T>);

impl<'r, T: ?Sized + WithLt<L>, L: LtList<Head = Lt1<'r>>> WithLt<L> for Mut<T>
where
    T::Reified: 'r,
{
    type Reified = &'r mut T::Reified;
}

impl<T: ?Sized + Tagged> Tagged for Mut<T> {
    type Tag = Mut<T::Tag>;
}

impl<T: ?Sized + Tag> Tag for Mut<T> {
    type LifetimesTag = T::LifetimesTag;
}

#[macro_export]
macro_rules! tag_for {
    (<$l0:lifetime, $l1:lifetime, $l2:lifetime $(,)?> $type:ty) => {
        $crate::tag::DynTag<$crate::lt_list::EmptyLt<$crate::lt_list::EmptyLt<$crate::lt_list::EmptyLt<()>>>, dyn for<$l0, $l1, $l2> $crate::tag::TagDef<$crate::lt_list::Lt3<$l0, $l1, $l2>, T = $type>>
    };
    (<$l0:lifetime, $l1:lifetime $(,)?> $type:ty) => {
        $crate::tag::DynTag<$crate::lt_list::EmptyLt<$crate::lt_list::EmptyLt<()>>, dyn for<$l0, $l1> $crate::tag::TagDef<$crate::lt_list::Lt2<$l0, $l1>, T = $type>>
    };
    (<$l0:lifetime $(,)?> $type:ty) => {
        $crate::tag::DynTag<$crate::lt_list::EmptyLt<()>, dyn for<$l0> $crate::tag::TagDef<$crate::lt_list::Lt1<$l0>, T = $type>>
    };
    ($type:ty) => {
        $crate::tag::DynTag<(), dyn $crate::tag::TagDef<$crate::lt_list::Lt0, T = $type>>
    };
}
use tag_for;

#[macro_export]
macro_rules! impl_tagged {
    (
        $vis:vis struct $name:ident< $($tail:tt)*
    ) => {
        $crate::impl_tagged! {
            @scan_generic
            {
                $vis struct $name
            }
            ()
            ()
            ()
            ()
            ()
            {$($tail)*}
        }
    };
    (
            @scan_generic
            {$($struct:tt)*}
            ($($generics:tt)*)
            ($($reify_generics:tt)*)
            ($($lt_generics:tt)*)
            ($($tag_generics:tt)*)
            ($($const_generics:tt)*)
            {$(,)? $generic:ident $(,)? $($tail:tt)*}
    ) => {
        $crate::impl_tagged! {
            @scan_generic
            {$($struct)*}
            ($($generics)* $generic,)
            ($($reify_generics)* $generic::Reified,)
            ($($lt_generics)*)
            ($($tag_generics)* $generic,)
            ($($const_generics)*)
            {$($tail)*}
        }
    };
    (
            @scan_generic
            {$($struct:tt)*}
            ($($generics:tt)*)
            ($($reify_generics:tt)*)
            ($($lt_generics:tt)*)
            ($($tag_generics:tt)*)
            ($($const_generics:tt)*)
            {$(,)? $lt:lifetime $($tail:tt)*}
    ) => {
        $crate::impl_tagged! {
            @scan_generic
            {$($struct)*}
            ($($generics)* $lt,)
            ($($reify_generics)* $lt,)
            ($($lt_generics)* $lt,)
            ($($tag_generics)*)
            ($($const_generics)*)
            {$($tail)*}
        }
    };
    (
            @scan_generic
            {
                $vis:vis struct $name:ident
            }
            ($($generics:tt)*)
            ($($reify_generics:tt)*)
            ($($lt_generic:lifetime,)*)
            ($($tag_generic:ident,)*)
            ($($const_generics:tt)*)
            {$(,)? > $(where $($where:tt)*)?}
    ) => {
        const _: () = {
            $vis struct Tag<$($tag_generic: ?Sized)*>(::core::marker::PhantomData<fn() -> ($(*const $tag_generic,)*)>);

            impl<$($generics)*> Tagged for $name<$($generics)*>
            where
                $($tag_generic: Tagged,)*
            {
                type Tag = Tag<$($tag_generic::Tag)*>;
            }

            impl<$($lt_generic,)* $($tag_generic,)* L: LtList> WithLt<$crate::impl_tagged!(@lifetime $($lt_generic,)* L)> for Tag<$($tag_generic,)*>
            where
                $($tag_generic: ?Sized + WithLt<L>,)*
                $($tag_generic::Reified: Sized,)*
                $($($where)*)?
            {
                type Reified = $name<$($reify_generics)*>;
            }

            impl<$($tag_generic: ?Sized + Tagged,)*> Tagged for Tag<$($tag_generic,)*> {
                type Tag = Tag<$($tag_generic::Tag)*>;
            }

            impl<$($tag_generic: ?Sized + supply::tag::Tag,)*> supply::tag::Tag for Tag<$($tag_generic,)*> {
                type LifetimesTag = $crate::impl_tagged!(@lifetime_tag $($lt_generic,)* <($($tag_generic::LifetimesTag,)*) as supply::lt_list::MergeTuple>::LtStructure);
            }
        };
    };
    (
        @lifetime $l0:lifetime, $L:ty
    ) => {
        $crate::lt_list::Lt1<$l0, $L>
    };
    (
        @lifetime_tag $l0:lifetime, $L:ty
    ) => {
        $crate::lt_list::EmptyLt<$L>
    };
    (
        @lifetime $L:ty
    ) => {
        $L
    };
    (
        @lifetime_tag $L:ty
    ) => {
        $L
    };
}