Skip to content

Commit d3322e2

Browse files
committed
Auto merge of rust-lang#106981 - joboet:std_remove_box_syntax, r=thomcc
Do not use box syntax in `std` See rust-lang#94970 and rust-lang#49733. About half of the `box` instances in `std` do not even need to allocate, the other half can simply be replaced with `Box::new`. `@rustbot` label +T-libs r? rust-lang/libs
2 parents a5fa99e + 7f2cf19 commit d3322e2

File tree

9 files changed

+35
-59
lines changed

9 files changed

+35
-59
lines changed

library/std/src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,6 @@
238238
#![feature(allocator_internals)]
239239
#![feature(allow_internal_unsafe)]
240240
#![feature(allow_internal_unstable)]
241-
#![feature(box_syntax)]
242241
#![feature(c_unwind)]
243242
#![feature(cfg_target_thread_local)]
244243
#![feature(concat_idents)]

library/std/src/sys/hermit/thread.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ impl Thread {
2727
p: Box<dyn FnOnce()>,
2828
core_id: isize,
2929
) -> io::Result<Thread> {
30-
let p = Box::into_raw(box p);
30+
let p = Box::into_raw(Box::new(p));
3131
let tid = abi::spawn2(
3232
thread_start,
33-
p as usize,
33+
p.expose_addr(),
3434
abi::Priority::into(abi::NORMAL_PRIO),
3535
stack,
3636
core_id,

library/std/src/sys/hermit/thread_local_dtor.rs

+7-16
Original file line numberDiff line numberDiff line change
@@ -5,32 +5,23 @@
55
// The this solution works like the implementation of macOS and
66
// doesn't additional OS support
77

8-
use crate::cell::Cell;
9-
use crate::ptr;
8+
use crate::mem;
109

1110
#[thread_local]
12-
static DTORS: Cell<*mut List> = Cell::new(ptr::null_mut());
13-
14-
type List = Vec<(*mut u8, unsafe extern "C" fn(*mut u8))>;
11+
static mut DTORS: Vec<(*mut u8, unsafe extern "C" fn(*mut u8))> = Vec::new();
1512

1613
pub unsafe fn register_dtor(t: *mut u8, dtor: unsafe extern "C" fn(*mut u8)) {
17-
if DTORS.get().is_null() {
18-
let v: Box<List> = box Vec::new();
19-
DTORS.set(Box::into_raw(v));
20-
}
21-
22-
let list: &mut List = &mut *DTORS.get();
14+
let list = &mut DTORS;
2315
list.push((t, dtor));
2416
}
2517

2618
// every thread call this function to run through all possible destructors
2719
pub unsafe fn run_dtors() {
28-
let mut ptr = DTORS.replace(ptr::null_mut());
29-
while !ptr.is_null() {
30-
let list = Box::from_raw(ptr);
31-
for (ptr, dtor) in list.into_iter() {
20+
let mut list = mem::take(&mut DTORS);
21+
while !list.is_empty() {
22+
for (ptr, dtor) in list {
3223
dtor(ptr);
3324
}
34-
ptr = DTORS.replace(ptr::null_mut());
25+
list = mem::take(&mut DTORS);
3526
}
3627
}

library/std/src/sys/solid/thread_local_dtor.rs

+12-20
Original file line numberDiff line numberDiff line change
@@ -5,43 +5,35 @@
55

66
use super::{abi, itron::task};
77
use crate::cell::Cell;
8-
use crate::ptr;
8+
use crate::mem;
99

1010
#[thread_local]
11-
static DTORS: Cell<*mut List> = Cell::new(ptr::null_mut());
11+
static REGISTERED: Cell<bool> = Cell::new(false);
1212

13-
type List = Vec<(*mut u8, unsafe extern "C" fn(*mut u8))>;
13+
#[thread_local]
14+
static mut DTORS: Vec<(*mut u8, unsafe extern "C" fn(*mut u8))> = Vec::new();
1415

1516
pub unsafe fn register_dtor(t: *mut u8, dtor: unsafe extern "C" fn(*mut u8)) {
16-
if DTORS.get().is_null() {
17+
if !REGISTERED.get() {
1718
let tid = task::current_task_id_aborting();
18-
let v: Box<List> = box Vec::new();
19-
DTORS.set(Box::into_raw(v));
20-
2119
// Register `tls_dtor` to make sure the TLS destructors are called
2220
// for tasks created by other means than `std::thread`
2321
unsafe { abi::SOLID_TLS_AddDestructor(tid as i32, tls_dtor) };
22+
REGISTERED.set(true);
2423
}
2524

26-
let list: &mut List = unsafe { &mut *DTORS.get() };
25+
let list = unsafe { &mut DTORS };
2726
list.push((t, dtor));
2827
}
2928

3029
pub unsafe fn run_dtors() {
31-
let ptr = DTORS.get();
32-
if !ptr.is_null() {
33-
// Swap the destructor list, call all registered destructors,
34-
// and repeat this until the list becomes permanently empty.
35-
while let Some(list) = Some(crate::mem::replace(unsafe { &mut *ptr }, Vec::new()))
36-
.filter(|list| !list.is_empty())
37-
{
38-
for (ptr, dtor) in list.into_iter() {
39-
unsafe { dtor(ptr) };
40-
}
30+
let mut list = mem::take(unsafe { &mut DTORS });
31+
while !list.is_empty() {
32+
for (ptr, dtor) in list {
33+
unsafe { dtor(ptr) };
4134
}
4235

43-
// Drop the destructor list
44-
unsafe { Box::from_raw(DTORS.replace(ptr::null_mut())) };
36+
list = mem::take(unsafe { &mut DTORS });
4537
}
4638
}
4739

library/std/src/sys/unix/thread.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ unsafe impl Sync for Thread {}
4949
impl Thread {
5050
// unsafe: see thread::Builder::spawn_unchecked for safety requirements
5151
pub unsafe fn new(stack: usize, p: Box<dyn FnOnce()>) -> io::Result<Thread> {
52-
let p = Box::into_raw(box p);
52+
let p = Box::into_raw(Box::new(p));
5353
let mut native: libc::pthread_t = mem::zeroed();
5454
let mut attr: libc::pthread_attr_t = mem::zeroed();
5555
assert_eq!(libc::pthread_attr_init(&mut attr), 0);

library/std/src/sys/unix/thread_local_dtor.rs

+10-15
Original file line numberDiff line numberDiff line change
@@ -57,39 +57,34 @@ pub unsafe fn register_dtor(t: *mut u8, dtor: unsafe extern "C" fn(*mut u8)) {
5757
#[cfg(target_os = "macos")]
5858
pub unsafe fn register_dtor(t: *mut u8, dtor: unsafe extern "C" fn(*mut u8)) {
5959
use crate::cell::Cell;
60+
use crate::mem;
6061
use crate::ptr;
6162

6263
#[thread_local]
6364
static REGISTERED: Cell<bool> = Cell::new(false);
65+
66+
#[thread_local]
67+
static mut DTORS: Vec<(*mut u8, unsafe extern "C" fn(*mut u8))> = Vec::new();
68+
6469
if !REGISTERED.get() {
6570
_tlv_atexit(run_dtors, ptr::null_mut());
6671
REGISTERED.set(true);
6772
}
6873

69-
type List = Vec<(*mut u8, unsafe extern "C" fn(*mut u8))>;
70-
71-
#[thread_local]
72-
static DTORS: Cell<*mut List> = Cell::new(ptr::null_mut());
73-
if DTORS.get().is_null() {
74-
let v: Box<List> = box Vec::new();
75-
DTORS.set(Box::into_raw(v));
76-
}
77-
7874
extern "C" {
7975
fn _tlv_atexit(dtor: unsafe extern "C" fn(*mut u8), arg: *mut u8);
8076
}
8177

82-
let list: &mut List = &mut *DTORS.get();
78+
let list = &mut DTORS;
8379
list.push((t, dtor));
8480

8581
unsafe extern "C" fn run_dtors(_: *mut u8) {
86-
let mut ptr = DTORS.replace(ptr::null_mut());
87-
while !ptr.is_null() {
88-
let list = Box::from_raw(ptr);
89-
for (ptr, dtor) in list.into_iter() {
82+
let mut list = mem::take(&mut DTORS);
83+
while !list.is_empty() {
84+
for (ptr, dtor) in list {
9085
dtor(ptr);
9186
}
92-
ptr = DTORS.replace(ptr::null_mut());
87+
list = mem::take(&mut DTORS);
9388
}
9489
}
9590
}

library/std/src/sys/windows/thread.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ pub struct Thread {
2222
impl Thread {
2323
// unsafe: see thread::Builder::spawn_unchecked for safety requirements
2424
pub unsafe fn new(stack: usize, p: Box<dyn FnOnce()>) -> io::Result<Thread> {
25-
let p = Box::into_raw(box p);
25+
let p = Box::into_raw(Box::new(p));
2626

2727
// FIXME On UNIX, we guard against stack sizes that are too small but
2828
// that's because pthreads enforces that stacks are at least

library/std/src/sys_common/thread_local_dtor.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ pub unsafe fn register_dtor_fallback(t: *mut u8, dtor: unsafe extern "C" fn(*mut
3030
static DTORS: StaticKey = StaticKey::new(Some(run_dtors));
3131
type List = Vec<(*mut u8, unsafe extern "C" fn(*mut u8))>;
3232
if DTORS.get().is_null() {
33-
let v: Box<List> = box Vec::new();
33+
let v: Box<List> = Box::new(Vec::new());
3434
DTORS.set(Box::into_raw(v) as *mut u8);
3535
}
3636
let list: &mut List = &mut *(DTORS.get() as *mut List);

library/std/src/thread/local.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1110,8 +1110,7 @@ pub mod os {
11101110
let ptr = if ptr.is_null() {
11111111
// If the lookup returned null, we haven't initialized our own
11121112
// local copy, so do that now.
1113-
let ptr: Box<Value<T>> = box Value { inner: LazyKeyInner::new(), key: self };
1114-
let ptr = Box::into_raw(ptr);
1113+
let ptr = Box::into_raw(Box::new(Value { inner: LazyKeyInner::new(), key: self }));
11151114
// SAFETY: At this point we are sure there is no value inside
11161115
// ptr so setting it will not affect anyone else.
11171116
unsafe {

0 commit comments

Comments
 (0)