Re: [go-nuts] map type conversion

2023-07-26 Thread Nigel van Keulen
Maybe my question could be of interest in this topic. I am not sure, and 
haven't tested if it works with maps, and I am also not sure if it is 
supposed to work / will work consistently. 
https://groups.google.com/g/golang-nuts/c/0RqzD2jNADE/m/3Hf14DllBAAJ?utm_medium=email&utm_source=footer
Op dinsdag 18 juli 2023 om 09:22:33 UTC+2 schreef Jakob Borg:

> On 17 Jul 2023, at 14:50, Leonard Mittmann  wrote:
>
>
> Yes, but my thought was that it might be possible to do an unsafe 
> conversion as the underlying types are the same. I just have no idea how.
>
>
> https://go.dev/play/p/ViLnLvInv-1
>
> The unsafe conversion appears to work, but it’s unsafe and not obvious to 
> me that this is guaranteed to work. It’s allowed to convert one pointer to 
> another if they have an “equivalent memory layout”, and I would guess that 
> two map types with equivalent underlying types would share memory layout, 
> but I don’t think the specs say anything about map memory layouts one way 
> or the other.
>
> //jb
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/18774f52-1d62-45fb-bdde-6322c0476a23n%40googlegroups.com.


[go-nuts] unsafe.Pointer conversions

2023-07-26 Thread Nigel van Keulen
Since unsafe.Pointer can convert between types that have the same 
underlying type, can unsafe.Pointer also convert between function types 
who's arguments & return values have the same underlying type?

The code I would like to use this technique on:
```
type JSExtFunc func(this Value, args Args) interface{} 

func (f JSExtFunc) MarshalJS() js.Func { 
// I am not sure if this is correct. 
var function = *(*func(this js.Value, args []js.Value) interface
{})(unsafe.Pointer(&f)) 
return js.FuncOf(function)
 } 
// Arguments for wrapped functions. 
type Args []js.Value 
type Value js.Value
```

I have tried this out in the go playground, and it does seem to work.
https://go.dev/play/p/XVwRatkuTQv

However, this is undocumented in the language specification. 
How likely is this that this code would break, using the same version of Go?
Is this perhaps something to be added in the language specification?

Maybe even a more interesting question, breaking all safety guards, could 
this be extended by casting a function which takes a uint64 (`func 
myFunc(uint64)`) to a function which takes two uint32's? (`*(*func(uint32, 
uint32)(unsafe.Pointer(ptrToMyFuncValue)))`)

The latter is something I do not nescessarily desire an answer to, but it 
does make me curious.

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/2975445d-69ff-46fe-bc44-3cc4bb515fa8n%40googlegroups.com.