Trait collections::boxed::FnBox [] [src]

pub trait FnBox<A> {
    type Output;
    fn call_box(self: Box<Self>, args: A) -> Self::Output;
}
Unstable

: Newly introduced

FnBox`FnBoxis a version of theFnOnceintended for use with boxed closure objects. The idea is that where one would normally store aBoxin a data structure, you should useBox. The two traits behave essentially the same, except that aFnBoxclosure can only be called if it is boxed. (Note thatFnBoxmay be deprecated in the future ifBox` closures become directly usable.)

Example

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<FnBox() -> i32> and not Box<FnOnce() -> i32>.

#![feature(fnbox)] extern crate collections; use std::boxed::FnBox; use std::collections::HashMap; fn make_map() -> HashMap<i32, Box<FnBox() -> i32>> { let mut map: HashMap<i32, Box<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); } }
#![feature(fnbox)]

use std::boxed::FnBox;
use std::collections::HashMap;

fn make_map() -> HashMap<i32, Box<FnBox() -> i32>> {
    let mut map: HashMap<i32, Box<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);
    }
}

Associated Types

type Output

Unstable

: Newly introduced

Required Methods

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

Unstable

: Newly introduced

Implementors