aboutsummaryrefslogtreecommitdiff
path: root/src/reflect/value.go
diff options
context:
space:
mode:
authorMatthew Dempsky <mdempsky@google.com>2021-07-22 12:50:30 -0700
committerMatthew Dempsky <mdempsky@google.com>2021-07-22 12:50:31 -0700
commita27e325c59691fba23c094ab07fd5735737ac8ba (patch)
tree2dc7632b501359c291d95d7d15cd37f347727cd8 /src/reflect/value.go
parent5cb84f0604797df436d8fde548d4f797b3a6c245 (diff)
parent798ec73519a7226d6d436e42498a54aed23b8468 (diff)
downloadgo-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.go20
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 {