feat: buffer.Unbounded 增加新的构造函数,支持省略 generateNil 函数,新增 IsClosed 函数检查无界缓冲区是否已经关闭
This commit is contained in:
parent
9b68def3da
commit
e9bc9fb481
|
@ -13,6 +13,13 @@ func NewUnbounded[V any](generateNil func() V) *Unbounded[V] {
|
||||||
return &Unbounded[V]{c: make(chan V, 1), nil: generateNil()}
|
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 是无界缓冲区的实现
|
// Unbounded 是无界缓冲区的实现
|
||||||
type Unbounded[V any] struct {
|
type Unbounded[V any] struct {
|
||||||
c chan V
|
c chan V
|
||||||
|
@ -72,3 +79,10 @@ func (slf *Unbounded[V]) Close() {
|
||||||
slf.closed = true
|
slf.closed = true
|
||||||
close(slf.c)
|
close(slf.c)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// IsClosed 是否已关闭
|
||||||
|
func (slf *Unbounded[V]) IsClosed() bool {
|
||||||
|
slf.mu.Lock()
|
||||||
|
defer slf.mu.Unlock()
|
||||||
|
return slf.closed
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue