github.com/hsfzxjy/dgo/go@v0.2.0/ext.go (about)

     1  package dgo
     2  
     3  import (
     4  	"runtime/cgo"
     5  )
     6  
     7  type hasCallback interface{ call() }
     8  
     9  type ExtSlice struct {
    10  	slice     any
    11  	peer      any
    12  	finalizer func(any)
    13  }
    14  
    15  func Ext[T any](slice []T, finalizer func(any), peer any) ExtSlice {
    16  	return ExtSlice{slice, peer, finalizer}
    17  }
    18  
    19  func (s ExtSlice) asCgoHandle() cgo.Handle { return cgo.NewHandle(s) }
    20  func (s ExtSlice) call() {
    21  	if s.finalizer != nil {
    22  		s.finalizer(s.peer)
    23  	}
    24  }