github.com/minio/minio@v0.0.0-20240328213742-3f72439b8a27/internal/pubsub/mask.go (about)

     1  package pubsub
     2  
     3  import (
     4  	"math"
     5  	"math/bits"
     6  )
     7  
     8  // Mask allows filtering by a bitset mask.
     9  type Mask uint64
    10  
    11  const (
    12  	// MaskAll is the mask for all entries.
    13  	MaskAll Mask = math.MaxUint64
    14  )
    15  
    16  // MaskFromMaskable extracts mask from an interface.
    17  func MaskFromMaskable(m Maskable) Mask {
    18  	return Mask(m.Mask())
    19  }
    20  
    21  // Contains returns whether *all* flags in other is present in t.
    22  func (t Mask) Contains(other Mask) bool {
    23  	return t&other == other
    24  }
    25  
    26  // Overlaps returns whether *any* flags in t overlaps with other.
    27  func (t Mask) Overlaps(other Mask) bool {
    28  	return t&other != 0
    29  }
    30  
    31  // SingleType returns whether t has a single type set.
    32  func (t Mask) SingleType() bool {
    33  	return bits.OnesCount64(uint64(t)) == 1
    34  }
    35  
    36  // FromUint64 will set a mask to the uint64 value.
    37  func (t *Mask) FromUint64(m uint64) {
    38  	*t = Mask(m)
    39  }
    40  
    41  // Merge will merge other into t.
    42  func (t *Mask) Merge(other Mask) {
    43  	*t |= other
    44  }
    45  
    46  // MergeMaskable will merge other into t.
    47  func (t *Mask) MergeMaskable(other Maskable) {
    48  	*t |= Mask(other.Mask())
    49  }
    50  
    51  // SetIf will add other if b is true.
    52  func (t *Mask) SetIf(b bool, other Mask) {
    53  	if b {
    54  		*t |= other
    55  	}
    56  }
    57  
    58  // Mask returns the mask as a uint64.
    59  func (t Mask) Mask() uint64 {
    60  	return uint64(t)
    61  }
    62  
    63  // Maskable implementations must return their mask as a 64 bit uint.
    64  type Maskable interface {
    65  	Mask() uint64
    66  }