[][src]Trait std::boxed::FnBox

pub trait FnBox<A>: FnOnce<A> {
    fn call_box(self: Box<Self>, args: A) -> Self::Output;
}
Deprecated since 1.37.0:

use FnOnce, FnMut, or Fn instead

🔬 This is a nightly-only experimental API. (fnbox #28796)

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);
    }
}Run

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);
    }
}Run

Required methods

fn call_box(self: Box<Self>, args: A) -> Self::Output

Deprecated since 1.37.0:

use FnOnce, FnMut, or Fn instead

🔬 This is a nightly-only experimental API. (fnbox #28796)

Performs the call operation.

Loading content...

Implementors

impl<A, F> FnBox<A> for F where
    F: FnOnce<A>, 
[src]

Loading content...