github.com/mitranim/gg@v0.1.17/constraints.go (about)

     1  package gg
     2  
     3  import (
     4  	"database/sql/driver"
     5  	"fmt"
     6  	"time"
     7  )
     8  
     9  // Short for "signed integer".
    10  type Sint interface {
    11  	~int8 | ~int16 | ~int32 | ~int64 | ~int
    12  }
    13  
    14  // Short for "unsigned integer".
    15  type Uint interface {
    16  	~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uint
    17  }
    18  
    19  // Describes all built-in integer types and their typedefs.
    20  type Int interface{ Sint | Uint }
    21  
    22  // Describes all built-in float types and their typedefs.
    23  type Float interface{ ~float32 | ~float64 }
    24  
    25  // Describes all built-in complex number types and their typedefs.
    26  type Complex interface{ ~complex64 | ~complex128 }
    27  
    28  /*
    29  Describes all built-in numeric types and their typedefs, excluding complex
    30  numbers.
    31  */
    32  type Num interface{ Int | Float }
    33  
    34  /*
    35  Describes all built-in signed numeric types and their typedefs, excluding
    36  complex numbers.
    37  */
    38  type Signed interface{ Sint | Float }
    39  
    40  // Describes all types that support the "+" operator.
    41  type Plusable interface{ Num | ~string }
    42  
    43  // Set of "primitive" types which may be constant.
    44  type Prim interface{ ~bool | ~string | Num }
    45  
    46  /*
    47  Describes built-in or well-known types which don't implement text encoding and
    48  decoding intrinsically, but whose text encoding and decoding is supported
    49  across the Go library ecosystem extrinsically.
    50  */
    51  type Textable interface{ Prim | ~[]byte }
    52  
    53  /*
    54  Describes text types: strings and byte slices. All types compatible with this
    55  interface can be freely cast to `[]byte` via `ToBytes` and to `string` via
    56  `ToString`, subject to safety gotchas described in those functions' comments.
    57  */
    58  type Text interface{ ~string | ~[]byte }
    59  
    60  /*
    61  Describes all primitive types that support the "<" operator. Counterpart to
    62  `Lesser` which describes types that support comparison via the `.Less` method.
    63  */
    64  type LesserPrim interface {
    65  	Num | Float | ~uintptr | ~string
    66  }
    67  
    68  /*
    69  Describes arbitrary types that support comparison via `.Less`, similar to "<".
    70  Used by various sorting/ordering utilities.
    71  */
    72  type Lesser[A any] interface{ Less(A) bool }
    73  
    74  /*
    75  Short for "primary keyed". See type `Coll` which acts as an ordered map where
    76  each value is indexed on its primary key. Keys must be non-zero. A zero value
    77  is considered an invalid key.
    78  */
    79  type Pker[A comparable] interface{ Pk() A }
    80  
    81  /*
    82  Implemented by various utility types where zero value is considered null in
    83  encoding/decoding contexts such as JSON and SQL.
    84  */
    85  type Nullable interface{ IsNull() bool }
    86  
    87  /*
    88  Used by some 3rd party libraries. Implemented by some of our types for
    89  compatibility.
    90  */
    91  type AnyGetter Getter[any]
    92  
    93  // Implemented by utility types that wrap arbitrary types, such as `Opt`.
    94  type Getter[A any] interface{ Get() A }
    95  
    96  // Implemented by utility types that wrap arbitrary types, such as `Opt`.
    97  type Setter[A any] interface{ Set(A) }
    98  
    99  /*
   100  Similar to `Getter`, but for types that may perform work in `.Get`, this must
   101  avoid that work and be very cheap to call. Used by `Mem`.
   102  */
   103  type Peeker[A any] interface{ Peek() A }
   104  
   105  /*
   106  Implemented by utility types that wrap arbitrary types, such as `Opt`. The
   107  returned pointer must reference the memory of the wrapper, instead of referring
   108  to new memory. Its mutation must affect the wrapper. If the wrapper is nil,
   109  this should return nil.
   110  */
   111  type Ptrer[A any] interface{ Ptr() *A }
   112  
   113  /*
   114  Must clear the receiver. In collection types backed by slices and maps, this
   115  should reduce length to 0, but is allowed to keep capacity.
   116  */
   117  type Clearer interface{ Clear() }
   118  
   119  /*
   120  Interface for types that support parsing from a string. Counterpart to
   121  `encoding.TextUnmarshaler`. Implemented by some utility types.
   122  */
   123  type Parser interface{ Parse(string) error }
   124  
   125  type ParserPtr[A any] interface {
   126  	*A
   127  	Parser
   128  }
   129  
   130  // Copy of `sql.Scanner`. Copied here to avoid a huge import.
   131  type Scanner interface{ Scan(any) error }
   132  
   133  // Used by some utility functions.
   134  type ClearerPtrGetter[A any] interface {
   135  	Clearer
   136  	Ptrer[A]
   137  }
   138  
   139  // Used by some utility functions.
   140  type NullableValGetter[A any] interface {
   141  	Nullable
   142  	Getter[A]
   143  }
   144  
   145  // Used by some utilities.
   146  type Runner interface{ Run() }
   147  
   148  /*
   149  Appends a text representation to the given buffer, returning the modified
   150  buffer. Counterpart to `fmt.Stringer`. All types that implement this interface
   151  should also implement `fmt.Stringer`, and in most cases this should be
   152  semantically equivalent to appending the output of `.String`. However, this
   153  interface allows significantly more efficient text encoding.
   154  */
   155  type AppenderTo interface{ AppendTo([]byte) []byte }
   156  
   157  /*
   158  Combination of interfaces related to text encoding implemented by some types in
   159  this package.
   160  */
   161  type Encoder interface {
   162  	fmt.Stringer
   163  	AppenderTo
   164  	Nullable
   165  	driver.Valuer
   166  }
   167  
   168  /*
   169  Combination of interfaces related to text decoding implemented by some types in
   170  this package.
   171  */
   172  type Decoder interface {
   173  	Clearer
   174  	Parser
   175  	Scanner
   176  }
   177  
   178  /*
   179  Implemented by the `Err` type. Used by `ErrTrace` to retrieve stack traces from
   180  arbitrary error types. This interface is also implemented by trace-enabled
   181  errors in "github.com/pkg/errors".
   182  */
   183  type StackTraced interface{ StackTrace() []uintptr }
   184  
   185  /*
   186  The method `.Init` must modify the receiver, initializing any components that
   187  need initialization, for example using `make` to create inner maps or chans.
   188  The receiver must be mutable, usually a pointer. See `IniterPtr` for a more
   189  precise type constraint. Also see `Initer1` which is more commonly used in this
   190  library.
   191  */
   192  type Initer interface{ Init() }
   193  
   194  // Pointer version of `Initer`.
   195  type IniterPtr[A any] interface {
   196  	*A
   197  	Initer
   198  }
   199  
   200  /*
   201  The method `.Init` must modify the receiver, initializing any components that
   202  need initialization, for example using `make` to create inner maps or chans.
   203  The receiver must be mutable, usually a pointer. See `Initer1Ptr` for a more
   204  precise type constraint. Also see nullary `Initer`.
   205  */
   206  type Initer1[A any] interface{ Init(A) }
   207  
   208  // Pointer version of `Initer1`.
   209  type Initer1Ptr[A, B any] interface {
   210  	*A
   211  	Initer1[B]
   212  }
   213  
   214  /*
   215  The method `.Default` must modify the receiver, applying default values to its
   216  components, usually to struct fields. The receiver must be mutable, usually a
   217  pointer. See `DefaulterPtr` for a more precise type constraint.
   218  */
   219  type Defaulter interface{ Default() }
   220  
   221  // Pointer version of `Defaulter`.
   222  type DefaulterPtr[A any] interface {
   223  	*A
   224  	Defaulter
   225  }
   226  
   227  /*
   228  Interface for values which are convertible to `time.Duration` or can specify
   229  a lifetime for other values. Used by `Mem`.
   230  */
   231  type Durationer interface{ Duration() time.Duration }
   232  
   233  /*
   234  Implemented by various types such as `context.Context`, `sql.Rows`, and our own
   235  `Errs`.
   236  */
   237  type Errer interface{ Err() error }
   238  
   239  // Used by various "iterator" types such as `sql.Rows`.
   240  type Nexter interface{ Next() bool }
   241  
   242  /*
   243  Implemented by some standard library types such as `time.Time` and
   244  `reflect.IsZero`. Our generic function `IsZero` automatically invokes this
   245  method on inputs that implement it.
   246  */
   247  type Zeroable interface{ IsZero() bool }
   248  
   249  /*
   250  Implemented by some types such as `time.Time`, and invoked automatically by our
   251  function `Equal`.
   252  */
   253  type Equaler[A any] interface{ Equal(A) bool }
   254  
   255  /*
   256  Allows to customize/override `ErrFind`, which prioritizes this interface over
   257  the default behavior.
   258  */
   259  type ErrFinder interface{ Find(func(error) bool) error }
   260  
   261  /*
   262  Must return the length of a collection, such as a slice, map, text, etc.
   263  Implemented by various collection types in this package.
   264  */
   265  type Lener interface{ Len() int }