Documentation
¶
Overview ¶
Package flagged provides a minimal, extensible API for manipulating and inspecting compact bitflags, while remaining dependency- and allocation-free.
Index ¶
- func New[T BitFlags8 | BitFlags16 | BitFlags32 | BitFlags64](f T) *T
- type BitFlags
- type BitFlags8
- func (f BitFlags8) AllOf(idx ...BitIndex) bool
- func (f BitFlags8) AllSet() bool
- func (f BitFlags8) AnyOf(idx ...BitIndex) bool
- func (f BitFlags8) AnySet() bool
- func (f *BitFlags8) BitFlags() BitFlags
- func (f BitFlags8) Is(idx BitIndex) (set bool)
- func (f BitFlags8) PrettyString() string
- func (f *BitFlags8) Reset(idx BitIndex) (old bool)
- func (f *BitFlags8) ResetAll()
- func (f *BitFlags8) Set(idx BitIndex) (old bool)
- func (f *BitFlags8) SetAll()
- func (f *BitFlags8) SetTo(idx BitIndex, new bool) (old bool)
- func (BitFlags8) Size() int
- func (f BitFlags8) String() string
- func (f *BitFlags8) Toggle(idx BitIndex) (new bool)
- type BitFlags16
- func (f BitFlags16) AllOf(idx ...BitIndex) bool
- func (f BitFlags16) AllSet() bool
- func (f BitFlags16) AnyOf(idx ...BitIndex) bool
- func (f BitFlags16) AnySet() bool
- func (f *BitFlags16) BitFlags() BitFlags
- func (f BitFlags16) Is(idx BitIndex) (set bool)
- func (f BitFlags16) PrettyString() string
- func (f *BitFlags16) Reset(idx BitIndex) (old bool)
- func (f *BitFlags16) ResetAll()
- func (f *BitFlags16) Set(idx BitIndex) (old bool)
- func (f *BitFlags16) SetAll()
- func (f *BitFlags16) SetTo(idx BitIndex, new bool) (old bool)
- func (BitFlags16) Size() int
- func (f BitFlags16) String() string
- func (f *BitFlags16) Toggle(idx BitIndex) (new bool)
- type BitFlags32
- func (f BitFlags32) AllOf(idx ...BitIndex) bool
- func (f BitFlags32) AllSet() bool
- func (f BitFlags32) AnyOf(idx ...BitIndex) bool
- func (f BitFlags32) AnySet() bool
- func (f *BitFlags32) BitFlags() BitFlags
- func (f BitFlags32) Is(idx BitIndex) (set bool)
- func (f BitFlags32) PrettyString() string
- func (f *BitFlags32) Reset(idx BitIndex) (old bool)
- func (f *BitFlags32) ResetAll()
- func (f *BitFlags32) Set(idx BitIndex) (old bool)
- func (f *BitFlags32) SetAll()
- func (f *BitFlags32) SetTo(idx BitIndex, new bool) (old bool)
- func (BitFlags32) Size() int
- func (f BitFlags32) String() string
- func (f *BitFlags32) Toggle(idx BitIndex) (new bool)
- type BitFlags64
- func (f BitFlags64) AllOf(idx ...BitIndex) bool
- func (f BitFlags64) AllSet() bool
- func (f BitFlags64) AnyOf(idx ...BitIndex) bool
- func (f BitFlags64) AnySet() bool
- func (f *BitFlags64) BitFlags() BitFlags
- func (f BitFlags64) Is(idx BitIndex) (set bool)
- func (f BitFlags64) PrettyString() string
- func (f *BitFlags64) Reset(idx BitIndex) (old bool)
- func (f *BitFlags64) ResetAll()
- func (f *BitFlags64) Set(idx BitIndex) (old bool)
- func (f *BitFlags64) SetAll()
- func (f *BitFlags64) SetTo(idx BitIndex, new bool) (old bool)
- func (BitFlags64) Size() int
- func (f BitFlags64) String() string
- func (f *BitFlags64) Toggle(idx BitIndex) (new bool)
- type BitIndex
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func New ¶
func New[T BitFlags8 | BitFlags16 | BitFlags32 | BitFlags64](f T) *T
New is a helper function for creating pointer to one of the BitFlags types. It's useful for returning a value that implements the BitFlags interface.
Types ¶
type BitFlags ¶
type BitFlags interface {
// Is reports whether the bit at index idx is set to true or not.
// It panics if idx is out of the allowed range [0, Size-1].
Is(idx BitIndex) (set bool)
// Set sets the bit at index idx to true, returning its old value.
// It panics if idx is out of the allowed range [0, Size-1].
Set(idx BitIndex) (old bool)
// Reset sets the bit at index idx to false, returning its old value.
// It panics if idx is out of the allowed range [0, Size-1].
Reset(idx BitIndex) (old bool)
// SetTo sets the bit at index idx to new, returning its old value.
// It panics if idx is out of the allowed range [0, Size-1].
SetTo(idx BitIndex, new bool) (old bool)
// Toggle toggles the bit at index idx, returning its new value.
// It panics if idx is out of the allowed range [0, Size-1].
Toggle(idx BitIndex) (new bool)
// SetAll sets all bits to true.
SetAll()
// ResetAll sets all bits to false.
ResetAll()
// AnySet reports whether any of the bits are set to true.
AnySet() bool
// AllSet reports whether all the bits are set to true.
AllSet() bool
// AnyOf reports whether any of the bits at indexes idx are set to true.
// If no indexes are passed, it acts as [BitFlags.AnySet].
AnyOf(idx ...BitIndex) bool
// AllOf reports whether all the bits at indexes idx are set to true.
// If no indexes are passed, it acts as [BitFlags.AllSet].
AllOf(idx ...BitIndex) bool
// Size is the number of bits included in this [BitFlags] value.
// It represents the bit width of the underlying uint.
// It's one of 8, 16, 32, 64.
Size() int
// String returns the binary representation of this [BitFlags] value,
// formatted like fmt's %b verb, but with leading zeros to preserve
// the full bit width of the underlying type (Size).
String() string
// PrettyString returns a human-readable binary representation of
// this [BitFlags] value, with 1 represented as 'I' and 0 as 'O', and
// with '|' delimiter between each bit and '_' delimiter each 8 bits.
//
// Example:
//
// String() // "0000010001000100"
// PrettyString() // "O|O|O|O|O|I|O|O_O|I|O|O|O|I|O|O"
PrettyString() string
}
BitFlags represents the set of methods allowed on one of the typed bit flags types (BitFlags8, BitFlags16, BitFlags32, BitFlags64). It makes it easy to write generic flag-aware code, regardless of the underlying type or its size.
type BitFlags8 ¶
type BitFlags8 uint8
BitFlags8 is a wrapper for uint8 bit flags, carrying 8 flags at max.
func (BitFlags8) PrettyString ¶
type BitFlags16 ¶
type BitFlags16 uint16
BitFlags16 is a wrapper for uint16 bit flags, carrying 16 flags at max.
func (BitFlags16) AllOf ¶
func (f BitFlags16) AllOf(idx ...BitIndex) bool
func (BitFlags16) AllSet ¶
func (f BitFlags16) AllSet() bool
func (BitFlags16) AnyOf ¶
func (f BitFlags16) AnyOf(idx ...BitIndex) bool
func (BitFlags16) AnySet ¶
func (f BitFlags16) AnySet() bool
func (*BitFlags16) BitFlags ¶
func (f *BitFlags16) BitFlags() BitFlags
func (BitFlags16) Is ¶
func (f BitFlags16) Is(idx BitIndex) (set bool)
func (BitFlags16) PrettyString ¶
func (f BitFlags16) PrettyString() string
func (*BitFlags16) Reset ¶
func (f *BitFlags16) Reset(idx BitIndex) (old bool)
func (*BitFlags16) ResetAll ¶
func (f *BitFlags16) ResetAll()
func (*BitFlags16) Set ¶
func (f *BitFlags16) Set(idx BitIndex) (old bool)
func (*BitFlags16) SetAll ¶
func (f *BitFlags16) SetAll()
func (BitFlags16) Size ¶
func (BitFlags16) Size() int
func (BitFlags16) String ¶
func (f BitFlags16) String() string
func (*BitFlags16) Toggle ¶
func (f *BitFlags16) Toggle(idx BitIndex) (new bool)
type BitFlags32 ¶
type BitFlags32 uint32
BitFlags32 is a wrapper for uint32 bit flags, carrying 32 flags at max.
func (BitFlags32) AllOf ¶
func (f BitFlags32) AllOf(idx ...BitIndex) bool
func (BitFlags32) AllSet ¶
func (f BitFlags32) AllSet() bool
func (BitFlags32) AnyOf ¶
func (f BitFlags32) AnyOf(idx ...BitIndex) bool
func (BitFlags32) AnySet ¶
func (f BitFlags32) AnySet() bool
func (*BitFlags32) BitFlags ¶
func (f *BitFlags32) BitFlags() BitFlags
func (BitFlags32) Is ¶
func (f BitFlags32) Is(idx BitIndex) (set bool)
func (BitFlags32) PrettyString ¶
func (f BitFlags32) PrettyString() string
func (*BitFlags32) Reset ¶
func (f *BitFlags32) Reset(idx BitIndex) (old bool)
func (*BitFlags32) ResetAll ¶
func (f *BitFlags32) ResetAll()
func (*BitFlags32) Set ¶
func (f *BitFlags32) Set(idx BitIndex) (old bool)
func (*BitFlags32) SetAll ¶
func (f *BitFlags32) SetAll()
func (BitFlags32) Size ¶
func (BitFlags32) Size() int
func (BitFlags32) String ¶
func (f BitFlags32) String() string
func (*BitFlags32) Toggle ¶
func (f *BitFlags32) Toggle(idx BitIndex) (new bool)
type BitFlags64 ¶
type BitFlags64 uint64
BitFlags64 is a wrapper for uint64 bit flags, carrying 64 flags at max.
func (BitFlags64) AllOf ¶
func (f BitFlags64) AllOf(idx ...BitIndex) bool
func (BitFlags64) AllSet ¶
func (f BitFlags64) AllSet() bool
func (BitFlags64) AnyOf ¶
func (f BitFlags64) AnyOf(idx ...BitIndex) bool
func (BitFlags64) AnySet ¶
func (f BitFlags64) AnySet() bool
func (*BitFlags64) BitFlags ¶
func (f *BitFlags64) BitFlags() BitFlags
func (BitFlags64) Is ¶
func (f BitFlags64) Is(idx BitIndex) (set bool)
func (BitFlags64) PrettyString ¶
func (f BitFlags64) PrettyString() string
func (*BitFlags64) Reset ¶
func (f *BitFlags64) Reset(idx BitIndex) (old bool)
func (*BitFlags64) ResetAll ¶
func (f *BitFlags64) ResetAll()
func (*BitFlags64) Set ¶
func (f *BitFlags64) Set(idx BitIndex) (old bool)
func (*BitFlags64) SetAll ¶
func (f *BitFlags64) SetAll()
func (BitFlags64) Size ¶
func (BitFlags64) Size() int
func (BitFlags64) String ¶
func (f BitFlags64) String() string
func (*BitFlags64) Toggle ¶
func (f *BitFlags64) Toggle(idx BitIndex) (new bool)
type BitIndex ¶
type BitIndex = int
BitIndex is a marker type denoting that its values should be used as bit indexes, passed to the different BitFlags methods. If the value is outside BitFlags range, the methods will panic. The allowed range is [0, [BitFlags.Size]-1].
Example:
const ( myOption1 flagged.BitIndex = iota myOption2 myOption3 )