|
147 | 147 |
|
148 | 148 | #![stable(feature = "rust1", since = "1.0.0")]
|
149 | 149 |
|
| 150 | +mod function; |
150 | 151 | mod range;
|
151 | 152 |
|
| 153 | +#[stable(feature = "rust1", since = "1.0.0")] |
| 154 | +pub use self::function::{Fn, FnMut, FnOnce}; |
| 155 | + |
152 | 156 | #[stable(feature = "rust1", since = "1.0.0")]
|
153 | 157 | pub use self::range::{Range, RangeFrom, RangeFull, RangeTo};
|
154 | 158 |
|
@@ -2200,191 +2204,6 @@ impl<'a, T: ?Sized> DerefMut for &'a mut T {
|
2200 | 2204 | fn deref_mut(&mut self) -> &mut T { *self }
|
2201 | 2205 | }
|
2202 | 2206 |
|
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 |
| - |
2388 | 2207 | /// Trait that indicates that this is a pointer or a wrapper for one,
|
2389 | 2208 | /// where unsizing can be performed on the pointee.
|
2390 | 2209 | ///
|
|
0 commit comments