Skip to content

Make ParserSpan noncopyable #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions Sources/BinaryParsing/Parser Types/ParserSpan.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
/// A non-owning, non-escaping view for parsing binary data.
///
/// You can access a `ParserSpan` from a
public struct ParserSpan: ~Escapable, BitwiseCopyable {
public struct ParserSpan: ~Escapable, ~Copyable {
@usableFromInline
var _bytes: RawSpan
@usableFromInline
Expand All @@ -29,6 +29,14 @@ public struct ParserSpan: ~Escapable, BitwiseCopyable {
self._upperBound = _bytes.byteCount
}

@inlinable
@lifetime(copy other)
init(copying other: borrowing ParserSpan) {
self._bytes = other._bytes
self._lowerBound = other._lowerBound
self._upperBound = other._upperBound
}

@unsafe
@inlinable
@lifetime(borrow buffer)
Expand Down Expand Up @@ -91,7 +99,7 @@ extension ParserSpan {
mutating func divide(at index: Int) -> ParserSpan {
precondition(index >= _lowerBound)
precondition(index <= _upperBound)
var result = self
var result = ParserSpan(copying: self)
result._upperBound = index
self._lowerBound = index
return result
Expand Down Expand Up @@ -193,7 +201,7 @@ extension ParserSpan {
_ body: (inout ParserSpan) throws(E) -> T
) throws(E) -> T {
// Make a mutable copy to perform the work in `body`.
var copy = self
var copy = ParserSpan(copying: self)
let result = try body(&copy)
// `body` didn't throw, so update `self`.
self = copy
Expand Down
8 changes: 4 additions & 4 deletions Sources/BinaryParsing/Parser Types/Seeking.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ extension ParserSpan {
public func seeking(toRange range: ParserRange)
throws(ParsingError) -> ParserSpan
{
var result = self
var result = ParserSpan(copying: self)
try result.seek(toRange: range)
return result
}
Expand All @@ -25,7 +25,7 @@ extension ParserSpan {
public func seeking(toRelativeOffset offset: some FixedWidthInteger)
throws(ParsingError) -> ParserSpan
{
var result = self
var result = ParserSpan(copying: self)
try result.seek(toRelativeOffset: offset)
return result
}
Expand All @@ -35,7 +35,7 @@ extension ParserSpan {
public func seeking(toAbsoluteOffset offset: some FixedWidthInteger)
throws(ParsingError) -> ParserSpan
{
var result = self
var result = ParserSpan(copying: self)
try result.seek(toAbsoluteOffset: offset)
return result
}
Expand All @@ -45,7 +45,7 @@ extension ParserSpan {
public func seeking(toOffsetFromEnd offset: some FixedWidthInteger)
throws(ParsingError) -> ParserSpan
{
var result = self
var result = ParserSpan(copying: self)
try result.seek(toOffsetFromEnd: offset)
return result
}
Expand Down
2 changes: 1 addition & 1 deletion Tests/BinaryParsingTests/SeekingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ struct SeekingTests {

// Absolute offset is always referent to original bounds
var slice1 = try input.sliceSpan(byteCount: 4)
var slice2 = slice1
var slice2 = try input.seeking(toRange: slice1.parserRange)
#expect(slice1.startPosition == 2)
#expect(slice1.count == 4)

Expand Down
2 changes: 1 addition & 1 deletion Tests/BinaryParsingTests/TestingSupport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import Testing

/// Returns a Boolean value indicating whether two parser spans are identical,
/// representing the same subregion of the same span of memory.
func === (lhs: ParserSpan, rhs: ParserSpan) -> Bool {
func === (lhs: borrowing ParserSpan, rhs: borrowing ParserSpan) -> Bool {
guard lhs.startPosition == rhs.startPosition,
lhs.count == rhs.count
else { return false }
Expand Down