cell

package
v1.0.200 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 17, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cell

type Cell[T any] struct {
	// contains filtered or unexported fields
}

Cell is a thread-safe wrapper around a value T. It provides safe concurrent access through a read-write mutex.

func New

func New[T any](val T) *Cell[T]

New creates a new Cell with the provided value.

Example:

c := cell.New(42)
config := cell.New(Config{Port: 8080, Debug: false})

func (*Cell[T]) Get

func (c *Cell[T]) Get() T

Get returns the current value stored in the Cell.

func (*Cell[T]) Replace

func (c *Cell[T]) Replace(new T) T

Replace atomically replaces the current value with the new one and returns the previous value.

func (*Cell[T]) Set

func (c *Cell[T]) Set(value T)

Set replaces the current value with the given value.

func (*Cell[T]) Swap

func (c *Cell[T]) Swap(other *Cell[T])

Swap swaps the values of two cells.

func (*Cell[T]) Update

func (c *Cell[T]) Update(fn func(T) T)

Update atomically updates the value using the provided function. The function receives the current value and should return the new value. This operation is atomic and thread-safe.

type LazyCell

type LazyCell[T any] struct {
	// contains filtered or unexported fields
}

LazyCell is a thread-safe, lazy-initialization wrapper around a computation. The computation function is executed at most once, on the first call to Force(). Subsequent calls return the cached result. Internally uses Cell for thread-safe operations.

func NewLazy

func NewLazy[T any](fn func() T) *LazyCell[T]

NewLazy creates a new LazyCell wrapper around the given computation function.

The function will not be executed until the first call to Force(). The function should be idempotent and side-effect free for predictable behavior.

Example:

expensive := cell.NewLazy(func() int {
    time.Sleep(1 * time.Second)
    return 42
})

// Function not called yet
result := expensive.Force() // Function called here
result2 := expensive.Force() // Cached result returned

func (*LazyCell[T]) Force

func (l *LazyCell[T]) Force() T

Force executes the computation function (if not already executed) and returns the result.

The function is guaranteed to be called at most once, even in concurrent scenarios. All subsequent calls return the same cached value.

This method is thread-safe and can be called from multiple goroutines concurrently.

func (*LazyCell[T]) Get

func (l *LazyCell[T]) Get() Option[T]

Get returns Some(value) if the lazy value has been computed, None otherwise. This method never triggers the computation - it only returns already computed results.

Example:

if val := lazy.Get(); val.IsSome() {
    fmt.Println("Already computed:", val.Some())
} else {
    fmt.Println("Not computed yet")
}

type OnceCell

type OnceCell[T any] struct {
	// contains filtered or unexported fields
}

OnceCell is a thread-safe cell which can be set exactly once. After being set, it provides immutable access to the stored value. This is equivalent to Rust's OnceCell.

func NewOnce

func NewOnce[T any]() *OnceCell[T]

NewOnce creates a new empty OnceCell.

Example:

cell := cell.NewOnce[int]()
result := cell.Set(42)
if result.IsOk() {
    println("Value set successfully")
}
value := cell.Get()
if value.IsSome() {
    println("Value:", value.Some())
}

func (*OnceCell[T]) Get

func (o *OnceCell[T]) Get() Option[T]

Get returns Some(value) if the cell has been set, None otherwise. This method never blocks and is very fast after the cell has been set.

Example:

cell := cell.NewOnce[int]()
val := cell.Get()
if val.IsNone() {
    println("Cell is empty")
}
cell.Set(42)
val = cell.Get()
println("Value:", val.Some()) // Prints: Value: 42

func (*OnceCell[T]) GetOrInit

func (o *OnceCell[T]) GetOrInit(init func() T) T

GetOrInit returns the value if the cell has been set, or sets and returns the result of calling the init function. The init function is guaranteed to be called at most once.

Example:

cell := cell.NewOnce[string]()
value := cell.GetOrInit(func() string {
    return "initialized"
})
println(value) // Prints: initialized

value2 := cell.GetOrInit(func() string {
    return "this won't be called"
})
println(value2) // Prints: initialized

func (*OnceCell[T]) Set

func (o *OnceCell[T]) Set(value T) Result[Unit]

Set attempts to store a value in the cell. Returns Ok(()) if the value was stored, Err if the cell was already set. This operation is thread-safe and will succeed for exactly one caller.

Example:

cell := cell.NewOnce[string]()
result := cell.Set("hello")  // Returns Ok(())
result2 := cell.Set("world") // Returns Err("value already set")

func (*OnceCell[T]) Take

func (o *OnceCell[T]) Take() Option[T]

Take removes and returns the value from the cell, if it has been set. After calling this method, the cell becomes empty.

Example:

cell := cell.NewOnce[int]()
cell.Set(42)
value := cell.Take()
println(value.Some()) // 42
println(cell.Get().IsNone()) // true

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL