Skip to content

Commit e05f7b7

Browse files
authored
add small stack type (#1422)
1 parent b2a861d commit e05f7b7

File tree

3 files changed

+32
-1
lines changed

3 files changed

+32
-1
lines changed

.changeset/gold-fishes-smash.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@livekit/protocol": patch
3+
---
4+
5+
add stack utility

utils/closers.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package utils
1616

1717
import (
1818
"io"
19+
"slices"
1920

2021
"go.uber.org/multierr"
2122
)
@@ -28,7 +29,7 @@ func CombineClosers(cs ...io.Closer) Closers {
2829

2930
func (s *Closers) Close() error {
3031
var err error
31-
for _, c := range *s {
32+
for _, c := range slices.Backward(*s) {
3233
if c != nil {
3334
err = multierr.Append(err, c.Close())
3435
}

utils/stack/stack.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package stack
2+
3+
type Stack[T any] []T
4+
5+
func (s Stack[T]) Empty() bool {
6+
return len(s) == 0
7+
}
8+
9+
func (s *Stack[T]) Reset() {
10+
*s = (*s)[:0]
11+
}
12+
13+
func (s *Stack[T]) Push(v T) {
14+
*s = append(*s, v)
15+
}
16+
17+
func (s *Stack[T]) Pop() T {
18+
v := (*s)[len(*s)-1]
19+
*s = (*s)[:len(*s)-1]
20+
return v
21+
}
22+
23+
func (s Stack[T]) Peek() T {
24+
return s[len(s)-1]
25+
}

0 commit comments

Comments
 (0)