@@ -13,54 +13,54 @@ type Future[T any] interface {
1313
1414 // Map creates a new Future by applying a function to the successful
1515 // result of this Future.
16- Map (func (* T ) (* T , error )) Future [T ]
16+ Map (func (T ) (T , error )) Future [T ]
1717
1818 // FlatMap creates a new Future by applying a function to the successful
1919 // result of this Future.
20- FlatMap (func (* T ) (Future [T ], error )) Future [T ]
20+ FlatMap (func (T ) (Future [T ], error )) Future [T ]
2121
2222 // Join blocks until the Future is completed and returns either a result
2323 // or an error.
24- Join () (* T , error )
24+ Join () (T , error )
2525
2626 // Get blocks for at most the given time duration for this Future to
2727 // complete and returns either a result or an error.
28- Get (time.Duration ) (* T , error )
28+ Get (time.Duration ) (T , error )
2929
3030 // Recover handles any error that this Future might contain using a
3131 // resolver function.
32- Recover (func () (* T , error )) Future [T ]
32+ Recover (func () (T , error )) Future [T ]
3333
3434 // RecoverWith handles any error that this Future might contain using
3535 // another Future.
3636 RecoverWith (Future [T ]) Future [T ]
3737
3838 // complete completes the Future with either a value or an error.
39- // Is used by Promise internally.
40- complete (* T , error )
39+ // It is used by [ Promise] internally.
40+ complete (T , error )
4141}
4242
43- // FutureImpl implements the Future interface.
44- type FutureImpl [T any ] struct {
43+ // futureImpl implements the Future interface.
44+ type futureImpl [T any ] struct {
4545 acceptOnce sync.Once
4646 completeOnce sync.Once
4747 done chan any
48- value * T
48+ value T
4949 err error
5050}
5151
52- // Verify FutureImpl satisfies the Future interface.
53- var _ Future [any ] = (* FutureImpl [any ])(nil )
52+ // Verify futureImpl satisfies the Future interface.
53+ var _ Future [any ] = (* futureImpl [any ])(nil )
5454
55- // NewFuture returns a new Future.
56- func NewFuture [T any ]() Future [T ] {
57- return & FutureImpl [T ]{
55+ // newFuture returns a new Future.
56+ func newFuture [T any ]() Future [T ] {
57+ return & futureImpl [T ]{
5858 done : make (chan any , 1 ),
5959 }
6060}
6161
6262// accept blocks once, until the Future result is available.
63- func (fut * FutureImpl [T ]) accept () {
63+ func (fut * futureImpl [T ]) accept () {
6464 fut .acceptOnce .Do (func () {
6565 result := <- fut .done
6666 fut .setResult (result )
@@ -69,7 +69,7 @@ func (fut *FutureImpl[T]) accept() {
6969
7070// acceptTimeout blocks once, until the Future result is available or until
7171// the timeout expires.
72- func (fut * FutureImpl [T ]) acceptTimeout (timeout time.Duration ) {
72+ func (fut * futureImpl [T ]) acceptTimeout (timeout time.Duration ) {
7373 fut .acceptOnce .Do (func () {
7474 timer := time .NewTimer (timeout )
7575 defer timer .Stop ()
@@ -83,23 +83,24 @@ func (fut *FutureImpl[T]) acceptTimeout(timeout time.Duration) {
8383}
8484
8585// setResult assigns a value to the Future instance.
86- func (fut * FutureImpl [T ]) setResult (result any ) {
86+ func (fut * futureImpl [T ]) setResult (result any ) {
8787 switch value := result .(type ) {
8888 case error :
8989 fut .err = value
9090 default :
91- fut .value = value .(* T )
91+ fut .value = value .(T )
9292 }
9393}
9494
9595// Map creates a new Future by applying a function to the successful result
9696// 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 ] {
98- next := NewFuture [T ]()
97+ func (fut * futureImpl [T ]) Map (f func (T ) (T , error )) Future [T ] {
98+ next := newFuture [T ]()
9999 go func () {
100100 fut .accept ()
101101 if fut .err != nil {
102- next .complete (nil , fut .err )
102+ var zero T
103+ next .complete (zero , fut .err )
103104 } else {
104105 next .complete (f (fut .value ))
105106 }
@@ -109,16 +110,18 @@ func (fut *FutureImpl[T]) Map(f func(*T) (*T, error)) Future[T] {
109110
110111// FlatMap creates a new Future by applying a function to the successful result
111112// 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 ] {
113- next := NewFuture [T ]()
113+ func (fut * futureImpl [T ]) FlatMap (f func (T ) (Future [T ], error )) Future [T ] {
114+ next := newFuture [T ]()
114115 go func () {
115116 fut .accept ()
116117 if fut .err != nil {
117- next .complete (nil , fut .err )
118+ var zero T
119+ next .complete (zero , fut .err )
118120 } else {
119121 tfut , terr := f (fut .value )
120122 if terr != nil {
121- next .complete (nil , terr )
123+ var zero T
124+ next .complete (zero , terr )
122125 } else {
123126 next .complete (tfut .Join ())
124127 }
@@ -129,23 +132,23 @@ func (fut *FutureImpl[T]) FlatMap(f func(*T) (Future[T], error)) Future[T] {
129132
130133// Join blocks until the Future is completed and returns either
131134// a result or an error.
132- func (fut * FutureImpl [T ]) Join () (* T , error ) {
135+ func (fut * futureImpl [T ]) Join () (T , error ) {
133136 fut .accept ()
134137 return fut .value , fut .err
135138}
136139
137140// Get blocks for at most the given time duration for this Future to
138141// complete and returns either a result or an error.
139- func (fut * FutureImpl [T ]) Get (timeout time.Duration ) (* T , error ) {
142+ func (fut * futureImpl [T ]) Get (timeout time.Duration ) (T , error ) {
140143 fut .acceptTimeout (timeout )
141144 return fut .value , fut .err
142145}
143146
144147// Recover handles any error that this Future might contain using
145148// a given resolver function.
146149// Returns the result as a new Future.
147- func (fut * FutureImpl [T ]) Recover (f func () (* T , error )) Future [T ] {
148- next := NewFuture [T ]()
150+ func (fut * futureImpl [T ]) Recover (f func () (T , error )) Future [T ] {
151+ next := newFuture [T ]()
149152 go func () {
150153 fut .accept ()
151154 if fut .err != nil {
@@ -160,8 +163,8 @@ func (fut *FutureImpl[T]) Recover(f func() (*T, error)) Future[T] {
160163// RecoverWith handles any error that this Future might contain using
161164// another Future.
162165// Returns the result as a new Future.
163- func (fut * FutureImpl [T ]) RecoverWith (rf Future [T ]) Future [T ] {
164- next := NewFuture [T ]()
166+ func (fut * futureImpl [T ]) RecoverWith (rf Future [T ]) Future [T ] {
167+ next := newFuture [T ]()
165168 go func () {
166169 fut .accept ()
167170 if fut .err != nil {
@@ -174,14 +177,12 @@ func (fut *FutureImpl[T]) RecoverWith(rf Future[T]) Future[T] {
174177}
175178
176179// complete completes the Future with either a value or an error.
177- func (fut * FutureImpl [T ]) complete (value * T , err error ) {
180+ func (fut * futureImpl [T ]) complete (value T , err error ) {
178181 fut .completeOnce .Do (func () {
179- go func () {
180- if err != nil {
181- fut .done <- err
182- } else {
183- fut .done <- value
184- }
185- }()
182+ if err != nil {
183+ fut .done <- err
184+ } else {
185+ fut .done <- value
186+ }
186187 })
187188}
0 commit comments