FilterResults fixed

This commit is contained in:
milarin 2023-03-25 11:49:59 +01:00
parent a864cb930d
commit e9fdfd6e42

View File

@ -49,6 +49,17 @@ func (r Result[T]) Get() (T, error) {
return *r.value, r.err
}
func (r Result[T]) GetSafe() T {
if r.err != nil {
panic(r.err)
}
return *r.value
}
func (r Result[T]) Err() error {
return r.err
}
func FilterSuccess[T any](source <-chan Result[T]) <-chan T {
succeeded := Filter(source, func(r Result[T]) bool { return r.Success() })
@ -76,13 +87,11 @@ func FilterResults[T any](source <-chan Result[T]) (succeeded <-chan T, failed <
defer close(fail)
for r := range source {
v, err := r.Get()
if err == nil {
succ <- v
} else {
fail <- err
if r.Fail() {
fail <- r.Err()
continue
}
succ <- r.GetSafe()
}
}()