Re: [go-nuts] unsafe string to []byte

2021-08-03 Thread Robert Engels
That’s cool. I don’t think most libraries using unsafe go to that level of detail or scrutiny - which can be the source of a lot of subtle failures. > On Aug 3, 2021, at 3:35 PM, 'Bryan C. Mills' via golang-nuts > wrote: > > For what it's worth, my unsafeslice.OfString makes a best effort to

Re: [go-nuts] unsafe string to []byte

2021-08-03 Thread 'Bryan C. Mills' via golang-nuts
For what it's worth, my unsafeslice.OfString makes a best effort to detect mutations of the sort that would occur when a Write implementation violates the io.Writer contract. It allows for vary levels of safety. Under `-race` it

Re: [go-nuts] unsafe string to []byte

2021-07-27 Thread Robert Engels
Agreed. If a Go library uses unsafe I avoid it. > On Jul 27, 2021, at 9:54 AM, 'Axel Wagner' via golang-nuts > wrote: > >  >> On Tue, Jul 27, 2021 at 4:15 PM Steve Roth wrote: > >> The implementation of io.WriteString appears to allocate a new byte slice >> and copy the string into it: >>

Re: [go-nuts] unsafe string to []byte

2021-07-27 Thread 'Axel Wagner' via golang-nuts
On Tue, Jul 27, 2021 at 4:15 PM Steve Roth wrote: > The implementation of io.WriteString appears to allocate a new byte slice > and copy the string into it: > > w.Write([]byte(s)) > > Only if the writer does not implement `io.StringWriter`. Avoiding this allocation where possible is exactly why `

[go-nuts] unsafe string to []byte

2021-07-27 Thread Steve Roth
The implementation of io.WriteString appears to allocate a new byte slice and copy the string into it: w.Write([]byte(s)) Many third party libraries avoid the allocation and copy with techniques like: var b []byte sh := (*reflect.StringHeader)(unsafe.Pointer(&s)) bh := (*reflect.SliceHeader)(uns