File: value.go

package info (click to toggle)
golang-golang-x-exp 0.0~git20250911.df92998-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 7,284 kB
  • sloc: ansic: 1,900; objc: 276; sh: 270; asm: 48; makefile: 27
file content (71 lines) | stat: -rw-r--r-- 1,692 bytes parent folder | download
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// Copyright 2023 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// Code generated by "gen.bash" from internal/trace; DO NOT EDIT.

//go:build go1.23

package trace

import (
	"fmt"
	"unsafe"
)

// Value is a dynamically-typed value obtained from a trace.
type Value struct {
	kind    ValueKind
	pointer unsafe.Pointer
	scalar  uint64
}

// ValueKind is the type of a dynamically-typed value from a trace.
type ValueKind uint8

const (
	ValueBad ValueKind = iota
	ValueUint64
	ValueString
)

// Kind returns the ValueKind of the value.
//
// It represents the underlying structure of the value.
//
// New ValueKinds may be added in the future. Users of this type must be robust
// to that possibility.
func (v Value) Kind() ValueKind {
	return v.kind
}

// Uint64 returns the uint64 value for a ValueUint64.
//
// Panics if this Value's Kind is not ValueUint64.
func (v Value) Uint64() uint64 {
	if v.kind != ValueUint64 {
		panic("Uint64 called on Value of a different Kind")
	}
	return v.scalar
}

// String returns the string value for a ValueString, and otherwise
// a string representation of the value for other kinds of values.
func (v Value) String() string {
	if v.kind == ValueString {
		return unsafe.String((*byte)(v.pointer), int(v.scalar))
	}
	switch v.kind {
	case ValueUint64:
		return fmt.Sprintf("Value{Uint64(%d)}", v.Uint64())
	}
	return "Value{Bad}"
}

func uint64Value(x uint64) Value {
	return Value{kind: ValueUint64, scalar: x}
}

func stringValue(s string) Value {
	return Value{kind: ValueString, scalar: uint64(len(s)), pointer: unsafe.Pointer(unsafe.StringData(s))}
}