File: sig-from-bare-fn.rs

package info (click to toggle)
rustc 1.85.0%2Bdfsg3-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental, forky, sid, trixie
  • size: 893,396 kB
  • sloc: xml: 158,127; python: 35,830; javascript: 19,497; cpp: 19,002; sh: 17,245; ansic: 13,127; asm: 4,376; makefile: 1,051; perl: 29; lisp: 29; ruby: 19; sql: 11
file content (47 lines) | stat: -rw-r--r-- 1,041 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
//@ check-pass
//@ edition: 2021

// Make sure that we infer the args of an async closure even if it's passed to
// a function that requires the async closure implement `Fn*` but does *not* have
// a `Future` bound on the return type.

use std::future::Future;

trait TryStream {
    type Ok;
    type Err;
}

trait TryFuture {
    type Ok;
    type Err;
}

impl<F, T, E> TryFuture for F where F: Future<Output = Result<T, E>> {
    type Ok = T;
    type Err = E;
}

trait TryStreamExt: TryStream {
    fn try_for_each<F, Fut>(&self, f: F)
    where
        F: FnMut(Self::Ok) -> Fut,
        Fut: TryFuture<Ok = (), Err = Self::Err>;
}

impl<S> TryStreamExt for S where S: TryStream {
    fn try_for_each<F, Fut>(&self, f: F)
    where
        F: FnMut(Self::Ok) -> Fut,
        Fut: TryFuture<Ok = (), Err = Self::Err>,
    { }
}

fn test(stream: impl TryStream<Ok = &'static str, Err = ()>) {
    stream.try_for_each(async |s| {
        s.trim(); // Make sure we know the type of `s` at this point.
        Ok(())
    });
}

fn main() {}