internal/lib/reflect: Value.SetZero

This commit is contained in:
visualfc
2024-11-14 10:25:23 +08:00
parent 15a6c779b8
commit 792716eefc

View File

@@ -908,58 +908,55 @@ func (v Value) IsZero() bool {
// SetZero sets v to be the zero value of v's type. // SetZero sets v to be the zero value of v's type.
// It panics if CanSet returns false. // It panics if CanSet returns false.
func (v Value) SetZero() { func (v Value) SetZero() {
/* v.mustBeAssignable()
v.mustBeAssignable() switch v.kind() {
switch v.kind() { case Bool:
case Bool: *(*bool)(v.ptr) = false
*(*bool)(v.ptr) = false case Int:
case Int: *(*int)(v.ptr) = 0
*(*int)(v.ptr) = 0 case Int8:
case Int8: *(*int8)(v.ptr) = 0
*(*int8)(v.ptr) = 0 case Int16:
case Int16: *(*int16)(v.ptr) = 0
*(*int16)(v.ptr) = 0 case Int32:
case Int32: *(*int32)(v.ptr) = 0
*(*int32)(v.ptr) = 0 case Int64:
case Int64: *(*int64)(v.ptr) = 0
*(*int64)(v.ptr) = 0 case Uint:
case Uint: *(*uint)(v.ptr) = 0
*(*uint)(v.ptr) = 0 case Uint8:
case Uint8: *(*uint8)(v.ptr) = 0
*(*uint8)(v.ptr) = 0 case Uint16:
case Uint16: *(*uint16)(v.ptr) = 0
*(*uint16)(v.ptr) = 0 case Uint32:
case Uint32: *(*uint32)(v.ptr) = 0
*(*uint32)(v.ptr) = 0 case Uint64:
case Uint64: *(*uint64)(v.ptr) = 0
*(*uint64)(v.ptr) = 0 case Uintptr:
case Uintptr: *(*uintptr)(v.ptr) = 0
*(*uintptr)(v.ptr) = 0 case Float32:
case Float32: *(*float32)(v.ptr) = 0
*(*float32)(v.ptr) = 0 case Float64:
case Float64: *(*float64)(v.ptr) = 0
*(*float64)(v.ptr) = 0 case Complex64:
case Complex64: *(*complex64)(v.ptr) = 0
*(*complex64)(v.ptr) = 0 case Complex128:
case Complex128: *(*complex128)(v.ptr) = 0
*(*complex128)(v.ptr) = 0 case String:
case String: *(*string)(v.ptr) = ""
*(*string)(v.ptr) = "" case Slice:
case Slice: *(*unsafeheaderSlice)(v.ptr) = unsafeheaderSlice{}
*(*unsafeheader.Slice)(v.ptr) = unsafeheader.Slice{} case Interface:
case Interface: *(*[2]unsafe.Pointer)(v.ptr) = [2]unsafe.Pointer{}
*(*[2]unsafe.Pointer)(v.ptr) = [2]unsafe.Pointer{} case Chan, Func, Map, Pointer, UnsafePointer:
case Chan, Func, Map, Pointer, UnsafePointer: *(*unsafe.Pointer)(v.ptr) = nil
*(*unsafe.Pointer)(v.ptr) = nil case Array, Struct:
case Array, Struct: typedmemclr(v.typ(), v.ptr)
typedmemclr(v.typ(), v.ptr) default:
default: // This should never happen, but will act as a safeguard for later,
// This should never happen, but will act as a safeguard for later, // as a default value doesn't makes sense here.
// as a default value doesn't makes sense here. panic(&ValueError{"reflect.Value.SetZero", v.Kind()})
panic(&ValueError{"reflect.Value.SetZero", v.Kind()}) }
}
*/
panic("todo: reflect.Value.SetZero")
} }
// Kind returns v's Kind. // Kind returns v's Kind.