Skip to content

Commit 3564861

Browse files
authored
remove ListOfMatchable (#286)
1 parent b2ad16d commit 3564861

4 files changed

Lines changed: 18 additions & 56 deletions

File tree

util.go

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -123,16 +123,7 @@ func ForEach(m Multiaddr, cb func(c Component) bool) {
123123
}
124124
}
125125

126-
type componentList []Component
127-
128-
func (m componentList) Get(i int) meg.Matchable {
129-
return &m[i]
130-
}
131-
132-
func (m componentList) Len() int {
133-
return len(m)
134-
}
135126
func (m Multiaddr) Match(p ...meg.Pattern) (bool, error) {
136127
matcher := meg.PatternToMatcher(p...)
137-
return meg.Match(matcher, componentList(m))
128+
return meg.Match(matcher, m)
138129
}

x/meg/bench_test.go

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,8 @@ func preallocateCapture() *preallocatedCapture {
2929

3030
var webrtcMatchPrealloc *preallocatedCapture
3131

32-
type componentList []multiaddr.Component
33-
34-
func (m componentList) Get(i int) meg.Matchable {
35-
return &m[i]
36-
}
37-
38-
func (m componentList) Len() int {
39-
return len(m)
40-
}
41-
4232
func (p *preallocatedCapture) IsWebRTCDirectMultiaddr(addr multiaddr.Multiaddr) (bool, int) {
43-
found, _ := meg.Match(p.matcher, componentList(addr))
33+
found, _ := meg.Match(p.matcher, addr)
4434
return found, len(p.certHashes)
4535
}
4636

@@ -117,7 +107,7 @@ func isWebTransportMultiaddrPrealloc() *preallocatedCapture {
117107

118108
func IsWebTransportMultiaddrPrealloc(m multiaddr.Multiaddr) (bool, int) {
119109
p := isWebTransportMultiaddrPrealloc()
120-
found, _ := meg.Match(p.matcher, componentList(m))
110+
found, _ := meg.Match(p.matcher, m)
121111
return found, len(p.certHashes)
122112
}
123113

@@ -375,7 +365,7 @@ func BenchmarkIsWebTransportMultiaddrNoCapturePrealloc(b *testing.B) {
375365

376366
b.ResetTimer()
377367
for i := 0; i < b.N; i++ {
378-
isWT, _ := meg.Match(wtPreallocNoCapture, componentList(addr))
368+
isWT, _ := meg.Match(wtPreallocNoCapture, addr)
379369
if !isWT {
380370
b.Fatal("unexpected result")
381371
}

x/meg/meg.go

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -67,25 +67,16 @@ type Matchable interface {
6767
Bytes() []byte
6868
}
6969

70-
// ListOfMatchable is anything list-like that contains Matchable items.
71-
// This allows us to convert a slice of []T as a []Matchable when *T implements
72-
// Matchable. In the future, this may not be required if Go generics allows us
73-
// to say S ~[]T, and *T implements Matchable. This may also not be required if
74-
// we move this out of its own package and depend on Multiaddr and Components
75-
// directly.
76-
type ListOfMatchable interface {
77-
Get(i int) Matchable
78-
Len() int
79-
}
80-
8170
// Match returns whether the given Components match the Matcher
8271
//
8372
// Errors are used to communicate capture errors.
8473
// If the error is non-nil the returned bool will be false.
8574
//
86-
// Components must be a ListOfMatchable to allow us to use a slice of []T as a
87-
// []Matchable when *T implements Matchable.
88-
func Match[L ListOfMatchable](matcher Matcher, components L) (bool, error) {
75+
// The PT pattern is from: https://go.dev/blog/generic-interfaces
76+
func Match[T any, PT interface {
77+
*T
78+
Matchable
79+
}](matcher Matcher, components []T) (bool, error) {
8980
states := matcher.states
9081
startStateIdx := matcher.startIdx
9182

@@ -108,14 +99,14 @@ func Match[L ListOfMatchable](matcher Matcher, components L) (bool, error) {
10899

109100
currentStates = appendState(currentStates, states, startStateIdx, nil, visitedBitSet)
110101

111-
for ic := range components.Len() {
102+
for ic := range len(components) {
112103
clear(visitedBitSet)
113104
if len(currentStates.states) == 0 {
114105
return false, nil
115106
}
116107
for i, stateIndex := range currentStates.states {
117108
s := &states[stateIndex]
118-
cPtr := components.Get(ic)
109+
cPtr := PT(&components[ic])
119110
if s.codeOrKind == matchAny || (s.codeOrKind >= 0 && s.codeOrKind == cPtr.Code()) {
120111
cm := currentStates.captures[i]
121112
if s.capture != nil {

x/meg/meg_test.go

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ func TestCapture(t *testing.T) {
226226
_ = testCases
227227
for _, tc := range testCases {
228228
state, assert := tc.setup()
229-
if matches, _ := Match(state, codeAndValueList(tc.parts)); !matches {
229+
if matches, _ := Match(state, tc.parts); !matches {
230230
t.Fatalf("failed to match %v with %v", tc.parts, state)
231231
}
232232
assert()
@@ -235,7 +235,7 @@ func TestCapture(t *testing.T) {
235235

236236
func TestPreferExactOverAny(t *testing.T) {
237237
t.Run("Optional", func(t *testing.T) {
238-
m := codeAndValueList{
238+
m := []codeAndValue{
239239
{0, "hello"},
240240
{1, "foo"},
241241
{42, "A"},
@@ -260,7 +260,7 @@ func TestPreferExactOverAny(t *testing.T) {
260260
})
261261

262262
t.Run("Or", func(t *testing.T) {
263-
m := codeAndValueList{
263+
m := []codeAndValue{
264264
{1, "foo"},
265265
{42, "A"},
266266
{42, "B"},
@@ -282,7 +282,7 @@ func TestPreferExactOverAny(t *testing.T) {
282282
}
283283
})
284284
t.Run("OneOrMore", func(t *testing.T) {
285-
m := codeAndValueList{
285+
m := []codeAndValue{
286286
{1, "foo"},
287287
{42, "A"},
288288
{42, "B"},
@@ -305,7 +305,7 @@ func TestPreferExactOverAny(t *testing.T) {
305305
}
306306

307307
func TestCaptureWithAny(t *testing.T) {
308-
m := codeAndValueList{
308+
m := []codeAndValue{
309309
{0, "hello"},
310310
{1, "foo"},
311311
{42, "A"},
@@ -334,25 +334,15 @@ func TestCaptureWithAny(t *testing.T) {
334334
}
335335
}
336336

337-
type codeAndValueList []codeAndValue
338-
339-
func (c codeAndValueList) Get(i int) Matchable {
340-
return &c[i]
341-
}
342-
343-
func (c codeAndValueList) Len() int {
344-
return len(c)
345-
}
346-
347-
func codesToCodeAndValue(codes []int) codeAndValueList {
337+
func codesToCodeAndValue(codes []int) []codeAndValue {
348338
out := make([]codeAndValue, len(codes))
349339
for i, c := range codes {
350340
out[i] = codeAndValue{code: c}
351341
}
352342
return out
353343
}
354344

355-
func bytesToCodeAndValue(codes []byte) codeAndValueList {
345+
func bytesToCodeAndValue(codes []byte) []codeAndValue {
356346
out := make([]codeAndValue, len(codes))
357347
for i, c := range codes {
358348
out[i] = codeAndValue{code: int(c)}

0 commit comments

Comments
 (0)