Skip to content

Commit 6b754ac

Browse files
committed
feat(sdk): Add Chunk::as_ptr.
This patch adds the new `Chunk::as_ptr` method. It simplifies the code a little bit.
1 parent 88f75a8 commit 6b754ac

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

crates/matrix-sdk/src/event_cache/linked_chunk.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ impl<Item, Gap, const CAP: usize> LinkedChunk<Item, Gap, CAP> {
9898
if last_chunk.is_first_chunk().not() {
9999
// Maybe `last_chunk` is the same as the previous `self.last` chunk, but it's
100100
// OK.
101-
self.last = Some(NonNull::from(last_chunk));
101+
self.last = Some(last_chunk.as_ptr());
102102
}
103103

104104
self.length += number_of_items;
@@ -176,7 +176,7 @@ impl<Item, Gap, const CAP: usize> LinkedChunk<Item, Gap, CAP> {
176176
if chunk.is_first_chunk().not() && chunk.is_last_chunk() {
177177
// Maybe `chunk` is the same as the previous `self.last` chunk, but it's
178178
// OK.
179-
self.last = Some(NonNull::from(chunk));
179+
self.last = Some(chunk.as_ptr());
180180
}
181181

182182
self.length += number_of_items;
@@ -241,7 +241,7 @@ impl<Item, Gap, const CAP: usize> LinkedChunk<Item, Gap, CAP> {
241241
if chunk.is_first_chunk().not() && chunk.is_last_chunk() {
242242
// Maybe `chunk` is the same as the previous `self.last` chunk, but it's
243243
// OK.
244-
self.last = Some(NonNull::from(chunk));
244+
self.last = Some(chunk.as_ptr());
245245
}
246246

247247
Ok(())
@@ -719,6 +719,11 @@ impl<Item, Gap, const CAPACITY: usize> Chunk<Item, Gap, CAPACITY> {
719719
NonNull::from(Box::leak(chunk_box))
720720
}
721721

722+
/// Get the pointer to `Self`.
723+
pub fn as_ptr(&self) -> NonNull<Self> {
724+
NonNull::from(self)
725+
}
726+
722727
/// Check whether this current chunk is a gap chunk.
723728
pub fn is_gap(&self) -> bool {
724729
matches!(self.content, ChunkContent::Gap(..))
@@ -848,7 +853,7 @@ impl<Item, Gap, const CAPACITY: usize> Chunk<Item, Gap, CAPACITY> {
848853
// Link to the new chunk.
849854
self.next = Some(new_chunk_ptr);
850855
// Link the new chunk to this one.
851-
new_chunk.previous = Some(NonNull::from(self));
856+
new_chunk.previous = Some(self.as_ptr());
852857

853858
new_chunk
854859
}

0 commit comments

Comments
 (0)