Skip to content

Commit 5fdb965

Browse files
Copilotadlerjohn
andcommitted
Export DeepCopy function for external library users
Co-authored-by: adlerjohn <[email protected]>
1 parent 6e4704f commit 5fdb965

File tree

2 files changed

+19
-19
lines changed

2 files changed

+19
-19
lines changed

extendeddatasquare.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -234,15 +234,15 @@ func (eds *ExtendedDataSquare) deepCopy(codec Codec) (ExtendedDataSquare, error)
234234

235235
// Col returns a column slice.
236236
// The returned slice references internal data and should not be modified.
237-
// If modification is needed, the caller should make a copy using deepCopy().
237+
// If modification is needed, the caller should make a copy using DeepCopy().
238238
func (eds *ExtendedDataSquare) Col(colIdx uint) [][]byte {
239239
return eds.col(colIdx)
240240
}
241241

242242
// ColRoots returns the Merkle roots of all the columns in the square. Returns
243243
// an error if the EDS is incomplete (i.e. some shares are nil).
244244
// The returned slice references internal data and should not be modified.
245-
// If modification is needed, the caller should make a copy using deepCopy().
245+
// If modification is needed, the caller should make a copy using DeepCopy().
246246
func (eds *ExtendedDataSquare) ColRoots() ([][]byte, error) {
247247
colRoots, err := eds.getColRoots()
248248
if err != nil {
@@ -253,15 +253,15 @@ func (eds *ExtendedDataSquare) ColRoots() ([][]byte, error) {
253253

254254
// Row returns a row slice.
255255
// The returned slice references internal data and should not be modified.
256-
// If modification is needed, the caller should make a copy using deepCopy().
256+
// If modification is needed, the caller should make a copy using DeepCopy().
257257
func (eds *ExtendedDataSquare) Row(rowIdx uint) [][]byte {
258258
return eds.row(rowIdx)
259259
}
260260

261261
// RowRoots returns the Merkle roots of all the rows in the square. Returns an
262262
// error if the EDS is incomplete (i.e. some shares are nil).
263263
// The returned slice references internal data and should not be modified.
264-
// If modification is needed, the caller should make a copy using deepCopy().
264+
// If modification is needed, the caller should make a copy using DeepCopy().
265265
func (eds *ExtendedDataSquare) RowRoots() ([][]byte, error) {
266266
rowRoots, err := eds.getRowRoots()
267267
if err != nil {
@@ -270,11 +270,11 @@ func (eds *ExtendedDataSquare) RowRoots() ([][]byte, error) {
270270
return rowRoots, nil
271271
}
272272

273-
// deepCopy returns a deep copy of the given slice of byte slices.
273+
// DeepCopy returns a deep copy of the given slice of byte slices.
274274
// This function is available for callers who need to make a copy of data
275275
// returned by methods like Row(), Col(), RowRoots(), ColRoots(), or Flattened()
276276
// when they intend to modify the data.
277-
func deepCopy(original [][]byte) [][]byte {
277+
func DeepCopy(original [][]byte) [][]byte {
278278
dest := make([][]byte, len(original))
279279
for i, cell := range original {
280280
if cell == nil {
@@ -293,7 +293,7 @@ func (eds *ExtendedDataSquare) Width() uint {
293293

294294
// Flattened returns the extended data square as a flattened slice of bytes.
295295
// The returned slice references internal data and should not be modified.
296-
// If modification is needed, the caller should make a copy using deepCopy().
296+
// If modification is needed, the caller should make a copy using DeepCopy().
297297
func (eds *ExtendedDataSquare) Flattened() [][]byte {
298298
return eds.dataSquare.Flattened()
299299
}

extendeddatasquare_test.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,7 @@ func TestDeepCopy(t *testing.T) {
433433
require.NoError(t, err)
434434
}
435435

436-
copied := deepCopy(original)
436+
copied := DeepCopy(original)
437437
require.Equal(t, original, copied)
438438

439439
// modify the original and ensure the copy is not affected
@@ -442,7 +442,7 @@ func TestDeepCopy(t *testing.T) {
442442
}
443443

444444
// TestDirectReferences verifies that the exported methods return direct references
445-
// to internal data for performance reasons, and demonstrates how to use deepCopy
445+
// to internal data for performance reasons, and demonstrates how to use DeepCopy
446446
// when modification is needed.
447447
func TestDirectReferences(t *testing.T) {
448448
codec := NewLeoRSCodec()
@@ -459,8 +459,8 @@ func TestDirectReferences(t *testing.T) {
459459
// Should be the same underlying slice (direct reference)
460460
require.True(t, &row1[0] == &row2[0], "Row() should return direct references")
461461

462-
// If caller needs to modify, they should use deepCopy
463-
rowCopy := deepCopy(row1)
462+
// If caller needs to modify, they should use DeepCopy
463+
rowCopy := DeepCopy(row1)
464464
rowCopy[0][0]++ // safe to modify
465465

466466
// Original should be unchanged
@@ -474,8 +474,8 @@ func TestDirectReferences(t *testing.T) {
474474
// Should be the same underlying slice (direct reference)
475475
require.True(t, &col1[0] == &col2[0], "Col() should return direct references")
476476

477-
// If caller needs to modify, they should use deepCopy
478-
colCopy := deepCopy(col1)
477+
// If caller needs to modify, they should use DeepCopy
478+
colCopy := DeepCopy(col1)
479479
colCopy[0][0]++ // safe to modify
480480

481481
// Original should be unchanged
@@ -491,8 +491,8 @@ func TestDirectReferences(t *testing.T) {
491491
// Should be the same underlying slice (direct reference)
492492
require.True(t, &roots1[0] == &roots2[0], "RowRoots() should return direct references")
493493

494-
// If caller needs to modify, they should use deepCopy
495-
rootsCopy := deepCopy(roots1)
494+
// If caller needs to modify, they should use DeepCopy
495+
rootsCopy := DeepCopy(roots1)
496496
rootsCopy[0][0]++ // safe to modify
497497

498498
// Original should be unchanged
@@ -508,8 +508,8 @@ func TestDirectReferences(t *testing.T) {
508508
// Should be the same underlying slice (direct reference)
509509
require.True(t, &roots1[0] == &roots2[0], "ColRoots() should return direct references")
510510

511-
// If caller needs to modify, they should use deepCopy
512-
rootsCopy := deepCopy(roots1)
511+
// If caller needs to modify, they should use DeepCopy
512+
rootsCopy := DeepCopy(roots1)
513513
rootsCopy[0][0]++ // safe to modify
514514

515515
// Original should be unchanged
@@ -526,8 +526,8 @@ func TestDirectReferences(t *testing.T) {
526526
// But the underlying byte slices should be the same (shared data)
527527
require.True(t, &flat1[0][0] == &flat2[0][0], "Flattened() should share underlying byte slices")
528528

529-
// If caller needs to modify, they should use deepCopy
530-
flatCopy := deepCopy(flat1)
529+
// If caller needs to modify, they should use DeepCopy
530+
flatCopy := DeepCopy(flat1)
531531
flatCopy[0][0]++ // safe to modify
532532

533533
// Original should be unchanged

0 commit comments

Comments
 (0)