all: rename deep "Copy" methods to conventional Go name "Clone"

This commit is contained in:
Brad Fitzpatrick
2020-02-27 12:20:29 -08:00
parent 14559340ee
commit 25797c8c2a
7 changed files with 42 additions and 27 deletions

View File

@@ -58,7 +58,7 @@ func (b *FakeBackend) SetPrefs(new *Prefs) {
panic("FakeBackend.SetPrefs got nil prefs")
}
b.notify(Notify{Prefs: new.Copy()})
b.notify(Notify{Prefs: new.Clone()})
if new.WantRunning && !b.live {
b.newState(Starting)
b.newState(Running)

View File

@@ -47,7 +47,7 @@ func (h *Handle) Start(opts Options) error {
h.engineStatusCache = EngineStatus{}
h.stateCache = NoState
if opts.Prefs != nil {
h.prefsCache = opts.Prefs.Copy()
h.prefsCache = opts.Prefs.Clone()
}
xopts := opts
xopts.Notify = h.notify
@@ -69,7 +69,7 @@ func (h *Handle) notify(n Notify) {
h.stateCache = *n.State
}
if n.Prefs != nil {
h.prefsCache = n.Prefs.Copy()
h.prefsCache = n.Prefs.Clone()
}
if n.NetMap != nil {
h.netmapCache = n.NetMap
@@ -89,14 +89,14 @@ func (h *Handle) Prefs() *Prefs {
h.mu.Lock()
defer h.mu.Unlock()
return h.prefsCache.Copy()
return h.prefsCache.Clone()
}
func (h *Handle) UpdatePrefs(updateFn func(p *Prefs)) {
h.mu.Lock()
defer h.mu.Unlock()
new := h.prefsCache.Copy()
new := h.prefsCache.Clone()
updateFn(new)
h.prefsCache = new
h.b.SetPrefs(new)

View File

@@ -212,7 +212,7 @@ func (b *LocalBackend) Start(opts Options) error {
b.logf("Failed to save new controlclient state: %v", err)
}
}
b.send(Notify{Prefs: b.prefs.Copy()})
b.send(Notify{Prefs: b.prefs.Clone()})
}
if newSt.NetMap != nil {
if b.netMapCache != nil && b.cmpDiff != nil {
@@ -282,7 +282,7 @@ func (b *LocalBackend) Start(opts Options) error {
blid := b.backendLogID
b.logf("Backend: logs: be:%v fe:%v\n", blid, opts.FrontendLogID)
b.send(Notify{BackendLogID: &blid})
b.send(Notify{Prefs: b.prefs.Copy()})
b.send(Notify{Prefs: b.prefs.Clone()})
cli.Login(nil, controlclient.LoginDefault)
return nil
@@ -383,7 +383,7 @@ func (b *LocalBackend) loadStateLocked(key StateKey, prefs *Prefs, legacyPath st
if key == "" {
// Frontend fully owns the state, we just need to obey it.
b.logf("Using frontend prefs")
b.prefs = prefs.Copy()
b.prefs = prefs.Clone()
b.stateKey = ""
return nil
}
@@ -535,7 +535,7 @@ func (b *LocalBackend) SetPrefs(new *Prefs) {
}
}
oldHi := b.hiCache
newHi := oldHi.Copy()
newHi := oldHi.Clone()
newHi.RoutableIPs = append([]wgcfg.CIDR(nil), b.prefs.AdvertiseRoutes...)
b.hiCache = newHi
cli := b.c

View File

@@ -156,8 +156,9 @@ func PrefsFromBytes(b []byte, enforceDefaults bool) (*Prefs, error) {
return p, err
}
// Copy returns a deep copy of p.
func (p *Prefs) Copy() *Prefs {
// Clone returns a deep copy of p.
func (p *Prefs) Clone() *Prefs {
// TODO: write a faster/non-Fatal-y Clone implementation?
p2, err := PrefsFromBytes(p.ToBytes(), false)
if err != nil {
log.Fatalf("Prefs was uncopyable: %v\n", err)

View File

@@ -195,7 +195,7 @@ func checkPrefs(t *testing.T, p Prefs) {
if !p.Equals(&p) {
t.Fatalf("p != p\n")
}
p2 = p.Copy()
p2 = p.Clone()
p2.RouteAll = true
if p.Equals(p2) {
t.Fatalf("p == p2\n")
@@ -213,7 +213,7 @@ func checkPrefs(t *testing.T, p Prefs) {
if !p2.Equals(p2b) {
t.Fatalf("p2 != p2b\n%#v\n%#v\n", p2, p2b)
}
p2c = p2.Copy()
p2c = p2.Clone()
if !p2b.Equals(p2c) {
t.Fatalf("p2b != p2c\n")
}