github.com/xyproto/u-root@v6.0.1-0.20200302025726-5528e0c77a3c+incompatible/cmds/core/elvish/eval/vars/ptr.go (about)

     1  package vars
     2  
     3  import (
     4  	"reflect"
     5  
     6  	"github.com/u-root/u-root/cmds/core/elvish/eval/vals"
     7  )
     8  
     9  type ptr struct {
    10  	ptr interface{}
    11  }
    12  
    13  // FromPtr creates a variable from a pointer. The variable is kept in sync
    14  // with the value the pointer points to, using elvToGo and goToElv conversions
    15  // when Get and Set.
    16  func FromPtr(p interface{}) Var {
    17  	return ptr{p}
    18  }
    19  
    20  // Get returns the value pointed by the pointer, after conversion using FromGo.
    21  func (v ptr) Get() interface{} {
    22  	return vals.FromGo(reflect.Indirect(reflect.ValueOf(v.ptr)).Interface())
    23  }
    24  
    25  // Get sets the value pointed by the pointer, after conversion using ScanToGo.
    26  func (v ptr) Set(val interface{}) error {
    27  	return vals.ScanToGo(val, v.ptr)
    28  }