diff options
author | Matthew Dempsky <mdempsky@google.com> | 2021-07-22 12:50:30 -0700 |
---|---|---|
committer | Matthew Dempsky <mdempsky@google.com> | 2021-07-22 12:50:31 -0700 |
commit | a27e325c59691fba23c094ab07fd5735737ac8ba (patch) | |
tree | 2dc7632b501359c291d95d7d15cd37f347727cd8 /src/reflect/value.go | |
parent | 5cb84f0604797df436d8fde548d4f797b3a6c245 (diff) | |
parent | 798ec73519a7226d6d436e42498a54aed23b8468 (diff) | |
download | go-a27e325c59691fba23c094ab07fd5735737ac8ba.tar.gz go-a27e325c59691fba23c094ab07fd5735737ac8ba.zip |
[dev.typeparams] all: merge master (798ec73) into dev.typeparams
Merge List:
+ 2021-07-22 798ec73519 runtime: don't clear timerModifiedEarliest if adjustTimers is 0
+ 2021-07-22 fdb45acd1f runtime: move mem profile sampling into m-acquired section
+ 2021-07-21 3e48c0381f reflect: add missing copyright header
+ 2021-07-21 48c88f1b1b reflect: add Value.CanConvert
+ 2021-07-20 9e26569293 cmd/go: don't add C compiler ID to hash for standard library
+ 2021-07-20 d568e6e075 runtime/debug: skip TestPanicOnFault on netbsd/arm
Change-Id: I87e1cd4614bb3b00807f18dfdd02664dcaecaebd
Diffstat (limited to 'src/reflect/value.go')
-rw-r--r-- | src/reflect/value.go | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/src/reflect/value.go b/src/reflect/value.go index 7589966e22..b4b2d2e38b 100644 --- a/src/reflect/value.go +++ b/src/reflect/value.go @@ -2807,6 +2807,26 @@ func (v Value) Convert(t Type) Value { return op(v, t) } +// CanConvert reports whether the value v can be converted to type t. +// If v.CanConvert(t) returns true then v.Convert(t) will not panic. +func (v Value) CanConvert(t Type) bool { + vt := v.Type() + if !vt.ConvertibleTo(t) { + return false + } + // Currently the only conversion that is OK in terms of type + // but that can panic depending on the value is converting + // from slice to pointer-to-array. + if vt.Kind() == Slice && t.Kind() == Ptr && t.Elem().Kind() == Array { + n := t.Elem().Len() + h := (*unsafeheader.Slice)(v.ptr) + if n > h.Len { + return false + } + } + return true +} + // convertOp returns the function to convert a value of type src // to a value of type dst. If the conversion is illegal, convertOp returns nil. func convertOp(dst, src *rtype) func(Value, Type) Value { |