Documentation ¶
Overview ¶
Package rc provides reference-counted cells.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Ref ¶
type Ref[T any] struct { // contains filtered or unexported fields }
A Ref is a reference to a refcounted cell containing a T. It must not be moved after it is first used (but it is ok to move the return values of NewRef/AddRef *before* using them). The zero value is an already-released reference to some non-existant cell; it is not useful.
func NewRef ¶
NewRef returns a Ref pointing to value. When all references are released, the function release will be called.
func NewRefInPlace ¶
NewRefInPlace returns a ref pointing at a value. It constructs the value by passing a pointer to the zero value to the mk callback, which should return the function to run on release.
This is useful when the value cannot be moved after construction, but otherwise you will likely find NewRef more convinenet.
func (*Ref[T]) AddRef ¶
AddRef returns a new reference to the same underlying data as the receiver. The references are not interchangable: to release the underlying data you must call Release on each Ref separately, and you cannot access the value through a released Ref even if you know there are other live references to it.
Panics if this reference has already been released.
func (*Ref[T]) IsValid ¶
Return true iff this is a valid ref, i.e. Value() will return without panicking. You may call this on a nil reference.
func (*Ref[T]) Release ¶
func (r *Ref[T]) Release()
Release this reference to the value. If this is the last reference, this calls the release function that was passed to NewRef.
Release is idempotent: calling it twice on the same reference has no effect. This is handy as it allows you to defer a call to Release and then still have the option of a releasing a reference early.
func (*Ref[T]) Steal ¶
Steal steals the receiver, releasing it and returning a different reference to the same cell. The refcount is unchanged, but this is useful to enforce ownership invariants.