Skip to content

Commit

Permalink
Rollup merge of rust-lang#59818 - crlf0710:eliminate_libstd_fnbox, r=…
Browse files Browse the repository at this point in the history
…cramertj

Eliminate `FnBox` usages from libstd.
  • Loading branch information
Centril committed Apr 12, 2019
2 parents 85c7cc2 + 6635fbe commit 66548ae
Show file tree
Hide file tree
Showing 10 changed files with 15 additions and 24 deletions.
3 changes: 1 addition & 2 deletions src/libstd/sys/cloudabi/thread.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate::boxed::FnBox;
use crate::cmp;
use crate::ffi::CStr;
use crate::io;
Expand All @@ -22,7 +21,7 @@ unsafe impl Sync for Thread {}

impl Thread {
// unsafe: see thread::Builder::spawn_unchecked for safety requirements
pub unsafe fn new(stack: usize, p: Box<dyn FnBox()>) -> io::Result<Thread> {
pub unsafe fn new(stack: usize, p: Box<dyn FnOnce()>) -> io::Result<Thread> {
let p = box p;
let mut native: libc::pthread_t = mem::zeroed();
let mut attr: libc::pthread_attr_t = mem::zeroed();
Expand Down
3 changes: 1 addition & 2 deletions src/libstd/sys/redox/thread.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate::boxed::FnBox;
use crate::ffi::CStr;
use crate::io;
use crate::mem;
Expand All @@ -19,7 +18,7 @@ unsafe impl Sync for Thread {}

impl Thread {
// unsafe: see thread::Builder::spawn_unchecked for safety requirements
pub unsafe fn new(_stack: usize, p: Box<dyn FnBox()>) -> io::Result<Thread> {
pub unsafe fn new(_stack: usize, p: Box<dyn FnOnce()>) -> io::Result<Thread> {
let p = box p;

let id = cvt(syscall::clone(syscall::CLONE_VM | syscall::CLONE_FS | syscall::CLONE_FILES))?;
Expand Down
8 changes: 3 additions & 5 deletions src/libstd/sys/sgx/thread.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#![cfg_attr(test, allow(dead_code))] // why is this necessary?
use crate::boxed::FnBox;
use crate::ffi::CStr;
use crate::io;
use crate::time::Duration;
Expand All @@ -13,17 +12,16 @@ pub const DEFAULT_MIN_STACK_SIZE: usize = 4096;
mod task_queue {
use crate::sync::{Mutex, MutexGuard, Once};
use crate::sync::mpsc;
use crate::boxed::FnBox;

pub type JoinHandle = mpsc::Receiver<()>;

pub(super) struct Task {
p: Box<dyn FnBox()>,
p: Box<dyn FnOnce()>,
done: mpsc::Sender<()>,
}

impl Task {
pub(super) fn new(p: Box<dyn FnBox()>) -> (Task, JoinHandle) {
pub(super) fn new(p: Box<dyn FnOnce()>) -> (Task, JoinHandle) {
let (done, recv) = mpsc::channel();
(Task { p, done }, recv)
}
Expand Down Expand Up @@ -51,7 +49,7 @@ mod task_queue {

impl Thread {
// unsafe: see thread::Builder::spawn_unchecked for safety requirements
pub unsafe fn new(_stack: usize, p: Box<dyn FnBox()>)
pub unsafe fn new(_stack: usize, p: Box<dyn FnOnce()>)
-> io::Result<Thread>
{
let mut queue_lock = task_queue::lock();
Expand Down
3 changes: 1 addition & 2 deletions src/libstd/sys/unix/thread.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate::boxed::FnBox;
use crate::cmp;
use crate::ffi::CStr;
use crate::io;
Expand Down Expand Up @@ -39,7 +38,7 @@ unsafe fn pthread_attr_setstacksize(_attr: *mut libc::pthread_attr_t,

impl Thread {
// unsafe: see thread::Builder::spawn_unchecked for safety requirements
pub unsafe fn new(stack: usize, p: Box<dyn FnBox()>)
pub unsafe fn new(stack: usize, p: Box<dyn FnOnce()>)
-> io::Result<Thread> {
let p = box p;
let mut native: libc::pthread_t = mem::zeroed();
Expand Down
3 changes: 1 addition & 2 deletions src/libstd/sys/wasi/thread.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate::boxed::FnBox;
use crate::cmp;
use crate::ffi::CStr;
use crate::io;
Expand All @@ -13,7 +12,7 @@ pub const DEFAULT_MIN_STACK_SIZE: usize = 4096;

impl Thread {
// unsafe: see thread::Builder::spawn_unchecked for safety requirements
pub unsafe fn new(_stack: usize, _p: Box<dyn FnBox()>)
pub unsafe fn new(_stack: usize, _p: Box<dyn FnOnce()>)
-> io::Result<Thread>
{
unsupported()
Expand Down
3 changes: 1 addition & 2 deletions src/libstd/sys/wasm/thread.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate::boxed::FnBox;
use crate::ffi::CStr;
use crate::io;
use crate::sys::{unsupported, Void};
Expand All @@ -10,7 +9,7 @@ pub const DEFAULT_MIN_STACK_SIZE: usize = 4096;

impl Thread {
// unsafe: see thread::Builder::spawn_unchecked for safety requirements
pub unsafe fn new(_stack: usize, _p: Box<dyn FnBox()>)
pub unsafe fn new(_stack: usize, _p: Box<dyn FnOnce()>)
-> io::Result<Thread>
{
unsupported()
Expand Down
3 changes: 1 addition & 2 deletions src/libstd/sys/windows/thread.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate::boxed::FnBox;
use crate::io;
use crate::ffi::CStr;
use crate::mem;
Expand All @@ -20,7 +19,7 @@ pub struct Thread {

impl Thread {
// unsafe: see thread::Builder::spawn_unchecked for safety requirements
pub unsafe fn new(stack: usize, p: Box<dyn FnBox()>)
pub unsafe fn new(stack: usize, p: Box<dyn FnOnce()>)
-> io::Result<Thread> {
let p = box p;

Expand Down
5 changes: 2 additions & 3 deletions src/libstd/sys_common/at_exit_imp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@
//!
//! Documentation can be found on the `rt::at_exit` function.

use crate::boxed::FnBox;
use crate::ptr;
use crate::mem;
use crate::sys_common::mutex::Mutex;

type Queue = Vec<Box<dyn FnBox()>>;
type Queue = Vec<Box<dyn FnOnce()>>;

// NB these are specifically not types from `std::sync` as they currently rely
// on poisoning and this module needs to operate at a lower level than requiring
Expand Down Expand Up @@ -61,7 +60,7 @@ pub fn cleanup() {
}
}

pub fn push(f: Box<dyn FnBox()>) -> bool {
pub fn push(f: Box<dyn FnOnce()>) -> bool {
unsafe {
let _guard = LOCK.lock();
if init() {
Expand Down
3 changes: 1 addition & 2 deletions src/libstd/sys_common/thread.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate::boxed::FnBox;
use crate::env;
use crate::sync::atomic::{self, Ordering};
use crate::sys::stack_overflow;
Expand All @@ -11,7 +10,7 @@ pub unsafe fn start_thread(main: *mut u8) {
let _handler = stack_overflow::Handler::new();

// Finally, let's run some code.
Box::from_raw(main as *mut Box<dyn FnBox()>)()
Box::from_raw(main as *mut Box<dyn FnOnce()>)()
}

pub fn min_stack() -> usize {
Expand Down
5 changes: 3 additions & 2 deletions src/libstd/thread/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,6 @@
#![stable(feature = "rust1", since = "1.0.0")]

use crate::any::Any;
use crate::boxed::FnBox;
use crate::cell::UnsafeCell;
use crate::ffi::{CStr, CString};
use crate::fmt;
Expand Down Expand Up @@ -488,7 +487,9 @@ impl Builder {
// returning.
native: Some(imp::Thread::new(
stack_size,
mem::transmute::<Box<dyn FnBox() + 'a>, Box<dyn FnBox() + 'static>>(Box::new(main))
mem::transmute::<Box<dyn FnOnce() + 'a>, Box<dyn FnOnce() + 'static>>(Box::new(
main,
)),
)?),
thread: my_thread,
packet: Packet(my_packet),
Expand Down

0 comments on commit 66548ae

Please sign in to comment.