@@ -500,6 +500,32 @@ pub mod experimental {
500
500
}
501
501
}
502
502
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
+
503
529
/// The position of a chunk in a [`LinkedChunk`].
504
530
///
505
531
/// 0 represents the latest chunk.
@@ -868,6 +894,16 @@ pub mod experimental {
868
894
}
869
895
}
870
896
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
+
871
907
#[ test]
872
908
fn test_push_events ( ) {
873
909
let mut events = Events :: < char , 3 > :: new ( ) ;
0 commit comments