Search Apps Documentation Source Content File Folder Download Copy Actions Download State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

types.gno

2.12 Kb · 82 lines
 1package types
 2
 3import (
 4	"encoding/hex"
 5	"strconv"
 6)
 7
 8type ClientId uint32
 9
10func (id ClientId) String() string {
11	return strconv.FormatUint(uint64(id), 10)
12}
13
14type ConnectionId uint32
15
16func (id ConnectionId) String() string {
17	return strconv.FormatUint(uint64(id), 10)
18}
19
20type Timestamp uint64
21
22func (ts Timestamp) String() string {
23	return strconv.FormatUint(uint64(ts), 10)
24}
25
26type Bytes []byte
27
28func (bz Bytes) String() string {
29	return "0x" + hex.EncodeToString(bz)
30}
31
32func (bz Bytes) Bytes() []byte {
33	out := make([]byte, len(bz))
34	copy(out, bz)
35
36	return out
37}
38
39// Clone returns a fresh Bytes detached from any external/readonly realm
40// ownership of bz.
41//
42// Under gno's InterRealm v2 borrow rules a value that crossed a realm boundary
43// is externally stored, and a direct slice->slice conversion of it (a plain
44// types.Bytes->[]byte cast, or a []byte->string->[]byte round-trip, which gno
45// elides) keeps the foreign ownership and panics with "illegal conversion of
46// readonly or externally stored value" when later stored or re-emitted. copy is
47// an element-wise read, not a conversion, so it is permitted on a readonly
48// source and yields a value owned by the caller's realm. Use Clone before
49// persisting or re-emitting Bytes that arrived from another realm.
50func (bz Bytes) Clone() Bytes {
51	return Bytes(bz.Bytes())
52}
53
54// CloneBytes returns a fresh copy of b, or nil for nil input. It is the []byte
55// counterpart of Bytes.Clone: realm packages use it to detach the InterRealm v2
56// ownership of a slice that crossed a realm boundary before storing or
57// re-emitting it. copy is an element-wise read (permitted on a foreign/readonly
58// slice); a slice->slice conversion or a []byte->string->[]byte round-trip (which
59// gno elides) would keep the foreign ownership and later panic with "illegal
60// conversion of readonly or externally stored value".
61func CloneBytes(b []byte) []byte {
62	if b == nil {
63		return nil
64	}
65
66	out := make([]byte, len(b))
67	copy(out, b)
68
69	return out
70}
71
72type H256 [32]byte
73
74func (h H256) String() string {
75	return "0x" + hex.EncodeToString(h[:])
76}
77
78type ClientType string
79
80func (ct ClientType) String() string {
81	return string(ct)
82}