-
Notifications
You must be signed in to change notification settings - Fork 375
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use new
unsafe
functions introduced in go 1.20
This eliminates usage of deprecated and error prone `reflect.SliceHeader` and `reflect.StringHeader`. Backwards compatibility saved with build tags
- Loading branch information
Showing
5 changed files
with
46 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
//go:build !purego | ||
// +build !purego | ||
//go:build !purego && !go1.20 | ||
// +build !purego,!go1.20 | ||
|
||
package ws | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
//go:build !purego && go1.20 | ||
// +build !purego,go1.20 | ||
|
||
package ws | ||
|
||
import "unsafe" | ||
|
||
func strToBytes(str string) (bts []byte) { | ||
return unsafe.Slice(unsafe.StringData(str), len(str)) | ||
} | ||
|
||
func btsToString(bts []byte) (str string) { | ||
return unsafe.String(&bts[0], len(bts)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
//go:build go1.20 | ||
// +build go1.20 | ||
|
||
package wsutil | ||
|
||
import ( | ||
"unsafe" | ||
) | ||
|
||
func fakeMake(n int) []byte { | ||
return unsafe.Slice((*byte)(nil), n) // len and cap are 'n' | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
//go:build !go1.20 | ||
// +build !go1.20 | ||
|
||
package wsutil | ||
|
||
import ( | ||
"reflect" | ||
"unsafe" | ||
) | ||
|
||
func fakeMake(n int) (r []byte) { | ||
rh := (*reflect.SliceHeader)(unsafe.Pointer(&r)) | ||
*rh = reflect.SliceHeader{ | ||
Len: n, | ||
Cap: n, | ||
} | ||
return r | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters