1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
use traits::FnBox;

/// `BoxFnOnce` boxes any `FnOnce` 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, `BoxFnOnce` tries to provide a safe
/// implementation.
///
/// Instead of `Box<FnOnce(Args...) -> Result>` (or `Box<FnBox(Args...)
/// -> Result>`) the box type is `BoxFnOnce<(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 ommitted:
/// `BoxFnOnce<(Args...,)>`.
///
/// Internally it is implemented similar to `Box<FnBox()>`, but there is
/// no `FnOnce` implementation for `BoxFnOnce`.
///
/// You can build boxes for diverging functions too, but specifying the
/// type (like `BoxFnOnce<(), !>`) is not possible as the `!` type is
/// experimental.
///
/// If you need to send the FnOnce use `SendBoxFnOnce` instead.
///
/// # Examples
///
/// Move value into closure and box it:
///
/// ```
/// use boxfnonce::BoxFnOnce;
/// let s = String::from("foo");
/// let f : BoxFnOnce<()> = BoxFnOnce::from(|| {
///     println!("Got called: {}", s);
///     drop(s);
/// });
/// f.call();
/// ```
///
/// Move value into closure to return it, and box the closure:
///
/// ```
/// use boxfnonce::BoxFnOnce;
/// let s = String::from("foo");
/// let f : BoxFnOnce<(), String> = BoxFnOnce::from(|| {
///     println!("Got called: {}", s);
///     s
/// });
/// assert_eq!(f.call(), "foo".to_string());
/// ```
pub struct BoxFnOnce<Arguments, Result = ()> (Box<FnBox<Arguments, Result>>);

impl<Args, Result> BoxFnOnce<Args, Result> {
	/// call inner function, consumes the box.
	///
	/// `call_tuple` can be used if the arguments are available as
	/// tuple. Each usable instance of BoxFnOnce<(...), Result> has a
	/// separate `call` method for passing arguments "untupled".
	#[inline]
	pub fn call_tuple(self, args: Args) -> Result {
		self.0.call(args)
	}

	/// `BoxFnOnce::new` is an alias for `BoxFnOnce::from`.
	#[inline]
	pub fn new<F>(func: F) -> Self
	where Self: From<F>
	{
		Self::from(func)
	}
}

build_n_args!(BoxFnOnce[]: );
build_n_args!(BoxFnOnce[]: a1: A1);
build_n_args!(BoxFnOnce[]: a1: A1, a2: A2);
build_n_args!(BoxFnOnce[]: a1: A1, a2: A2, a3: A3);
build_n_args!(BoxFnOnce[]: a1: A1, a2: A2, a3: A3, a4: A4);
build_n_args!(BoxFnOnce[]: a1: A1, a2: A2, a3: A3, a4: A4, a5: A5);
build_n_args!(BoxFnOnce[]: a1: A1, a2: A2, a3: A3, a4: A4, a5: A5, a6: A6);
build_n_args!(BoxFnOnce[]: a1: A1, a2: A2, a3: A3, a4: A4, a5: A5, a6: A6, a7: A7);
build_n_args!(BoxFnOnce[]: a1: A1, a2: A2, a3: A3, a4: A4, a5: A5, a6: A6, a7: A7, a8: A8);
build_n_args!(BoxFnOnce[]: a1: A1, a2: A2, a3: A3, a4: A4, a5: A5, a6: A6, a7: A7, a8: A8, a9: A9);
build_n_args!(BoxFnOnce[]: a1: A1, a2: A2, a3: A3, a4: A4, a5: A5, a6: A6, a7: A7, a8: A8, a9: A9, a10: A10);

#[cfg(test)]
mod test {
	use super::BoxFnOnce;

	use std::rc::Rc;

	struct NoSendString(String, Rc<()>);
	impl NoSendString {
		fn into(self) -> String {
			self.0
		}
	}

	fn closure_string() -> NoSendString {
		NoSendString(String::from("abc"), Rc::new(()))
	}

	#[test]
	fn test_arg0() {
		let f = BoxFnOnce::from({
			let s = closure_string();
			move || -> String {
				s.into()
			}
		});
		assert_eq!(f.call(), "abc");
	}

	#[test]
	fn test_arg1() {
		let f = BoxFnOnce::from({
			let s = closure_string();
			move |_| -> String {
				s.into()
			}
		});
		assert_eq!(f.call(0), "abc");
	}

	#[test]
	fn test_arg1_fixed_argument_type() {
		let f : BoxFnOnce<(i32,), String> = BoxFnOnce::from({
			let s = closure_string();
			move |_| -> String {
				s.into()
			}
		});
		assert_eq!(f.call(0), "abc");
	}

	#[test]
	fn test_arg2() {
		let f = BoxFnOnce::from({
			let s = closure_string();
			move |_, _| -> String {
				s.into()
			}
		});
		assert_eq!(f.call(0, 0), "abc");
	}

	#[test]
	fn test_arg3() {
		let f = BoxFnOnce::from({
			let s = closure_string();
			move |_, _, _| -> String {
				s.into()
			}
		});
		assert_eq!(f.call(0, 0, 0), "abc");
	}

	#[test]
	fn test_arg4_void() {
		let f = BoxFnOnce::from({
			let s = closure_string();
			move |_, _, _, _| {
				drop(s);
			}
		});
		f.call(0, 0, 0, 0);
	}

	#[test]
	#[should_panic(expected = "inner diverging")]
	fn test_arg4_diverging() {
		let f = BoxFnOnce::from({
			let s = closure_string();
			move |_, _, _, _| -> ! {
				drop(s);
				panic!("inner diverging");
			}
		});
		f.call(0, 0, 0, 0);
	}
}