Skip to content

Commit e4684eb

Browse files
committed
Use borrowing instead of inout in matching
1 parent c6b09ac commit e4684eb

File tree

3 files changed

+25
-25
lines changed

3 files changed

+25
-25
lines changed

Sources/BinaryParseKit/Utils/ParsingUtils.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import BinaryParsing
99
/// Matches the given bytes in the input parser span.
1010
/// - Warning: This function is used by `@ParseEnum` macro and should not be used directly.
1111
@inline(__always)
12-
public func __match(_ bytes: borrowing [UInt8], in input: inout BinaryParsing.ParserSpan) -> Bool {
12+
public func __match(_ bytes: borrowing [UInt8], in input: borrowing BinaryParsing.ParserSpan) -> Bool {
1313
if bytes.isEmpty { return true }
1414

1515
do {

Sources/BinaryParseKitMacros/Macros/ParseEnum/ConstructParseEnumMacro.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public struct ConstructEnumParseMacro: ExtensionMacro {
7272
)
7373
} else if let toBeMatched = caseParseInfo.bytesToMatch(of: type) {
7474
// Byte-array-based matching: use __matchBytes with inout span
75-
ExprSyntax("\(raw: Constants.UtilityFunctions.matchBytes)(\(toBeMatched), in: &span)")
75+
ExprSyntax("\(raw: Constants.UtilityFunctions.matchBytes)(\(toBeMatched), in: span)")
7676
} else if caseParseInfo.matchAction.target.isDefaultMatch {
7777
// Default matching: always true
7878
ExprSyntax("true")

Tests/BinaryParseKitMacroTests/BinaryParseKitEnumTests.swift

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,11 @@ extension BinaryParseKitMacroTests {
3636
3737
extension TestEnum: BinaryParseKit.Parsable {
3838
public init(parsing span: inout BinaryParsing.ParserSpan) throws(BinaryParsing.ThrownParsingError) {
39-
if BinaryParseKit.__match([0x08], in: &span) {
39+
if BinaryParseKit.__match([0x08], in: span) {
4040
self = .a
4141
return
4242
}
43-
if BinaryParseKit.__match([0x01, 0x02], in: &span) {
43+
if BinaryParseKit.__match([0x01, 0x02], in: span) {
4444
self = .b
4545
return
4646
}
@@ -98,11 +98,11 @@ extension BinaryParseKitMacroTests {
9898
9999
extension TestEnum: BinaryParseKit.Parsable {
100100
internal init(parsing span: inout BinaryParsing.ParserSpan) throws(BinaryParsing.ThrownParsingError) {
101-
if BinaryParseKit.__match([0x02, 0x03], in: &span) {
101+
if BinaryParseKit.__match([0x02, 0x03], in: span) {
102102
self = .a
103103
return
104104
}
105-
if BinaryParseKit.__match([0x01], in: &span) {
105+
if BinaryParseKit.__match([0x01], in: span) {
106106
self = .b
107107
return
108108
}
@@ -161,11 +161,11 @@ extension BinaryParseKitMacroTests {
161161
162162
extension TestEnum: BinaryParseKit.Parsable {
163163
internal init(parsing span: inout BinaryParsing.ParserSpan) throws(BinaryParsing.ThrownParsingError) {
164-
if BinaryParseKit.__match((TestEnum.a as any BinaryParseKit.Matchable).bytesToMatch(), in: &span) {
164+
if BinaryParseKit.__match((TestEnum.a as any BinaryParseKit.Matchable).bytesToMatch(), in: span) {
165165
self = .a
166166
return
167167
}
168-
if BinaryParseKit.__match((TestEnum.b as any BinaryParseKit.Matchable).bytesToMatch(), in: &span) {
168+
if BinaryParseKit.__match((TestEnum.b as any BinaryParseKit.Matchable).bytesToMatch(), in: span) {
169169
self = .b
170170
return
171171
}
@@ -233,15 +233,15 @@ extension BinaryParseKitMacroTests {
233233
234234
extension TestEnum: BinaryParseKit.Parsable {
235235
internal init(parsing span: inout BinaryParsing.ParserSpan) throws(BinaryParsing.ThrownParsingError) {
236-
if BinaryParseKit.__match([0x08], in: &span) {
236+
if BinaryParseKit.__match([0x08], in: span) {
237237
// Parse `__macro_local_16__parse_0th_arg_fMu_` of type SomeType with byte count
238238
BinaryParseKit.__assertSizedParsable((SomeType).self)
239239
let __macro_local_16__parse_0th_arg_fMu_ = try SomeType(parsing: &span, byteCount: 1)
240240
// construct `a` with above associated values
241241
self = .a(__macro_local_16__parse_0th_arg_fMu_)
242242
return
243243
}
244-
if BinaryParseKit.__match([0x01, 0x02], in: &span) {
244+
if BinaryParseKit.__match([0x01, 0x02], in: span) {
245245
// Parse `__macro_local_16__parse_0th_arg_fMu0_` of type Int
246246
BinaryParseKit.__assertParsable((Int).self)
247247
let __macro_local_16__parse_0th_arg_fMu0_ = try Int(parsing: &span)
@@ -252,7 +252,7 @@ extension BinaryParseKitMacroTests {
252252
self = .b(__macro_local_16__parse_0th_arg_fMu0_, value: value)
253253
return
254254
}
255-
if BinaryParseKit.__match([0x09], in: &span) {
255+
if BinaryParseKit.__match([0x09], in: span) {
256256
// Parse `code` of type UInt8 with endianness
257257
BinaryParseKit.__assertEndianParsable((UInt8).self)
258258
let code = try UInt8(parsing: &span, endianness: .little)
@@ -327,12 +327,12 @@ extension BinaryParseKitMacroTests {
327327
328328
extension TestEnum: BinaryParseKit.Parsable {
329329
internal init(parsing span: inout BinaryParsing.ParserSpan) throws(BinaryParsing.ThrownParsingError) {
330-
if BinaryParseKit.__match([0x01], in: &span) {
330+
if BinaryParseKit.__match([0x01], in: span) {
331331
try span.seek(toRelativeOffset: [0x01].count)
332332
self = .a
333333
return
334334
}
335-
if BinaryParseKit.__match([0x02, 0x03], in: &span) {
335+
if BinaryParseKit.__match([0x02, 0x03], in: span) {
336336
self = .b
337337
return
338338
}
@@ -392,12 +392,12 @@ extension BinaryParseKitMacroTests {
392392
393393
extension TestEnum: BinaryParseKit.Parsable {
394394
internal init(parsing span: inout BinaryParsing.ParserSpan) throws(BinaryParsing.ThrownParsingError) {
395-
if BinaryParseKit.__match([0x01], in: &span) {
395+
if BinaryParseKit.__match([0x01], in: span) {
396396
try span.seek(toRelativeOffset: [0x01].count)
397397
self = .a
398398
return
399399
}
400-
if BinaryParseKit.__match([0x02, 0x03], in: &span) {
400+
if BinaryParseKit.__match([0x02, 0x03], in: span) {
401401
self = .b
402402
return
403403
}
@@ -757,23 +757,23 @@ extension BinaryParseKitMacroTests {
757757
758758
extension TestEnum: BinaryParseKit.Parsable {
759759
internal init(parsing span: inout BinaryParsing.ParserSpan) throws(BinaryParsing.ThrownParsingError) {
760-
if BinaryParseKit.__match((TestEnum.a as any BinaryParseKit.Matchable).bytesToMatch(), in: &span) {
760+
if BinaryParseKit.__match((TestEnum.a as any BinaryParseKit.Matchable).bytesToMatch(), in: span) {
761761
// Parse `value` of type Int
762762
BinaryParseKit.__assertParsable((Int).self)
763763
let value = try Int(parsing: &span)
764764
// construct `a` with above associated values
765765
self = .a(value: value)
766766
return
767767
}
768-
if BinaryParseKit.__match((TestEnum.b as any BinaryParseKit.Matchable).bytesToMatch(), in: &span) {
768+
if BinaryParseKit.__match((TestEnum.b as any BinaryParseKit.Matchable).bytesToMatch(), in: span) {
769769
// Parse `value` of type Int
770770
BinaryParseKit.__assertParsable((Int).self)
771771
let value = try Int(parsing: &span)
772772
// construct `b` with above associated values
773773
self = .b(value: value)
774774
return
775775
}
776-
if BinaryParseKit.__match((TestEnum.c as any BinaryParseKit.Matchable).bytesToMatch(), in: &span) {
776+
if BinaryParseKit.__match((TestEnum.c as any BinaryParseKit.Matchable).bytesToMatch(), in: span) {
777777
// Parse `__macro_local_16__parse_0th_arg_fMu_` of type Int
778778
BinaryParseKit.__assertParsable((Int).self)
779779
let __macro_local_16__parse_0th_arg_fMu_ = try Int(parsing: &span)
@@ -901,11 +901,11 @@ extension BinaryParseKitMacroTests {
901901
extension TestEnum: BinaryParseKit.Parsable {
902902
\#(testCase.parsingAccessor
903903
.description) init(parsing span: inout BinaryParsing.ParserSpan) throws(BinaryParsing.ThrownParsingError) {
904-
if BinaryParseKit.__match([0x01], in: &span) {
904+
if BinaryParseKit.__match([0x01], in: span) {
905905
self = .a
906906
return
907907
}
908-
if BinaryParseKit.__match([0x02], in: &span) {
908+
if BinaryParseKit.__match([0x02], in: span) {
909909
self = .b
910910
return
911911
}
@@ -1151,7 +1151,7 @@ extension BinaryParseKitMacroTests {
11511151

11521152
extension TestEnum: BinaryParseKit.Parsable {
11531153
internal init(parsing span: inout BinaryParsing.ParserSpan) throws(BinaryParsing.ThrownParsingError) {
1154-
if BinaryParseKit.__match([0x01], in: &span) {
1154+
if BinaryParseKit.__match([0x01], in: span) {
11551155
// Parse bitmask fields for `flags`
11561156
let __macro_local_19__bitmask_totalBitsfMu_ = 1 + 7
11571157
let __macro_local_19__bitmask_byteCountfMu_ = (__macro_local_19__bitmask_totalBitsfMu_ + 7) / 8
@@ -1179,7 +1179,7 @@ extension BinaryParseKitMacroTests {
11791179
self = .flags(__macro_local_15__mask_0th_arg_fMu_, __macro_local_15__mask_1th_arg_fMu_)
11801180
return
11811181
}
1182-
if BinaryParseKit.__match([0x02], in: &span) {
1182+
if BinaryParseKit.__match([0x02], in: span) {
11831183
// Parse `__macro_local_16__parse_0th_arg_fMu_` of type UInt16
11841184
BinaryParseKit.__assertParsable((UInt16).self)
11851185
let __macro_local_16__parse_0th_arg_fMu_ = try UInt16(parsing: &span)
@@ -1279,7 +1279,7 @@ extension BinaryParseKitMacroTests {
12791279

12801280
extension TestEnum: BinaryParseKit.Parsable {
12811281
internal init(parsing span: inout BinaryParsing.ParserSpan) throws(BinaryParsing.ThrownParsingError) {
1282-
if BinaryParseKit.__match([0x01], in: &span) {
1282+
if BinaryParseKit.__match([0x01], in: span) {
12831283
// Parse bitmask fields for `flags`
12841284
let __macro_local_19__bitmask_totalBitsfMu_ = 1 + 2
12851285
let __macro_local_19__bitmask_byteCountfMu_ = (__macro_local_19__bitmask_totalBitsfMu_ + 7) / 8
@@ -1520,7 +1520,7 @@ extension BinaryParseKitMacroTests {
15201520

15211521
extension TestEnum: BinaryParseKit.Parsable {
15221522
internal init(parsing span: inout BinaryParsing.ParserSpan) throws(BinaryParsing.ThrownParsingError) {
1523-
if BinaryParseKit.__match([0x01], in: &span) {
1523+
if BinaryParseKit.__match([0x01], in: span) {
15241524
// Parse bitmask fields for `a`
15251525
let __macro_local_19__bitmask_totalBitsfMu_ = (Flag).bitCount
15261526
let __macro_local_19__bitmask_byteCountfMu_ = (__macro_local_19__bitmask_totalBitsfMu_ + 7) / 8
@@ -1539,7 +1539,7 @@ extension BinaryParseKitMacroTests {
15391539
self = .a(__macro_local_15__mask_0th_arg_fMu_)
15401540
return
15411541
}
1542-
if BinaryParseKit.__match([0x02], in: &span) {
1542+
if BinaryParseKit.__match([0x02], in: span) {
15431543
// Parse `__macro_local_16__parse_0th_arg_fMu_` of type Flag
15441544
BinaryParseKit.__assertParsable((Flag).self)
15451545
let __macro_local_16__parse_0th_arg_fMu_ = try Flag(parsing: &span)

0 commit comments

Comments
 (0)