@@ -86,26 +86,16 @@ pub enum LinkedChunkUpdate<Item, Gap> {
86
86
87
87
/// Links of a `LinkedChunk`, i.e. the first and last [`Chunk`].
88
88
///
89
- /// That's an internal type to make the borrow checker happy. Indeed, to add
90
- /// safety around these `NonNull`s, we want to use the borrow checker as much as
91
- /// possible. For example, `Self::latest_chunk_mut` takes `&mut self` and
92
- /// returns `&mut Chunk`, which is great and safe and all. Except that, if the
93
- /// `first` and `last` fields are used directly inside [`LinkedChunk`], the
94
- /// borrow checker considers that `self` is entirely used mutably. Thus, when we
95
- /// use another field of `LinkedChunk` mutably or immutably, it violates the
96
- /// mutability rules. The borrow checker is smart though: If we extract some
97
- /// values in a particular type, which are placed in a single field, the borrow
98
- /// checker knows that the other fields are safe to use mutably or immutably.
99
- /// The checks are per-field, not per-struct. That's why `first` and `last` have
100
- /// been extracted in a separate type, this type.
101
- struct LinkedChunkLinks < const CHUNK_CAPACITY : usize , Item , Gap > {
89
+ /// This type was introduced to avoid borrow checking errors when mutably
90
+ /// referencing a subset of fields of a `LinkedChunk`.
91
+ struct LinkedChunkEnds < const CHUNK_CAPACITY : usize , Item , Gap > {
102
92
/// The first chunk.
103
93
first : NonNull < Chunk < CHUNK_CAPACITY , Item , Gap > > ,
104
94
/// The last chunk.
105
95
last : Option < NonNull < Chunk < CHUNK_CAPACITY , Item , Gap > > > ,
106
96
}
107
97
108
- impl < const CAP : usize , Item , Gap > LinkedChunkLinks < CAP , Item , Gap > {
98
+ impl < const CAP : usize , Item , Gap > LinkedChunkEnds < CAP , Item , Gap > {
109
99
/// Get the first chunk, as an immutable reference.
110
100
fn first_chunk ( & self ) -> & Chunk < CAP , Item , Gap > {
111
101
unsafe { self . first . as_ref ( ) }
@@ -164,7 +154,7 @@ impl<const CAP: usize, Item, Gap> LinkedChunkLinks<CAP, Item, Gap> {
164
154
/// otherwise it will grow in memory for the eternity.
165
155
pub struct LinkedChunk < const CHUNK_CAPACITY : usize , Item , Gap > {
166
156
/// The links to the chunks, i.e. the first and the last chunk.
167
- links : LinkedChunkLinks < CHUNK_CAPACITY , Item , Gap > ,
157
+ links : LinkedChunkEnds < CHUNK_CAPACITY , Item , Gap > ,
168
158
/// The number of items hold by this linked chunk.
169
159
length : usize ,
170
160
/// The generator of chunk identifiers.
0 commit comments