Add unsafe string and slice conversions

This commit is contained in:
Fabian Reinartz 2016-12-15 11:56:41 +01:00
parent 9ceed5378e
commit b2f1db5666
1 changed files with 23 additions and 0 deletions

23
db.go
View File

@ -6,10 +6,12 @@ import (
"fmt"
"os"
"path/filepath"
"reflect"
"sort"
"strconv"
"sync"
"time"
"unsafe"
"golang.org/x/sync/errgroup"
@ -490,15 +492,36 @@ func (es MultiError) Error() string {
return buf.String()
}
// Add adds the error to the error list if it is not nil.
func (es MultiError) Add(err error) {
if err != nil {
es = append(es, err)
}
}
// Err returns the error list as an error or nil if it is empty.
func (es MultiError) Err() error {
if len(es) == 0 {
return nil
}
return es
}
func yoloString(b []byte) string {
h := reflect.StringHeader{
Data: uintptr(unsafe.Pointer(&b[0])),
Len: len(b),
}
return *((*string)(unsafe.Pointer(&h)))
}
func yoloBytes(s string) []byte {
sh := (*reflect.StringHeader)(unsafe.Pointer(&s))
h := reflect.SliceHeader{
Cap: sh.Len,
Len: sh.Len,
Data: sh.Data,
}
return *((*[]byte)(unsafe.Pointer(&h)))
}