Remove FnBox.
This commit is contained in:
parent
8d6f4b96df
commit
a99a7b7f35
|
@ -117,9 +117,7 @@ fn main () {
|
|||
}
|
||||
```
|
||||
|
||||
One of the objectives of this feature is to allow `Box<dyn FnOnce>`, instead of `Box<dyn FnBox>` in the future. See [#28796] for details.
|
||||
|
||||
[#28796]: https://github.com/rust-lang/rust/issues/28796
|
||||
One of the objectives of this feature is to allow `Box<dyn FnOnce>`.
|
||||
|
||||
## Variable length arrays
|
||||
|
||||
|
|
|
@ -1,32 +0,0 @@
|
|||
# `fnbox`
|
||||
|
||||
The tracking issue for this feature is [#28796]
|
||||
|
||||
[#28796]: https://github.com/rust-lang/rust/issues/28796
|
||||
|
||||
------------------------
|
||||
|
||||
This had been a temporary alternative to the following impls:
|
||||
|
||||
```rust,ignore
|
||||
impl<A, F> FnOnce for Box<F> where F: FnOnce<A> + ?Sized {}
|
||||
impl<A, F> FnMut for Box<F> where F: FnMut<A> + ?Sized {}
|
||||
impl<A, F> Fn for Box<F> where F: Fn<A> + ?Sized {}
|
||||
```
|
||||
|
||||
The impls are parallel to these (relatively old) impls:
|
||||
|
||||
```rust,ignore
|
||||
impl<A, F> FnOnce for &mut F where F: FnMut<A> + ?Sized {}
|
||||
impl<A, F> FnMut for &mut F where F: FnMut<A> + ?Sized {}
|
||||
impl<A, F> Fn for &mut F where F: Fn<A> + ?Sized {}
|
||||
impl<A, F> FnOnce for &F where F: Fn<A> + ?Sized {}
|
||||
impl<A, F> FnMut for &F where F: Fn<A> + ?Sized {}
|
||||
impl<A, F> Fn for &F where F: Fn<A> + ?Sized {}
|
||||
```
|
||||
|
||||
Before the introduction of [`unsized_locals`][unsized_locals], we had been unable to provide the former impls. That means, unlike `&dyn Fn()` or `&mut dyn FnMut()` we could not use `Box<dyn FnOnce()>` at that time.
|
||||
|
||||
[unsized_locals]: ../language-features/unsized-locals.md
|
||||
|
||||
`FnBox()` is an alternative approach to `Box<dyn FnBox()>` is delegated to `FnBox::call_box` which doesn't need unsized locals. As we now have `Box<dyn FnOnce()>` working, the `fnbox` feature is going to be removed.
|
|
@ -761,85 +761,6 @@ impl<A, F: Fn<A> + ?Sized> Fn<A> for Box<F> {
|
|||
}
|
||||
}
|
||||
|
||||
/// `FnBox` is deprecated and will be removed.
|
||||
/// `Box<dyn FnOnce()>` can be called directly, since Rust 1.35.0.
|
||||
///
|
||||
/// `FnBox` is a version of the `FnOnce` intended for use with boxed
|
||||
/// closure objects. The idea was that where one would normally store a
|
||||
/// `Box<dyn FnOnce()>` in a data structure, you whould use
|
||||
/// `Box<dyn FnBox()>`. The two traits behave essentially the same, except
|
||||
/// that a `FnBox` closure can only be called if it is boxed.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// Here is a snippet of code which creates a hashmap full of boxed
|
||||
/// once closures and then removes them one by one, calling each
|
||||
/// closure as it is removed. Note that the type of the closures
|
||||
/// stored in the map is `Box<dyn FnBox() -> i32>` and not `Box<dyn FnOnce()
|
||||
/// -> i32>`.
|
||||
///
|
||||
/// ```
|
||||
/// #![feature(fnbox)]
|
||||
/// #![allow(deprecated)]
|
||||
///
|
||||
/// use std::boxed::FnBox;
|
||||
/// use std::collections::HashMap;
|
||||
///
|
||||
/// fn make_map() -> HashMap<i32, Box<dyn FnBox() -> i32>> {
|
||||
/// let mut map: HashMap<i32, Box<dyn FnBox() -> i32>> = HashMap::new();
|
||||
/// map.insert(1, Box::new(|| 22));
|
||||
/// map.insert(2, Box::new(|| 44));
|
||||
/// map
|
||||
/// }
|
||||
///
|
||||
/// fn main() {
|
||||
/// let mut map = make_map();
|
||||
/// for i in &[1, 2] {
|
||||
/// let f = map.remove(&i).unwrap();
|
||||
/// assert_eq!(f(), i * 22);
|
||||
/// }
|
||||
/// }
|
||||
/// ```
|
||||
///
|
||||
/// In Rust 1.35.0 or later, use `FnOnce`, `FnMut`, or `Fn` instead:
|
||||
///
|
||||
/// ```
|
||||
/// use std::collections::HashMap;
|
||||
///
|
||||
/// fn make_map() -> HashMap<i32, Box<dyn FnOnce() -> i32>> {
|
||||
/// let mut map: HashMap<i32, Box<dyn FnOnce() -> i32>> = HashMap::new();
|
||||
/// map.insert(1, Box::new(|| 22));
|
||||
/// map.insert(2, Box::new(|| 44));
|
||||
/// map
|
||||
/// }
|
||||
///
|
||||
/// fn main() {
|
||||
/// let mut map = make_map();
|
||||
/// for i in &[1, 2] {
|
||||
/// let f = map.remove(&i).unwrap();
|
||||
/// assert_eq!(f(), i * 22);
|
||||
/// }
|
||||
/// }
|
||||
/// ```
|
||||
#[rustc_paren_sugar]
|
||||
#[unstable(feature = "fnbox", issue = "28796")]
|
||||
#[rustc_deprecated(reason = "use `FnOnce`, `FnMut`, or `Fn` instead", since = "1.37.0")]
|
||||
pub trait FnBox<A>: FnOnce<A> {
|
||||
/// Performs the call operation.
|
||||
fn call_box(self: Box<Self>, args: A) -> Self::Output;
|
||||
}
|
||||
|
||||
#[unstable(feature = "fnbox", issue = "28796")]
|
||||
#[rustc_deprecated(reason = "use `FnOnce`, `FnMut`, or `Fn` instead", since = "1.37.0")]
|
||||
#[allow(deprecated, deprecated_in_future)]
|
||||
impl<A, F> FnBox<A> for F
|
||||
where F: FnOnce<A>
|
||||
{
|
||||
fn call_box(self: Box<F>, args: A) -> F::Output {
|
||||
self.call_once(args)
|
||||
}
|
||||
}
|
||||
|
||||
#[unstable(feature = "coerce_unsized", issue = "27732")]
|
||||
impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<Box<U>> for Box<T> {}
|
||||
|
||||
|
|
|
@ -262,7 +262,6 @@
|
|||
#![feature(exhaustive_patterns)]
|
||||
#![feature(external_doc)]
|
||||
#![feature(fn_traits)]
|
||||
#![feature(fnbox)]
|
||||
#![feature(generator_trait)]
|
||||
#![feature(hash_raw_entry)]
|
||||
#![feature(hashmap_internals)]
|
||||
|
|
|
@ -1,13 +0,0 @@
|
|||
#![feature(fnbox)]
|
||||
#![allow(deprecated, deprecated_in_future)]
|
||||
|
||||
use std::boxed::FnBox;
|
||||
|
||||
fn call_it<T>(f: Box<dyn FnBox() -> T>) -> T {
|
||||
f()
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let s = "hello".to_owned();
|
||||
assert_eq!(&call_it(Box::new(|| s)) as &str, "hello");
|
||||
}
|
Loading…
Reference in New Issue