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

23
db.go
View file

@ -6,10 +6,12 @@ import (
"fmt" "fmt"
"os" "os"
"path/filepath" "path/filepath"
"reflect"
"sort" "sort"
"strconv" "strconv"
"sync" "sync"
"time" "time"
"unsafe"
"golang.org/x/sync/errgroup" "golang.org/x/sync/errgroup"
@ -490,15 +492,36 @@ func (es MultiError) Error() string {
return buf.String() return buf.String()
} }
// Add adds the error to the error list if it is not nil.
func (es MultiError) Add(err error) { func (es MultiError) Add(err error) {
if err != nil { if err != nil {
es = append(es, err) es = append(es, err)
} }
} }
// Err returns the error list as an error or nil if it is empty.
func (es MultiError) Err() error { func (es MultiError) Err() error {
if len(es) == 0 { if len(es) == 0 {
return nil return nil
} }
return es 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)))
}