-
Notifications
You must be signed in to change notification settings - Fork 0
/
cast.go
23 lines (19 loc) · 836 Bytes
/
cast.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package types
import "fmt"
// Cast pereforms a type assertion on the given source to the target type.
// If the type assertion succeeds, Ok[Target] is returned.
// If the type assertion fails, Error[TTarget] is returned.
func Cast[TSource, TTarget any](source TSource) Result[TTarget] {
var zero TTarget
return Castf[TSource, TTarget](source, "unable to cast %T to %T", source, zero)
}
// Castf pereforms a type assertion on the given source to the target type.
// If the type assertion succeeds, Ok[Target] is returned.
// If the type assertion fails, Error[TTarget] with the giiven message is returned.
func Castf[TSource, TTarget any](source TSource, format string, args ...any) Result[TTarget] {
target, ok := any(source).(TTarget)
if !ok {
return NewError[TTarget](fmt.Errorf(format, args...))
}
return NewOk(target)
}