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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
//! Serializable and debuggable closures.
//!
//! <p style="font-family: 'Fira Sans',sans-serif;padding:0.3em 0"><strong>
//! <a href="https://crates.io/crates/serde_closure">📦&nbsp;&nbsp;Crates.io</a>&nbsp;&nbsp;│&nbsp;&nbsp;<a href="https://github.com/alecmocatta/serde_closure">📑&nbsp;&nbsp;GitHub</a>&nbsp;&nbsp;│&nbsp;&nbsp;<a href="https://constellation.zulipchat.com/#narrow/stream/213236-subprojects">💬&nbsp;&nbsp;Chat</a>
//! </strong></p>
//!
//! This library provides macros that wrap closures to make them serializable
//! and debuggable.
//!
//! ```
//! use serde_closure::{traits::Fn, Fn};
//!
//! let one = 1;
//! let plus_one = Fn!(|x: i32| x + one);
//!
//! assert_eq!(2, plus_one.call((1,))); // this works on stable and nightly
//! // assert_eq!(2, plus_one(1));      // this only works on nightly
//! println!("{:#?}", plus_one);
//!
//! // prints:
//! // Fn<main::{{closure}} at main.rs:6:15> {
//! //     one: 1,
//! //     source: "| x : i32 | x + one",
//! // }
//! ```
//!
//! This library aims to work in as simple and safe a way as possible. On stable
//! Rust the wrapped closures implement [`traits::FnOnce`], [`traits::FnMut`]
//! and [`traits::Fn`], and on nightly Rust [`std::ops::FnOnce`],
//! [`std::ops::FnMut`] and [`std::ops::Fn`] are implemented as well using the
//! `unboxed_closures` and `fn_traits` features (rust issue
//! [#29625](https://github.com/rust-lang/rust/issues/29625)).
//!
//!  * There are three macros, [`FnOnce`](macro@FnOnce), [`FnMut`](macro@FnMut)
//!    and [`Fn`](macro@Fn), corresponding to the three types of Rust closure.
//!  * Wrap your closure with one of the macros and it will now implement
//!    `Copy`, `Clone`, `PartialEq`, `Eq`, `Hash`, `PartialOrd`, `Ord`,
//!    `Serialize`, `Deserialize` and `Debug`.
//!  * There are some minor syntax limitations, which are documented below.
//!  * This crate has one unavoidable but documented and sound usage of
//!    `unsafe`.
//!
//! # Examples of wrapped closures
//! **Inferred, non-capturing closure:**
//! ```
//! # (
//! |a| a+1
//! # )(0i32);
//! ```
//! ```
//! # use serde_closure::{traits::FnMut, FnMut};
//! # (
//! FnMut!(|a| a+1)
//! # ).call_mut((0i32,));
//! ```
//!
//! **Annotated, non-capturing closure:**
//! ```
//! # (
//! |a: String| -> String { a.to_uppercase() }
//! # )(String::from("abc"));
//! ```
//! ```
//! # use serde_closure::{traits::FnMut, FnMut};
//! # (
//! FnMut!(|a: String| -> String { a.to_uppercase() })
//! # ).call_mut((String::from("abc"),));
//! ```
//!
//! **Inferred closure, capturing `num`:**
//! ```
//! let mut num = 0;
//! # (
//! |a| num += a
//! # )(1i32);
//! ```
//! ```
//! # use serde_closure::{traits::FnMut, FnMut};
//! let mut num = 0;
//! # (
//! FnMut!(|a| num += a)
//! # ).call_mut((1i32,));
//! ```
//!
//! **`move` closure, capturing `hello` and `world`:**
//! ```
//! let hello = String::from("hello");
//! let mut world = String::new();
//! # (
//! move |name| {
//!     world += (hello.to_uppercase() + name).as_str();
//! }
//! # )("abc");
//! ```
//! ```
//! # use serde_closure::{traits::FnMut, FnMut};
//! let hello = String::from("hello");
//! let mut world = String::new();
//! # (
//! FnMut!(move |name| {
//!     world += (hello.to_uppercase() + name).as_str();
//! })
//! # ).call_mut(("abc",));
//! ```
//!
//! # Limitations
//! There are currently some minor limitations:
//!
//!  * Use of types that start with a lowercase letter need might need to be
//!    disambiguated from variables. If you see an error like the following, fix
//!    the case of the type, or append it with `my_struct::<>` to disambiguate.
//! ```text
//! error[E0308]: mismatched types
//!    --> tests/test.rs:450:4
//!     |
//! 449 |       FnOnce!(move || {
//!     |  _____-
//! 450 | |         my_struct;
//!     | |         ^^^^^^^^^ expected struct `serde_closure::internal::a_variable`, found struct `my_struct`
//! 451 | |     });
//!     | |______- in this macro invocation
//!     |
//!     = note: expected type `serde_closure::internal::a_variable`
//!                found type `my_struct`
//! ```
//!
//!  * Use of variables that start with an uppercase letter might need to be
//!    disambiguated from types. If you see an error like the following, fix the
//!    case of the variable, or wrap it with `(MyVariable)` to disambiguate.
//! ```text
//! error: imports cannot refer to local variables
//!    --> tests/test.rs:422:3
//!     |
//! 417 |       FnOnce!(move || {
//!     |  _____-
//! 418 | |         MyVariable;
//!     | |         ^^^^^^^^^^
//! 419 | |     });
//!     | |______- in this macro invocation
//!     |
//! ```
//!
//!  * Functions and closures called inside the closure might need to be
//!    disambiguated. This can be done the same as above with `function::<>` for
//!    functions and `(closure)` for closures.
//!
//! # Serializing between processes
//!
//! Closures created by this crate are unnameable – i.e. just like normal
//! closures, there is no Rust syntax available with which to write the type.
//! What this means is that to deserialize a closure, you either need to specify
//! the precise type you're deserializing without naming it (which is possible
//! but not particularly practical), or *erase* the type by storing it in a
//! [trait object](https://doc.rust-lang.org/beta/book/ch17-02-trait-objects.html).
//!
//! The [`serde_traitobject`](https://github.com/alecmocatta/serde_traitobject)
//! crate enables trait objects to be safely serialized and sent between other
//! processes running the same binary.
//!
//! For example, if you have multiple forks of a process, or the same binary
//! running on each of a cluster of machines,
//! [`serde_traitobject`](https://github.com/alecmocatta/serde_traitobject)
//! would help you to send serializable closures between them. This can be done
//! by upcasting the closure to a `Box<dyn serde_traitobject::Fn()>`, which is
//! automatically serializable and deserializable with
//! [`serde`](https://github.com/serde-rs/serde).

