Documentation ¶
Overview ¶
Package clone provides functions to deep clone any Go data. It also provides a wrapper to protect a pointer from any unexpected mutation.
Index ¶
- func Clone[T any](t T) T
- func ComparablePtr[T comparable](s *T) (d *T)
- func DecimalPtr(s *decimal.Decimal) (d *decimal.Decimal)
- func GormModelPtr(s *gorm.Model) (d *gorm.Model)
- func IsScalar(k reflect.Kind) bool
- func Map[T Clonable[T], K comparable](s map[K]T) (d map[K]T)
- func MarkAsOpaquePointer(t reflect.Type)
- func MarkAsScalar(t reflect.Type)
- func RegisterAtomicPointer[T any]()
- func SetCustomFunc(t reflect.Type, fn Func)
- func Slice[T Clonable[T], TS ~[]T](s TS) (d TS)
- func SliceComparable[T comparable, TS ~[]T](s TS) (d TS)
- func SliceComparablePtr[T comparable, TS ~[]*T](s TS) (d TS)
- func Slowly[T any](t T) T
- func TimeLocationPtr(s *time.Location) (d *time.Location)
- func TimePtr(s *time.Time) (d *time.Time)
- func Undo[T any](t T)
- func Unwrap[T any](t T) T
- func Wrap[T any](t T) T
- type Allocator
- func (a *Allocator) Clone(val reflect.Value) reflect.Value
- func (a *Allocator) CloneSlowly(val reflect.Value) reflect.Value
- func (a *Allocator) MakeChan(t reflect.Type, buffer int) reflect.Value
- func (a *Allocator) MakeMap(t reflect.Type, n int) reflect.Value
- func (a *Allocator) MakeSlice(t reflect.Type, len, cap int) reflect.Value
- func (a *Allocator) MarkAsOpaquePointer(t reflect.Type)
- func (a *Allocator) MarkAsScalar(t reflect.Type)
- func (a *Allocator) New(t reflect.Type) reflect.Value
- func (a *Allocator) SetCustomFunc(t reflect.Type, fn Func)
- type AllocatorMethods
- type Clonable
- type Cloner
- type Func
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Clone ¶
func Clone[T any](t T) T
Clone recursively deep clone v to a new value in heap. It assumes that there is no pointer cycle in v, e.g. v has a pointer points to v itself. If there is a pointer cycle, use Slowly instead.
Clone allocates memory and deeply copies values inside v in depth-first sequence. There are a few special rules for following types.
- Scalar types: all number-like types are copied by value.
- func: Copied by value as func is an opaque pointer at runtime.
- string: Copied by value as string is immutable by design.
- unsafe.Pointer: Copied by value as we don't know what's in it.
- chan: A new empty chan is created as we cannot read data inside the old chan.
Unlike many other packages, Clone is able to clone unexported fields of any struct. Use this feature wisely.
func ComparablePtr ¶
func ComparablePtr[T comparable](s *T) (d *T)
func IsScalar ¶
IsScalar returns true if k should be considered as a scalar type.
For the sake of performance, string is considered as a scalar type unless arena is enabled. If we need to deep copy string value in some cases, we can create a new allocator with custom isScalar function in which we can return false when k is reflect.String.
// Create a new allocator which treats string as non-scalar type. allocator := NewAllocator(nil, &AllocatorMethods{ IsScalar: func(k reflect.Kind) bool { return k != reflect.String && IsScalar(k) }, })
func Map ¶
func Map[T Clonable[T], K comparable](s map[K]T) (d map[K]T)
func MarkAsOpaquePointer ¶
MarkAsOpaquePointer marks t as an opaque pointer in heap allocator, so that all clone methods will copy t by value. If t is not a pointer, MarkAsOpaquePointer ignores t.
Here is a list of types marked as opaque pointers by default:
- `elliptic.Curve`, which is `*elliptic.CurveParam` or `elliptic.p256Curve`;
- `reflect.Type`, which is `*reflect.rtype` defined in `runtime`.
func MarkAsScalar ¶
MarkAsScalar marks t as a scalar type in heap allocator, so that all clone methods will copy t by value. If t is not struct or pointer to struct, MarkAsScalar ignores t.
In the most cases, it's not necessary to call it explicitly. If a struct type contains scalar type fields only, the struct will be marked as scalar automatically.
Here is a list of types marked as scalar by default:
- time.Time
- reflect.Value
func RegisterAtomicPointer ¶
func RegisterAtomicPointer[T any]()
RegisterAtomicPointer registers a custom clone function for atomic.Pointer[T].
func SetCustomFunc ¶
SetCustomFunc sets a custom clone function for type t in heap allocator. If t is not struct or pointer to struct, SetCustomFunc ignores t.
If fn is nil, remove the custom clone function for type t.
func SliceComparable ¶
func SliceComparable[T comparable, TS ~[]T](s TS) (d TS)
func SliceComparablePtr ¶
func SliceComparablePtr[T comparable, TS ~[]*T](s TS) (d TS)
func Slowly ¶
func Slowly[T any](t T) T
Slowly recursively deep clone v to a new value in heap. It marks all cloned values internally, thus it can clone v with cycle pointer.
Slowly works exactly the same as Clone. See Clone doc for more details.
func Undo ¶
func Undo[T any](t T)
Undo discards any change made in wrapped value. If v is not a wrapped value, nothing happens.
func Unwrap ¶
func Unwrap[T any](t T) T
Unwrap returns v's original value if v is a wrapped value. Otherwise, simply returns v itself.
func Wrap ¶
func Wrap[T any](t T) T
Wrap creates a wrapper of v, which must be a pointer. If v is not a pointer, Wrap simply returns v and do nothing.
The wrapper is a deep clone of v's value. It holds a shadow copy to v internally.
t := &T{Foo: 123} v := Wrap(t).(*T) // v is a clone of t. reflect.DeepEqual(t, v) == true // v equals t. v.Foo = 456 // v.Foo is changed, but t.Foo doesn't change. orig := Unwrap(v) // Use `Unwrap` to discard wrapper and return original value, which is t. orig.(*T) == t // orig and t is exactly the same. Undo(v) // Use `Undo` to discard any change on v. v.Foo == t.Foo // Now, the value of v and t are the same again.
Types ¶
type Allocator ¶
type Allocator struct {
// contains filtered or unexported fields
}
Allocator is a utility type for memory allocation.
func FromHeap ¶
func FromHeap() *Allocator
FromHeap creates an allocator which allocate memory from heap.
func NewAllocator ¶
func NewAllocator(pool unsafe.Pointer, methods *AllocatorMethods) (allocator *Allocator)
NewAllocator creates an allocator which allocate memory from the pool. Both pool and methods are optional.
If methods.New is not nil, the allocator itself is created by calling methods.New.
The pool is a pointer to the memory pool which is opaque to the allocator. It's methods' responsibility to allocate memory from the pool properly.
func (*Allocator) Clone ¶
Clone recursively deep clone val to a new value with memory allocated from a.
func (*Allocator) CloneSlowly ¶
CloneSlowly recursively deep clone val to a new value with memory allocated from a. It marks all cloned values internally, thus it can clone v with cycle pointer.
func (*Allocator) MakeSlice ¶
MakeSlice creates a new zero-initialized slice value of t with len and cap.
func (*Allocator) MarkAsOpaquePointer ¶
MarkAsOpaquePointer marks t as an opaque pointer so that all clone methods will copy t by value. If t is not a pointer, MarkAsOpaquePointer ignores t.
Here is a list of types marked as opaque pointers by default:
- `elliptic.Curve`, which is `*elliptic.CurveParam` or `elliptic.p256Curve`;
- `reflect.Type`, which is `*reflect.rtype` defined in `runtime`.
func (*Allocator) MarkAsScalar ¶
MarkAsScalar marks t as a scalar type so that all clone methods will copy t by value. If t is not struct or pointer to struct, MarkAsScalar ignores t.
In the most cases, it's not necessary to call it explicitly. If a struct type contains scalar type fields only, the struct will be marked as scalar automatically.
Here is a list of types marked as scalar by default:
- time.Time
- reflect.Value
type AllocatorMethods ¶
type AllocatorMethods struct { // Parent is the allocator which handles all unhandled methods. // If it's nil, it will be the default allocator. Parent *Allocator New func(pool unsafe.Pointer, t reflect.Type) reflect.Value MakeSlice func(pool unsafe.Pointer, t reflect.Type, len, cap int) reflect.Value MakeMap func(pool unsafe.Pointer, t reflect.Type, n int) reflect.Value MakeChan func(pool unsafe.Pointer, t reflect.Type, buffer int) reflect.Value IsScalar func(k reflect.Kind) bool }
AllocatorMethods defines all methods required by allocator. If any of these methods is nil, allocator will use default method which allocates memory from heap.
type Cloner ¶
type Cloner struct {
// contains filtered or unexported fields
}
Cloner implements clone API with given allocator.
func MakeCloner ¶
MakeCloner creates a cloner with given allocator.
func (Cloner) Clone ¶
func (c Cloner) Clone(v interface{}) interface{}
Clone clones v with given allocator.
func (Cloner) CloneSlowly ¶
func (c Cloner) CloneSlowly(v interface{}) interface{}
CloneSlowly clones v with given allocator. It can clone v with cycle pointer.