forked from chai2010/advanced-go-programming-book
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathch2-3-x-slice-to-y-slice.plantuml
More file actions
36 lines (21 loc) · 892 Bytes
/
ch2-3-x-slice-to-y-slice.plantuml
File metadata and controls
36 lines (21 loc) · 892 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
' Copyright 2017 <chaishushan{AT}gmail.com>. All rights reserved.
' Use of this source code is governed by a Apache
' license that can be found in the LICENSE file.
'var p []X
'var q []Y // q = p
'pHdr := (*reflect.SliceHeader)(unsafe.Pointer(&p))
'qHdr := (*reflect.SliceHeader)(unsafe.Pointer(&q))
'pHdr.Data = qHdr.Data
'pHdr.Len = qHdr.Len * unsafe.Sizeof(q[0]) / unsafe.Sizeof(p[0])
'pHdr.Cap = qHdr.Cap * unsafe.Sizeof(q[0]) / unsafe.Sizeof(p[0])
@startuml
title []X <=> []Y
participant "var x []X\nvar y []Y" as slice
participant "var px *SliceHeader\nvar py *SliceHeader" as slice_header
== []X and []Y to *reflect.SliceHeader ==
slice -> slice_header: px := (*reflect.SliceHeader)(unsafe.Pointer(&x))\npy := (*reflect.SliceHeader)(unsafe.Pointer(&y))
== copy *px to *py ==
slice_header -> slice_header: ~*py = *px
== y changed by *py ==
slice -> slice: y = x
@enduml