pointer

package
v2.0.0 Latest Latest
Warning

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

Go to latest
Published: Mar 25, 2022 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

pointer is a pointer to a value of type T.

Example (SimpleNewAndUse)
package main

import (
	"fmt"

	"github.com/snowmerak/generics-for-go/v2/types/pointer"
)

func main() {
	i := 10
	a := pointer.New(&i)
	*a.Load() = 20
	fmt.Println(i)
}
Output:

20
Example (SwapPointer)
package main

import (
	"fmt"

	"github.com/snowmerak/generics-for-go/v2/types/pointer"
)

func main() {
	i := 10
	a := pointer.New(&i)
	n := 20
	a.CompareAndSwap(&n, &i)
	fmt.Println(*a.Load())
}
Output:

20

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Pointer

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

Pointer is a container of T pointer.

func New

func New[T any](pointer *T) Pointer[T]

New returns a new Pointer.

func (*Pointer[T]) Clone

func (p *Pointer[T]) Clone() Pointer[T]

Clone returns a new Pointer with the same value as the old one.

func (*Pointer[T]) CompareAndSwap

func (p *Pointer[T]) CompareAndSwap(old *T, new *T) bool

CompareAndSwap sets the value of the Pointer if the current value is equal to the expected value.

func (*Pointer[T]) IsNil

func (p *Pointer[T]) IsNil() bool

IsNil returns true if the Pointer is nil.

func (*Pointer[T]) Load

func (p *Pointer[T]) Load() *T

Load returns the value of the Pointer.

func (*Pointer[T]) Run

func (p *Pointer[T]) Run(f func(*T)) bool

Run is a function that takes a pointer of type T. if pointer is nil, returns false.

func (*Pointer[T]) Store

func (p *Pointer[T]) Store(pointer *T)

Store sets the value of the Pointer.

func (*Pointer[T]) Swap

func (p *Pointer[T]) Swap(new *T) *T

Swap sets the value of the Pointer and returns the previous value.

Jump to

Keyboard shortcuts

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