Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Box::into_raw instead of mem::forget-in-disguise #763

Merged
merged 1 commit into from
Jan 7, 2022
Merged
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
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