konst_macro_rules/
into_iter.rs

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
use core::{marker::PhantomData, mem::ManuallyDrop};

pub mod range_into_iter;
pub mod slice_into_iter;

pub trait IntoIterKind {
    type Kind;
}

pub struct IsStdKind {}

pub struct IsNonIteratorKind {}

pub struct IsIteratorKind {}

///
#[repr(transparent)]
pub struct IntoIterWrapper<I, K> {
    pub iter: ManuallyDrop<I>,
    pub marker: IsIntoIterKind<I, K>,
}

mod is_into_iter_kind {
    use super::*;

    pub struct IsIntoIterKind<T, K>(PhantomData<(fn() -> PhantomData<T>, fn() -> K)>);

    impl<T> IsIntoIterKind<T, T::Kind>
    where
        T: IntoIterKind,
    {
        pub const NEW: Self = Self(PhantomData);
    }
}
pub use is_into_iter_kind::IsIntoIterKind;

impl<T> IntoIterWrapper<T, IsStdKind> {
    #[inline(always)]
    pub const fn coerce(self) -> Self {
        self
    }
}

impl<T> IntoIterWrapper<T, IsNonIteratorKind> {
    #[inline(always)]
    pub const fn coerce(self) -> T {
        ManuallyDrop::into_inner(self.iter)
    }
}

impl<T> IntoIterWrapper<T, IsIteratorKind> {
    #[inline(always)]
    pub const fn coerce(self) -> Self {
        self
    }

    #[inline(always)]
    pub const fn const_into_iter(self) -> T {
        ManuallyDrop::into_inner(self.iter)
    }
}

////////////////////////////////////////////////////////////////////////////////

pub struct EmptyIter;

impl EmptyIter {
    #[inline(always)]
    pub const fn next(self) -> Option<(core::convert::Infallible, Self)> {
        None
    }
}

impl IntoIterKind for EmptyIter {
    type Kind = IsIteratorKind;
}

////////////////////////////////////////////////////////////////////////////////

#[macro_export]
macro_rules! into_iter_macro {
    ($iter:expr) => {
        $crate::__::IntoIterWrapper {
            iter: $crate::__::ManuallyDrop::new($iter),
            marker: $crate::__::IsIntoIterKind::NEW,
        }
        .coerce()
        .const_into_iter()
    };
}