Skip to content

Commit 216f0df

Browse files
committed
refactor(linked chunk): invert the position of insert_items_at parameters
Same logic as `replace_gap_at`.
1 parent 129e9e1 commit 216f0df

File tree

4 files changed

+39
-41
lines changed

4 files changed

+39
-41
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -563,8 +563,8 @@ mod tests {
563563

564564
linked_chunk
565565
.insert_items_at(
566-
['w', 'x', 'y', 'z'],
567566
linked_chunk.item_position(|item| *item == 'b').unwrap(),
567+
['w', 'x', 'y', 'z'],
568568
)
569569
.unwrap();
570570
assert_items_eq!(linked_chunk, ['a', 'w', 'x'] ['y', 'z', 'b'] ['c'] ['d']);
@@ -646,7 +646,7 @@ mod tests {
646646
);
647647

648648
linked_chunk
649-
.insert_items_at(['m'], linked_chunk.item_position(|item| *item == 'a').unwrap())
649+
.insert_items_at(linked_chunk.item_position(|item| *item == 'a').unwrap(), ['m'])
650650
.unwrap();
651651
assert_items_eq!(
652652
linked_chunk,
@@ -709,7 +709,7 @@ mod tests {
709709
apply_and_assert_eq(&mut accumulator, as_vector.take(), &[VectorDiff::Remove { index: 5 }]);
710710

711711
linked_chunk
712-
.insert_items_at(['z'], linked_chunk.item_position(|item| *item == 'h').unwrap())
712+
.insert_items_at(linked_chunk.item_position(|item| *item == 'h').unwrap(), ['z'])
713713
.unwrap();
714714

715715
assert_items_eq!(

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

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,7 @@ impl<const CAP: usize, Item, Gap> LinkedChunk<CAP, Item, Gap> {
466466
///
467467
/// Because the `position` can be invalid, this method returns a
468468
/// `Result`.
469-
pub fn insert_items_at<I>(&mut self, items: I, position: Position) -> Result<(), Error>
469+
pub fn insert_items_at<I>(&mut self, position: Position, items: I) -> Result<(), Error>
470470
where
471471
Item: Clone,
472472
Gap: Clone,
@@ -2265,11 +2265,11 @@ mod tests {
22652265

22662266
// Insert inside the last chunk.
22672267
{
2268-
let position_of_e = linked_chunk.item_position(|item| *item == 'e').unwrap();
2268+
let pos_e = linked_chunk.item_position(|item| *item == 'e').unwrap();
22692269

22702270
// Insert 4 elements, so that it overflows the chunk capacity. It's important to
22712271
// see whether chunks are correctly updated and linked.
2272-
linked_chunk.insert_items_at(['w', 'x', 'y', 'z'], position_of_e)?;
2272+
linked_chunk.insert_items_at(pos_e, ['w', 'x', 'y', 'z'])?;
22732273

22742274
assert_items_eq!(
22752275
linked_chunk,
@@ -2302,8 +2302,8 @@ mod tests {
23022302

23032303
// Insert inside the first chunk.
23042304
{
2305-
let position_of_a = linked_chunk.item_position(|item| *item == 'a').unwrap();
2306-
linked_chunk.insert_items_at(['l', 'm', 'n', 'o'], position_of_a)?;
2305+
let pos_a = linked_chunk.item_position(|item| *item == 'a').unwrap();
2306+
linked_chunk.insert_items_at(pos_a, ['l', 'm', 'n', 'o'])?;
23072307

23082308
assert_items_eq!(
23092309
linked_chunk,
@@ -2336,8 +2336,8 @@ mod tests {
23362336

23372337
// Insert inside a middle chunk.
23382338
{
2339-
let position_of_c = linked_chunk.item_position(|item| *item == 'c').unwrap();
2340-
linked_chunk.insert_items_at(['r', 's'], position_of_c)?;
2339+
let pos_c = linked_chunk.item_position(|item| *item == 'c').unwrap();
2340+
linked_chunk.insert_items_at(pos_c, ['r', 's'])?;
23412341

23422342
assert_items_eq!(
23432343
linked_chunk,
@@ -2358,11 +2358,10 @@ mod tests {
23582358

23592359
// Insert at the end of a chunk.
23602360
{
2361-
let position_of_f = linked_chunk.item_position(|item| *item == 'f').unwrap();
2362-
let position_after_f =
2363-
Position(position_of_f.chunk_identifier(), position_of_f.index() + 1);
2361+
let pos_f = linked_chunk.item_position(|item| *item == 'f').unwrap();
2362+
let pos_f = Position(pos_f.chunk_identifier(), pos_f.index() + 1);
23642363

2365-
linked_chunk.insert_items_at(['p', 'q'], position_after_f)?;
2364+
linked_chunk.insert_items_at(pos_f, ['p', 'q'])?;
23662365
assert_items_eq!(
23672366
linked_chunk,
23682367
['l', 'm', 'n'] ['o', 'a', 'b'] ['r', 's', 'c'] ['d', 'w', 'x'] ['y', 'z', 'e'] ['f', 'p', 'q']
@@ -2377,7 +2376,7 @@ mod tests {
23772376
// Insert in a chunk that does not exist.
23782377
{
23792378
assert_matches!(
2380-
linked_chunk.insert_items_at(['u', 'v'], Position(ChunkIdentifier(128), 0)),
2379+
linked_chunk.insert_items_at(Position(ChunkIdentifier(128), 0), ['u', 'v'],),
23812380
Err(Error::InvalidChunkIdentifier { identifier: ChunkIdentifier(128) })
23822381
);
23832382
assert!(linked_chunk.updates().unwrap().take().is_empty());
@@ -2386,7 +2385,7 @@ mod tests {
23862385
// Insert in a chunk that exists, but at an item that does not exist.
23872386
{
23882387
assert_matches!(
2389-
linked_chunk.insert_items_at(['u', 'v'], Position(ChunkIdentifier(0), 128)),
2388+
linked_chunk.insert_items_at(Position(ChunkIdentifier(0), 128), ['u', 'v'],),
23902389
Err(Error::InvalidItemIndex { index: 128 })
23912390
);
23922391
assert!(linked_chunk.updates().unwrap().take().is_empty());
@@ -2411,7 +2410,7 @@ mod tests {
24112410
);
24122411

24132412
assert_matches!(
2414-
linked_chunk.insert_items_at(['u', 'v'], Position(ChunkIdentifier(6), 0)),
2413+
linked_chunk.insert_items_at(Position(ChunkIdentifier(6), 0), ['u', 'v'],),
24152414
Err(Error::ChunkIsAGap { identifier: ChunkIdentifier(6) })
24162415
);
24172416
}
@@ -2446,11 +2445,11 @@ mod tests {
24462445
);
24472446

24482447
// Insert inside the last chunk.
2449-
let position_of_e = linked_chunk.item_position(|item| *item == 'e').unwrap();
2448+
let pos_e = linked_chunk.item_position(|item| *item == 'e').unwrap();
24502449

24512450
// Insert 4 elements, so that it overflows the chunk capacity. It's important to
24522451
// see whether chunks are correctly updated and linked.
2453-
linked_chunk.insert_items_at(['w', 'x', 'y', 'z'], position_of_e)?;
2452+
linked_chunk.insert_items_at(pos_e, ['w', 'x', 'y', 'z'])?;
24542453

24552454
assert_items_eq!(
24562455
linked_chunk,
@@ -2508,8 +2507,8 @@ mod tests {
25082507
);
25092508

25102509
// Insert inside the first chunk.
2511-
let position_of_a = linked_chunk.item_position(|item| *item == 'a').unwrap();
2512-
linked_chunk.insert_items_at(['l', 'm', 'n', 'o'], position_of_a)?;
2510+
let pos_a = linked_chunk.item_position(|item| *item == 'a').unwrap();
2511+
linked_chunk.insert_items_at(pos_a, ['l', 'm', 'n', 'o'])?;
25132512

25142513
assert_items_eq!(
25152514
linked_chunk,
@@ -2572,8 +2571,8 @@ mod tests {
25722571
]
25732572
);
25742573

2575-
let position_of_d = linked_chunk.item_position(|item| *item == 'd').unwrap();
2576-
linked_chunk.insert_items_at(['r', 's'], position_of_d)?;
2574+
let pos_d = linked_chunk.item_position(|item| *item == 'd').unwrap();
2575+
linked_chunk.insert_items_at(pos_d, ['r', 's'])?;
25772576

25782577
assert_items_eq!(
25792578
linked_chunk,
@@ -2625,11 +2624,10 @@ mod tests {
26252624
);
26262625

26272626
// Insert at the end of a chunk.
2628-
let position_of_e = linked_chunk.item_position(|item| *item == 'e').unwrap();
2629-
let position_after_e =
2630-
Position(position_of_e.chunk_identifier(), position_of_e.index() + 1);
2627+
let pos_e = linked_chunk.item_position(|item| *item == 'e').unwrap();
2628+
let pos_after_e = Position(pos_e.chunk_identifier(), pos_e.index() + 1);
26312629

2632-
linked_chunk.insert_items_at(['p', 'q'], position_after_e)?;
2630+
linked_chunk.insert_items_at(pos_after_e, ['p', 'q'])?;
26332631
assert_items_eq!(
26342632
linked_chunk,
26352633
['a', 'b', 'c'] ['d', 'e', 'p'] ['q']
@@ -2679,7 +2677,7 @@ mod tests {
26792677
// Insert in a chunk that does not exist.
26802678
{
26812679
assert_matches!(
2682-
linked_chunk.insert_items_at(['u', 'v'], Position(ChunkIdentifier(128), 0)),
2680+
linked_chunk.insert_items_at(Position(ChunkIdentifier(128), 0), ['u', 'v'],),
26832681
Err(Error::InvalidChunkIdentifier { identifier: ChunkIdentifier(128) })
26842682
);
26852683
assert!(linked_chunk.updates().unwrap().take().is_empty());
@@ -2688,7 +2686,7 @@ mod tests {
26882686
// Insert in a chunk that exists, but at an item that does not exist.
26892687
{
26902688
assert_matches!(
2691-
linked_chunk.insert_items_at(['u', 'v'], Position(ChunkIdentifier(0), 128)),
2689+
linked_chunk.insert_items_at(Position(ChunkIdentifier(0), 128), ['u', 'v'],),
26922690
Err(Error::InvalidItemIndex { index: 128 })
26932691
);
26942692
assert!(linked_chunk.updates().unwrap().take().is_empty());
@@ -2697,7 +2695,7 @@ mod tests {
26972695
// Insert in a gap.
26982696
{
26992697
assert_matches!(
2700-
linked_chunk.insert_items_at(['u', 'v'], Position(ChunkIdentifier(1), 0)),
2698+
linked_chunk.insert_items_at(Position(ChunkIdentifier(1), 0), ['u', 'v'],),
27012699
Err(Error::ChunkIsAGap { identifier: ChunkIdentifier(1) })
27022700
);
27032701
}
@@ -3039,7 +3037,7 @@ mod tests {
30393037
// Insert in a chunk that does not exist.
30403038
{
30413039
assert_matches!(
3042-
linked_chunk.insert_items_at(['u', 'v'], Position(ChunkIdentifier(128), 0)),
3040+
linked_chunk.insert_items_at(Position(ChunkIdentifier(128), 0), ['u', 'v'],),
30433041
Err(Error::InvalidChunkIdentifier { identifier: ChunkIdentifier(128) })
30443042
);
30453043
assert!(linked_chunk.updates().unwrap().take().is_empty());
@@ -3048,7 +3046,7 @@ mod tests {
30483046
// Insert in a chunk that exists, but at an item that does not exist.
30493047
{
30503048
assert_matches!(
3051-
linked_chunk.insert_items_at(['u', 'v'], Position(ChunkIdentifier(0), 128)),
3049+
linked_chunk.insert_items_at(Position(ChunkIdentifier(0), 128), ['u', 'v'],),
30523050
Err(Error::InvalidItemIndex { index: 128 })
30533051
);
30543052
assert!(linked_chunk.updates().unwrap().take().is_empty());

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -226,8 +226,8 @@ mod tests {
226226

227227
// Inserting items in the middle.
228228
{
229-
let b_pos = linked_chunk.item_position(|c| *c == 'b').unwrap();
230-
linked_chunk.insert_items_at(['d', 'e'], b_pos).unwrap();
229+
let pos_b = linked_chunk.item_position(|c| *c == 'b').unwrap();
230+
linked_chunk.insert_items_at(pos_b, ['d', 'e']).unwrap();
231231
tracker.flush_updates(false);
232232
assert_order_fully_loaded(&linked_chunk, &tracker);
233233
}
@@ -451,8 +451,8 @@ mod tests {
451451

452452
// Inserting items in the middle.
453453
{
454-
let h_pos = linked_chunk.item_position(|c| *c == 'h').unwrap();
455-
linked_chunk.insert_items_at(['j', 'k'], h_pos).unwrap();
454+
let pos_h = linked_chunk.item_position(|c| *c == 'h').unwrap();
455+
linked_chunk.insert_items_at(pos_h, ['j', 'k']).unwrap();
456456
tracker.flush_updates(false);
457457

458458
// The previous items are still ordered.

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,10 @@ impl EventLinkedChunk {
102102
/// Insert events at a specified position.
103103
pub fn insert_events_at(
104104
&mut self,
105-
events: Vec<Event>,
106105
position: Position,
106+
events: Vec<Event>,
107107
) -> Result<(), Error> {
108-
self.chunks.insert_items_at(events, position)?;
108+
self.chunks.insert_items_at(position, events)?;
109109
Ok(())
110110
}
111111

@@ -351,7 +351,7 @@ impl EventLinkedChunk {
351351
// before those.
352352
trace!("inserted events before the first known event");
353353

354-
self.insert_events_at(events.to_vec(), pos)
354+
self.insert_events_at(pos, events.to_vec())
355355
.expect("pos is a valid position we just read above");
356356

357357
Some(pos)
@@ -623,14 +623,14 @@ mod tests {
623623

624624
linked_chunk.push_events([event_0, event_1]);
625625

626-
let position_of_event_1 = linked_chunk
626+
let pos_ev1 = linked_chunk
627627
.events()
628628
.find_map(|(position, event)| {
629629
(event.event_id().unwrap() == event_id_1).then_some(position)
630630
})
631631
.unwrap();
632632

633-
linked_chunk.insert_events_at(vec![event_2], position_of_event_1).unwrap();
633+
linked_chunk.insert_events_at(pos_ev1, vec![event_2]).unwrap();
634634

635635
assert_events_eq!(
636636
linked_chunk.events(),

0 commit comments

Comments
 (0)