github.com/ActiveState/cli@v0.0.0-20240508170324-6801f60cd051/internal/rtutils/ptr/ptr.go (about)

     1  package ptr
     2  
     3  import (
     4  	"reflect"
     5  )
     6  
     7  // To makes a pointer to a value
     8  func To[T any](v T) *T {
     9  	return &v
    10  }
    11  
    12  // From makes a value from a pointer
    13  func From[T any](v *T, fallback T) T {
    14  	if IsNil(v) {
    15  		return fallback
    16  	}
    17  	return *v
    18  }
    19  
    20  // Clone create a new pointer with a different memory address than the original, effectively cloning it
    21  func Clone[T any](v *T) *T {
    22  	if v == nil {
    23  		return nil
    24  	}
    25  	t := *v
    26  	return &t
    27  }
    28  
    29  // IsNil asserts whether the underlying type is nil, which `interface{} == nil` does not
    30  func IsNil(i interface{}) bool {
    31  	return i == nil || reflect.ValueOf(i).IsNil()
    32  }