github.com/qioalice/ekago/v3@v3.3.2-0.20221202205325-5c262d586ee4/internal/ekaclike/go_interface.go (about)

     1  // Copyright © 2020. All rights reserved.
     2  // Author: Ilya Stroy.
     3  // Contacts: iyuryevich@pm.me, https://github.com/qioalice
     4  // License: https://opensource.org/licenses/MIT
     5  
     6  package ekaclike
     7  
     8  import (
     9  	"unsafe"
    10  )
    11  
    12  type (
    13  	/*
    14  		Interface represents what "any" is
    15  		in internal Golang parts.
    16  	*/
    17  	Interface struct {
    18  		Type uintptr        // pointer to the type definition struct
    19  		Word unsafe.Pointer // pointer to the value
    20  	}
    21  )
    22  
    23  /*
    24  Pack does the reverse thing that UnpackInterface does.
    25  It returns Golang any object that current Interface describes.
    26  */
    27  func (i Interface) Pack() (i2 any) {
    28  
    29  	if i.Type == 0 {
    30  		return nil
    31  	}
    32  
    33  	i2I := (*Interface)(unsafe.Pointer(&i2))
    34  
    35  	i2I.Type = i.Type
    36  	i2I.Word = i.Word
    37  
    38  	return
    39  }
    40  
    41  /*
    42  UnpackInterface exposes Golang any internal parts
    43  and returns it.
    44  If passed argument is absolutely nil, returns an empty Interface object.
    45  */
    46  func UnpackInterface(i any) Interface {
    47  	if i == nil {
    48  		return Interface{}
    49  	}
    50  	return *(*Interface)(unsafe.Pointer(&i))
    51  }