github.com/searKing/golang/go@v1.2.117/exp/types/conv.go (about)

     1  // Copyright 2023 The searKing Author. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package types
     6  
     7  import (
     8  	"unsafe"
     9  
    10  	"github.com/searKing/golang/go/exp/constraints"
    11  )
    12  
    13  // Conv converts a number[T] into an unlimited untyped number, and assigned back to number[S].
    14  // static_cast<S>(v)
    15  // https://go.dev/ref/spec#Constants
    16  func Conv[T, S constraints.Number](v T) S {
    17  	return S(v)
    18  }
    19  
    20  // ConvBinary writes a number[T] into a byte slice, and reads the byte slice into a number[S].
    21  // binary.Write( T -> []byte ) => binary.Read( []byte, S )
    22  func ConvBinary[T, S any](v T) S {
    23  	return *(*S)(unsafe.Pointer(&v))
    24  }