Skip to content

Commit

Permalink
!drop
Browse files Browse the repository at this point in the history
  • Loading branch information
Hywan committed Feb 29, 2024
1 parent 33becc2 commit 3b4ef21
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions crates/matrix-sdk/src/event_cache/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,32 @@ pub mod experimental {
}
}

impl<T, const C: usize> Drop for LinkedChunk<T, C> {
fn drop(&mut self) {
// Take the latest chunk.
let mut current_chunk_ptr = self.last.or(Some(self.first));

// As long as we have another chunk…
while let Some(chunk_ptr) = current_chunk_ptr {
// Disconnect the chunk by updating `previous_chunk.next` pointer.
let previous_ptr = unsafe { chunk_ptr.as_ref() }.previous;

if let Some(mut previous_ptr) = previous_ptr {
unsafe { previous_ptr.as_mut() }.next = None;
}

// Re-box the chunk, and let Rust does its job.
let _chunk_boxed = unsafe { Box::from_raw(chunk_ptr.as_ptr()) };

// Update the `current_chunk_ptr`.
current_chunk_ptr = previous_ptr;
}

// At this step, all chunks have been dropped, including
// `self.first`.
}
}

/// The position of a chunk in a [`LinkedChunk`].
///
/// 0 represents the latest chunk.
Expand Down Expand Up @@ -868,6 +894,16 @@ pub mod experimental {
}
}

#[test]
fn test_empty() {
let events = Events::<char, 3>::new();

assert_eq!(events.len(), 0);

// This test also ensures that `Drop` for `LinkedChunk` works when
// there is only one chunk.
}

#[test]
fn test_push_events() {
let mut events = Events::<char, 3>::new();
Expand Down

0 comments on commit 3b4ef21

Please sign in to comment.