Mustify functions implemented

This commit is contained in:
milarin 2023-03-25 09:46:32 +01:00
parent 9b95bdb3dd
commit e274a0473a

101
mustify.go Normal file
View File

@ -0,0 +1,101 @@
package adverr
// Mustify wraps the given function in a Must call
func Mustify[T any](f func() (value T, err error)) func() T {
return func() T { return Must(f()) }
}
// Mustify2 wraps the given function in a Must2 call
func Mustify2[A, B any](f func() (a A, b B, err error)) func() (A, B) {
return func() (A, B) { return Must2(f()) }
}
// Mustify3 wraps the given function in a Must3 call
func Mustify3[A, B, C any](f func() (a A, b B, c C, err error)) func() (A, B, C) {
return func() (A, B, C) { return Must3(f()) }
}
// Mustify4 wraps the given function in a Must4 call
func Mustify4[A, B, C, D any](f func() (a A, b B, c C, d D, err error)) func() (A, B, C, D) {
return func() (A, B, C, D) { return Must4(f()) }
}
// Mustify1i wraps the given function in a Must call
func Mustify1i[I, O any](f func(I) (O, error)) func(I) O {
return func(a I) O { return Must(f(a)) }
}
// Mustify1i2 wraps the given function in a Must2 call
func Mustify1i2[I, O1, O2 any](f func(I) (a O1, b O2, err error)) func(I) (O1, O2) {
return func(a I) (O1, O2) { return Must2(f(a)) }
}
// Mustify1i3 wraps the given function in a Must3 call
func Mustify1i3[I, O1, O2, O3 any](f func(I) (a O1, b O2, c O3, err error)) func(I) (O1, O2, O3) {
return func(a I) (O1, O2, O3) { return Must3(f(a)) }
}
// Mustify1i4 wraps the given function in a Must4 call
func Mustify1i4[I, O1, O2, O3, O4 any](f func(I) (a O1, b O2, c O3, d O4, err error)) func(I) (O1, O2, O3, O4) {
return func(a I) (O1, O2, O3, O4) { return Must4(f(a)) }
}
// Mustify2i wraps the given function in a Must call
func Mustify2i[I1, I2, O any](f func(I1, I2) (O, error)) func(I1, I2) O {
return func(a I1, b I2) O { return Must(f(a, b)) }
}
// Mustify2i2 wraps the given function in a Must2 call
func Mustify2i2[I1, I2, O1, O2 any](f func(I1, I2) (a O1, b O2, err error)) func(I1, I2) (O1, O2) {
return func(a I1, b I2) (O1, O2) { return Must2(f(a, b)) }
}
// Mustify2i3 wraps the given function in a Must3 call
func Mustify2i3[I1, I2, O1, O2, O3 any](f func(I1, I2) (a O1, b O2, c O3, err error)) func(I1, I2) (O1, O2, O3) {
return func(a I1, b I2) (O1, O2, O3) { return Must3(f(a, b)) }
}
// Mustify2i4 wraps the given function in a Must4 call
func Mustify2i4[I1, I2, O1, O2, O3, O4 any](f func(I1, I2) (a O1, b O2, c O3, d O4, err error)) func(I1, I2) (O1, O2, O3, O4) {
return func(a I1, b I2) (O1, O2, O3, O4) { return Must4(f(a, b)) }
}
// Mustify3i wraps the given function in a Must call
func Mustify3i[I1, I2, I3, O any](f func(I1, I2, I3) (O, error)) func(I1, I2, I3) O {
return func(a I1, b I2, c I3) O { return Must(f(a, b, c)) }
}
// Mustify3i2 wraps the given function in a Must2 call
func Mustify3i2[I1, I2, I3, O1, O2 any](f func(I1, I2, I3) (a O1, b O2, err error)) func(I1, I2, I3) (O1, O2) {
return func(a I1, b I2, c I3) (O1, O2) { return Must2(f(a, b, c)) }
}
// Mustify3i3 wraps the given function in a Must3 call
func Mustify3i3[I1, I2, I3, O1, O2, O3 any](f func(I1, I2, I3) (a O1, b O2, c O3, err error)) func(I1, I2, I3) (O1, O2, O3) {
return func(a I1, b I2, c I3) (O1, O2, O3) { return Must3(f(a, b, c)) }
}
// Mustify3i4 wraps the given function in a Must4 call
func Mustify3i4[I1, I2, I3, O1, O2, O3, O4 any](f func(I1, I2, I3) (a O1, b O2, c O3, d O4, err error)) func(I1, I2, I3) (O1, O2, O3, O4) {
return func(a I1, b I2, c I3) (O1, O2, O3, O4) { return Must4(f(a, b, c)) }
}
// Mustify4i wraps the given function in a Must call
func Mustify4i[I1, I2, I3, I4, O any](f func(I1, I2, I3, I4) (O, error)) func(I1, I2, I3, I4) O {
return func(a I1, b I2, c I3, d I4) O { return Must(f(a, b, c, d)) }
}
// Mustify4i2 wraps the given function in a Must2 call
func Mustify4i2[I1, I2, I3, I4, O1, O2 any](f func(I1, I2, I3, I4) (a O1, b O2, err error)) func(I1, I2, I3, I4) (O1, O2) {
return func(a I1, b I2, c I3, d I4) (O1, O2) { return Must2(f(a, b, c, d)) }
}
// Mustify4i3 wraps the given function in a Must3 call
func Mustify4i3[I1, I2, I3, I4, O1, O2, O3 any](f func(I1, I2, I3, I4) (a O1, b O2, c O3, err error)) func(I1, I2, I3, I4) (O1, O2, O3) {
return func(a I1, b I2, c I3, d I4) (O1, O2, O3) { return Must3(f(a, b, c, d)) }
}
// Mustify4i4 wraps the given function in a Must4 call
func Mustify4i4[I1, I2, I3, I4, O1, O2, O3, O4 any](f func(I1, I2, I3, I4) (a O1, b O2, c O3, d O4, err error)) func(I1, I2, I3, I4) (O1, O2, O3, O4) {
return func(a I1, b I2, c I3, d I4) (O1, O2, O3, O4) { return Must4(f(a, b, c, d)) }
}