Use the modern unsafe replacement of reflect

As of Go1.20, “safer” versions of pointer casting are available to point to the
same address. Use them.
This commit is contained in:
Romain Doumenc 2023-09-30 12:48:53 +01:00
parent d8d85b3536
commit 951bf2c8bd
No known key found for this signature in database
2 changed files with 2 additions and 6 deletions

2
go.mod
View File

@ -1,3 +1,3 @@
module github.com/stianeikeland/go-rpio/v4
go 1.15
go 1.21

View File

@ -73,7 +73,6 @@ import (
"encoding/binary"
"errors"
"os"
"reflect"
"sync"
"syscall"
"time"
@ -803,10 +802,7 @@ func memMap(fd uintptr, base int64) (mem []uint32, mem8 []byte, err error) {
return
}
// Convert mapped byte memory to unsafe []uint32 pointer, adjust length as needed
header := *(*reflect.SliceHeader)(unsafe.Pointer(&mem8))
header.Len /= (32 / 8) // (32 bit = 4 bytes)
header.Cap /= (32 / 8)
mem = *(*[]uint32)(unsafe.Pointer(&header))
mem = unsafe.Slice((*uint32)(unsafe.Pointer(unsafe.SliceData(mem8))), len(mem8)/4)
return
}