#![doc(html_root_url = "https://docs.rs/serde_closure/0.3.1")]
#![cfg_attr(nightly, feature(unboxed_closures, fn_traits))]
#![warn(
	missing_copy_implementations,
	missing_debug_implementations,
	missing_docs,
	trivial_casts,
	trivial_numeric_casts,
	unused_import_braces,
	unused_qualifications,
	unused_results,
	clippy::pedantic
)] // from https://github.com/rust-unofficial/patterns/blob/master/anti_patterns/deny-warnings.md
#![allow(clippy::inline_always)]

/// Macro that wraps a closure, evaluating to a [`FnOnce`](structs::FnOnce)
/// struct that implements [`traits::FnOnce`] (and [`std::ops::FnOnce`] on
/// nightly), [`Debug`](std::fmt::Debug), [`Serialize`](serde::Serialize) and
/// [`Deserialize`](serde::Deserialize), and various convenience traits.
///
/// See the [readme](self) for examples.
pub use serde_closure_derive::FnOnce;

/// Macro that wraps a closure, evaluating to a [`FnMut`](structs::FnMut) struct
/// that implements [`traits::FnMut`] (and [`std::ops::FnMut`] on nightly),
/// [`Debug`](std::fmt::Debug), [`Serialize`](serde::Serialize) and
/// [`Deserialize`](serde::Deserialize), and various convenience traits.
///
/// See the [readme](self) for examples.
pub use serde_closure_derive::FnMut;

/// Macro that wraps a closure, evaluating to a [`Fn`](structs::Fn) struct that
/// implements [`traits::Fn`] (and [`std::ops::Fn`] on nightly),
/// [`Debug`](std::fmt::Debug), [`Serialize`](serde::Serialize) and
/// [`Deserialize`](serde::Deserialize), and various convenience traits.
///
/// See the [readme](self) for examples.
pub use serde_closure_derive::Fn;

/// Attribute macro that can be applied to items to desugar trait bounds
/// `FnOnce(…) -> …`, `FnMut(…) -> …` and `Fn(…) -> …` to `FnOnce<(…), Output = …>`,
/// `FnMut<(…), Output = …>` and `Fn<(…), Output = …>`. This is just a
/// convenience to enable parenthesized arguments for non `std::ops::*` traits
/// on stable Rust.
///
/// See `tests/stable.rs` for examples.
pub use serde_closure_derive::desugar;

#[doc(hidden)]
#[macro_export]
macro_rules! FnMutNamed {
	(pub type $name:ident<$($t:ident),*> = |$self:ident $(,$env:ident: $env_type:ty)*|$($arg:pat=> $arg_type:ty),*| -> $output:ty where $($bound_ty:ident : $bound_trait:tt),* $body:block) => (
		#[derive(::serde::Serialize, ::serde::Deserialize)]
		pub struct $name<$($t),*>
		where
			$($bound_ty: $bound_trait),*
		{
			$($env: $env_type,)*
			marker: ::core::marker::PhantomData<fn() -> ($($t,)*)>,
		}
		const _: () = {
			impl<$($t),*> $name<$($t),*>
			where
				$($bound_ty: $bound_trait),*
			{
				#[allow(clippy::new_without_default)]
				pub fn new($($env: $env_type),*) -> Self {
					Self {
						$($env: $env,)*
						marker: ::core::marker::PhantomData,
					}
				}
				fn run(&mut $self, ($($arg,)*): ($($arg_type,)*)) -> $output
					$body
			}
			impl<$($t),*> Clone for $name<$($t),*>
			where
				$($bound_ty: $bound_trait,)*
				$($env_type: Clone,)*
			{
				fn clone(&self) -> Self {
					Self {
						$($env: self.$env.clone(),)*
						marker: ::core::marker::PhantomData,
					}
				}
			}
			impl<$($t),*> ::serde_closure::traits::FnOnce<($($arg_type,)*)> for $name<$($t),*>
			where
				$($bound_ty: $bound_trait),*
			{
				type Output = $output;

				fn call_once(mut self, args: ($($arg_type,)*)) -> Self::Output {
					self.run(args)
				}
			}
			impl<$($t),*> ::serde_closure::traits::FnMut<($($arg_type,)*)> for $name<$($t),*>
			where
				$($bound_ty: $bound_trait),*
			{
				fn call_mut(&mut self, args: ($($arg_type,)*)) -> Self::Output {
					unsafe { $crate::internal::transmute(self.run(args)) }
				}
			}
		};
	)
}

#[doc(hidden)]
pub mod internal {
	pub use core;
	pub use serde;
	pub use std;

	use std::marker::PhantomData;

	pub trait FnOnce<Args> {
		type Output;

		fn call_once(self, args: Args) -> Self::Output;
	}
	pub trait FnMut<Args>: FnOnce<Args> {
		fn call_mut(&mut self, args: Args) -> Self::Output;
	}
	pub trait Fn<Args>: FnMut<Args> {
		fn call(&self, args: Args) -> Self::Output;
	}

	#[inline(always)]
	pub fn to_phantom<T>(_t: &T) -> PhantomData<fn(T)> {
		PhantomData
	}
	#[inline(always)]
	pub fn is_phantom<T>(_t: &T, _marker: PhantomData<fn(T)>) {}

	#[allow(missing_copy_implementations, missing_debug_implementations)]
	pub struct ZeroSizedAssertion;

	#[allow(
		missing_copy_implementations,
		missing_debug_implementations,
		non_camel_case_types
	)]
	pub struct a_variable;

	#[allow(clippy::missing_safety_doc)]
	pub unsafe fn transmute<T, U>(e: T) -> U {
		use std::mem::{self, align_of, size_of};
		assert_eq!(
			(size_of::<T>(), align_of::<T>()),
			(size_of::<U>(), align_of::<U>())
		);
		let ret = mem::transmute_copy(&e);
		mem::forget(e);
		ret
	}
}

