github.com/webmafia/fast@v0.10.0/slice.go (about)

     1  package fast
     2  
     3  import "unsafe"
     4  
     5  //go:inline
     6  func SliceToSlice[From any, To any](from []From, toLength int) []To {
     7  	fromHeader := (*sliceHeader)(unsafe.Pointer(&from))
     8  	toHeader := sliceHeader{
     9  		Data: fromHeader.Data,
    10  		Len:  toLength,
    11  		Cap:  toLength,
    12  	}
    13  
    14  	return *(*[]To)(unsafe.Pointer(&toHeader))
    15  }
    16  
    17  //go:inline
    18  func SliceUnsafePointer[T any](slice []T) unsafe.Pointer {
    19  	header := *(*sliceHeader)(unsafe.Pointer(&slice))
    20  	return header.Data
    21  }