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
use crate::entry::{IntoCollectionMut, NextOccupiedFromOccupied, PrevOccupiedFromOccupied};

use super::super::{
    get_entry::{GetEntryByIndex, GetFirstEntry, GetLastEntry},
    occupied_entry::{KeyedOccupiedEntry, OccupiedEntry},
};

pub struct OccupiedMutSliceEntry<'c, V> {
    slice: &'c mut [V],
    index: usize,
}

impl<'c, V> OccupiedEntry<'c> for OccupiedMutSliceEntry<'c, V> {
    type Value = V;

    fn get_value<'e>(&'e self) -> &'e Self::Value
    where
        'c: 'e,
    {
        // SAFETY: the bounds checks are done when creating an occupied entry so we know they passed.
        unsafe { self.slice.get_unchecked(self.index) }
    }

    fn get_value_mut<'e>(&'e mut self) -> &mut Self::Value
    where
        'c: 'e,
    {
        // SAFETY: the bounds checks are done when creating an occupied entry so we know they passed.
        unsafe { self.slice.get_unchecked_mut(self.index) }
    }

    fn into_value_mut<'e>(self) -> &'c mut Self::Value {
        // SAFETY: the bounds checks are done when creating an occupied entry so we know they passed.
        unsafe { self.slice.get_unchecked_mut(self.index) }
    }
}

impl<'c, V> KeyedOccupiedEntry<'c> for OccupiedMutSliceEntry<'c, V> {
    type Key = usize;

    type BorrowedKey = usize;

    fn get_pair<'e>(&'e self) -> (&'e Self::Key, &'e Self::Value)
    where
        'c: 'e,
    {
        // SAFETY: the bounds checks are done when creating an occupied entry so we know they passed.
        (&self.index, unsafe { self.slice.get_unchecked(self.index) })
    }

    fn get_pair_mut<'e>(&'e mut self) -> (&'e Self::Key, &'e mut Self::Value)
    where
        'c: 'e,
    {
        // SAFETY: the bounds checks are done when creating an occupied entry so we know they passed.
        (&self.index, unsafe {
            self.slice.get_unchecked_mut(self.index)
        })
    }

    fn into_pair(self) -> (Self::BorrowedKey, &'c mut Self::Value) {
        // SAFETY: the bounds checks are done when creating an occupied entry so we know they passed.
        (self.index, unsafe {
            self.slice.get_unchecked_mut(self.index)
        })
    }
}

impl<'c, V> NextOccupiedFromOccupied<'c> for OccupiedMutSliceEntry<'c, V> {
    fn get_next_occupied(mut self) -> Option<Self> {
        self.index += 1;
        if self.index < self.slice.len() {
            Some(self)
        } else {
            None
        }
    }
}

impl<'c, V> PrevOccupiedFromOccupied<'c> for OccupiedMutSliceEntry<'c, V> {
    fn get_prev_occupied(mut self) -> Option<Self> {
        if self.index == 0 {
            None
        } else {
            self.index -= 1;
            Some(self)
        }
    }
}

impl<V> GetFirstEntry<V> for [V] {
    type Occupied<'c> = OccupiedMutSliceEntry<'c, V>
    where
        Self: 'c;

    fn get_first_occupied<'c>(&'c mut self) -> Option<Self::Occupied<'c>> {
        if self.is_empty() {
            None
        } else {
            Some(OccupiedMutSliceEntry {
                slice: self,
                index: 0,
            })
        }
    }
}

impl<V> GetLastEntry<V> for [V] {
    type Occupied<'c> = OccupiedMutSliceEntry<'c, V>
    where
        Self: 'c;

    fn get_last_occupied<'c>(&'c mut self) -> Option<Self::Occupied<'c>> {
        if self.is_empty() {
            None
        } else {
            Some(OccupiedMutSliceEntry {
                index: self.len() - 1,
                slice: self,
            })
        }
    }
}

impl<V> GetEntryByIndex<V> for [V] {
    type Occupied<'c> = OccupiedMutSliceEntry<'c, V>
    where
        Self: 'c;

    fn get_occupied<'c>(&'c mut self, index: usize) -> Option<Self::Occupied<'c>> {
        if index < self.len() {
            Some(OccupiedMutSliceEntry { slice: self, index })
        } else {
            None
        }
    }
}

impl<'c, V> IntoCollectionMut<'c> for OccupiedMutSliceEntry<'c, V> {
    type Collection = [V];

    fn into_collection_mut(self) -> &'c mut Self::Collection {
        self.slice
    }
}