-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
reflect: fix incorrect nil copy #5
Open
alresvor
wants to merge
1
commit into
golang-design:main
Choose a base branch
from
alresvor:patch-3
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里很有意思,为什么UnsafePointer不能复制呢?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
我看 UnsafePointer 不像 Ptr 类型一样可以推出它实际的类型,没办法用 reflect.New 新建一个(也许可以?)
大概只能像 Func 或 Int 类型一样,直接进行复制一份
不知道你是怎么决定这个值怎么复制的,我对反射不太了解,这里的判断如果不去掉,调用 copyPointer() 会报错的,所以就先去掉了 😄
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这是个好问题,确实之前没有考虑过unsafe.pointer的实际类型..
是不是能够把 unsafe.pointer 直接当成整数处理呢?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
个人认为是不可以的,因为是不正确的,例如 DeepCopy 把 UnsafePointer 当成 Int 复制一份:
那么就会出现这种非常常见的错误,所以复制它毫无意义,因为没有复制底层数据
当然可以当作无事发生,因为这种类型还是比较少见的,不过这样就不算 DeepCopy 了,如果要保证正确性的话,应该忽略处理它,直接报错
不过也许还有其他解决办法,比如现在的问题是 unsafe.Pointer 无法确认真正类型
但是如果把 copyAny 改为泛型,那么遇到 unsafe.Pointer 类型,可以再进行一次转换调用
这样就可以当作一个正常的 Ptrinter 类型走 copyPointer 流程处理了,例如判断 src 是 unsafe.Pointer 后可以:
dst = copyPointer((*T)(src), ptrs, copyConf)
这只是一个想法,我没有验证过
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
唉 ,我最后说的方法应该不行,我想的太简单了...
因为 DeepCopy(unsafe.Porint(xxxx)) 时,参数就是 unsafe.Pointer 类型,根本没法推断,除非添加一个类型推断参数
但是如果复制结构字段时可不能指定类型,所以无法实现 😔