gitee.com/h79/goutils@v1.22.10/common/bit_flag.go (about)

     1  package common
     2  
     3  type Bit[T BitType] interface {
     4  	Int() T
     5  }
     6  
     7  type BitType interface {
     8  	int8 | int | int32 | int64 | uint8 | uint | uint32 | uint64
     9  }
    10  
    11  func IsBit[T BitType](v T, b T) bool {
    12  	return v&b == b
    13  }
    14  
    15  type Flag[T BitType] struct {
    16  	FInt T `json:"flag,omitempty"`
    17  }
    18  
    19  func WithFlag[T BitType](flag T) Flag[T] {
    20  	return Flag[T]{FInt: flag}
    21  }
    22  
    23  func (f Flag[T]) Int() T {
    24  	return f.FInt
    25  }
    26  
    27  func (f Flag[T]) IsBit(bit Bit[T]) bool {
    28  	return f.FInt&bit.Int() == bit.Int()
    29  }
    30  
    31  func (f Flag[T]) Must(i T) Flag[T] {
    32  	f.FInt |= i
    33  	return f
    34  }
    35  
    36  func (f Flag[T]) MustFlag(i Flag[T]) Flag[T] {
    37  	f.FInt |= i.Int()
    38  	return f
    39  }