2017-08-08 07:34:37 -05:00
|
|
|
/// The version of the call operator that takes an immutable receiver.
|
2017-06-07 20:59:57 -05:00
|
|
|
///
|
2017-08-08 07:34:37 -05:00
|
|
|
/// Instances of `Fn` can be called repeatedly without mutating state.
|
2017-08-07 14:27:47 -05:00
|
|
|
///
|
2019-12-26 07:27:55 -06:00
|
|
|
/// *This trait (`Fn`) is not to be confused with [function pointers]
|
2017-08-08 07:34:37 -05:00
|
|
|
/// (`fn`).*
|
|
|
|
///
|
|
|
|
/// `Fn` is implemented automatically by closures which only take immutable
|
|
|
|
/// references to captured variables or don't capture anything at all, as well
|
2019-12-26 07:27:55 -06:00
|
|
|
/// as (safe) [function pointers] (with some caveats, see their documentation
|
2017-08-08 07:34:37 -05:00
|
|
|
/// for more details). Additionally, for any type `F` that implements `Fn`, `&F`
|
|
|
|
/// implements `Fn`, too.
|
|
|
|
///
|
|
|
|
/// Since both [`FnMut`] and [`FnOnce`] are supertraits of `Fn`, any
|
|
|
|
/// instance of `Fn` can be used as a parameter where a [`FnMut`] or [`FnOnce`]
|
|
|
|
/// is expected.
|
2017-06-07 20:59:57 -05:00
|
|
|
///
|
2017-08-08 07:34:37 -05:00
|
|
|
/// Use `Fn` as a bound when you want to accept a parameter of function-like
|
2018-11-26 20:59:49 -06:00
|
|
|
/// type and need to call it repeatedly and without mutating state (e.g., when
|
2017-08-08 07:34:37 -05:00
|
|
|
/// calling it concurrently). If you do not need such strict requirements, use
|
|
|
|
/// [`FnMut`] or [`FnOnce`] as bounds.
|
2017-06-07 20:59:57 -05:00
|
|
|
///
|
2017-08-07 14:27:47 -05:00
|
|
|
/// See the [chapter on closures in *The Rust Programming Language*][book] for
|
2017-08-08 07:34:37 -05:00
|
|
|
/// some more information on this topic.
|
2017-06-07 20:59:57 -05:00
|
|
|
///
|
2017-08-07 14:27:47 -05:00
|
|
|
/// Also of note is the special syntax for `Fn` traits (e.g.
|
|
|
|
/// `Fn(usize, bool) -> usize`). Those interested in the technical details of
|
2017-08-08 07:34:37 -05:00
|
|
|
/// this can refer to [the relevant section in the *Rustonomicon*][nomicon].
|
2017-06-07 20:59:57 -05:00
|
|
|
///
|
2019-01-01 11:53:07 -06:00
|
|
|
/// [book]: ../../book/ch13-01-closures.html
|
2017-08-08 07:34:37 -05:00
|
|
|
/// [function pointers]: ../../std/primitive.fn.html
|
2017-08-07 14:27:47 -05:00
|
|
|
/// [nomicon]: ../../nomicon/hrtb.html
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ## Calling a closure
|
2017-06-07 20:59:57 -05:00
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// let square = |x| x * x;
|
|
|
|
/// assert_eq!(square(5), 25);
|
|
|
|
/// ```
|
|
|
|
///
|
2017-08-07 14:27:47 -05:00
|
|
|
/// ## Using a `Fn` parameter
|
2017-06-07 20:59:57 -05:00
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// fn call_with_one<F>(func: F) -> usize
|
|
|
|
/// where F: Fn(usize) -> usize {
|
|
|
|
/// func(1)
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// let double = |x| x * 2;
|
|
|
|
/// assert_eq!(call_with_one(double), 2);
|
|
|
|
/// ```
|
|
|
|
#[lang = "fn"]
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
|
|
|
#[rustc_paren_sugar]
|
2018-08-12 14:21:53 -05:00
|
|
|
#[rustc_on_unimplemented(
|
2019-11-24 03:43:32 -06:00
|
|
|
on(
|
|
|
|
Args = "()",
|
2020-07-23 07:52:48 -05:00
|
|
|
note = "wrap the `{Self}` in a closure with no arguments: `|| {{ /* code */ }}`"
|
2019-11-24 03:43:32 -06:00
|
|
|
),
|
|
|
|
message = "expected a `{Fn}<{Args}>` closure, found `{Self}`",
|
|
|
|
label = "expected an `Fn<{Args}>` closure, found `{Self}`"
|
2018-08-12 14:21:53 -05:00
|
|
|
)]
|
2017-06-07 20:59:57 -05:00
|
|
|
#[fundamental] // so that regex can rely that `&str: !FnMut`
|
2019-02-07 04:44:06 -06:00
|
|
|
#[must_use = "closures are lazy and do nothing unless called"]
|
2019-11-24 03:43:32 -06:00
|
|
|
pub trait Fn<Args>: FnMut<Args> {
|
2017-08-07 14:27:47 -05:00
|
|
|
/// Performs the call operation.
|
2017-06-07 20:59:57 -05:00
|
|
|
#[unstable(feature = "fn_traits", issue = "29625")]
|
|
|
|
extern "rust-call" fn call(&self, args: Args) -> Self::Output;
|
|
|
|
}
|
|
|
|
|
2017-08-08 07:34:37 -05:00
|
|
|
/// The version of the call operator that takes a mutable receiver.
|
|
|
|
///
|
|
|
|
/// Instances of `FnMut` can be called repeatedly and may mutate state.
|
2017-06-07 20:59:57 -05:00
|
|
|
///
|
2017-08-08 07:34:37 -05:00
|
|
|
/// `FnMut` is implemented automatically by closures which take mutable
|
|
|
|
/// references to captured variables, as well as all types that implement
|
2019-12-26 07:27:55 -06:00
|
|
|
/// [`Fn`], e.g., (safe) [function pointers] (since `FnMut` is a supertrait of
|
2017-08-08 07:34:37 -05:00
|
|
|
/// [`Fn`]). Additionally, for any type `F` that implements `FnMut`, `&mut F`
|
|
|
|
/// implements `FnMut`, too.
|
2017-08-07 14:27:47 -05:00
|
|
|
///
|
2017-08-08 07:34:37 -05:00
|
|
|
/// Since [`FnOnce`] is a supertrait of `FnMut`, any instance of `FnMut` can be
|
|
|
|
/// used where a [`FnOnce`] is expected, and since [`Fn`] is a subtrait of
|
|
|
|
/// `FnMut`, any instance of [`Fn`] can be used where `FnMut` is expected.
|
|
|
|
///
|
|
|
|
/// Use `FnMut` as a bound when you want to accept a parameter of function-like
|
|
|
|
/// type and need to call it repeatedly, while allowing it to mutate state.
|
|
|
|
/// If you don't want the parameter to mutate state, use [`Fn`] as a
|
|
|
|
/// bound; if you don't need to call it repeatedly, use [`FnOnce`].
|
2017-08-07 14:27:47 -05:00
|
|
|
///
|
|
|
|
/// See the [chapter on closures in *The Rust Programming Language*][book] for
|
2017-08-08 07:34:37 -05:00
|
|
|
/// some more information on this topic.
|
2017-08-07 14:27:47 -05:00
|
|
|
///
|
|
|
|
/// Also of note is the special syntax for `Fn` traits (e.g.
|
|
|
|
/// `Fn(usize, bool) -> usize`). Those interested in the technical details of
|
2017-08-08 07:34:37 -05:00
|
|
|
/// this can refer to [the relevant section in the *Rustonomicon*][nomicon].
|
2017-08-07 14:27:47 -05:00
|
|
|
///
|
2019-01-01 11:53:07 -06:00
|
|
|
/// [book]: ../../book/ch13-01-closures.html
|
2017-08-08 07:34:37 -05:00
|
|
|
/// [function pointers]: ../../std/primitive.fn.html
|
2017-08-07 14:27:47 -05:00
|
|
|
/// [nomicon]: ../../nomicon/hrtb.html
|
2017-06-07 20:59:57 -05:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
2017-08-07 14:27:47 -05:00
|
|
|
/// ## Calling a mutably capturing closure
|
2017-06-07 20:59:57 -05:00
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// let mut x = 5;
|
|
|
|
/// {
|
|
|
|
/// let mut square_x = || x *= x;
|
|
|
|
/// square_x();
|
|
|
|
/// }
|
|
|
|
/// assert_eq!(x, 25);
|
|
|
|
/// ```
|
|
|
|
///
|
2017-08-07 14:27:47 -05:00
|
|
|
/// ## Using a `FnMut` parameter
|
2017-06-07 20:59:57 -05:00
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// fn do_twice<F>(mut func: F)
|
|
|
|
/// where F: FnMut()
|
|
|
|
/// {
|
|
|
|
/// func();
|
|
|
|
/// func();
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// let mut x: usize = 1;
|
|
|
|
/// {
|
|
|
|
/// let add_two_to_x = || x += 2;
|
|
|
|
/// do_twice(add_two_to_x);
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// assert_eq!(x, 5);
|
|
|
|
/// ```
|
|
|
|
#[lang = "fn_mut"]
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
|
|
|
#[rustc_paren_sugar]
|
2018-08-12 14:21:53 -05:00
|
|
|
#[rustc_on_unimplemented(
|
2019-11-24 03:43:32 -06:00
|
|
|
on(
|
|
|
|
Args = "()",
|
2020-07-23 07:52:48 -05:00
|
|
|
note = "wrap the `{Self}` in a closure with no arguments: `|| {{ /* code */ }}`"
|
2019-11-24 03:43:32 -06:00
|
|
|
),
|
|
|
|
message = "expected a `{FnMut}<{Args}>` closure, found `{Self}`",
|
|
|
|
label = "expected an `FnMut<{Args}>` closure, found `{Self}`"
|
2018-08-12 14:21:53 -05:00
|
|
|
)]
|
2017-06-07 20:59:57 -05:00
|
|
|
#[fundamental] // so that regex can rely that `&str: !FnMut`
|
2019-02-07 04:44:06 -06:00
|
|
|
#[must_use = "closures are lazy and do nothing unless called"]
|
2019-11-24 03:43:32 -06:00
|
|
|
pub trait FnMut<Args>: FnOnce<Args> {
|
2017-08-07 14:27:47 -05:00
|
|
|
/// Performs the call operation.
|
2017-06-07 20:59:57 -05:00
|
|
|
#[unstable(feature = "fn_traits", issue = "29625")]
|
|
|
|
extern "rust-call" fn call_mut(&mut self, args: Args) -> Self::Output;
|
|
|
|
}
|
|
|
|
|
2017-08-08 07:34:37 -05:00
|
|
|
/// The version of the call operator that takes a by-value receiver.
|
|
|
|
///
|
|
|
|
/// Instances of `FnOnce` can be called, but might not be callable multiple
|
|
|
|
/// times. Because of this, if the only thing known about a type is that it
|
|
|
|
/// implements `FnOnce`, it can only be called once.
|
|
|
|
///
|
2020-07-26 19:05:49 -05:00
|
|
|
/// `FnOnce` is implemented automatically by closures that might consume captured
|
2018-11-26 20:59:49 -06:00
|
|
|
/// variables, as well as all types that implement [`FnMut`], e.g., (safe)
|
2019-12-26 07:27:55 -06:00
|
|
|
/// [function pointers] (since `FnOnce` is a supertrait of [`FnMut`]).
|
2017-06-07 20:59:57 -05:00
|
|
|
///
|
2017-08-08 07:34:37 -05:00
|
|
|
/// Since both [`Fn`] and [`FnMut`] are subtraits of `FnOnce`, any instance of
|
|
|
|
/// [`Fn`] or [`FnMut`] can be used where a `FnOnce` is expected.
|
2017-08-07 14:27:47 -05:00
|
|
|
///
|
2017-08-08 07:34:37 -05:00
|
|
|
/// Use `FnOnce` as a bound when you want to accept a parameter of function-like
|
|
|
|
/// type and only need to call it once. If you need to call the parameter
|
|
|
|
/// repeatedly, use [`FnMut`] as a bound; if you also need it to not mutate
|
|
|
|
/// state, use [`Fn`].
|
2017-08-07 14:27:47 -05:00
|
|
|
///
|
|
|
|
/// See the [chapter on closures in *The Rust Programming Language*][book] for
|
2017-08-08 07:34:37 -05:00
|
|
|
/// some more information on this topic.
|
2017-08-07 14:27:47 -05:00
|
|
|
///
|
|
|
|
/// Also of note is the special syntax for `Fn` traits (e.g.
|
|
|
|
/// `Fn(usize, bool) -> usize`). Those interested in the technical details of
|
2017-08-08 07:34:37 -05:00
|
|
|
/// this can refer to [the relevant section in the *Rustonomicon*][nomicon].
|
2017-08-07 14:27:47 -05:00
|
|
|
///
|
2019-01-01 11:53:07 -06:00
|
|
|
/// [book]: ../../book/ch13-01-closures.html
|
2017-08-08 07:34:37 -05:00
|
|
|
/// [function pointers]: ../../std/primitive.fn.html
|
2017-08-07 14:27:47 -05:00
|
|
|
/// [nomicon]: ../../nomicon/hrtb.html
|
2017-06-07 20:59:57 -05:00
|
|
|
///
|
|
|
|
/// # Examples
|
2019-08-24 06:38:09 -05:00
|
|
|
///
|
2017-08-07 14:27:47 -05:00
|
|
|
/// ## Using a `FnOnce` parameter
|
2017-06-07 20:59:57 -05:00
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// fn consume_with_relish<F>(func: F)
|
|
|
|
/// where F: FnOnce() -> String
|
|
|
|
/// {
|
|
|
|
/// // `func` consumes its captured variables, so it cannot be run more
|
2017-08-07 14:27:47 -05:00
|
|
|
/// // than once.
|
2017-06-07 20:59:57 -05:00
|
|
|
/// println!("Consumed: {}", func());
|
|
|
|
///
|
|
|
|
/// println!("Delicious!");
|
|
|
|
///
|
|
|
|
/// // Attempting to invoke `func()` again will throw a `use of moved
|
2017-08-07 14:27:47 -05:00
|
|
|
/// // value` error for `func`.
|
2017-06-07 20:59:57 -05:00
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// let x = String::from("x");
|
|
|
|
/// let consume_and_return_x = move || x;
|
|
|
|
/// consume_with_relish(consume_and_return_x);
|
|
|
|
///
|
|
|
|
/// // `consume_and_return_x` can no longer be invoked at this point
|
|
|
|
/// ```
|
|
|
|
#[lang = "fn_once"]
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
|
|
|
#[rustc_paren_sugar]
|
2018-08-12 14:21:53 -05:00
|
|
|
#[rustc_on_unimplemented(
|
2019-11-24 03:43:32 -06:00
|
|
|
on(
|
|
|
|
Args = "()",
|
2020-07-23 07:52:48 -05:00
|
|
|
note = "wrap the `{Self}` in a closure with no arguments: `|| {{ /* code */ }}`"
|
2019-11-24 03:43:32 -06:00
|
|
|
),
|
|
|
|
message = "expected a `{FnOnce}<{Args}>` closure, found `{Self}`",
|
|
|
|
label = "expected an `FnOnce<{Args}>` closure, found `{Self}`"
|
2018-08-12 14:21:53 -05:00
|
|
|
)]
|
2017-06-07 20:59:57 -05:00
|
|
|
#[fundamental] // so that regex can rely that `&str: !FnMut`
|
2019-02-07 04:44:06 -06:00
|
|
|
#[must_use = "closures are lazy and do nothing unless called"]
|
2017-06-07 20:59:57 -05:00
|
|
|
pub trait FnOnce<Args> {
|
|
|
|
/// The returned type after the call operator is used.
|
2020-07-16 08:12:59 -05:00
|
|
|
#[lang = "fn_once_output"]
|
2017-06-07 20:59:57 -05:00
|
|
|
#[stable(feature = "fn_once_output", since = "1.12.0")]
|
|
|
|
type Output;
|
|
|
|
|
2017-08-07 14:27:47 -05:00
|
|
|
/// Performs the call operation.
|
2017-06-07 20:59:57 -05:00
|
|
|
#[unstable(feature = "fn_traits", issue = "29625")]
|
|
|
|
extern "rust-call" fn call_once(self, args: Args) -> Self::Output;
|
|
|
|
}
|
|
|
|
|
|
|
|
mod impls {
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2019-11-24 03:43:32 -06:00
|
|
|
impl<A, F: ?Sized> Fn<A> for &F
|
|
|
|
where
|
|
|
|
F: Fn<A>,
|
2017-06-07 20:59:57 -05:00
|
|
|
{
|
|
|
|
extern "rust-call" fn call(&self, args: A) -> F::Output {
|
|
|
|
(**self).call(args)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2019-11-24 03:43:32 -06:00
|
|
|
impl<A, F: ?Sized> FnMut<A> for &F
|
|
|
|
where
|
|
|
|
F: Fn<A>,
|
2017-06-07 20:59:57 -05:00
|
|
|
{
|
|
|
|
extern "rust-call" fn call_mut(&mut self, args: A) -> F::Output {
|
|
|
|
(**self).call(args)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2019-11-24 03:43:32 -06:00
|
|
|
impl<A, F: ?Sized> FnOnce<A> for &F
|
|
|
|
where
|
|
|
|
F: Fn<A>,
|
2017-06-07 20:59:57 -05:00
|
|
|
{
|
|
|
|
type Output = F::Output;
|
|
|
|
|
|
|
|
extern "rust-call" fn call_once(self, args: A) -> F::Output {
|
|
|
|
(*self).call(args)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2019-11-24 03:43:32 -06:00
|
|
|
impl<A, F: ?Sized> FnMut<A> for &mut F
|
|
|
|
where
|
|
|
|
F: FnMut<A>,
|
2017-06-07 20:59:57 -05:00
|
|
|
{
|
|
|
|
extern "rust-call" fn call_mut(&mut self, args: A) -> F::Output {
|
|
|
|
(*self).call_mut(args)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2019-11-24 03:43:32 -06:00
|
|
|
impl<A, F: ?Sized> FnOnce<A> for &mut F
|
|
|
|
where
|
|
|
|
F: FnMut<A>,
|
2017-06-07 20:59:57 -05:00
|
|
|
{
|
|
|
|
type Output = F::Output;
|
2017-08-01 07:03:03 -05:00
|
|
|
extern "rust-call" fn call_once(self, args: A) -> F::Output {
|
2017-06-07 20:59:57 -05:00
|
|
|
(*self).call_mut(args)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|