remove usage of package unsafe for package gtype/grand

This commit is contained in:
John
2021-09-20 12:58:10 +08:00
parent 81e54be0c1
commit fe2eccfda4
6 changed files with 11 additions and 17 deletions

View File

@ -4,14 +4,11 @@
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
// Package gtype provides kinds of high performance and concurrent-safe basic variable types.
// Package gtype provides high performance and concurrent-safe basic variable types.
package gtype
// Type is alias of Interface.
type Type = Interface
// New is alias of NewInterface.
// See NewInterface.
func New(value ...interface{}) *Type {
func New(value ...interface{}) *Interface {
return NewInterface(value...)
}

View File

@ -51,7 +51,7 @@ func (v *Bool) Set(value bool) (old bool) {
return
}
// Val atomically loads and returns t.valueue.
// Val atomically loads and returns t.value.
func (v *Bool) Val() bool {
return atomic.LoadInt32(&v.value) > 0
}

View File

@ -11,7 +11,6 @@ import (
"math"
"strconv"
"sync/atomic"
"unsafe"
)
// Float32 is a struct for concurrent-safe operation for type float32.
@ -51,7 +50,7 @@ func (v *Float32) Add(delta float32) (new float32) {
old := math.Float32frombits(v.value)
new = old + delta
if atomic.CompareAndSwapUint32(
(*uint32)(unsafe.Pointer(&v.value)),
&v.value,
math.Float32bits(old),
math.Float32bits(new),
) {

View File

@ -11,7 +11,6 @@ import (
"math"
"strconv"
"sync/atomic"
"unsafe"
)
// Float64 is a struct for concurrent-safe operation for type float64.
@ -51,7 +50,7 @@ func (v *Float64) Add(delta float64) (new float64) {
old := math.Float64frombits(v.value)
new = old + delta
if atomic.CompareAndSwapUint64(
(*uint64)(unsafe.Pointer(&v.value)),
&v.value,
math.Float64bits(old),
math.Float64bits(new),
) {

View File

@ -22,7 +22,7 @@ func Test_Bytes(t *testing.T) {
t.AssertEQ(iClone.Set([]byte("123")), []byte("abc"))
t.AssertEQ(iClone.Val(), []byte("123"))
//空参测试
// 空参测试
i1 := gtype.NewBytes()
t.AssertEQ(i1.Val(), nil)
})

View File

@ -10,7 +10,6 @@ package grand
import (
"encoding/binary"
"time"
"unsafe"
)
var (
@ -20,7 +19,7 @@ var (
characters = letters + digits + symbols // 94
)
// Intn returns a int number which is between 0 and max: [0, max).
// Intn returns an int number which is between 0 and max: [0, max).
//
// Note that:
// 1. The `max` can only be greater than 0, or else it returns `max` directly;
@ -95,7 +94,7 @@ func S(n int, symbols ...bool) string {
b[i] = characters[numberBytes[i]%62]
}
}
return *(*string)(unsafe.Pointer(&b))
return string(b)
}
// D returns a random time.Duration between min and max: [min, max].
@ -147,7 +146,7 @@ func Digits(n int) string {
for i := range b {
b[i] = digits[numberBytes[i]%10]
}
return *(*string)(unsafe.Pointer(&b))
return string(b)
}
// Letters returns a random string which contains only letters, and its length is `n`.
@ -162,7 +161,7 @@ func Letters(n int) string {
for i := range b {
b[i] = letters[numberBytes[i]%52]
}
return *(*string)(unsafe.Pointer(&b))
return string(b)
}
// Symbols returns a random string which contains only symbols, and its length is `n`.
@ -177,7 +176,7 @@ func Symbols(n int) string {
for i := range b {
b[i] = symbols[numberBytes[i]%32]
}
return *(*string)(unsafe.Pointer(&b))
return string(b)
}
// Perm returns, as a slice of n int numbers, a pseudo-random permutation of the integers [0,n).