Skip to content

Commit

Permalink
WIPvariance
Browse files Browse the repository at this point in the history
  • Loading branch information
douglas-raillard-arm committed Nov 14, 2023
1 parent b74c35d commit 6bb386c
Showing 1 changed file with 15 additions and 9 deletions.
24 changes: 15 additions & 9 deletions tools/analysis/traceevent/src/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use core::{
fmt::{Debug, Formatter},
ops::{Deref, DerefMut as _},
marker::PhantomData,
cell::UnsafeCell,
};
use std::{
collections::{btree_map::Entry, BTreeMap},
Expand Down Expand Up @@ -95,11 +96,16 @@ pub struct EventVisitor<'i, 'h, 'edm, InitDescF, T = ()> {
pub timestamp: Timestamp,
pub buffer_id: &'h BufferId,

// Using *mut here means EventVisitor is invariant in any lifetime contained in T.
// However, the only values we store in the EventDescMap are either owned by it or have a
// longer lifetime ('h outlives 'edm), so it's sound to be covariant in 'edm.
// So in practice we use 'static but then we cast back to 'h.
desc_map: *mut EventDescMap<'static, T, InitDescF>,
// Using UnsafeCell ensures that the compiler understands that anything derived from what we
// stored in it can change at any time, even if the EventVisitor is only manipulated via shared
// ref.
desc_map: UnsafeCell<
// Using *mut here means EventVisitor is invariant in any lifetime contained in T.
// However, the only values we store in the EventDescMap are either owned by it or have a
// longer lifetime ('h outlives 'edm), so it's sound to be covariant in 'edm. So in
// practice we use 'static but then we cast back to 'h.
*mut EventDescMap<'static, T, InitDescF>
>,
_phantom_desc_map: PhantomData<&'edm mut ()>,

scratch: &'i ScratchAlloc,
Expand Down Expand Up @@ -139,7 +145,7 @@ impl<'i, 'h, 'edm, InitDescF, T> EventVisitor<'i, 'h, 'edm, InitDescF, T> {
timestamp,
buffer_id,
scratch,
desc_map,
desc_map: UnsafeCell::new(desc_map),
event_desc: OnceCell::new(),
_phantom_desc_map: PhantomData,
}
Expand Down Expand Up @@ -262,13 +268,13 @@ where
Ok(&desc.name)
}

fn desc_map(&self) -> &'edm mut EventDescMap<'h, T, InitDescF> {
fn desc_map(&'edm self) -> &'edm mut EventDescMap<'h, T, InitDescF> {
// SAFETY: This comes from an &'edm mut reference in the first place, and since
// EventVisitor::new() requires an &'edm mut EventDescMap, we cannot accidentally
// borrow it mutably more than once. This makes it safe to turn it back to an &'edm
// mut.
let desc_map: *mut EventDescMap<'static, T, InitDescF> = self.desc_map;
let desc_map: &'edm mut EventDescMap<'static, T, InitDescF> = unsafe { &mut *desc_map };
let desc_map: *mut *mut EventDescMap<'static, T, InitDescF> = self.desc_map.get();
let desc_map: &'edm mut EventDescMap<'static, T, InitDescF> = unsafe { &mut **desc_map };
let desc_map: &'edm mut EventDescMap<'h, T, InitDescF> = unsafe{core::mem::transmute(desc_map)};
desc_map
}
Expand Down

0 comments on commit 6bb386c

Please sign in to comment.