Skip to content

Commit b9c8e99

Browse files
author
Clar Charr
committed
Move Fn to module.
1 parent cee5a2d commit b9c8e99

File tree

4 files changed

+206
-193
lines changed

4 files changed

+206
-193
lines changed

src/libcore/ops/function.rs

Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
1+
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
/// A version of the call operator that takes an immutable receiver.
12+
///
13+
/// # Examples
14+
///
15+
/// Closures automatically implement this trait, which allows them to be
16+
/// invoked. Note, however, that `Fn` takes an immutable reference to any
17+
/// captured variables. To take a mutable capture, implement [`FnMut`], and to
18+
/// consume the capture, implement [`FnOnce`].
19+
///
20+
/// [`FnMut`]: trait.FnMut.html
21+
/// [`FnOnce`]: trait.FnOnce.html
22+
///
23+
/// ```
24+
/// let square = |x| x * x;
25+
/// assert_eq!(square(5), 25);
26+
/// ```
27+
///
28+
/// Closures can also be passed to higher-level functions through a `Fn`
29+
/// parameter (or a `FnMut` or `FnOnce` parameter, which are supertraits of
30+
/// `Fn`).
31+
///
32+
/// ```
33+
/// fn call_with_one<F>(func: F) -> usize
34+
/// where F: Fn(usize) -> usize {
35+
/// func(1)
36+
/// }
37+
///
38+
/// let double = |x| x * 2;
39+
/// assert_eq!(call_with_one(double), 2);
40+
/// ```
41+
#[lang = "fn"]
42+
#[stable(feature = "rust1", since = "1.0.0")]
43+
#[rustc_paren_sugar]
44+
#[fundamental] // so that regex can rely that `&str: !FnMut`
45+
pub trait Fn<Args> : FnMut<Args> {
46+
/// This is called when the call operator is used.
47+
#[unstable(feature = "fn_traits", issue = "29625")]
48+
extern "rust-call" fn call(&self, args: Args) -> Self::Output;
49+
}
50+
51+
/// A version of the call operator that takes a mutable receiver.
52+
///
53+
/// # Examples
54+
///
55+
/// Closures that mutably capture variables automatically implement this trait,
56+
/// which allows them to be invoked.
57+
///
58+
/// ```
59+
/// let mut x = 5;
60+
/// {
61+
/// let mut square_x = || x *= x;
62+
/// square_x();
63+
/// }
64+
/// assert_eq!(x, 25);
65+
/// ```
66+
///
67+
/// Closures can also be passed to higher-level functions through a `FnMut`
68+
/// parameter (or a `FnOnce` parameter, which is a supertrait of `FnMut`).
69+
///
70+
/// ```
71+
/// fn do_twice<F>(mut func: F)
72+
/// where F: FnMut()
73+
/// {
74+
/// func();
75+
/// func();
76+
/// }
77+
///
78+
/// let mut x: usize = 1;
79+
/// {
80+
/// let add_two_to_x = || x += 2;
81+
/// do_twice(add_two_to_x);
82+
/// }
83+
///
84+
/// assert_eq!(x, 5);
85+
/// ```
86+
#[lang = "fn_mut"]
87+
#[stable(feature = "rust1", since = "1.0.0")]
88+
#[rustc_paren_sugar]
89+
#[fundamental] // so that regex can rely that `&str: !FnMut`
90+
pub trait FnMut<Args> : FnOnce<Args> {
91+
/// This is called when the call operator is used.
92+
#[unstable(feature = "fn_traits", issue = "29625")]
93+
extern "rust-call" fn call_mut(&mut self, args: Args) -> Self::Output;
94+
}
95+
96+
/// A version of the call operator that takes a by-value receiver.
97+
///
98+
/// # Examples
99+
///
100+
/// By-value closures automatically implement this trait, which allows them to
101+
/// be invoked.
102+
///
103+
/// ```
104+
/// let x = 5;
105+
/// let square_x = move || x * x;
106+
/// assert_eq!(square_x(), 25);
107+
/// ```
108+
///
109+
/// By-value Closures can also be passed to higher-level functions through a
110+
/// `FnOnce` parameter.
111+
///
112+
/// ```
113+
/// fn consume_with_relish<F>(func: F)
114+
/// where F: FnOnce() -> String
115+
/// {
116+
/// // `func` consumes its captured variables, so it cannot be run more
117+
/// // than once
118+
/// println!("Consumed: {}", func());
119+
///
120+
/// println!("Delicious!");
121+
///
122+
/// // Attempting to invoke `func()` again will throw a `use of moved
123+
/// // value` error for `func`
124+
/// }
125+
///
126+
/// let x = String::from("x");
127+
/// let consume_and_return_x = move || x;
128+
/// consume_with_relish(consume_and_return_x);
129+
///
130+
/// // `consume_and_return_x` can no longer be invoked at this point
131+
/// ```
132+
#[lang = "fn_once"]
133+
#[stable(feature = "rust1", since = "1.0.0")]
134+
#[rustc_paren_sugar]
135+
#[fundamental] // so that regex can rely that `&str: !FnMut`
136+
pub trait FnOnce<Args> {
137+
/// The returned type after the call operator is used.
138+
#[stable(feature = "fn_once_output", since = "1.12.0")]
139+
type Output;
140+
141+
/// This is called when the call operator is used.
142+
#[unstable(feature = "fn_traits", issue = "29625")]
143+
extern "rust-call" fn call_once(self, args: Args) -> Self::Output;
144+
}
145+
146+
mod impls {
147+
#[stable(feature = "rust1", since = "1.0.0")]
148+
impl<'a,A,F:?Sized> Fn<A> for &'a F
149+
where F : Fn<A>
150+
{
151+
extern "rust-call" fn call(&self, args: A) -> F::Output {
152+
(**self).call(args)
153+
}
154+
}
155+
156+
#[stable(feature = "rust1", since = "1.0.0")]
157+
impl<'a,A,F:?Sized> FnMut<A> for &'a F
158+
where F : Fn<A>
159+
{
160+
extern "rust-call" fn call_mut(&mut self, args: A) -> F::Output {
161+
(**self).call(args)
162+
}
163+
}
164+
165+
#[stable(feature = "rust1", since = "1.0.0")]
166+
impl<'a,A,F:?Sized> FnOnce<A> for &'a F
167+
where F : Fn<A>
168+
{
169+
type Output = F::Output;
170+
171+
extern "rust-call" fn call_once(self, args: A) -> F::Output {
172+
(*self).call(args)
173+
}
174+
}
175+
176+
#[stable(feature = "rust1", since = "1.0.0")]
177+
impl<'a,A,F:?Sized> FnMut<A> for &'a mut F
178+
where F : FnMut<A>
179+
{
180+
extern "rust-call" fn call_mut(&mut self, args: A) -> F::Output {
181+
(*self).call_mut(args)
182+
}
183+
}
184+
185+
#[stable(feature = "rust1", since = "1.0.0")]
186+
impl<'a,A,F:?Sized> FnOnce<A> for &'a mut F
187+
where F : FnMut<A>
188+
{
189+
type Output = F::Output;
190+
extern "rust-call" fn call_once(mut self, args: A) -> F::Output {
191+
(*self).call_mut(args)
192+
}
193+
}
194+
}

