Move Fn to module.
This commit is contained in:
parent
cee5a2dd20
commit
b9c8e99955
4 changed files with 206 additions and 193 deletions
194
src/libcore/ops/function.rs
Normal file
194
src/libcore/ops/function.rs
Normal file
|
@ -0,0 +1,194 @@
|
|||
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
/// A version of the call operator that takes an immutable receiver.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// Closures automatically implement this trait, which allows them to be
|
||||
/// invoked. Note, however, that `Fn` takes an immutable reference to any
|
||||
/// captured variables. To take a mutable capture, implement [`FnMut`], and to
|
||||
/// consume the capture, implement [`FnOnce`].
|
||||
///
|
||||
/// [`FnMut`]: trait.FnMut.html
|
||||
/// [`FnOnce`]: trait.FnOnce.html
|
||||
///
|
||||
/// ```
|
||||
/// let square = |x| x * x;
|
||||
/// assert_eq!(square(5), 25);
|
||||
/// ```
|
||||
///
|
||||
/// Closures can also be passed to higher-level functions through a `Fn`
|
||||
/// parameter (or a `FnMut` or `FnOnce` parameter, which are supertraits of
|
||||
/// `Fn`).
|
||||
///
|
||||
/// ```
|
||||
/// 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]
|
||||
#[fundamental] // so that regex can rely that `&str: !FnMut`
|
||||
pub trait Fn<Args> : FnMut<Args> {
|
||||
/// This is called when the call operator is used.
|
||||
#[unstable(feature = "fn_traits", issue = "29625")]
|
||||
extern "rust-call" fn call(&self, args: Args) -> Self::Output;
|
||||
}
|
||||
|
||||
/// A version of the call operator that takes a mutable receiver.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// Closures that mutably capture variables automatically implement this trait,
|
||||
/// which allows them to be invoked.
|
||||
///
|
||||
/// ```
|
||||
/// let mut x = 5;
|
||||
/// {
|
||||
/// let mut square_x = || x *= x;
|
||||
/// square_x();
|
||||
/// }
|
||||
/// assert_eq!(x, 25);
|
||||
/// ```
|
||||
///
|
||||
/// Closures can also be passed to higher-level functions through a `FnMut`
|
||||
/// parameter (or a `FnOnce` parameter, which is a supertrait of `FnMut`).
|
||||
///
|
||||
/// ```
|
||||
/// 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]
|
||||
#[fundamental] // so that regex can rely that `&str: !FnMut`
|
||||
pub trait FnMut<Args> : FnOnce<Args> {
|
||||
/// This is called when the call operator is used.
|
||||
#[unstable(feature = "fn_traits", issue = "29625")]
|
||||
extern "rust-call" fn call_mut(&mut self, args: Args) -> Self::Output;
|
||||
}
|
||||
|
||||
/// A version of the call operator that takes a by-value receiver.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// By-value closures automatically implement this trait, which allows them to
|
||||
/// be invoked.
|
||||
///
|
||||
/// ```
|
||||
/// let x = 5;
|
||||
/// let square_x = move || x * x;
|
||||
/// assert_eq!(square_x(), 25);
|
||||
/// ```
|
||||
///
|
||||
/// By-value Closures can also be passed to higher-level functions through a
|
||||
/// `FnOnce` parameter.
|
||||
///
|
||||
/// ```
|
||||
/// fn consume_with_relish<F>(func: F)
|
||||
/// where F: FnOnce() -> String
|
||||
/// {
|
||||
/// // `func` consumes its captured variables, so it cannot be run more
|
||||
/// // than once
|
||||
/// println!("Consumed: {}", func());
|
||||
///
|
||||
/// println!("Delicious!");
|
||||
///
|
||||
/// // Attempting to invoke `func()` again will throw a `use of moved
|
||||
/// // value` error for `func`
|
||||
/// }
|
||||
///
|
||||
/// 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]
|
||||
#[fundamental] // so that regex can rely that `&str: !FnMut`
|
||||
pub trait FnOnce<Args> {
|
||||
/// The returned type after the call operator is used.
|
||||
#[stable(feature = "fn_once_output", since = "1.12.0")]
|
||||
type Output;
|
||||
|
||||
/// This is called when the call operator is used.
|
||||
#[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")]
|
||||
impl<'a,A,F:?Sized> Fn<A> for &'a F
|
||||
where F : Fn<A>
|
||||
{
|
||||
extern "rust-call" fn call(&self, args: A) -> F::Output {
|
||||
(**self).call(args)
|
||||
}
|
||||
}
|
||||
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
impl<'a,A,F:?Sized> FnMut<A> for &'a F
|
||||
where F : Fn<A>
|
||||
{
|
||||
extern "rust-call" fn call_mut(&mut self, args: A) -> F::Output {
|
||||
(**self).call(args)
|
||||
}
|
||||
}
|
||||
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
impl<'a,A,F:?Sized> FnOnce<A> for &'a F
|
||||
where F : Fn<A>
|
||||
{
|
||||
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")]
|
||||
impl<'a,A,F:?Sized> FnMut<A> for &'a mut F
|
||||
where F : FnMut<A>
|
||||
{
|
||||
extern "rust-call" fn call_mut(&mut self, args: A) -> F::Output {
|
||||
(*self).call_mut(args)
|
||||
}
|
||||
}
|
||||
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
impl<'a,A,F:?Sized> FnOnce<A> for &'a mut F
|
||||
where F : FnMut<A>
|
||||
{
|
||||
type Output = F::Output;
|
||||
extern "rust-call" fn call_once(mut self, args: A) -> F::Output {
|
||||
(*self).call_mut(args)
|
||||
}
|
||||
}
|
||||
}
|
|
@ -147,8 +147,12 @@
|
|||
|
||||
#![stable(feature = "rust1", since = "1.0.0")]
|
||||
|
||||
mod function;
|
||||
mod range;
|
||||
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
pub use self::function::{Fn, FnMut, FnOnce};
|
||||
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
pub use self::range::{Range, RangeFrom, RangeFull, RangeTo};
|
||||
|
||||
|
@ -2200,191 +2204,6 @@ impl<'a, T: ?Sized> DerefMut for &'a mut T {
|
|||
fn deref_mut(&mut self) -> &mut T { *self }
|
||||
}
|
||||
|
||||
/// A version of the call operator that takes an immutable receiver.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// Closures automatically implement this trait, which allows them to be
|
||||
/// invoked. Note, however, that `Fn` takes an immutable reference to any
|
||||
/// captured variables. To take a mutable capture, implement [`FnMut`], and to
|
||||
/// consume the capture, implement [`FnOnce`].
|
||||
///
|
||||
/// [`FnMut`]: trait.FnMut.html
|
||||
/// [`FnOnce`]: trait.FnOnce.html
|
||||
///
|
||||
/// ```
|
||||
/// let square = |x| x * x;
|
||||
/// assert_eq!(square(5), 25);
|
||||
/// ```
|
||||
///
|
||||
/// Closures can also be passed to higher-level functions through a `Fn`
|
||||
/// parameter (or a `FnMut` or `FnOnce` parameter, which are supertraits of
|
||||
/// `Fn`).
|
||||
///
|
||||
/// ```
|
||||
/// 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]
|
||||
#[fundamental] // so that regex can rely that `&str: !FnMut`
|
||||
pub trait Fn<Args> : FnMut<Args> {
|
||||
/// This is called when the call operator is used.
|
||||
#[unstable(feature = "fn_traits", issue = "29625")]
|
||||
extern "rust-call" fn call(&self, args: Args) -> Self::Output;
|
||||
}
|
||||
|
||||
/// A version of the call operator that takes a mutable receiver.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// Closures that mutably capture variables automatically implement this trait,
|
||||
/// which allows them to be invoked.
|
||||
///
|
||||
/// ```
|
||||
/// let mut x = 5;
|
||||
/// {
|
||||
/// let mut square_x = || x *= x;
|
||||
/// square_x();
|
||||
/// }
|
||||
/// assert_eq!(x, 25);
|
||||
/// ```
|
||||
///
|
||||
/// Closures can also be passed to higher-level functions through a `FnMut`
|
||||
/// parameter (or a `FnOnce` parameter, which is a supertrait of `FnMut`).
|
||||
///
|
||||
/// ```
|
||||
/// 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]
|
||||
#[fundamental] // so that regex can rely that `&str: !FnMut`
|
||||
pub trait FnMut<Args> : FnOnce<Args> {
|
||||
/// This is called when the call operator is used.
|
||||
#[unstable(feature = "fn_traits", issue = "29625")]
|
||||
extern "rust-call" fn call_mut(&mut self, args: Args) -> Self::Output;
|
||||
}
|
||||
|
||||
/// A version of the call operator that takes a by-value receiver.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// By-value closures automatically implement this trait, which allows them to
|
||||
/// be invoked.
|
||||
///
|
||||
/// ```
|
||||
/// let x = 5;
|
||||
/// let square_x = move || x * x;
|
||||
/// assert_eq!(square_x(), 25);
|
||||
/// ```
|
||||
///
|
||||
/// By-value Closures can also be passed to higher-level functions through a
|
||||
/// `FnOnce` parameter.
|
||||
///
|
||||
/// ```
|
||||
/// fn consume_with_relish<F>(func: F)
|
||||
/// where F: FnOnce() -> String
|
||||
/// {
|
||||
/// // `func` consumes its captured variables, so it cannot be run more
|
||||
/// // than once
|
||||
/// println!("Consumed: {}", func());
|
||||
///
|
||||
/// println!("Delicious!");
|
||||
///
|
||||
/// // Attempting to invoke `func()` again will throw a `use of moved
|
||||
/// // value` error for `func`
|
||||
/// }
|
||||
///
|
||||
/// 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]
|
||||
#[fundamental] // so that regex can rely that `&str: !FnMut`
|
||||
pub trait FnOnce<Args> {
|
||||
/// The returned type after the call operator is used.
|
||||
#[stable(feature = "fn_once_output", since = "1.12.0")]
|
||||
type Output;
|
||||
|
||||
/// This is called when the call operator is used.
|
||||
#[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")]
|
||||
impl<'a,A,F:?Sized> Fn<A> for &'a F
|
||||
where F : Fn<A>
|
||||
{
|
||||
extern "rust-call" fn call(&self, args: A) -> F::Output {
|
||||
(**self).call(args)
|
||||
}
|
||||
}
|
||||
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
impl<'a,A,F:?Sized> FnMut<A> for &'a F
|
||||
where F : Fn<A>
|
||||
{
|
||||
extern "rust-call" fn call_mut(&mut self, args: A) -> F::Output {
|
||||
(**self).call(args)
|
||||
}
|
||||
}
|
||||
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
impl<'a,A,F:?Sized> FnOnce<A> for &'a F
|
||||
where F : Fn<A>
|
||||
{
|
||||
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")]
|
||||
impl<'a,A,F:?Sized> FnMut<A> for &'a mut F
|
||||
where F : FnMut<A>
|
||||
{
|
||||
extern "rust-call" fn call_mut(&mut self, args: A) -> F::Output {
|
||||
(*self).call_mut(args)
|
||||
}
|
||||
}
|
||||
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
impl<'a,A,F:?Sized> FnOnce<A> for &'a mut F
|
||||
where F : FnMut<A>
|
||||
{
|
||||
type Output = F::Output;
|
||||
extern "rust-call" fn call_once(mut self, args: A) -> F::Output {
|
||||
(*self).call_mut(args)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Trait that indicates that this is a pointer or a wrapper for one,
|
||||
/// where unsizing can be performed on the pointee.
|
||||
///
|
||||
|
|
|
@ -28,12 +28,12 @@ fn main() {
|
|||
|
||||
//~ TRANS_ITEM fn function_as_argument::take_fn_once[0]<u32, &str, fn(u32, &str)>
|
||||
//~ TRANS_ITEM fn function_as_argument::function[0]<u32, &str>
|
||||
//~ TRANS_ITEM fn core::ops[0]::FnOnce[0]::call_once[0]<fn(u32, &str), (u32, &str)>
|
||||
//~ TRANS_ITEM fn core::ops[0]::function[0]::FnOnce[0]::call_once[0]<fn(u32, &str), (u32, &str)>
|
||||
take_fn_once(function, 0u32, "abc");
|
||||
|
||||
//~ TRANS_ITEM fn function_as_argument::take_fn_once[0]<char, f64, fn(char, f64)>
|
||||
//~ TRANS_ITEM fn function_as_argument::function[0]<char, f64>
|
||||
//~ TRANS_ITEM fn core::ops[0]::FnOnce[0]::call_once[0]<fn(char, f64), (char, f64)>
|
||||
//~ TRANS_ITEM fn core::ops[0]::function[0]::FnOnce[0]::call_once[0]<fn(char, f64), (char, f64)>
|
||||
take_fn_once(function, 'c', 0f64);
|
||||
|
||||
//~ TRANS_ITEM fn function_as_argument::take_fn_pointer[0]<i32, ()>
|
||||
|
|
|
@ -40,27 +40,27 @@ fn take_foo_mut<T, F: FnMut(T) -> T>(mut f: F, arg: T) -> T {
|
|||
fn main() {
|
||||
//~ TRANS_ITEM fn trait_method_as_argument::take_foo_once[0]<u32, fn(u32) -> u32>
|
||||
//~ TRANS_ITEM fn trait_method_as_argument::{{impl}}[0]::foo[0]
|
||||
//~ TRANS_ITEM fn core::ops[0]::FnOnce[0]::call_once[0]<fn(u32) -> u32, (u32)>
|
||||
//~ TRANS_ITEM fn core::ops[0]::function[0]::FnOnce[0]::call_once[0]<fn(u32) -> u32, (u32)>
|
||||
take_foo_once(Trait::foo, 0u32);
|
||||
|
||||
//~ TRANS_ITEM fn trait_method_as_argument::take_foo_once[0]<char, fn(char) -> char>
|
||||
//~ TRANS_ITEM fn trait_method_as_argument::Trait[0]::foo[0]<char>
|
||||
//~ TRANS_ITEM fn core::ops[0]::FnOnce[0]::call_once[0]<fn(char) -> char, (char)>
|
||||
//~ TRANS_ITEM fn core::ops[0]::function[0]::FnOnce[0]::call_once[0]<fn(char) -> char, (char)>
|
||||
take_foo_once(Trait::foo, 'c');
|
||||
|
||||
//~ TRANS_ITEM fn trait_method_as_argument::take_foo[0]<u32, fn(u32) -> u32>
|
||||
//~ TRANS_ITEM fn core::ops[0]::Fn[0]::call[0]<fn(u32) -> u32, (u32)>
|
||||
//~ TRANS_ITEM fn core::ops[0]::function[0]::Fn[0]::call[0]<fn(u32) -> u32, (u32)>
|
||||
take_foo(Trait::foo, 0u32);
|
||||
|
||||
//~ TRANS_ITEM fn trait_method_as_argument::take_foo[0]<char, fn(char) -> char>
|
||||
//~ TRANS_ITEM fn core::ops[0]::Fn[0]::call[0]<fn(char) -> char, (char)>
|
||||
//~ TRANS_ITEM fn core::ops[0]::function[0]::Fn[0]::call[0]<fn(char) -> char, (char)>
|
||||
take_foo(Trait::foo, 'c');
|
||||
|
||||
//~ TRANS_ITEM fn trait_method_as_argument::take_foo_mut[0]<u32, fn(u32) -> u32>
|
||||
//~ TRANS_ITEM fn core::ops[0]::FnMut[0]::call_mut[0]<fn(char) -> char, (char)>
|
||||
//~ TRANS_ITEM fn core::ops[0]::function[0]::FnMut[0]::call_mut[0]<fn(char) -> char, (char)>
|
||||
take_foo_mut(Trait::foo, 0u32);
|
||||
|
||||
//~ TRANS_ITEM fn trait_method_as_argument::take_foo_mut[0]<char, fn(char) -> char>
|
||||
//~ TRANS_ITEM fn core::ops[0]::FnMut[0]::call_mut[0]<fn(u32) -> u32, (u32)>
|
||||
//~ TRANS_ITEM fn core::ops[0]::function[0]::FnMut[0]::call_mut[0]<fn(u32) -> u32, (u32)>
|
||||
take_foo_mut(Trait::foo, 'c');
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue