Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions oscars/src/collectors/mark_sweep/pointers/gc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,17 @@ impl<T: Trace> Gc<T> {
marker: PhantomData,
}
}

/// Cast a `&Gc<T>` to `&Gc<U>` without consuming the handle.
///
/// # Safety
///
/// Caller must ensure that `U` is the correct runtime type for `this`.
#[inline]
#[must_use]
pub unsafe fn cast_ref_unchecked<U: Trace + 'static>(this: &Self) -> &Gc<U> {
unsafe { &*(this as *const Self).cast::<Gc<U>>() }
}
}

impl<T: Trace> Gc<T> {
Expand Down
22 changes: 22 additions & 0 deletions oscars/src/collectors/mark_sweep/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,28 @@ fn ptr_eq_distinguishes_equal_values() {
);
}

#[test]
fn cast_ref_unchecked_preserves_identity_and_value() {
let collector = &mut MarkSweepGarbageCollector::default()
.with_page_size(256)
.with_heap_threshold(512);

let typed = Gc::new_in(13u32, collector);
let erased_as_u64: Gc<u64> = unsafe { Gc::cast_unchecked(typed.clone()) };

// SAFETY: `erased_as_u64` points to an allocation that actually stores `u32`.
let recovered_ref: &Gc<u32> = unsafe { Gc::cast_ref_unchecked(&erased_as_u64) };

assert_eq!(
**recovered_ref, 13u32,
"cast_ref_unchecked recovered wrong value"
);
assert!(
Gc::ptr_eq(&typed, recovered_ref),
"cast_ref_unchecked should preserve pointer identity"
);
}

#[test]
fn multi_gc() {
let collector = &mut MarkSweepGarbageCollector::default()
Expand Down
11 changes: 11 additions & 0 deletions oscars/src/collectors/mark_sweep_arena2/pointers/gc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,17 @@ impl<T: Trace> Gc<T> {
marker: PhantomData,
}
}

/// Cast a `&Gc<T>` to `&Gc<U>` without consuming the handle.
///
/// # Safety
///
/// Caller must ensure that `U` is the correct runtime type for `this`.
#[inline]
#[must_use]
pub unsafe fn cast_ref_unchecked<U: Trace + 'static>(this: &Self) -> &Gc<U> {
unsafe { &*(this as *const Self).cast::<Gc<U>>() }
}
}

impl<T: Trace> Gc<T> {
Expand Down
22 changes: 22 additions & 0 deletions oscars/src/collectors/mark_sweep_arena2/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,28 @@ fn ptr_eq_distinguishes_equal_values() {
);
}

#[test]
fn cast_ref_unchecked_preserves_identity_and_value() {
let collector = &mut MarkSweepGarbageCollector::default()
.with_arena_size(256)
.with_heap_threshold(512);

let typed = Gc::new_in(13u32, collector);
let erased_as_u64: Gc<u64> = unsafe { Gc::cast_unchecked(typed.clone()) };

// SAFETY: `erased_as_u64` points to an allocation that actually stores `u32`.
let recovered_ref: &Gc<u32> = unsafe { Gc::cast_ref_unchecked(&erased_as_u64) };

assert_eq!(
**recovered_ref, 13u32,
"cast_ref_unchecked recovered wrong value"
);
assert!(
Gc::ptr_eq(&typed, recovered_ref),
"cast_ref_unchecked should preserve pointer identity"
);
}

#[test]
fn multi_gc() {
let collector = &mut MarkSweepGarbageCollector::default()
Expand Down