src/libcore/ops/mod.rs

Lines changed: 4 additions & 185 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,12 @@
147147
148148
#![stable(feature = "rust1", since = "1.0.0")]
149149

150+
mod function;
150151
mod range;
151152

153+
#[stable(feature = "rust1", since = "1.0.0")]
154+
pub use self::function::{Fn, FnMut, FnOnce};
155+
152156
#[stable(feature = "rust1", since = "1.0.0")]
153157
pub use self::range::{Range, RangeFrom, RangeFull, RangeTo};
154158

@@ -2200,191 +2204,6 @@ impl<'a, T: ?Sized> DerefMut for &'a mut T {
22002204
fn deref_mut(&mut self) -> &mut T { *self }
22012205
}
22022206

2203-
/// A version of the call operator that takes an immutable receiver.
2204-
///
2205-
/// # Examples
2206-
///
2207-
/// Closures automatically implement this trait, which allows them to be
2208-
/// invoked. Note, however, that `Fn` takes an immutable reference to any
2209-
/// captured variables. To take a mutable capture, implement [`FnMut`], and to
2210-
/// consume the capture, implement [`FnOnce`].
2211-
///
2212-
/// [`FnMut`]: trait.FnMut.html
2213-
/// [`FnOnce`]: trait.FnOnce.html
2214-
///
2215-
/// ```
2216-
/// let square = |x| x * x;
2217-
/// assert_eq!(square(5), 25);
2218-
/// ```
2219-
///
2220-
/// Closures can also be passed to higher-level functions through a `Fn`
2221-
/// parameter (or a `FnMut` or `FnOnce` parameter, which are supertraits of
2222-
/// `Fn`).
2223-
///
2224-
/// ```
2225-
/// fn call_with_one<F>(func: F) -> usize
2226-
/// where F: Fn(usize) -> usize {
2227-
/// func(1)
2228-
/// }
2229-
///
2230-
/// let double = |x| x * 2;
2231-
/// assert_eq!(call_with_one(double), 2);
2232-
/// ```
2233-
#[lang = "fn"]
2234-
#[stable(feature = "rust1", since = "1.0.0")]
2235-
#[rustc_paren_sugar]
2236-
#[fundamental] // so that regex can rely that `&str: !FnMut`
2237-
pub trait Fn<Args> : FnMut<Args> {
2238-
/// This is called when the call operator is used.
2239-
#[unstable(feature = "fn_traits", issue = "29625")]
2240-
extern "rust-call" fn call(&self, args: Args) -> Self::Output;
2241-
}
2242-
2243-
/// A version of the call operator that takes a mutable receiver.
2244-
///
2245-
/// # Examples
2246-
///
2247-
/// Closures that mutably capture variables automatically implement this trait,
2248-
/// which allows them to be invoked.
2249-
///
2250-
/// ```
2251-
/// let mut x = 5;
2252-
/// {
2253-
/// let mut square_x = || x *= x;
2254-
/// square_x();
2255-
/// }
2256-
/// assert_eq!(x, 25);
2257-
/// ```
2258-
///
2259-
/// Closures can also be passed to higher-level functions through a `FnMut`
2260-
/// parameter (or a `FnOnce` parameter, which is a supertrait of `FnMut`).
2261-
///
2262-
/// ```
2263-
/// fn do_twice<F>(mut func: F)
2264-
/// where F: FnMut()
2265-
/// {
2266-
/// func();
2267-
/// func();
2268-
/// }
2269-
///
2270-
/// let mut x: usize = 1;
2271-
/// {
2272-
/// let add_two_to_x = || x += 2;
2273-
/// do_twice(add_two_to_x);
2274-
/// }
2275-
///
2276-
/// assert_eq!(x, 5);
2277-
/// ```
2278-
#[lang = "fn_mut"]
2279-
#[stable(feature = "rust1", since = "1.0.0")]
2280-
#[rustc_paren_sugar]
2281-
#[fundamental] // so that regex can rely that `&str: !FnMut`
2282-
pub trait FnMut<Args> : FnOnce<Args> {
2283-
/// This is called when the call operator is used.
2284-
#[unstable(feature = "fn_traits", issue = "29625")]
2285-
extern "rust-call" fn call_mut(&mut self, args: Args) -> Self::Output;
2286-
}
2287-
2288-
/// A version of the call operator that takes a by-value receiver.
2289-
///
2290-
/// # Examples
2291-
///
2292-
/// By-value closures automatically implement this trait, which allows them to
2293-
/// be invoked.
2294-
///
2295-
/// ```
2296-
/// let x = 5;
2297-
/// let square_x = move || x * x;
2298-
/// assert_eq!(square_x(), 25);
2299-
/// ```
2300-
///
2301-
/// By-value Closures can also be passed to higher-level functions through a
2302-
/// `FnOnce` parameter.
2303-
///
2304-
/// ```
2305-
/// fn consume_with_relish<F>(func: F)
2306-
/// where F: FnOnce() -> String
2307-
/// {
2308-
/// // `func` consumes its captured variables, so it cannot be run more
2309-
/// // than once
2310-
/// println!("Consumed: {}", func());
2311-
///
2312-
/// println!("Delicious!");
2313-
///
2314-
/// // Attempting to invoke `func()` again will throw a `use of moved
2315-
/// // value` error for `func`
2316-
/// }
2317-
///
2318-
/// let x = String::from("x");
2319-
/// let consume_and_return_x = move || x;
2320-
/// consume_with_relish(consume_and_return_x);
2321-
///
2322-
/// // `consume_and_return_x` can no longer be invoked at this point
2323-
/// ```
2324-
#[lang = "fn_once"]
2325-
#[stable(feature = "rust1", since = "1.0.0")]
2326-
#[rustc_paren_sugar]
2327-
#[fundamental] // so that regex can rely that `&str: !FnMut`
2328-
pub trait FnOnce<Args> {
2329-
/// The returned type after the call operator is used.
2330-
#[stable(feature = "fn_once_output", since = "1.12.0")]
2331-
type Output;
2332-
2333-
/// This is called when the call operator is used.
2334-
#[unstable(feature = "fn_traits", issue = "29625")]
2335-
extern "rust-call" fn call_once(self, args: Args) -> Self::Output;
2336-
}
2337-
2338-
mod impls {
2339-
#[stable(feature = "rust1", since = "1.0.0")]
2340-
impl<'a,A,F:?Sized> Fn<A> for &'a F
2341-
where F : Fn<A>
2342-
{
2343-
extern "rust-call" fn call(&self, args: A) -> F::Output {
2344-
(**self).call(args)
2345-
}
2346-
}
2347-
2348-
#[stable(feature = "rust1", since = "1.0.0")]
2349-
impl<'a,A,F:?Sized> FnMut<A> for &'a F
2350-
where F : Fn<A>
2351-
{
2352-
extern "rust-call" fn call_mut(&mut self, args: A) -> F::Output {
2353-
(**self).call(args)
2354-
}
2355-
}
2356-
2357-
#[stable(feature = "rust1", since = "1.0.0")]
2358-
impl<'a,A,F:?Sized> FnOnce<A> for &'a F
2359-
where F : Fn<A>
2360-
{
2361-
type Output = F::Output;
2362-
2363-
extern "rust-call" fn call_once(self, args: A) -> F::Output {
2364-
(*self).call(args)
2365-
}
2366-
}
2367-
2368-
#[stable(feature = "rust1", since = "1.0.0")]
2369-
impl<'a,A,F:?Sized> FnMut<A> for &'a mut F
2370-
where F : FnMut<A>
2371-
{
2372-
extern "rust-call" fn call_mut(&mut self, args: A) -> F::Output {
2373-
(*self).call_mut(args)
2374-
}
2375-
}
2376-
2377-
#[stable(feature = "rust1", since = "1.0.0")]
2378-
impl<'a,A,F:?Sized> FnOnce<A> for &'a mut F
2379-
where F : FnMut<A>
2380-
{
2381-
type Output = F::Output;
2382-
extern "rust-call" fn call_once(mut self, args: A) -> F::Output {
2383-
(*self).call_mut(args)
2384-
}
2385-
}
2386-
}
2387-
23882207
/// Trait that indicates that this is a pointer or a wrapper for one,
23892208
/// where unsizing can be performed on the pointee.
23902209
///

0 commit comments

Comments
 (0)