net/tstun: move TAP support out to separate package feature/tap
Still behind the same ts_omit_tap build tag. See #14738 for background on the pattern. Updates #12614 Change-Id: I03fb3d2bf137111e727415bd8e713d8568156ecc Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
This commit is contained in:
committed by
Brad Fitzpatrick
parent
f1710f4a42
commit
d6abbc2e61
@@ -4,6 +4,8 @@
|
||||
// Package feature tracks which features are linked into the binary.
|
||||
package feature
|
||||
|
||||
import "reflect"
|
||||
|
||||
var in = map[string]bool{}
|
||||
|
||||
// Register notes that the named feature is linked into the binary.
|
||||
@@ -13,3 +15,40 @@ func Register(name string) {
|
||||
}
|
||||
in[name] = true
|
||||
}
|
||||
|
||||
// Hook is a func that can only be set once.
|
||||
//
|
||||
// It is not safe for concurrent use.
|
||||
type Hook[Func any] struct {
|
||||
f Func
|
||||
ok bool
|
||||
}
|
||||
|
||||
// IsSet reports whether the hook has been set.
|
||||
func (h *Hook[Func]) IsSet() bool {
|
||||
return h.ok
|
||||
}
|
||||
|
||||
// Set sets the hook function, panicking if it's already been set
|
||||
// or f is the zero value.
|
||||
//
|
||||
// It's meant to be called in init.
|
||||
func (h *Hook[Func]) Set(f Func) {
|
||||
if h.ok {
|
||||
panic("Set on already-set feature hook")
|
||||
}
|
||||
if reflect.ValueOf(f).IsZero() {
|
||||
panic("Set with zero value")
|
||||
}
|
||||
h.f = f
|
||||
h.ok = true
|
||||
}
|
||||
|
||||
// Get returns the hook function, or panics if it hasn't been set.
|
||||
// Use IsSet to check if it's been set.
|
||||
func (h *Hook[Func]) Get() Func {
|
||||
if !h.ok {
|
||||
panic("Get on unset feature hook, without IsSet")
|
||||
}
|
||||
return h.f
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user