Skip to content

Commit 019b4a2

Browse files
committed
chore(common): Rename EmptyChunk to EmptyChunkRule.
This patch renames `EmptyChunk` into `EmptyChunkRule`. Name suggested by @stefanceriu, it makes a lot more sense, thanks!
1 parent 30a9a97 commit 019b4a2

File tree

3 files changed

+52
-37
lines changed

3 files changed

+52
-37
lines changed

crates/matrix-sdk-common/src/linked_chunk/as_vector.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,7 @@ mod tests {
473473
use imbl::{vector, Vector};
474474

475475
use super::{
476-
super::{Chunk, ChunkIdentifierGenerator, EmptyChunk, LinkedChunk, Update},
476+
super::{Chunk, ChunkIdentifierGenerator, EmptyChunkRule, LinkedChunk, Update},
477477
VectorDiff,
478478
};
479479

@@ -632,7 +632,7 @@ mod tests {
632632
let removed_item = linked_chunk
633633
.remove_item_at(
634634
linked_chunk.item_position(|item| *item == 'c').unwrap(),
635-
EmptyChunk::Remove,
635+
EmptyChunkRule::Remove,
636636
)
637637
.unwrap();
638638
assert_eq!(removed_item, 'c');
@@ -655,7 +655,7 @@ mod tests {
655655
let removed_item = linked_chunk
656656
.remove_item_at(
657657
linked_chunk.item_position(|item| *item == 'z').unwrap(),
658-
EmptyChunk::Remove,
658+
EmptyChunkRule::Remove,
659659
)
660660
.unwrap();
661661
assert_eq!(removed_item, 'z');
@@ -888,7 +888,7 @@ mod tests {
888888
linked_chunk
889889
.remove_item_at(
890890
linked_chunk.item_position(|item| *item == 'c').unwrap(),
891-
EmptyChunk::Remove,
891+
EmptyChunkRule::Remove,
892892
)
893893
.unwrap();
894894

@@ -1006,7 +1006,7 @@ mod tests {
10061006
continue;
10071007
};
10081008

1009-
linked_chunk.remove_item_at(position, EmptyChunk::Remove).expect("Failed to remove an item");
1009+
linked_chunk.remove_item_at(position, EmptyChunkRule::Remove).expect("Failed to remove an item");
10101010
}
10111011
}
10121012
}

crates/matrix-sdk-common/src/linked_chunk/mod.rs

Lines changed: 45 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -457,12 +457,12 @@ impl<const CAP: usize, Item, Gap> LinkedChunk<CAP, Item, Gap> {
457457
///
458458
/// The chunk containing the item represented by `position` may be empty
459459
/// once the item has been removed. In this case, the chunk can be removed
460-
/// if `empty_chunk` contains [`EmptyChunk::Remove`], otherwise the chunk is
461-
/// kept if `empty_chunk` contains [`EmptyChunk::Keep`].
460+
/// if `empty_chunk_rule` contains [`EmptyChunkRule::Remove`], otherwise the
461+
/// chunk is kept if `empty_chunk_rule` contains [`EmptyChunkRule::Keep`].
462462
pub fn remove_item_at(
463463
&mut self,
464464
position: Position,
465-
empty_chunk: EmptyChunk,
465+
empty_chunk_rule: EmptyChunkRule,
466466
) -> Result<Item, Error> {
467467
let chunk_identifier = position.chunk_identifier();
468468
let item_index = position.index();
@@ -501,7 +501,7 @@ impl<const CAP: usize, Item, Gap> LinkedChunk<CAP, Item, Gap> {
501501

502502
// If removing empty chunk is desired, and if the `chunk` can be unlinked, and
503503
// if the `chunk` is not the first one, we can remove it.
504-
if empty_chunk.remove() && can_unlink_chunk && chunk.is_first_chunk().not() {
504+
if empty_chunk_rule.remove() && can_unlink_chunk && chunk.is_first_chunk().not() {
505505
// Unlink `chunk`.
506506
chunk.unlink(self.updates.as_mut());
507507

@@ -1593,15 +1593,15 @@ where
15931593

15941594
/// A type representing what to do when the system has to handle an empty chunk.
15951595
#[derive(Debug)]
1596-
pub enum EmptyChunk {
1596+
pub enum EmptyChunkRule {
15971597
/// Keep the empty chunk.
15981598
Keep,
15991599

16001600
/// Remove the empty chunk.
16011601
Remove,
16021602
}
16031603

1604-
impl EmptyChunk {
1604+
impl EmptyChunkRule {
16051605
fn remove(&self) -> bool {
16061606
matches!(self, Self::Remove)
16071607
}
@@ -1637,7 +1637,7 @@ mod tests {
16371637
use assert_matches::assert_matches;
16381638

16391639
use super::{
1640-
Chunk, ChunkContent, ChunkIdentifier, ChunkIdentifierGenerator, EmptyChunk, Error,
1640+
Chunk, ChunkContent, ChunkIdentifier, ChunkIdentifierGenerator, EmptyChunkRule, Error,
16411641
LinkedChunk, Position,
16421642
};
16431643

@@ -2576,21 +2576,24 @@ mod tests {
25762576
// that. The chunk is removed.
25772577
{
25782578
let position_of_f = linked_chunk.item_position(|item| *item == 'f').unwrap();
2579-
let removed_item = linked_chunk.remove_item_at(position_of_f, EmptyChunk::Remove)?;
2579+
let removed_item =
2580+
linked_chunk.remove_item_at(position_of_f, EmptyChunkRule::Remove)?;
25802581

25812582
assert_eq!(removed_item, 'f');
25822583
assert_items_eq!(linked_chunk, ['a', 'b', 'c'] ['d', 'e'] ['g', 'h', 'i'] ['j', 'k']);
25832584
assert_eq!(linked_chunk.num_items(), 10);
25842585

25852586
let position_of_e = linked_chunk.item_position(|item| *item == 'e').unwrap();
2586-
let removed_item = linked_chunk.remove_item_at(position_of_e, EmptyChunk::Remove)?;
2587+
let removed_item =
2588+
linked_chunk.remove_item_at(position_of_e, EmptyChunkRule::Remove)?;
25872589

25882590
assert_eq!(removed_item, 'e');
25892591
assert_items_eq!(linked_chunk, ['a', 'b', 'c'] ['d'] ['g', 'h', 'i'] ['j', 'k']);
25902592
assert_eq!(linked_chunk.num_items(), 9);
25912593

25922594
let position_of_d = linked_chunk.item_position(|item| *item == 'd').unwrap();
2593-
let removed_item = linked_chunk.remove_item_at(position_of_d, EmptyChunk::Remove)?;
2595+
let removed_item =
2596+
linked_chunk.remove_item_at(position_of_d, EmptyChunkRule::Remove)?;
25942597

25952598
assert_eq!(removed_item, 'd');
25962599
assert_items_eq!(linked_chunk, ['a', 'b', 'c'] ['g', 'h', 'i'] ['j', 'k']);
@@ -2611,19 +2614,22 @@ mod tests {
26112614
// that. The chunk is NOT removed because it's the first chunk.
26122615
{
26132616
let first_position = linked_chunk.item_position(|item| *item == 'a').unwrap();
2614-
let removed_item = linked_chunk.remove_item_at(first_position, EmptyChunk::Remove)?;
2617+
let removed_item =
2618+
linked_chunk.remove_item_at(first_position, EmptyChunkRule::Remove)?;
26152619

26162620
assert_eq!(removed_item, 'a');
26172621
assert_items_eq!(linked_chunk, ['b', 'c'] ['g', 'h', 'i'] ['j', 'k']);
26182622
assert_eq!(linked_chunk.num_items(), 7);
26192623

2620-
let removed_item = linked_chunk.remove_item_at(first_position, EmptyChunk::Remove)?;
2624+
let removed_item =
2625+
linked_chunk.remove_item_at(first_position, EmptyChunkRule::Remove)?;
26212626

26222627
assert_eq!(removed_item, 'b');
26232628
assert_items_eq!(linked_chunk, ['c'] ['g', 'h', 'i'] ['j', 'k']);
26242629
assert_eq!(linked_chunk.num_items(), 6);
26252630

2626-
let removed_item = linked_chunk.remove_item_at(first_position, EmptyChunk::Remove)?;
2631+
let removed_item =
2632+
linked_chunk.remove_item_at(first_position, EmptyChunkRule::Remove)?;
26272633

26282634
assert_eq!(removed_item, 'c');
26292635
assert_items_eq!(linked_chunk, [] ['g', 'h', 'i'] ['j', 'k']);
@@ -2643,19 +2649,22 @@ mod tests {
26432649
// that. The chunk is removed.
26442650
{
26452651
let first_position = linked_chunk.item_position(|item| *item == 'g').unwrap();
2646-
let removed_item = linked_chunk.remove_item_at(first_position, EmptyChunk::Remove)?;
2652+
let removed_item =
2653+
linked_chunk.remove_item_at(first_position, EmptyChunkRule::Remove)?;
26472654

26482655
assert_eq!(removed_item, 'g');
26492656
assert_items_eq!(linked_chunk, [] ['h', 'i'] ['j', 'k']);
26502657
assert_eq!(linked_chunk.num_items(), 4);
26512658

2652-
let removed_item = linked_chunk.remove_item_at(first_position, EmptyChunk::Remove)?;
2659+
let removed_item =
2660+
linked_chunk.remove_item_at(first_position, EmptyChunkRule::Remove)?;
26532661

26542662
assert_eq!(removed_item, 'h');
26552663
assert_items_eq!(linked_chunk, [] ['i'] ['j', 'k']);
26562664
assert_eq!(linked_chunk.num_items(), 3);
26572665

2658-
let removed_item = linked_chunk.remove_item_at(first_position, EmptyChunk::Remove)?;
2666+
let removed_item =
2667+
linked_chunk.remove_item_at(first_position, EmptyChunkRule::Remove)?;
26592668

26602669
assert_eq!(removed_item, 'i');
26612670
assert_items_eq!(linked_chunk, [] ['j', 'k']);
@@ -2676,15 +2685,17 @@ mod tests {
26762685
// The chunk is removed.
26772686
{
26782687
let position_of_k = linked_chunk.item_position(|item| *item == 'k').unwrap();
2679-
let removed_item = linked_chunk.remove_item_at(position_of_k, EmptyChunk::Remove)?;
2688+
let removed_item =
2689+
linked_chunk.remove_item_at(position_of_k, EmptyChunkRule::Remove)?;
26802690

26812691
assert_eq!(removed_item, 'k');
26822692
#[rustfmt::skip]
26832693
assert_items_eq!(linked_chunk, [] ['j']);
26842694
assert_eq!(linked_chunk.num_items(), 1);
26852695

26862696
let position_of_j = linked_chunk.item_position(|item| *item == 'j').unwrap();
2687-
let removed_item = linked_chunk.remove_item_at(position_of_j, EmptyChunk::Remove)?;
2697+
let removed_item =
2698+
linked_chunk.remove_item_at(position_of_j, EmptyChunkRule::Remove)?;
26882699

26892700
assert_eq!(removed_item, 'j');
26902701
assert_items_eq!(linked_chunk, []);
@@ -2718,27 +2729,31 @@ mod tests {
27182729
let _ = linked_chunk.updates().unwrap().take();
27192730

27202731
let position_of_c = linked_chunk.item_position(|item| *item == 'c').unwrap();
2721-
let removed_item = linked_chunk.remove_item_at(position_of_c, EmptyChunk::Remove)?;
2732+
let removed_item =
2733+
linked_chunk.remove_item_at(position_of_c, EmptyChunkRule::Remove)?;
27222734

27232735
assert_eq!(removed_item, 'c');
27242736
assert_items_eq!(linked_chunk, ['a', 'b'] [-] ['d']);
27252737
assert_eq!(linked_chunk.num_items(), 3);
27262738

27272739
let position_of_d = linked_chunk.item_position(|item| *item == 'd').unwrap();
2728-
let removed_item = linked_chunk.remove_item_at(position_of_d, EmptyChunk::Remove)?;
2740+
let removed_item =
2741+
linked_chunk.remove_item_at(position_of_d, EmptyChunkRule::Remove)?;
27292742

27302743
assert_eq!(removed_item, 'd');
27312744
assert_items_eq!(linked_chunk, ['a', 'b'] [-]);
27322745
assert_eq!(linked_chunk.num_items(), 2);
27332746

27342747
let first_position = linked_chunk.item_position(|item| *item == 'a').unwrap();
2735-
let removed_item = linked_chunk.remove_item_at(first_position, EmptyChunk::Remove)?;
2748+
let removed_item =
2749+
linked_chunk.remove_item_at(first_position, EmptyChunkRule::Remove)?;
27362750

27372751
assert_eq!(removed_item, 'a');
27382752
assert_items_eq!(linked_chunk, ['b'] [-]);
27392753
assert_eq!(linked_chunk.num_items(), 1);
27402754

2741-
let removed_item = linked_chunk.remove_item_at(first_position, EmptyChunk::Remove)?;
2755+
let removed_item =
2756+
linked_chunk.remove_item_at(first_position, EmptyChunkRule::Remove)?;
27422757

27432758
assert_eq!(removed_item, 'b');
27442759
assert_items_eq!(linked_chunk, [] [-]);
@@ -2780,19 +2795,19 @@ mod tests {
27802795
// chunk is NOT removed because we asked to keep it.
27812796
{
27822797
let position = linked_chunk.item_position(|item| *item == 'd').unwrap();
2783-
let removed_item = linked_chunk.remove_item_at(position, EmptyChunk::Keep)?;
2798+
let removed_item = linked_chunk.remove_item_at(position, EmptyChunkRule::Keep)?;
27842799

27852800
assert_eq!(removed_item, 'd');
27862801
assert_items_eq!(linked_chunk, ['a', 'b', 'c'] ['e', 'f'] ['g', 'h']);
27872802
assert_eq!(linked_chunk.num_items(), 7);
27882803

2789-
let removed_item = linked_chunk.remove_item_at(position, EmptyChunk::Keep)?;
2804+
let removed_item = linked_chunk.remove_item_at(position, EmptyChunkRule::Keep)?;
27902805

27912806
assert_eq!(removed_item, 'e');
27922807
assert_items_eq!(linked_chunk, ['a', 'b', 'c'] ['f'] ['g', 'h']);
27932808
assert_eq!(linked_chunk.num_items(), 6);
27942809

2795-
let removed_item = linked_chunk.remove_item_at(position, EmptyChunk::Keep)?;
2810+
let removed_item = linked_chunk.remove_item_at(position, EmptyChunkRule::Keep)?;
27962811

27972812
assert_eq!(removed_item, 'f');
27982813
assert_items_eq!(linked_chunk, ['a', 'b', 'c'] [] ['g', 'h']);
@@ -2812,13 +2827,13 @@ mod tests {
28122827
// chunk is NOT removed because we asked to keep it.
28132828
{
28142829
let position = linked_chunk.item_position(|item| *item == 'g').unwrap();
2815-
let removed_item = linked_chunk.remove_item_at(position, EmptyChunk::Keep)?;
2830+
let removed_item = linked_chunk.remove_item_at(position, EmptyChunkRule::Keep)?;
28162831

28172832
assert_eq!(removed_item, 'g');
28182833
assert_items_eq!(linked_chunk, ['a', 'b', 'c'] [] ['h']);
28192834
assert_eq!(linked_chunk.num_items(), 4);
28202835

2821-
let removed_item = linked_chunk.remove_item_at(position, EmptyChunk::Keep)?;
2836+
let removed_item = linked_chunk.remove_item_at(position, EmptyChunkRule::Keep)?;
28222837

28232838
assert_eq!(removed_item, 'h');
28242839
assert_items_eq!(linked_chunk, ['a', 'b', 'c'] [] []);
@@ -2837,19 +2852,19 @@ mod tests {
28372852
// chunk is NOT removed because we asked to keep it.
28382853
{
28392854
let position = linked_chunk.item_position(|item| *item == 'a').unwrap();
2840-
let removed_item = linked_chunk.remove_item_at(position, EmptyChunk::Keep)?;
2855+
let removed_item = linked_chunk.remove_item_at(position, EmptyChunkRule::Keep)?;
28412856

28422857
assert_eq!(removed_item, 'a');
28432858
assert_items_eq!(linked_chunk, ['b', 'c'] [] []);
28442859
assert_eq!(linked_chunk.num_items(), 2);
28452860

2846-
let removed_item = linked_chunk.remove_item_at(position, EmptyChunk::Keep)?;
2861+
let removed_item = linked_chunk.remove_item_at(position, EmptyChunkRule::Keep)?;
28472862

28482863
assert_eq!(removed_item, 'b');
28492864
assert_items_eq!(linked_chunk, ['c'] [] []);
28502865
assert_eq!(linked_chunk.num_items(), 1);
28512866

2852-
let removed_item = linked_chunk.remove_item_at(position, EmptyChunk::Keep)?;
2867+
let removed_item = linked_chunk.remove_item_at(position, EmptyChunkRule::Keep)?;
28532868

28542869
assert_eq!(removed_item, 'c');
28552870
assert_items_eq!(linked_chunk, [] [] []);

crates/matrix-sdk/src/event_cache/room/events.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use matrix_sdk_base::{
2323
},
2424
};
2525
use matrix_sdk_common::linked_chunk::{
26-
AsVector, Chunk, ChunkIdentifier, EmptyChunk, Error, Iter, IterBackward, LinkedChunk,
26+
AsVector, Chunk, ChunkIdentifier, EmptyChunkRule, Error, Iter, IterBackward, LinkedChunk,
2727
ObservableUpdates, Position,
2828
};
2929
use ruma::{
@@ -265,7 +265,7 @@ impl RoomEvents {
265265
self.chunks.remove_item_at(
266266
position,
267267
// If removing an event results in an empty chunk, the empty chunk is removed.
268-
EmptyChunk::Remove,
268+
EmptyChunkRule::Remove,
269269
)?;
270270
}
271271

0 commit comments

Comments
 (0)