Skip to content

Commit

Permalink
update to go1.20 unsafe
Browse files Browse the repository at this point in the history
  • Loading branch information
deankarn committed Feb 13, 2024
1 parent 8a496cb commit a37eb90
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
2 changes: 2 additions & 0 deletions unsafe/conversions.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build !go1.20

package unsafeext

import (
Expand Down
21 changes: 21 additions & 0 deletions unsafe/conversions_go121.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//go:build go1.20

package unsafeext

import (
"unsafe"
)

// BytesToString converts an array of bytes into a string without allocating.
// The byte slice passed to this function is not to be used after this call as it's unsafe; you have been warned.
func BytesToString(b []byte) string {
return unsafe.String(unsafe.SliceData(b), len(b))

Check failure on line 12 in unsafe/conversions_go121.go

View workflow job for this annotation

GitHub Actions / test (1.20.x, ubuntu-latest)

unsafe.SliceData requires go1.20 or later (-lang was set to go1.18; check go.mod)
}

// StringToBytes converts an existing string into an []byte without allocating.
// The string passed to these functions is not to be used again after this call as it's unsafe; you have been warned.
func StringToBytes(s string) (b []byte) {
d := unsafe.StringData(s)

Check failure on line 18 in unsafe/conversions_go121.go

View workflow job for this annotation

GitHub Actions / test (1.20.x, ubuntu-latest)

unsafe.StringData requires go1.20 or later (-lang was set to go1.18; check go.mod)
b = unsafe.Slice(d, len(s))
return
}

0 comments on commit a37eb90

Please sign in to comment.