github.com/tinygo-org/tinygo@v0.31.3-0.20240404173401-90b0bf646c27/src/sync/pool.go (about)

     1  package sync
     2  
     3  // Pool is a very simple implementation of sync.Pool.
     4  type Pool struct {
     5  	New   func() interface{}
     6  	items []interface{}
     7  }
     8  
     9  // Get returns an item in the pool, or the value of calling Pool.New() if there are no items.
    10  func (p *Pool) Get() interface{} {
    11  	if len(p.items) > 0 {
    12  		x := p.items[len(p.items)-1]
    13  		p.items = p.items[:len(p.items)-1]
    14  		return x
    15  	}
    16  	if p.New == nil {
    17  		return nil
    18  	}
    19  	return p.New()
    20  }
    21  
    22  // Put adds a value back into the pool.
    23  func (p *Pool) Put(x interface{}) {
    24  	p.items = append(p.items, x)
    25  }