github.com/NVIDIA/aistore@v1.3.23-0.20240517131212-7df6609be51d/cmn/cos/unsafe.go (about) 1 // Package cos provides common low-level types and utilities for all aistore projects. 2 /* 3 * Copyright (c) 2018-2024, NVIDIA CORPORATION. All rights reserved. 4 */ 5 package cos 6 7 import ( 8 "reflect" 9 "unsafe" 10 11 "github.com/NVIDIA/aistore/cmn/debug" 12 ) 13 14 const MLCG32 = 1103515245 // xxhash seed 15 16 // assorted common constants 17 const ( 18 SizeofI64 = int(unsafe.Sizeof(uint64(0))) 19 SizeofI32 = int(unsafe.Sizeof(uint32(0))) 20 SizeofI16 = int(unsafe.Sizeof(uint16(0))) 21 ) 22 23 // Unsafe cast (string => []byte) and ([]byte => string) 24 // ******* CAUTION! the result must never change ******* 25 26 // cast bytes to an immutable string 27 func UnsafeS(b []byte) string { 28 return *(*string)(unsafe.Pointer(&b)) 29 } 30 31 // cast string to immutable bytes 32 func UnsafeB(s string) []byte { 33 return unsafe.Slice(unsafe.StringData(s), len(s)) 34 } 35 36 // shallow copy 37 func CopyStruct(dst, src any) { 38 x := reflect.ValueOf(src) 39 debug.Assert(x.Kind() == reflect.Ptr) 40 starX := x.Elem() 41 y := reflect.New(starX.Type()) 42 starY := y.Elem() 43 starY.Set(starX) 44 reflect.ValueOf(dst).Elem().Set(y.Elem()) 45 }