bitmask

package
v4.0.1 Latest Latest
Warning

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

Go to latest
Published: Mar 26, 2026 License: AGPL-3.0 Imports: 3 Imported by: 0

Documentation

Overview

Package bitmask provides a generic, immutable bitmask type with set operations for unsigned integer types.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Bitmask

type Bitmask[T Unsigned] struct {
	// contains filtered or unexported fields
}

Bitmask is a generic, immutable bitmask over any unsigned integer type. Each operation returns a new Bitmask, leaving the original unchanged.

func FromValue

func FromValue[T Unsigned](value T) Bitmask[T]

FromValue creates a Bitmask from a raw integer value.

Example
package main

import (
	"fmt"

	"github.com/verygoodsoftwarenotvirus/platform/v4/bitmask"
)

type Permission uint8

const (
	Read Permission = 1 << iota
	Write
	Delete
	Admin
)

func main() {
	mask := bitmask.FromValue(Permission(5))
	fmt.Println(mask.Has(Read), mask.Has(Delete))
}
Output:
true true

func New

func New[T Unsigned](flags ...T) Bitmask[T]

New creates a Bitmask with the given flags set.

Example
package main

import (
	"fmt"

	"github.com/verygoodsoftwarenotvirus/platform/v4/bitmask"
)

type Permission uint8

const (
	Read Permission = 1 << iota
	Write
	Delete
	Admin
)

func main() {
	mask := bitmask.New(Read, Write)
	fmt.Println(mask.Value())
}
Output:
3
Example (Empty)
package main

import (
	"fmt"

	"github.com/verygoodsoftwarenotvirus/platform/v4/bitmask"
)

type Permission uint8

func main() {
	mask := bitmask.New[Permission]()
	fmt.Println(mask.IsEmpty())
}
Output:
true
Example (Permissions)
package main

import (
	"fmt"

	"github.com/verygoodsoftwarenotvirus/platform/v4/bitmask"
)

type Permission uint8

const (
	Read Permission = 1 << iota
	Write
	Delete
	Admin
)

func main() {
	userPerms := bitmask.New(Read, Write)
	required := bitmask.New(Read, Admin)

	if userPerms.HasAll(Read, Admin) {
		fmt.Println("full access")
	} else if userPerms.HasAny(Read, Admin) {
		missing := required.Difference(userPerms)
		fmt.Println("partial access, missing", missing.Count(), "permissions")
	}
}
Output:
partial access, missing 1 permissions

func (*Bitmask[T]) Clear

func (b *Bitmask[T]) Clear(flags ...T) Bitmask[T]

Clear returns a new Bitmask with the given flags cleared.

Example
package main

import (
	"fmt"

	"github.com/verygoodsoftwarenotvirus/platform/v4/bitmask"
)

type Permission uint8

const (
	Read Permission = 1 << iota
	Write
	Delete
	Admin
)

func main() {
	base := bitmask.New(Read, Write)
	mask := base.Clear(Write)
	fmt.Println(mask.Has(Read), mask.Has(Write))
}
Output:
true false

func (*Bitmask[T]) Count

func (b *Bitmask[T]) Count() int

Count returns the number of set bits.

Example
package main

import (
	"fmt"

	"github.com/verygoodsoftwarenotvirus/platform/v4/bitmask"
)

type Permission uint8

const (
	Read Permission = 1 << iota
	Write
	Delete
	Admin
)

func main() {
	mask := bitmask.New(Read, Write, Delete)
	fmt.Println(mask.Count())
}
Output:
3

func (*Bitmask[T]) Difference

func (b *Bitmask[T]) Difference(other Bitmask[T]) Bitmask[T]

Difference returns a new Bitmask with flags set in b but not in other.

Example
package main

import (
	"fmt"

	"github.com/verygoodsoftwarenotvirus/platform/v4/bitmask"
)

type Permission uint8

const (
	Read Permission = 1 << iota
	Write
	Delete
	Admin
)

func main() {
	a := bitmask.New(Read, Write, Delete)
	b := bitmask.New(Write)
	result := a.Difference(b)
	fmt.Println(result.Value())
}
Output:
5

func (*Bitmask[T]) Has

func (b *Bitmask[T]) Has(flag T) bool

Has reports whether the given flag is set.

Example
package main

import (
	"fmt"

	"github.com/verygoodsoftwarenotvirus/platform/v4/bitmask"
)

type Permission uint8

const (
	Read Permission = 1 << iota
	Write
	Delete
	Admin
)

func main() {
	mask := bitmask.New(Read, Write)
	fmt.Println(mask.Has(Read), mask.Has(Admin))
}
Output:
true false

func (*Bitmask[T]) HasAll

func (b *Bitmask[T]) HasAll(flags ...T) bool

HasAll reports whether all the given flags are set.

Example
package main

import (
	"fmt"

	"github.com/verygoodsoftwarenotvirus/platform/v4/bitmask"
)

type Permission uint8

const (
	Read Permission = 1 << iota
	Write
	Delete
	Admin
)

func main() {
	mask := bitmask.New(Read, Write, Delete)
	fmt.Println(mask.HasAll(Read, Write), mask.HasAll(Read, Admin))
}
Output:
true false

func (*Bitmask[T]) HasAny