pub mod traits {
	//! Supertraits of [`std::ops::FnOnce`], [`std::ops::FnMut`] and
	//! [`std::ops::Fn`] that are usable on stable Rust. They are implemented
	//! by closures created by the [`FnOnce`](macro@super::FnOnce),
	//! [`FnMut`](macro@super::FnMut) and [`Fn`](macro@super::Fn) macros.
	//!
	//! See the [readme](super) for examples.

	#![allow(non_snake_case)]

	use std::ops;

	/// Supertrait of [`std::ops::FnOnce`] that is usable on stable Rust. It is
	/// implemented by closures created by the [`FnOnce`](macro@super::FnOnce)
	/// macro.
	///
	/// See the [readme](super) for examples.
	pub trait FnOnce<Args> {
		/// The returned type after the call operator is used.
		type Output;

		/// Performs the call operation.
		fn call_once(self, args: Args) -> Self::Output;
	}

	/// Supertrait of [`std::ops::FnMut`] that is usable on stable Rust. It is
	/// implemented by closures created by the [`FnMut`](macro@super::FnMut)
	/// macro.
	///
	/// See the [readme](super) for examples.
	pub trait FnMut<Args>: FnOnce<Args> {
		/// Performs the call operation.
		fn call_mut(&mut self, args: Args) -> Self::Output;
	}

	/// Supertrait of [`std::ops::Fn`] that is usable on stable Rust. It is
	/// implemented by closures created by the [`Fn`](macro@super::Fn)
	/// macro.
	///
	/// See the [readme](super) for examples.
	pub trait Fn<Args>: FnMut<Args> {
		/// Performs the call operation.
		fn call(&self, args: Args) -> Self::Output;
	}

	/// A version of the [`FnOnce`] trait intended to be used for boxed trait
	/// objects to make them callable on stable Rust.
	///
	/// ```ignore
	/// let t: Box<dyn FnOnceBox(…) -> …> = …;
	/// let output = t.call_once_box((…,));
	/// ```
	pub trait FnOnceBox<A> {
		/// The returned type after the call operator is used.
		type Output;

		/// Performs the call operation on a `Box<dyn FnOnceBox(…) -> …>`.
		fn call_once_box(self: Box<Self>, args: A) -> Self::Output;
	}
	impl<A, F> FnOnceBox<A> for F
	where
		F: FnOnce<A>,
	{
		type Output = F::Output;

		fn call_once_box(self: Box<F>, args: A) -> F::Output {
			self.call_once(args)
		}
	}

	#[cfg(not(nightly))]
	macro_rules! fn_once {
		($($t:ident)*) => {
			impl<T, $($t,)* O> FnOnce<($($t,)*)> for T
			where
				T: ops::FnOnce($($t,)*) -> O,
			{
				type Output = O;

				fn call_once(self, ($($t,)*): ($($t,)*)) -> Self::Output {
					self($($t,)*)
				}
			}
			fn_once!(@recurse $($t)*);
		};
		(@recurse $first:ident $($t:ident)*) => {
			fn_once!($($t)*);
		};
		(@recurse) => {};
	}
	#[cfg(not(nightly))]
	fn_once!(A B C D E F G H I J K L);
	#[cfg(nightly)]
	impl<T, Args> FnOnce<Args> for T
	where
		T: ops::FnOnce<Args>,
	{
		type Output = T::Output;

		fn call_once(self, args: Args) -> Self::Output {
			self.call_once(args)
		}
	}

	#[cfg(not(nightly))]
	macro_rules! fn_mut {
		($($t:ident)*) => {
			impl<T, $($t,)* O> FnMut<($($t,)*)> for T
			where
				T: ops::FnMut($($t,)*) -> O,
			{
				fn call_mut(&mut self, ($($t,)*): ($($t,)*)) -> Self::Output {
					self($($t,)*)
				}
			}
			fn_mut!(@recurse $($t)*);
		};
		(@recurse $first:ident $($t:ident)*) => {
			fn_mut!($($t)*);
		};
		(@recurse) => {};
	}
	#[cfg(not(nightly))]
	fn_mut!(A B C D E F G H I J K L);
	#[cfg(nightly)]
	impl<T, Args> FnMut<Args> for T
	where
		T: ops::FnMut<Args>,
	{
		fn call_mut(&mut self, args: Args) -> Self::Output {
			self.call_mut(args)
		}
	}

