ToSliceContinuous implemented

This commit is contained in:
milarin 2023-04-14 19:05:27 +02:00
parent 9e62bab91e
commit 35264315fd

16
to.go
View File

@ -9,6 +9,22 @@ func ToSlice[T any](ch <-chan T) []T {
return s return s
} }
// ToSliceContinuous returns a slice containing all values read from ch.
// The returned slice will be a pointer slice to a continuous block of memory.
// All values will be copied.
func ToSliceContinuous[T any](ch <-chan *T) []*T {
values := make([]T, 0, cap(ch))
pointers := make([]*T, 0, cap(ch))
EachSuccessive(ch, func(value *T) {
pointers = append(pointers, value)
if value != nil {
values = append(values, *value)
}
})
return pointers
}
// ToSliceDeref returns a slice containing all values read from ch. // ToSliceDeref returns a slice containing all values read from ch.
// The returned slice will be a dereferenced and continuous block of memory. // The returned slice will be a dereferenced and continuous block of memory.
// Nil pointers are ignored. // Nil pointers are ignored.