From: qydysky Date: Mon, 25 Mar 2024 19:05:50 +0000 (+0800) Subject: 1 X-Git-Tag: v0.28.20240325191053 X-Git-Url: http://127.0.0.1:8081/?a=commitdiff_plain;h=fd627282493d8a9d742beb22443a97987385c2a6;p=part%2F.git 1 --- diff --git a/slice/Slice.go b/slice/Slice.go index 6ffd25a..b476870 100644 --- a/slice/Slice.go +++ b/slice/Slice.go @@ -1,9 +1,10 @@ package part import ( - "errors" "sync" "unsafe" + + perrors "github.com/qydysky/part/errors" ) type Buf[T any] struct { @@ -64,12 +65,14 @@ func (t *Buf[T]) AppendTo(to *Buf[T]) error { return to.Append(buf) } +var ErrOverMax = perrors.New("slices.Append", "ErrOverMax") + func (t *Buf[T]) Append(data []T) error { t.l.Lock() defer t.l.Unlock() if t.maxsize != 0 && len(t.buf)+len(data) > t.maxsize { - return errors.New("超出设定maxsize") + return ErrOverMax } else if len(t.buf) == 0 { t.buf = make([]T, len(data)) } else { @@ -83,6 +86,8 @@ func (t *Buf[T]) Append(data []T) error { return nil } +var ErrOverLen = perrors.New("slices.Remove", "ErrOverLen") + func (t *Buf[T]) RemoveFront(n int) error { if n <= 0 { return nil @@ -92,7 +97,7 @@ func (t *Buf[T]) RemoveFront(n int) error { defer t.l.Unlock() if t.bufsize < n { - return errors.New("尝试移除的数值大于长度") + return ErrOverLen } else if t.bufsize == n { t.bufsize = 0 } else { @@ -112,7 +117,7 @@ func (t *Buf[T]) RemoveBack(n int) error { defer t.l.Unlock() if t.bufsize < n { - return errors.New("尝试移除的数值大于长度") + return ErrOverLen } else if t.bufsize == n { t.bufsize = 0 } else { @@ -138,12 +143,14 @@ func (t *Buf[T]) GetModified() Modified { return t.modified } +var ErrNoSameBuf = perrors.New("slices.HadModified", "ErrNoSameBuf") + func (t *Buf[T]) HadModified(mt Modified) (modified bool, err error) { t.l.RLock() defer t.l.RUnlock() if t.modified.p != mt.p { - err = errors.New("不能对比不同buf") + err = ErrNoSameBuf } modified = t.modified.t != mt.t return diff --git a/slice/Slice_test.go b/slice/Slice_test.go index 2da7059..4521ffe 100644 --- a/slice/Slice_test.go +++ b/slice/Slice_test.go @@ -36,10 +36,10 @@ func TestXxx(t *testing.T) { if !b.IsEmpty() || b.Size() != 0 || !bytes.Equal(bc, []byte("abc")) || !bytes.Equal(b.GetPureBuf(), []byte("")) { t.Fatal() } - if e := b.RemoveFront(1); e == nil || e.Error() != "尝试移除的数值大于长度" { + if e := b.RemoveFront(1); e == nil || e != ErrOverLen { t.Fatal() } - if e := b.Append([]byte("abcdef")); e == nil || e.Error() != "超出设定maxsize" { + if e := b.Append([]byte("abcdef")); e == nil || e != ErrOverMax { t.Fatal() } b.Clear()