ref: 1334ea3e45aaf9d2620223215763443647aae5a7
parent: fe51d94ec1ae701737c3f6ece74904bdf9c0f17b
author: Runxi Yu <me@runxiyu.org>
date: Sat Feb 21 05:05:19 EST 2026
objectid: Add RawBytes
--- a/objectid/objectid.go
+++ b/objectid/objectid.go
@@ -159,6 +159,17 @@
return append([]byte(nil), id.data[:size]...)
}
+// RawBytes returns a direct byte slice view of the object ID bytes.
+//
+// The returned slice aliases the object ID's internal storage. Callers MUST
+// treat it as read-only and MUST NOT modify its contents.
+//
+// Use Bytes when an independent copy is required.
+func (id *ObjectID) RawBytes() []byte {+ size := id.Size()
+ return id.data[:size:size]
+}
+
// ParseHex parses an object ID from hex for the specified algorithm.
func ParseHex(algo Algorithm, s string) (ObjectID, error) {var id ObjectID
--- a/objectid/objectid_test.go
+++ b/objectid/objectid_test.go
@@ -127,6 +127,29 @@
}
}
+func TestRawBytesAliasesStorage(t *testing.T) {+ t.Parallel()
+
+ id, err := objectid.ParseHex(objectid.AlgorithmSHA1, "0123456789abcdef0123456789abcdef01234567")
+ if err != nil {+ t.Fatalf("ParseHex failed: %v", err)+ }
+
+ b := id.RawBytes()
+ if len(b) != id.Size() {+ t.Fatalf("RawBytes len = %d, want %d", len(b), id.Size())+ }
+ if cap(b) != len(b) {+ t.Fatalf("RawBytes cap = %d, want %d", cap(b), len(b))+ }
+
+ orig := id.String()
+ b[0] ^= 0xff
+ if id.String() == orig {+ t.Fatalf("RawBytes should alias object ID storage")+ }
+}
+
func TestAlgorithmSum(t *testing.T) {t.Parallel()
--
⑨