| 1 |
// SPDX-License-Identifier: BSD-3-Clause |
| 2 |
|
| 3 |
//go:build linux && go1.21 |
| 4 |
|
| 5 |
// Copyright (C) 2024-2025 SUSE LLC. All rights reserved. |
| 6 |
// Use of this source code is governed by a BSD-style |
| 7 |
// license that can be found in the LICENSE file. |
| 8 |
|
| 9 |
package gocompat |
| 10 |
|
| 11 |
import ( |
| 12 |
"cmp" |
| 13 |
"slices" |
| 14 |
"sync" |
| 15 |
) |
| 16 |
|
| 17 |
// SlicesDeleteFunc is equivalent to Go 1.21's slices.DeleteFunc. |
| 18 |
func SlicesDeleteFunc[S ~[]E, E any](slice S, delFn func(E) bool) S { |
| 19 |
return slices.DeleteFunc(slice, delFn) |
| 20 |
} |
| 21 |
|
| 22 |
// SlicesContains is equivalent to Go 1.21's slices.Contains. |
| 23 |
func SlicesContains[S ~[]E, E comparable](slice S, val E) bool { |
| 24 |
return slices.Contains(slice, val) |
| 25 |
} |
| 26 |
|
| 27 |
// SlicesClone is equivalent to Go 1.21's slices.Clone. |
| 28 |
func SlicesClone[S ~[]E, E any](slice S) S { |
| 29 |
return slices.Clone(slice) |
| 30 |
} |
| 31 |
|
| 32 |
// SyncOnceValue is equivalent to Go 1.21's sync.OnceValue. |
| 33 |
func SyncOnceValue[T any](f func() T) func() T { |
| 34 |
return sync.OnceValue(f) |
| 35 |
} |
| 36 |
|
| 37 |
// SyncOnceValues is equivalent to Go 1.21's sync.OnceValues. |
| 38 |
func SyncOnceValues[T1, T2 any](f func() (T1, T2)) func() (T1, T2) { |
| 39 |
return sync.OnceValues(f) |
| 40 |
} |
| 41 |
|
| 42 |
// CmpOrdered is equivalent to Go 1.21's cmp.Ordered generic type definition. |
| 43 |
type CmpOrdered = cmp.Ordered |
| 44 |
|
| 45 |
// CmpCompare is equivalent to Go 1.21's cmp.Compare. |
| 46 |
func CmpCompare[T CmpOrdered](x, y T) int { |
| 47 |
return cmp.Compare(x, y) |
| 48 |
} |
| 49 |
|
| 50 |
// Max2 is equivalent to Go 1.21's max builtin (but only for two parameters). |
| 51 |
func Max2[T CmpOrdered](x, y T) T { |
| 52 |
return max(x, y) |
| 53 |
} |