	#[cfg(not(nightly))]
	macro_rules! fn_ref {
		($($t:ident)*) => {
			impl<T, $($t,)* O> Fn<($($t,)*)> for T
			where
				T: ops::Fn($($t,)*) -> O,
			{
				fn call(&self, ($($t,)*): ($($t,)*)) -> Self::Output {
					self($($t,)*)
				}
			}
			fn_ref!(@recurse $($t)*);
		};
		(@recurse $first:ident $($t:ident)*) => {
			fn_ref!($($t)*);
		};
		(@recurse) => {};
	}
	#[cfg(not(nightly))]
	fn_ref!(A B C D E F G H I J K L);
	#[cfg(nightly)]
	impl<T, Args> Fn<Args> for T
	where
		T: ops::Fn<Args>,
	{
		fn call(&self, args: Args) -> Self::Output {
			self.call(args)
		}
	}
}

pub mod structs {
	//! Structs representing a serializable closure, created by the
	//! [`FnOnce`](macro@super::FnOnce), [`FnMut`](macro@super::FnMut) and
	//! [`Fn`](macro@super::Fn) macros. They implement [`traits::FnOnce`](super::traits::FnOnce),
	//! [`traits::FnMut`](super::traits::FnMut) and [`traits::Fn`](super::traits::Fn)
	//! respectively (and [`std::ops::FnOnce`], [`std::ops::FnMut`] and [`std::ops::Fn`]
	//! on nightly), as well as [`Debug`](std::fmt::Debug), [`Serialize`](serde::Serialize)
	//! and [`Deserialize`](serde::Deserialize), and various convenience traits.
	//!
	//! See the [readme](super) for examples.

	use serde::{Deserialize, Serialize};
	use std::fmt::{self, Debug};

	use super::internal;

	/// A struct representing a serializable closure, created by the
	/// [`FnOnce`](macro@super::FnOnce) macro. Implements [`traits::FnOnce`](super::traits::FnOnce)
	/// (and [`std::ops::FnOnce`] on nightly), [`Debug`], [`Serialize`] and
	/// [`Deserialize`], and various convenience
	/// traits.
	///
	/// See the [readme](super) for examples.
	#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
	#[serde(transparent)]
	pub struct FnOnce<F> {
		f: F,
	}
	impl<F> FnOnce<F> {
		/// Internal method
		#[doc(hidden)]
		#[inline(always)]
		pub fn internal_new<I>(f: F) -> Self
		where
			F: internal::FnOnce<I>,
		{
			Self { f }
		}
	}

	#[cfg(not(nightly))]
	impl<F, I> super::traits::FnOnce<I> for FnOnce<F>
	where
		F: internal::FnOnce<I>,
	{
		type Output = F::Output;
		#[inline(always)]
		fn call_once(self, args: I) -> Self::Output {
			self.f.call_once(args)
		}
	}
	#[cfg(nightly)]
	impl<F, I> std::ops::FnOnce<I> for FnOnce<F>
	where
		F: internal::FnOnce<I>,
	{
		type Output = F::Output;
		#[inline(always)]
		extern "rust-call" fn call_once(self, args: I) -> Self::Output {
			self.f.call_once(args)
		}
	}

