pub struct SendBoxFnOnce<'a, Arguments, Result = ()>(/* private fields */);
Expand description
SendBoxFnOnce
boxes any FnOnce + Send
function up to a certain
number of arguments (10 as of now).
As Box<FnOnce()>
doesn’t work yet, and Box<FnBox()>
will not be
available in stable rust, SendBoxFnOnce
tries to provide a safe
implementation.
Instead of Box<FnOnce(Args...) -> Result + 'a>
(or
Box<FnBox(Args...) -> Result + 'a>
) the box type is
SendBoxFnOnce<'a, (Args...,), Result>
(the arguments are always given
as tuple type). If the function doesn’t return a value (i.e. the
empty tuple) Result
can be omitted: SendBoxFnOnce<'a, (Args...,)>
.
Internally it is implemented similar to Box<FnBox()>
, but there is
no FnOnce
implementation for SendBoxFnOnce
.
You can build boxes for diverging functions too, but specifying the
type (like SendBoxFnOnce<(), !>
) is not possible as the !
type
is experimental.
§Examples
Move value into closure to return it, box the closure and send it:
use boxfnonce::SendBoxFnOnce;
use std::thread;
let s = String::from("foo");
let f : SendBoxFnOnce<(), String> = SendBoxFnOnce::from(|| {
println!("Got called: {}", s);
s
});
let result = thread::Builder::new().spawn(move || {
f.call()
}).unwrap().join().unwrap();
assert_eq!(result, "foo".to_string());
Implementations§
Source§impl<'a, Args, Result> SendBoxFnOnce<'a, Args, Result>
impl<'a, Args, Result> SendBoxFnOnce<'a, Args, Result>
Sourcepub fn call_tuple(self, args: Args) -> Result
pub fn call_tuple(self, args: Args) -> Result
call inner function, consumes the box.
call_tuple
can be used if the arguments are available as
tuple. Each usable instance of SendBoxFnOnce<(…), Result> has
a separate call
method for passing arguments “untupled”.