Skip to content

Commit 7a00876

Browse files
committed
!drop
1 parent 51ed2cb commit 7a00876

File tree

1 file changed

+36
-0
lines changed
  • crates/matrix-sdk/src/event_cache

1 file changed

+36
-0
lines changed

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,32 @@ pub mod experimental {
500500
}
501501
}
502502

503+
impl<T, const C: usize> Drop for LinkedChunk<T, C> {
504+
fn drop(&mut self) {
505+
// Take the latest chunk.
506+
let mut current_chunk_ptr = self.last.or(Some(self.first));
507+
508+
// As long as we have another chunk…
509+
while let Some(chunk_ptr) = current_chunk_ptr {
510+
// Disconnect the chunk by updating `previous_chunk.next` pointer.
511+
let previous_ptr = unsafe { chunk_ptr.as_ref() }.previous;
512+
513+
if let Some(mut previous_ptr) = previous_ptr {
514+
unsafe { previous_ptr.as_mut() }.next = None;
515+
}
516+
517+
// Re-box the chunk, and let Rust does its job.
518+
let _chunk_boxed = unsafe { Box::from_raw(chunk_ptr.as_ptr()) };
519+
520+
// Update the `current_chunk_ptr`.
521+
current_chunk_ptr = previous_ptr;
522+
}
523+
524+
// At this step, all chunks have been dropped, including
525+
// `self.first`.
526+
}
527+
}
528+
503529
/// The position of a chunk in a [`LinkedChunk`].
504530
///
505531
/// 0 represents the latest chunk.
@@ -868,6 +894,16 @@ pub mod experimental {
868894
}
869895
}
870896

897+
#[test]
898+
fn test_empty() {
899+
let events = Events::<char, 3>::new();
900+
901+
assert_eq!(events.len(), 0);
902+
903+
// This test also ensures that `Drop` for `LinkedChunk` works when
904+
// there is only one chunk.
905+
}
906+
871907
#[test]
872908
fn test_push_events() {
873909
let mut events = Events::<char, 3>::new();

0 commit comments

Comments
 (0)