Skip to content

Commit

Permalink
Len of channels
Browse files Browse the repository at this point in the history
  • Loading branch information
dgrr committed Mar 21, 2022
1 parent feb0c5e commit d59fdd9
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions channels.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ func (s Sender[T]) Send(data T) bool {
}
}

func (s Sender[T]) Len() int {
return len(s.ch)
}

func (s Sender[T]) Close() error {
close(s.ch)
return nil
Expand Down Expand Up @@ -57,6 +61,10 @@ func (r Receiver[T]) Range(fn func(T)) {
}
}

func (r Receiver[T]) Len() int {
return len(r.ch)
}

func (r Receiver[T]) RangeBool(fn func(T) bool) {
for v := range r.ch {
if !fn(v) {
Expand All @@ -76,6 +84,10 @@ func MakeChan[T any](size int) Channel[T] {
}
}

func (ch Channel[T]) Len() int {
return len(ch.ch)
}

func (ch Channel[T]) Split() (Sender[T], Receiver[T]) {
return MakeSender(ch.ch), MakeReceiver(ch.ch)
}
Expand Down

0 comments on commit d59fdd9

Please sign in to comment.