unsafeconv

package
v2.4.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 23, 2026 License: MIT Imports: 1 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Bytes added in v2.3.0

func Bytes[S ~string | ~[]byte](s S) []byte

Bytes returns a []byte view of s without allocation for any string- or byte-slice-backed type. It relies on string and slice headers both starting with the data pointer (guaranteed by the gc runtime layout, and the same assumption UnsafeString makes). When S's underlying type is string, the result aliases immutable memory and must only be read.

func Seq added in v2.4.0

func Seq[S ~string | ~[]byte](b []byte) S

Seq is Bytes' inverse: it returns b viewed as S without allocation, for either instantiation. It relies on the string header being a prefix of the slice header (data pointer, then length), so reinterpreting a slice header yields a valid string view; the []byte instantiation reads the full header back unchanged. The caller must guarantee b is never written again when S's underlying type is string, since the result would alias memory the runtime assumes immutable.

func UnsafeBytes

func UnsafeBytes(s string) []byte

UnsafeBytes returns a byte slice view of s without allocation.

func UnsafeString

func UnsafeString(b []byte) string

UnsafeString returns a string view of b without allocation.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL