boxfnonce

Struct SendBoxFnOnce

Source
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>

Source

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”.

Source

pub fn new<F>(func: F) -> Self
where Self: From<F>,

SendBoxFnOnce::new is an alias for SendBoxFnOnce::from.

Source§

impl<'a, Result> SendBoxFnOnce<'a, (), Result>

Source

pub fn call(self) -> Result

call inner function, consumes the box.

Source§

impl<'a, A1, Result> SendBoxFnOnce<'a, (A1,), Result>

Source

pub fn call(self, a1: A1) -> Result

call inner function, consumes the box.

Source§

impl<'a, A1, A2, Result> SendBoxFnOnce<'a, (A1, A2), Result>

Source

pub fn call(self, a1: A1, a2: A2) -> Result

call inner function, consumes the box.

Source§

impl<'a, A1, A2, A3, Result> SendBoxFnOnce<'a, (A1, A2, A3), Result>

Source

pub fn call(self, a1: A1, a2: A2, a3: A3) -> Result

call inner function, consumes the box.

Source§

impl<'a, A1, A2, A3, A4, Result> SendBoxFnOnce<'a, (A1, A2, A3, A4), Result>

Source

pub fn call(self, a1: A1, a2: A2, a3: A3, a4: A4) -> Result

call inner function, consumes the box.

Source§

impl<'a, A1, A2, A3, A4, A5, Result> SendBoxFnOnce<'a, (A1, A2, A3, A4, A5), Result>

Source

pub fn call(self, a1: A1, a2: A2, a3: A3, a4: A4, a5: A5) -> Result

call inner function, consumes the box.

Source§

impl<'a, A1, A2, A3, A4, A5, A6, Result> SendBoxFnOnce<'a, (A1, A2, A3, A4, A5, A6), Result>

Source

pub fn call(self, a1: A1, a2: A2, a3: A3, a4: A4, a5: A5, a6: A6) -> Result

call inner function, consumes the box.

Source§

impl<'a, A1, A2, A3, A4, A5, A6, A7, Result> SendBoxFnOnce<'a, (A1, A2, A3, A4, A5, A6, A7), Result>

Source

pub fn call( self, a1: A1, a2: A2, a3: A3, a4: A4, a5: A5, a6: A6, a7: A7, ) -> Result

call inner function, consumes the box.

Source§

impl<'a, A1, A2, A3, A4, A5, A6, A7, A8, Result> SendBoxFnOnce<'a, (A1, A2, A3, A4, A5, A6, A7, A8), Result>

Source

pub fn call( self, a1: A1, a2: A2, a3: A3, a4: A4, a5: A5, a6: A6, a7: A7, a8: A8, ) -> Result

call inner function, consumes the box.

Source§

impl<'a, A1, A2, A3, A4, A5, A6, A7, A8, A9, Result> SendBoxFnOnce<'a, (A1, A2, A3, A4, A5, A6, A7, A8, A9), Result>

Source

pub fn call( self, a1: A1, a2: A2, a3: A3, a4: A4, a5: A5, a6: A6, a7: A7, a8: A8, a9: A9, ) -> Result

call inner function, consumes the box.

Source§

impl<'a, A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, Result> SendBoxFnOnce<'a, (A1, A2, A3, A4, A5, A6, A7, A8, A9, A10), Result>

Source

pub fn call( self, a1: A1, a2: A2, a3: A3, a4: A4, a5: A5, a6: A6, a7: A7, a8: A8, a9: A9, a10: A10, ) -> Result

call inner function, consumes the box.

Trait Implementations§

Source§

impl<'a, Result, F: 'a + FnOnce() -> Result + Send> From<F> for SendBoxFnOnce<'a, (), Result>

Source§

fn from(func: F) -> Self

Converts to this type from the input type.
Source§

impl<'a, A1, Result, F: 'a + FnOnce(A1) -> Result + Send> From<F> for SendBoxFnOnce<'a, (A1,), Result>

Source§

fn from(func: F) -> Self

Converts to this type from the input type.
Source§

impl<'a, A1, A2, Result, F: 'a + FnOnce(A1, A2) -> Result + Send> From<F> for SendBoxFnOnce<'a, (A1, A2), Result>

Source§

fn from(func: F) -> Self

Converts to this type from the input type.
Source§

impl<'a, A1, A2, A3, Result, F: 'a + FnOnce(A1, A2, A3) -> Result + Send> From<F> for SendBoxFnOnce<'a, (A1, A2, A3), Result>

Source§

fn from(func: F) -> Self

Converts to this type from the input type.
Source§

impl<'a, A1, A2, A3, A4, Result, F: 'a + FnOnce(A1, A2, A3, A4) -> Result + Send> From<F> for SendBoxFnOnce<'a, (A1, A2, A3, A4), Result>

Source§

fn from(func: F) -> Self

Converts to this type from the input type.
Source§

impl<'a, A1, A2, A3, A4, A5, Result, F: 'a + FnOnce(A1, A2, A3, A4, A5) -> Result + Send> From<F> for SendBoxFnOnce<'a, (A1, A2, A3, A4, A5), Result>

Source§

fn from(func: F) -> Self

Converts to this type from the input type.
Source§

impl<'a, A1, A2, A3, A4, A5, A6, Result, F: 'a + FnOnce(A1, A2, A3, A4, A5, A6) -> Result + Send> From<F> for SendBoxFnOnce<'a, (A1, A2, A3, A4, A5, A6), Result>

Source§

fn from(func: F) -> Self

Converts to this type from the input type.
Source§

impl<'a, A1, A2, A3, A4, A5, A6, A7, Result, F: 'a + FnOnce(A1, A2, A3, A4, A5, A6, A7) -> Result + Send> From<F> for SendBoxFnOnce<'a, (A1, A2, A3, A4, A5, A6, A7), Result>

Source§

fn from(func: F) -> Self

Converts to this type from the input type.
Source§

impl<'a, A1, A2, A3, A4, A5, A6, A7, A8, Result, F: 'a + FnOnce(A1, A2, A3, A4, A5, A6, A7, A8) -> Result + Send> From<F> for SendBoxFnOnce<'a, (A1, A2, A3, A4, A5, A6, A7, A8), Result>

Source§

fn from(func: F) -> Self

Converts to this type from the input type.
Source§

impl<'a, A1, A2, A3, A4, A5, A6, A7, A8, A9, Result, F: 'a + FnOnce(A1, A2, A3, A4, A5, A6, A7, A8, A9) -> Result + Send> From<F> for SendBoxFnOnce<'a, (A1, A2, A3, A4, A5, A6, A7, A8, A9), Result>

Source§

fn from(func: F) -> Self

Converts to this type from the input type.
Source§

impl<'a, A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, Result, F: 'a + FnOnce(A1, A2, A3, A4, A5, A6, A7, A8, A9, A10) -> Result + Send> From<F> for SendBoxFnOnce<'a, (A1, A2, A3, A4, A5, A6, A7, A8, A9, A10), Result>

Source§

fn from(func: F) -> Self

Converts to this type from the input type.
Source§

impl<'a, Arguments, Result> From<SendBoxFnOnce<'a, Arguments, Result>> for BoxFnOnce<'a, Arguments, Result>

Source§

fn from(value: SendBoxFnOnce<'a, Arguments, Result>) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

§

impl<'a, Arguments, Result> Freeze for SendBoxFnOnce<'a, Arguments, Result>

§

impl<'a, Arguments, Result = ()> !RefUnwindSafe for SendBoxFnOnce<'a, Arguments, Result>

§

impl<'a, Arguments, Result> Send for SendBoxFnOnce<'a, Arguments, Result>

§

impl<'a, Arguments, Result = ()> !Sync for SendBoxFnOnce<'a, Arguments, Result>

§

impl<'a, Arguments, Result> Unpin for SendBoxFnOnce<'a, Arguments, Result>

§

impl<'a, Arguments, Result = ()> !UnwindSafe for SendBoxFnOnce<'a, Arguments, Result>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.