From 8617310c3c9e192080df6a0c7b4835d3f02e27b9 Mon Sep 17 00:00:00 2001 From: bjorn3 Date: Fri, 1 Nov 2019 20:58:30 +0100 Subject: [PATCH] Add FnBox back --- src/liballoc/boxed.rs | 13 +++++++++++++ src/libstd/prelude/v1.rs | 2 +- src/libstd/sys/cloudabi/thread.rs | 2 +- src/libstd/sys/hermit/thread.rs | 4 ++-- src/libstd/sys/unix/thread.rs | 4 ++-- src/libstd/sys_common/at_exit_imp.rs | 6 +++--- src/libstd/sys_common/mod.rs | 2 +- src/libstd/sys_common/thread.rs | 2 +- src/libstd/thread/mod.rs | 2 +- 9 files changed, 25 insertions(+), 12 deletions(-) diff --git a/src/liballoc/boxed.rs b/src/liballoc/boxed.rs index ef9b648..e32b870 100644 --- a/src/liballoc/boxed.rs +++ b/src/liballoc/boxed.rs @@ -1079,3 +1079,16 @@ impl Future for Box { F::poll(Pin::new(&mut *self), cx) } } + +#[stable(feature = "rust1", since = "1.0.0")] +pub trait FnBox: FnOnce { + #[stable(feature = "rust1", since = "1.0.0")] + extern "rust-call" fn call_box(self: Box, args: A) -> Self::Output; +} + +#[stable(feature = "rust1", since = "1.0.0")] +impl> FnBox for F { + extern "rust-call" fn call_box(self: Box, args: A) -> Self::Output { + >::call_once(*self, args) + } +} diff --git a/src/libstd/prelude/v1.rs b/src/libstd/prelude/v1.rs index 3e4cf91..1f50eb3 100644 --- a/src/libstd/prelude/v1.rs +++ b/src/libstd/prelude/v1.rs @@ -94,6 +94,6 @@ pub use core::prelude::v1::{ #[stable(feature = "rust1", since = "1.0.0")] #[doc(no_inline)] -pub use crate::boxed::Box; +pub use crate::boxed::{Box, FnBox}; #[stable(feature = "rust1", since = "1.0.0")] #[doc(no_inline)] pub use crate::string::{String, ToString}; diff --git a/src/libstd/sys/cloudabi/thread.rs b/src/libstd/sys/cloudabi/thread.rs index 240b6ea..6f71c6b 100644 --- a/src/libstd/sys/cloudabi/thread.rs +++ b/src/libstd/sys/cloudabi/thread.rs @@ -21,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) -> io::Result { + pub unsafe fn new(stack: usize, p: Box) -> io::Result { let p = box p; let mut native: libc::pthread_t = mem::zeroed(); let mut attr: libc::pthread_attr_t = mem::zeroed(); diff --git a/src/libstd/sys/hermit/thread.rs b/src/libstd/sys/hermit/thread.rs index 99a9c83..b8bc392 100644 --- a/src/libstd/sys/hermit/thread.rs +++ b/src/libstd/sys/hermit/thread.rs @@ -44,9 +44,9 @@ unsafe impl Sync for Thread {} pub const DEFAULT_MIN_STACK_SIZE: usize = 262144; impl Thread { pub unsafe fn new_with_coreid( _stack: usize, - p: Box, + p: Box, core_id: isize, ) -> io::Result { let p = box p; @@ -67,7 +67,7 @@ impl Thread { } } - pub unsafe fn new(stack: usize, p: Box) -> io::Result { + pub unsafe fn new(stack: usize, p: Box) -> io::Result { Thread::new_with_coreid(stack, p, -1 /* = no specific core */) } diff --git a/src/libstd/sys/unix/thread.rs b/src/libstd/sys/unix/thread.rs index 143cf2f..a6e8faf 100644 --- a/src/libstd/sys/unix/thread.rs +++ b/src/libstd/sys/unix/thread.rs @@ -38,8 +38,8 @@ 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) -> io::Result { + pub unsafe fn new(stack: usize, p: Box) -> io::Result { panic!("Warning: Threads are not yet fully supported, because cranelift doesn't support atomics."); let p = box p; let mut native: libc::pthread_t = mem::zeroed(); diff --git a/src/libstd/sys_common/at_exit_imp.rs b/src/libstd/sys_common/at_exit_imp.rs index cdb72ee..e523333 100644 --- a/src/libstd/sys_common/at_exit_imp.rs +++ b/src/libstd/sys_common/at_exit_imp.rs @@ -6,7 +6,7 @@ use crate::mem; use crate::ptr; use crate::sys_common::mutex::Mutex; -type Queue = Vec>; +type Queue = Vec>; // 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 @@ -53,14 +53,14 @@ pub fn cleanup() { let queue: Box = Box::from_raw(queue); for to_run in *queue { // We are not holding any lock, so reentrancy is fine. - to_run(); + to_run.call_box(()); } } } } } -pub fn push(f: Box) -> bool { +pub fn push(f: Box) -> bool { unsafe { let _guard = LOCK.lock(); if init() { diff --git a/src/libstd/sys_common/mod.rs b/src/libstd/sys_common/mod.rs index 7a0bcd0..668bef2 100644 --- a/src/libstd/sys_common/mod.rs +++ b/src/libstd/sys_common/mod.rs @@ -113,7 +113,7 @@ pub trait FromInner { /// closure will be run once the main thread exits. Returns `Err` to indicate /// that the closure could not be registered, meaning that it is not scheduled /// to be run. -pub fn at_exit(f: F) -> Result<(), ()> { +pub fn at_exit(f: F) -> Result<(), ()> { if at_exit_imp::push(Box::new(f)) { Ok(()) } else { Err(()) } } diff --git a/src/libstd/sys_common/thread.rs b/src/libstd/sys_common/thread.rs index c638be9..5c18a18 100644 --- a/src/libstd/sys_common/thread.rs +++ b/src/libstd/sys_common/thread.rs @@ -10,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)() + Box::from_raw(main as *mut Box).call_box(()) } pub fn min_stack() -> usize { diff --git a/src/libstd/thread/mod.rs b/src/libstd/thread/mod.rs index 0ffa6ac..4a3e3d6 100644 --- a/src/libstd/thread/mod.rs +++ b/src/libstd/thread/mod.rs @@ -485,7 +485,7 @@ impl Builder { // returning. native: Some(imp::Thread::new( stack_size, - mem::transmute::, Box>(Box::new( + mem::transmute::, Box>(Box::new( main, )), )?), -- 2.20.1