From 35264315fdc8ee02db753f2c097c6b31e51cc59e Mon Sep 17 00:00:00 2001 From: milarin Date: Fri, 14 Apr 2023 19:05:27 +0200 Subject: [PATCH] ToSliceContinuous implemented --- to.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) 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.