Skip to content

Commit

Permalink
Use Box::into_raw instead of mem::forget-in-disguise
Browse files Browse the repository at this point in the history
  • Loading branch information
taiki-e committed Jan 7, 2022
1 parent 59a313f commit 7f3f6cf
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 10 deletions.
8 changes: 3 additions & 5 deletions crossbeam-channel/src/flavors/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use std::cell::UnsafeCell;
use std::marker::PhantomData;
use std::mem::{self, MaybeUninit};
use std::mem::MaybeUninit;
use std::ptr;
use std::sync::atomic::{self, AtomicUsize, Ordering};
use std::time::Instant;
Expand Down Expand Up @@ -110,7 +110,7 @@ impl<T> Channel<T> {
// Allocate a buffer of `cap` slots initialized
// with stamps.
let buffer = {
let mut boxed: Box<[Slot<T>]> = (0..cap)
let boxed: Box<[Slot<T>]> = (0..cap)
.map(|i| {
// Set the stamp to `{ lap: 0, mark: 0, index: i }`.
Slot {
Expand All @@ -119,9 +119,7 @@ impl<T> Channel<T> {
}
})
.collect();
let ptr = boxed.as_mut_ptr();
mem::forget(boxed);
ptr
Box::into_raw(boxed) as *mut Slot<T>
};

Channel {
Expand Down
8 changes: 3 additions & 5 deletions crossbeam-queue/src/array_queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use alloc::boxed::Box;
use core::cell::UnsafeCell;
use core::fmt;
use core::marker::PhantomData;
use core::mem::{self, MaybeUninit};
use core::mem::MaybeUninit;
use core::sync::atomic::{self, AtomicUsize, Ordering};

use crossbeam_utils::{Backoff, CachePadded};
Expand Down Expand Up @@ -103,7 +103,7 @@ impl<T> ArrayQueue<T> {
// Allocate a buffer of `cap` slots initialized
// with stamps.
let buffer = {
let mut boxed: Box<[Slot<T>]> = (0..cap)
let boxed: Box<[Slot<T>]> = (0..cap)
.map(|i| {
// Set the stamp to `{ lap: 0, index: i }`.
Slot {
Expand All @@ -112,9 +112,7 @@ impl<T> ArrayQueue<T> {
}
})
.collect();
let ptr = boxed.as_mut_ptr();
mem::forget(boxed);
ptr
Box::into_raw(boxed) as *mut Slot<T>
};

// One lap is the smallest power of two greater than `cap`.
Expand Down

0 comments on commit 7f3f6cf

Please sign in to comment.