github.com/sagernet/sing@v0.2.6/common/upstream.go (about)

     1  package common
     2  
     3  type WithUpstream interface {
     4  	Upstream() any
     5  }
     6  
     7  func Cast[T any](obj any) (T, bool) {
     8  	if c, ok := obj.(T); ok {
     9  		return c, true
    10  	}
    11  	if u, ok := obj.(WithUpstream); ok {
    12  		return Cast[T](u.Upstream())
    13  	}
    14  	return DefaultValue[T](), false
    15  }
    16  
    17  func MustCast[T any](obj any) T {
    18  	value, ok := Cast[T](obj)
    19  	if !ok {
    20  		// make panic
    21  		return obj.(T)
    22  	}
    23  	return value
    24  }