	impl<F> Debug for FnOnce<F>
	where
		F: Debug,
	{
		fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
			Debug::fmt(&self.f, f)
		}
	}

	/// A struct representing a serializable closure, created by the
	/// [`FnMut`](macro@super::FnMut) macro. Implements [`traits::FnMut`](super::traits::FnMut)
	/// (and [`std::ops::FnMut`] on nightly), [`Debug`], [`Serialize`] and
	/// [`Deserialize`], and various convenience
	/// traits.
	///
	/// See the [readme](super) for examples.
	#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
	#[serde(transparent)]
	pub struct FnMut<F> {
		f: F,
	}
	impl<F> FnMut<F> {
		/// Internal method
		#[doc(hidden)]
		#[inline(always)]
		pub fn internal_new<I>(f: F) -> Self
		where
			F: internal::FnMut<I>,
		{
			Self { f }
		}
	}

	#[cfg(not(nightly))]
	impl<F, I> super::traits::FnOnce<I> for FnMut<F>
	where
		F: internal::FnOnce<I>,
	{
		type Output = F::Output;
		#[inline(always)]
		fn call_once(self, args: I) -> Self::Output {
			self.f.call_once(args)
		}
	}
	#[cfg(nightly)]
	impl<F, I> std::ops::FnOnce<I> for FnMut<F>
	where
		F: internal::FnOnce<I>,
	{
		type Output = F::Output;
		#[inline(always)]
		extern "rust-call" fn call_once(self, args: I) -> Self::Output {
			self.f.call_once(args)
		}
	}

	#[cfg(not(nightly))]
	impl<F, I> super::traits::FnMut<I> for FnMut<F>
	where
		F: internal::FnMut<I>,
	{
		#[inline(always)]
		fn call_mut(&mut self, args: I) -> Self::Output {
			self.f.call_mut(args)
		}
	}
	#[cfg(nightly)]
	impl<F, I> std::ops::FnMut<I> for FnMut<F>
	where
		F: internal::FnMut<I>,
	{
		#[inline(always)]
		extern "rust-call" fn call_mut(&mut self, args: I) -> Self::Output {
			self.f.call_mut(args)
		}
	}

	impl<F> Debug for FnMut<F>
	where
		F: Debug,
	{
		fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
			Debug::fmt(&self.f, f)
		}
	}

	/// A struct representing a serializable closure, created by the
	/// [`Fn`](macro@super::Fn) macro. Implements [`traits::Fn`](super::traits::Fn)
	/// (and [`std::ops::Fn`] on nightly), [`Debug`], [`Serialize`] and
	/// [`Deserialize`], and various convenience traits.
	///
	/// See the [readme](super) for examples.
	#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
	#[serde(transparent)]
	pub struct Fn<F> {
		f: F,
	}
	impl<F> Fn<F> {
		/// Internal method
		#[doc(hidden)]
		#[inline(always)]
		pub fn internal_new<I>(f: F) -> Self
		where
			F: internal::Fn<I>,
		{
			Self { f }
		}
	}

	#[cfg(not(nightly))]
	impl<F, I> super::traits::FnOnce<I> for Fn<F>
	where
		F: internal::FnOnce<I>,
	{
		type Output = F::Output;
		#[inline(always)]
		fn call_once(self, args: I) -> Self::Output {
			self.f.call_once(args)
		}
	}
	#[cfg(nightly)]
	impl<F, I> std::ops::FnOnce<I> for Fn<F>
	where
		F: internal::FnOnce<I>,
	{
		type Output = F::Output;
		#[inline(always)]
		extern "rust-call" fn call_once(self, args: I) -> Self::Output {
			self.f.call_once(args)
		}
	}

	#[cfg(not(nightly))]
	impl<F, I> super::traits::FnMut<I> for Fn<F>
	where
		F: internal::FnMut<I>,
	{
		#[inline(always)]
		fn call_mut(&mut self, args: I) -> Self::Output {
			self.f.call_mut(args)
		}
	}
	#[cfg(nightly)]
	impl<F, I> std::ops::FnMut<I> for Fn<F>
	where
		F: internal::FnMut<I>,
	{
		#[inline(always)]
		extern "rust-call" fn call_mut(&mut self, args: I) -> Self::Output {
			self.f.call_mut(args)
		}
	}

	#[cfg(not(nightly))]
	impl<F, I> super::traits::Fn<I> for Fn<F>
	where
		F: internal::Fn<I>,
	{
		#[inline(always)]
		fn call(&self, args: I) -> Self::Output {
			self.f.call(args)
		}
	}
	#[cfg(nightly)]
	impl<F, I> std::ops::Fn<I> for Fn<F>
	where
		F: internal::Fn<I>,
	{
		#[inline(always)]
		extern "rust-call" fn call(&self, args: I) -> Self::Output {
			self.f.call(args)
		}
	}

	impl<F> Debug for Fn<F>
	where
		F: Debug,
	{
		fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
			Debug::fmt(&self.f, f)
		}
	}
}