Skip to content

Commit

Permalink
unsafestr: update to use unsafe.Slice()
Browse files Browse the repository at this point in the history
Adds support for tinygo 0.32, thereby fixing #29

Passes tests on macos intel:
- go 1.21.9:
	- go test
- go 1.21.9 / tinygo 0.32 prerelease:
	- tinygo test
	- tinygo test -target wasi
  • Loading branch information
dkegel-fastly authored and philhofer committed Jun 12, 2024
1 parent 2ae61b8 commit fbbf495
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 8 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,15 @@ the write position by the length of the returned slice. This allows users
to write directly to the end of the buffer.


## Portability

Because it uses the unsafe package, there are theoretically
no promises about forward or backward portability.

To stay compatible with tinygo 0.32, unsafestr() has been updated
to use unsafe.Slice() as suggested by
https://tinygo.org/docs/guides/compatibility, which also required
bumping go.mod to require at least go 1.20.


## <a name="pkg-index">Index</a>
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module github.com/philhofer/fwd

go 1.15
go 1.20 // unsafe.StringData requires go1.20 or later
8 changes: 1 addition & 7 deletions writer_tinygo.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,10 @@
package fwd

import (
"reflect"
"unsafe"
)

// unsafe cast string as []byte
func unsafestr(b string) []byte {
l := uintptr(len(b))
return *(*[]byte)(unsafe.Pointer(&reflect.SliceHeader{
Len: l,
Cap: l,
Data: (*reflect.StringHeader)(unsafe.Pointer(&b)).Data,
}))
return unsafe.Slice(unsafe.StringData(b), len(b))
}

0 comments on commit fbbf495

Please sign in to comment.