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
use super::*;

use crate::{
    erased_types::IteratorItem,
    type_level::bools::*,
};

macro_rules! declare_iter_interface {
    (
        $k:ident=>$v:ident;
        $(#[$attr:meta])*
        interface=$interface:ident;
        type Item=$item:ty;
    ) => (
        $(#[$attr])*
        #[repr(C)]
        #[derive(StableAbi)]
        #[sabi(inside_abi_stable_crate)]
        pub struct $interface<$k,$v>(PhantomData<Tuple2<$k,$v>>);

        impl<$k,$v> $interface<$k,$v>{
            pub const NEW:Self=Self(PhantomData);
        }

        
        impl<'a,$k:'a,$v:'a> IteratorItem<'a> for $interface<$k,$v>{
            type Item=$item;
        }
    )
}


declare_iter_interface!{
    K=>V;
    /// The InterfaceType of the `Iter` RHashMap iterator.
    interface=RefIterInterface;
    type Item=Tuple2<&'a K,&'a V>;
}

crate::impl_InterfaceType!{
    impl<K,V> crate::InterfaceType for RefIterInterface<K,V> {
        type Send=False;
        type Sync=False;
        type Iterator=True;
        type Clone=True;
    }
}



declare_iter_interface!{
    K=>V;
    /// The InterfaceType of the `IterMut` RHashMap iterator.
    interface=MutIterInterface;
    type Item=Tuple2<&'a K,&'a mut V>;
}

crate::impl_InterfaceType!{
    impl<K,V> crate::InterfaceType for MutIterInterface<K,V> {
        type Send=False;
        type Sync=False;
        type Iterator=True;
    }
}



declare_iter_interface!{
    K=>V;
    /// The InterfaceType of the `Drain` RHashMap iterator.
    interface=ValIterInterface;
    type Item=Tuple2<K,V>;
    
}

crate::impl_InterfaceType!{
    impl<K,V> crate::InterfaceType for ValIterInterface<K,V> {
        type Send=False;
        type Sync=False;
        type Iterator=True;
    }
}


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

type IntoIterInner<'a,K,V>=
    DynTrait<'a,RBox<()>,ValIterInterface<K,V>>;



/// An iterator that yields all the entries of an RHashMap,
/// deallocating the hashmap afterwards.
///
/// This is an `Iterator<Item= Tuple2< K, V > >+!Send+!Sync`
#[repr(transparent)]
#[derive(StableAbi)]
#[sabi(inside_abi_stable_crate)]
pub struct IntoIter<K,V>{
    iter:IntoIterInner<'static,u32,u32>,
    // _marker:PhantomData<Tuple2<K,V>>,
    _marker:PhantomData<Tuple3<K,V,UnsafeIgnoredType<std::rc::Rc<()>>>>,
}


impl<K,V> IntoIter<K,V>{
/**

# Safety

This must be called only in `ErasedMap::into_val`.
*/
    pub(super)unsafe fn new<'a>(iter:DynTrait<'a,RBox<()>,ValIterInterface<K,V>>)->Self
    where   
        K:'a,
        V:'a,
    {
        IntoIter{
            iter:mem::transmute::<IntoIterInner<'a,K,V>,IntoIterInner<'static,u32,u32>>(iter),
            _marker:PhantomData,
        }
    }

    #[inline]
    fn iter(&self)->&IntoIterInner<'_,K,V>{
        unsafe{ transmute_reference::<IntoIterInner<'static,u32,u32>,_>(&self.iter) }
    }
    #[inline]
    fn iter_mut(&mut self)->&mut IntoIterInner<'_,K,V>{
        unsafe{ transmute_mut_reference::<IntoIterInner<'static,u32,u32>,_>(&mut self.iter) }
    }
}


impl<K,V> Iterator for IntoIter<K,V>{
    type Item=Tuple2<K,V>;

    #[inline]
    fn next(&mut self)->Option<Tuple2<K,V>>{
        self.iter_mut().next()
    }

    #[inline]
    fn nth(&mut self,nth:usize)->Option<Tuple2<K,V>>{
        self.iter_mut().nth(nth)
    }

    #[inline]
    fn size_hint(&self)->(usize,Option<usize>){
        self.iter().size_hint()
    }

    #[inline]
    fn count(mut self)->usize{
        self.iter_mut().by_ref().count()
    }

    #[inline]
    fn last(mut self)->Option<Tuple2<K,V>>{
        self.iter_mut().by_ref().last()
    }
}