func (b *Bitmask[T]) HasAny(flags ...T) bool

HasAny reports whether any of the given flags are set.

Example
package main

import (
	"fmt"

	"github.com/verygoodsoftwarenotvirus/platform/v4/bitmask"
)

type Permission uint8

const (
	Read Permission = 1 << iota
	Write
	Delete
	Admin
)

func main() {
	mask := bitmask.New(Read)
	fmt.Println(mask.HasAny(Read, Write), mask.HasAny(Delete, Admin))
}
Output:
true false

func (*Bitmask[T]) Intersect

func (b *Bitmask[T]) Intersect(other Bitmask[T]) Bitmask[T]

Intersect returns a new Bitmask with only the flags set in both bitmasks.

Example
package main

import (
	"fmt"

	"github.com/verygoodsoftwarenotvirus/platform/v4/bitmask"
)

type Permission uint8

const (
	Read Permission = 1 << iota
	Write
	Delete
	Admin
)

func main() {
	a := bitmask.New(Read, Write)
	b := bitmask.New(Write, Delete)
	result := b.Intersect(a)
	fmt.Println(result.Value())
}
Output:
2

func (*Bitmask[T]) IsEmpty

func (b *Bitmask[T]) IsEmpty() bool

IsEmpty reports whether no flags are set.

func (*Bitmask[T]) MarshalJSON

func (b *Bitmask[T]) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler by encoding the bitmask as a bare number.

Example
package main

import (
	"encoding/json"
	"fmt"

	"github.com/verygoodsoftwarenotvirus/platform/v4/bitmask"
)

type Permission uint8

const (
	Read Permission = 1 << iota
	Write
	Delete
	Admin
)

func main() {
	mask := bitmask.New(Read, Delete)

	data, err := json.Marshal(&mask)
	if err != nil {
		panic(err)
	}

	fmt.Println(string(data))
}
Output:
5

func (*Bitmask[T]) Set

func (b *Bitmask[T]) Set(flags ...T) Bitmask[T]

Set returns a new Bitmask with the given flags set.

Example
package main

import (
	"fmt"

	"github.com/verygoodsoftwarenotvirus/platform/v4/bitmask"
)

type Permission uint8

const (
	Read Permission = 1 << iota
	Write
	Delete
	Admin
)

func main() {
	base := bitmask.New(Read)
	mask := base.Set(Write, Delete)
	fmt.Println(mask.Has(Read), mask.Has(Write), mask.Has(Delete))
}
Output:
true true true

func (*Bitmask[T]) String

func (b *Bitmask[T]) String() string

String returns a zero-padded binary string representation of the bitmask.

Example
package main

import (
	"fmt"

	"github.com/verygoodsoftwarenotvirus/platform/v4/bitmask"
)

type Permission uint8

const (
	Read Permission = 1 << iota
	Write
	Delete
	Admin
)

func main() {
	mask := bitmask.New(Read, Delete)
	fmt.Println(mask.String())
}
Output:
00000101

func (*Bitmask[T]) Toggle

func (b *Bitmask[T]) Toggle(flags ...T) Bitmask[T]

Toggle returns a new Bitmask with the given flags toggled.

Example
package main

import (
	"fmt"

	"github.com/verygoodsoftwarenotvirus/platform/v4/bitmask"
)

type Permission uint8

const (
	Read Permission = 1 << iota
	Write
	Delete
	Admin
)

func main() {
	base := bitmask.New(Read)
	mask := base.Toggle(Read, Write)
	fmt.Println(mask.Has(Read), mask.Has(Write))
}
Output:
false true

func (*Bitmask[T]) Union

func (b *Bitmask[T]) Union(other Bitmask[T]) Bitmask[T]

Union returns a new Bitmask with the flags set in either bitmask.

Example
package main

import (
	"fmt"

	"github.com/verygoodsoftwarenotvirus/platform/v4/bitmask"
)

type Permission uint8

const (
	Read Permission = 1 << iota
	Write
	Delete
	Admin
)

func main() {
	a := bitmask.New(Read)
	b := bitmask.New(Write)
	result := a.Union(b)
	fmt.Println(result.Value())
}
Output:
3

func (*Bitmask[T]) UnmarshalJSON

func (b *Bitmask[T]) UnmarshalJSON(data []byte) error

UnmarshalJSON implements json.Unmarshaler by decoding a bare number into the bitmask.

Example
package main

import (
	"encoding/json"
	"fmt"

	"github.com/verygoodsoftwarenotvirus/platform/v4/bitmask"
)

type Permission uint8

const (
	Read Permission = 1 << iota
	Write
	Delete
	Admin
)

func main() {
	var mask bitmask.Bitmask[Permission]
	_ = json.Unmarshal([]byte("5"), &mask)
	fmt.Println(mask.Has(Read), mask.Has(Delete))
}
Output:
true true

func (*Bitmask[T]) Value

func (b *Bitmask[T]) Value() T

Value returns the underlying integer value.

type Unsigned

type Unsigned interface {
	~uint8 | ~uint16 | ~uint32 | ~uint64
}

Unsigned is a constraint for fixed-width unsigned integer types that can be used as bitmask values.

Jump to

Keyboard shortcuts

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