github.com/andoma-go/puddle/v2@v2.2.1/resource_list.go (about)

     1  package puddle
     2  
     3  type resList[T any] []*Resource[T]
     4  
     5  func (l *resList[T]) append(val *Resource[T]) { *l = append(*l, val) }
     6  
     7  func (l *resList[T]) popBack() *Resource[T] {
     8  	idx := len(*l) - 1
     9  	val := (*l)[idx]
    10  	(*l)[idx] = nil // Avoid memory leak
    11  	*l = (*l)[:idx]
    12  
    13  	return val
    14  }
    15  
    16  func (l *resList[T]) remove(val *Resource[T]) {
    17  	for i, elem := range *l {
    18  		if elem == val {
    19  			lastIdx := len(*l) - 1
    20  			(*l)[i] = (*l)[lastIdx]
    21  			(*l)[lastIdx] = nil // Avoid memory leak
    22  			(*l) = (*l)[:lastIdx]
    23  			return
    24  		}
    25  	}
    26  
    27  	panic("BUG: removeResource could not find res in slice")
    28  }