diff --git a/to.go b/to.go index 43ad408..86d8768 100644 --- a/to.go +++ b/to.go @@ -9,6 +9,22 @@ func ToSlice[T any](ch <-chan T) []T { 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. // The returned slice will be a dereferenced and continuous block of memory. // Nil pointers are ignored.