@@ -3,6 +3,7 @@ use {Future, Poll};
3
3
/// A future which is never resolved.
4
4
///
5
5
/// This future can be created with the `empty` function.
6
+ #[ derive( Copy , Clone ) ]
6
7
pub struct Never { }
7
8
8
9
/// Creates a future which never resolves, representing a computation that never
@@ -23,3 +24,68 @@ impl Future for Never {
23
24
Poll :: NotReady
24
25
}
25
26
}
27
+
28
+ /// A future representing a finished but erroneous computation.
29
+ ///
30
+ /// Created by the `failed` function.
31
+ pub struct Failed < E > {
32
+ e : Option < E > ,
33
+ }
34
+
35
+ /// Creates a "leaf future" from an immediate value of a failed computation.
36
+ ///
37
+ /// The returned future is similar to `done` where it will immediately run a
38
+ /// scheduled callback with the provided value.
39
+ ///
40
+ /// # Examples
41
+ ///
42
+ /// ```
43
+ /// use futures::*;
44
+ ///
45
+ /// let future_of_err_1 = failed::<u32, u32>(1);
46
+ /// ```
47
+ pub fn failed < T , E > ( e : E ) -> impl Future < Item = T , Error = E > {
48
+ Failed { e : Some ( e) } . map ( |x| x)
49
+ }
50
+
51
+ impl < E > Future for Failed < E > {
52
+ type Item = !;
53
+ type Error = E ;
54
+
55
+ fn poll ( & mut self ) -> Poll < !, E > {
56
+ Poll :: Err ( self . e . take ( ) . expect ( "cannot poll Failed twice" ) )
57
+ }
58
+ }
59
+
60
+ /// A future representing a finished successful computation.
61
+ ///
62
+ /// Created by the `finished` function.
63
+ pub struct Finished < T > {
64
+ t : Option < T > ,
65
+ }
66
+
67
+ /// Creates a "leaf future" from an immediate value of a finished and
68
+ /// successful computation.
69
+ ///
70
+ /// The returned future is similar to `done` where it will immediately run a
71
+ /// scheduled callback with the provided value.
72
+ ///
73
+ /// # Examples
74
+ ///
75
+ /// ```
76
+ /// use futures::*;
77
+ ///
78
+ /// let future_of_1 = finished::<u32, u32>(1);
79
+ /// ```
80
+ pub fn finished < T , E > ( t : T ) -> impl Future < Item = T , Error = E > {
81
+ Finished { t : Some ( t) } . map_err ( |x| x)
82
+ }
83
+
84
+ impl < T > Future for Finished < T > {
85
+ type Item = T ;
86
+ type Error = !;
87
+
88
+ fn poll ( & mut self ) -> Poll < T , !> {
89
+ Poll :: Ok ( self . t . take ( ) . expect ( "cannot poll Finished twice" ) )
90
+ }
91
+ }
0 commit comments