Skip to content

Commit b1e67b7

Browse files
authored
Merge pull request #188 from saethlin/field-retagging
Use ManuallyDrop with bumpalo's Box instead of mem::forget
2 parents c699cd1 + d325e2c commit b1e67b7

File tree

3 files changed

+22
-8
lines changed

3 files changed

+22
-8
lines changed

src/boxed.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ use {
130130
future::Future,
131131
hash::{Hash, Hasher},
132132
iter::FusedIterator,
133-
mem,
133+
mem::ManuallyDrop,
134134
ops::{Deref, DerefMut},
135135
pin::Pin,
136136
task::{Context, Poll},
@@ -280,9 +280,8 @@ impl<'a, T: ?Sized> Box<'a, T> {
280280
/// ```
281281
#[inline]
282282
pub fn into_raw(b: Box<'a, T>) -> *mut T {
283-
let ptr = b.0 as *mut T;
284-
mem::forget(b);
285-
ptr
283+
let mut b = ManuallyDrop::new(b);
284+
b.deref_mut().0 as *mut T
286285
}
287286

288287
/// Consumes and leaks the `Box`, returning a mutable reference,
@@ -662,20 +661,20 @@ impl<'a, F: ?Sized + Future + Unpin> Future for Box<'a, F> {
662661

663662
/// This impl replaces unsize coercion.
664663
impl<'a, T, const N: usize> From<Box<'a, [T; N]>> for Box<'a, [T]> {
665-
fn from(mut arr: Box<'a, [T; N]>) -> Box<'a, [T]> {
664+
fn from(arr: Box<'a, [T; N]>) -> Box<'a, [T]> {
665+
let mut arr = ManuallyDrop::new(arr);
666666
let ptr = core::ptr::slice_from_raw_parts_mut(arr.as_mut_ptr(), N);
667-
mem::forget(arr);
668667
unsafe { Box::from_raw(ptr) }
669668
}
670669
}
671670

672671
/// This impl replaces unsize coercion.
673672
impl<'a, T, const N: usize> TryFrom<Box<'a, [T]>> for Box<'a, [T; N]> {
674673
type Error = Box<'a, [T]>;
675-
fn try_from(mut slice: Box<'a, [T]>) -> Result<Box<'a, [T; N]>, Box<'a, [T]>> {
674+
fn try_from(slice: Box<'a, [T]>) -> Result<Box<'a, [T; N]>, Box<'a, [T]>> {
676675
if slice.len() == N {
676+
let mut slice = ManuallyDrop::new(slice);
677677
let ptr = slice.as_mut_ptr() as *mut [T; N];
678-
mem::forget(slice);
679678
Ok(unsafe { Box::from_raw(ptr) })
680679
} else {
681680
Err(slice)

tests/all/boxed.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#![cfg(feature = "boxed")]
2+
3+
use bumpalo::Bump;
4+
use bumpalo::boxed::Box;
5+
6+
#[test]
7+
fn into_raw_aliasing() {
8+
let bump = Bump::new();
9+
let boxed = Box::new_in(1, &bump);
10+
let raw = Box::into_raw(boxed);
11+
12+
let mut_ref = unsafe { &mut *raw };
13+
dbg!(mut_ref);
14+
}

tests/all/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,6 @@ mod tests;
1313
mod try_alloc_try_with;
1414
mod try_alloc_with;
1515
mod vec;
16+
mod boxed;
1617

1718
fn main() {}

0 commit comments

Comments
 (0)