@@ -7,40 +7,45 @@ import (
77)
88
99// Future represents a value which may or may not currently be available,
10- // but will be available at some point, or an error if that value could not be made available.
10+ // but will be available at some point, or an error if that value could
11+ // not be made available.
1112type Future [T any ] interface {
1213
13- // Map creates a new Future by applying a function to the successful result of this Future.
14- Map (func (T ) (T , error )) Future [T ]
14+ // Map creates a new Future by applying a function to the successful
15+ // result of this Future.
16+ Map (func (* T ) (* T , error )) Future [T ]
1517
16- // FlatMap creates a new Future by applying a function to the successful result of
17- // this Future.
18- FlatMap (func (T ) (Future [T ], error )) Future [T ]
18+ // FlatMap creates a new Future by applying a function to the successful
19+ // result of this Future.
20+ FlatMap (func (* T ) (Future [T ], error )) Future [T ]
1921
20- // Join blocks until the Future is completed and returns either a result or an error.
21- Join () (T , error )
22+ // Join blocks until the Future is completed and returns either a result
23+ // or an error.
24+ Join () (* T , error )
2225
23- // Get blocks for at most the given time duration for this Future to complete
24- // and returns either a result or an error.
25- Get (time.Duration ) (T , error )
26+ // Get blocks for at most the given time duration for this Future to
27+ // complete and returns either a result or an error.
28+ Get (time.Duration ) (* T , error )
2629
27- // Recover handles any error that this Future might contain using a resolver function.
28- Recover (func () (T , error )) Future [T ]
30+ // Recover handles any error that this Future might contain using a
31+ // resolver function.
32+ Recover (func () (* T , error )) Future [T ]
2933
30- // RecoverWith handles any error that this Future might contain using another Future.
34+ // RecoverWith handles any error that this Future might contain using
35+ // another Future.
3136RecoverWith (Future [T ]) Future [T ]
3237
3338// complete completes the Future with either a value or an error.
3439// Is used by Promise internally.
35- complete (T , error )
40+ complete (* T , error )
3641}
3742
3843// FutureImpl implements the Future interface.
3944type FutureImpl [T any ] struct {
4045acceptOnce sync.Once
4146completeOnce sync.Once
4247done chan any
43- value T
48+ value * T
4449err error
4550}
4651
@@ -62,7 +67,8 @@ func (fut *FutureImpl[T]) accept() {
6267})
6368}
6469
65- // acceptTimeout blocks once, until the Future result is available or until the timeout expires.
70+ // acceptTimeout blocks once, until the Future result is available or until
71+ // the timeout expires.
6672func (fut * FutureImpl [T ]) acceptTimeout (timeout time.Duration ) {
6773fut .acceptOnce .Do (func () {
6874timer := time .NewTimer (timeout )
@@ -82,40 +88,37 @@ func (fut *FutureImpl[T]) setResult(result any) {
8288case error :
8389fut .err = value
8490default :
85- fut .value = value .(T )
91+ fut .value = value .(* T )
8692}
8793}
8894
89- // Map creates a new Future by applying a function to the successful result of this Future
90- // and returns the result of the function as a new Future.
91- func (fut * FutureImpl [T ]) Map (f func (T ) (T , error )) Future [T ] {
95+ // Map creates a new Future by applying a function to the successful result
96+ // of this Future and returns the result of the function as a new Future.
97+ func (fut * FutureImpl [T ]) Map (f func (* T ) (* T , error )) Future [T ] {
9298next := NewFuture [T ]()
9399go func () {
94100fut .accept ()
95101if fut .err != nil {
96- var nilT T
97- next .complete (nilT , fut .err )
102+ next .complete (nil , fut .err )
98103} else {
99104next .complete (f (fut .value ))
100105}
101106}()
102107return next
103108}
104109
105- // FlatMap creates a new Future by applying a function to the successful result of
106- // this Future and returns the result of the function as a new Future.
107- func (fut * FutureImpl [T ]) FlatMap (f func (T ) (Future [T ], error )) Future [T ] {
110+ // FlatMap creates a new Future by applying a function to the successful result
111+ // of this Future and returns the result of the function as a new Future.
112+ func (fut * FutureImpl [T ]) FlatMap (f func (* T ) (Future [T ], error )) Future [T ] {
108113next := NewFuture [T ]()
109114go func () {
110115fut .accept ()
111116if fut .err != nil {
112- var nilT T
113- next .complete (nilT , fut .err )
117+ next .complete (nil , fut .err )
114118} else {
115119tfut , terr := f (fut .value )
116120if terr != nil {
117- var nilT T
118- next .complete (nilT , terr )
121+ next .complete (nil , terr )
119122} else {
120123next .complete (tfut .Join ())
121124}
@@ -124,22 +127,24 @@ func (fut *FutureImpl[T]) FlatMap(f func(T) (Future[T], error)) Future[T] {
124127return next
125128}
126129
127- // Join blocks until the Future is completed and returns either a result or an error.
128- func (fut * FutureImpl [T ]) Join () (T , error ) {
130+ // Join blocks until the Future is completed and returns either
131+ // a result or an error.
132+ func (fut * FutureImpl [T ]) Join () (* T , error ) {
129133fut .accept ()
130134return fut .value , fut .err
131135}
132136
133- // Get blocks for at most the given time duration for this Future to complete
134- // and returns either a result or an error.
135- func (fut * FutureImpl [T ]) Get (timeout time.Duration ) (T , error ) {
137+ // Get blocks for at most the given time duration for this Future to
138+ // complete and returns either a result or an error.
139+ func (fut * FutureImpl [T ]) Get (timeout time.Duration ) (* T , error ) {
136140fut .acceptTimeout (timeout )
137141return fut .value , fut .err
138142}
139143
140- // Recover handles any error that this Future might contain using a given resolver function.
144+ // Recover handles any error that this Future might contain using
145+ // a given resolver function.
141146// Returns the result as a new Future.
142- func (fut * FutureImpl [T ]) Recover (f func () (T , error )) Future [T ] {
147+ func (fut * FutureImpl [T ]) Recover (f func () (* T , error )) Future [T ] {
143148next := NewFuture [T ]()
144149go func () {
145150fut .accept ()
@@ -152,7 +157,8 @@ func (fut *FutureImpl[T]) Recover(f func() (T, error)) Future[T] {
152157return next
153158}
154159
155- // RecoverWith handles any error that this Future might contain using another Future.
160+ // RecoverWith handles any error that this Future might contain using
161+ // another Future.
156162// Returns the result as a new Future.
157163func (fut * FutureImpl [T ]) RecoverWith (rf Future [T ]) Future [T ] {
158164next := NewFuture [T ]()
@@ -168,7 +174,7 @@ func (fut *FutureImpl[T]) RecoverWith(rf Future[T]) Future[T] {
168174}
169175
170176// complete completes the Future with either a value or an error.
171- func (fut * FutureImpl [T ]) complete (value T , err error ) {
177+ func (fut * FutureImpl [T ]) complete (value * T , err error ) {
172178fut .completeOnce .Do (func () {
173179go func () {
174180if err != nil {
0 commit comments