Skip to content

Commit cfadb35

Browse files
author
Clar Charr
committed
Move more stuff out of markers.
1 parent 6f25280 commit cfadb35

File tree

5 files changed

+313
-311
lines changed

5 files changed

+313
-311
lines changed

src/libcore/ops/fn.rs

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

0 commit comments

Comments
 (0)