feat: buffer.Unbounded 增加新的构造函数,支持省略 generateNil 函数,新增 IsClosed 函数检查无界缓冲区是否已经关闭

This commit is contained in:
kercylan98 2023-09-19 12:37:18 +08:00
parent 9b68def3da
commit e9bc9fb481
1 changed files with 14 additions and 0 deletions

View File

@ -13,6 +13,13 @@ func NewUnbounded[V any](generateNil func() V) *Unbounded[V] {
return &Unbounded[V]{c: make(chan V, 1), nil: generateNil()}
}
// NewUnboundedN 与 NewUnbounded 相同,只是省略了 generateNil 参数
func NewUnboundedN[V any]() *Unbounded[V] {
return NewUnbounded[V](func() (v V) {
return v
})
}
// Unbounded 是无界缓冲区的实现
type Unbounded[V any] struct {
c chan V
@ -72,3 +79,10 @@ func (slf *Unbounded[V]) Close() {
slf.closed = true
close(slf.c)
}
// IsClosed 是否已关闭
func (slf *Unbounded[V]) IsClosed() bool {
slf.mu.Lock()
defer slf.mu.Unlock()
return slf.closed
}