Documentation
¶
Overview ¶
Package bitmask provides a generic, immutable bitmask type with set operations for unsigned integer types.
Index ¶
- type Bitmask
- func (b *Bitmask[T]) Clear(flags ...T) Bitmask[T]
- func (b *Bitmask[T]) Count() int
- func (b *Bitmask[T]) Difference(other Bitmask[T]) Bitmask[T]
- func (b *Bitmask[T]) Has(flag T) bool
- func (b *Bitmask[T]) HasAll(flags ...T) bool
- func (b *Bitmask[T]) HasAny(flags ...T) bool
- func (b *Bitmask[T]) Intersect(other Bitmask[T]) Bitmask[T]
- func (b *Bitmask[T]) IsEmpty() bool
- func (b *Bitmask[T]) MarshalJSON() ([]byte, error)
- func (b *Bitmask[T]) Set(flags ...T) Bitmask[T]
- func (b *Bitmask[T]) String() string
- func (b *Bitmask[T]) Toggle(flags ...T) Bitmask[T]
- func (b *Bitmask[T]) Union(other Bitmask[T]) Bitmask[T]
- func (b *Bitmask[T]) UnmarshalJSON(data []byte) error
- func (b *Bitmask[T]) Value() T
- type Unsigned
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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]) MarshalJSON ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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