Skip to content

Commit cabab28

Browse files
committed
timeline: get rid of ManuallyDrop in the TimelineInnerStateTransaction
1 parent a98779d commit cabab28

File tree

2 files changed

+16
-41
lines changed

2 files changed

+16
-41
lines changed

crates/matrix-sdk-ui/src/timeline/inner/state.rs

Lines changed: 10 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
use std::{
16-
collections::VecDeque,
17-
future::Future,
18-
mem::{self, ManuallyDrop},
19-
sync::Arc,
20-
};
15+
use std::{collections::VecDeque, future::Future, sync::Arc};
2116

2217
use eyeball_im::{ObservableVector, ObservableVectorTransaction, ObservableVectorTransactionEntry};
2318
use indexmap::IndexMap;
@@ -403,19 +398,21 @@ impl TimelineInnerState {
403398
}
404399

405400
fn transaction(&mut self) -> TimelineInnerStateTransaction<'_> {
406-
let items = ManuallyDrop::new(self.items.transaction());
407-
let meta = ManuallyDrop::new(self.meta.clone());
401+
let items = self.items.transaction();
402+
let meta = self.meta.clone();
408403
TimelineInnerStateTransaction { items, previous_meta: &mut self.meta, meta }
409404
}
410405
}
411406

412407
pub(in crate::timeline) struct TimelineInnerStateTransaction<'a> {
413-
pub items: ManuallyDrop<ObservableVectorTransaction<'a, Arc<TimelineItem>>>,
408+
/// A vector transaction over the items themselves. Holds temporary state
409+
/// until committed.
410+
pub items: ObservableVectorTransaction<'a, Arc<TimelineItem>>,
414411

415412
/// A clone of the previous meta, that we're operating on during the
416413
/// transaction, and that will be committed to the previous meta location in
417414
/// [`Self::commit`].
418-
pub meta: ManuallyDrop<TimelineInnerMetadata>,
415+
pub meta: TimelineInnerMetadata,
419416

420417
/// Pointer to the previous meta, only used during [`Self::commit`].
421418
previous_meta: &'a mut TimelineInnerMetadata,
@@ -624,19 +621,11 @@ impl TimelineInnerStateTransaction<'_> {
624621
self.meta.update_read_marker(&mut self.items);
625622
}
626623

627-
fn commit(mut self) {
628-
let Self { items, previous_meta, meta } = &mut self;
629-
630-
// Safety: self is forgotten to avoid double free from drop.
631-
let meta = unsafe { ManuallyDrop::take(meta) };
624+
fn commit(self) {
625+
let Self { items, previous_meta, meta } = self;
632626

633627
// Replace the pointer to the previous meta with the new one.
634-
**previous_meta = meta;
635-
636-
// Safety: self is forgotten to avoid double free from drop.
637-
let items = unsafe { ManuallyDrop::take(items) };
638-
639-
mem::forget(self);
628+
*previous_meta = meta;
640629

641630
items.commit();
642631
}
@@ -688,17 +677,6 @@ impl TimelineInnerStateTransaction<'_> {
688677
}
689678
}
690679

691-
impl Drop for TimelineInnerStateTransaction<'_> {
692-
fn drop(&mut self) {
693-
warn!("timeline state transaction cancelled");
694-
// Safety: self.items is not touched anymore, the only other place
695-
// dropping is Self::commit which makes sure to skip this Drop impl.
696-
unsafe {
697-
ManuallyDrop::drop(&mut self.items);
698-
}
699-
}
700-
}
701-
702680
#[derive(Clone, Debug)]
703681
pub(in crate::timeline) struct TimelineInnerMetadata {
704682
/// List of all the events as received in the timeline, even the ones that

crates/matrix-sdk-ui/src/timeline/read_receipts.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -370,11 +370,10 @@ impl TimelineInnerStateTransaction<'_> {
370370
receipt: &receipt,
371371
};
372372

373-
let meta = &mut *self.meta;
374-
meta.read_receipts.maybe_update_read_receipt(
373+
self.meta.read_receipts.maybe_update_read_receipt(
375374
full_receipt,
376375
is_own_user_id,
377-
&meta.all_events,
376+
&self.meta.all_events,
378377
&mut self.items,
379378
);
380379
}
@@ -406,11 +405,10 @@ impl TimelineInnerStateTransaction<'_> {
406405
receipt: &receipt,
407406
};
408407

409-
let meta = &mut *self.meta;
410-
meta.read_receipts.maybe_update_read_receipt(
408+
self.meta.read_receipts.maybe_update_read_receipt(
411409
full_receipt,
412410
user_id == own_user_id,
413-
&meta.all_events,
411+
&self.meta.all_events,
414412
&mut self.items,
415413
);
416414
}
@@ -436,11 +434,10 @@ impl TimelineInnerStateTransaction<'_> {
436434
let full_receipt =
437435
FullReceipt { event_id, user_id, receipt_type: ReceiptType::Read, receipt: &receipt };
438436

439-
let meta = &mut *self.meta;
440-
meta.read_receipts.maybe_update_read_receipt(
437+
self.meta.read_receipts.maybe_update_read_receipt(
441438
full_receipt,
442439
is_own_event,
443-
&meta.all_events,
440+
&self.meta.all_events,
444441
&mut self.items,
445442
);
446443
}

0 commit comments

Comments
 (0)