Skip to content
Open
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
55 changes: 55 additions & 0 deletions Sources/Parsing/Conversions/Memberwise.swift
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,14 @@ extension Conversion {
) -> Self where Self == Conversions.Memberwise<Values, Struct> {
.init(initializer: initializer)
}

@inlinable
@_disfavoredOverload
public static func memberwise<Values, Struct>(
_ initializer: @escaping (Values) throws -> Struct
) -> Self where Self == Conversions.ThrowingMemberwise<Values, Struct> {
.init(initializer: initializer)
}
}

extension Conversions {
Expand Down Expand Up @@ -170,4 +178,51 @@ extension Conversions {
return unsafeBitCast(output, to: Values.self)
}
}

public struct ThrowingMemberwise<Values, Struct>: Conversion {
@usableFromInline
let initializer: (Values) throws -> Struct

@usableFromInline
init(initializer: @escaping (Values) throws -> Struct) {
self.initializer = initializer
}

@inlinable
public func apply(_ input: Values) throws -> Struct {
try self.initializer(input)
}

@inlinable
public func unapply(_ output: Struct) throws -> Values {
let ptr = unsafeBitCast(Struct.self as Any.Type, to: UnsafeRawPointer.self)
guard ptr.load(as: Int.self) == 512
else {
throw ConvertingError(
"""
memberwise: Can't convert \(Values.self) to non-struct type \(Struct.self). This \
conversion should only be used with a memberwise initializer matching the memory layout \
of the struct. The "memberwise" initializer is the internal, compiler-generated \
initializer that specifies its arguments in the same order as the struct specifies its \
properties.
"""
)
}
guard
MemoryLayout<Struct>.alignment == MemoryLayout<Values>.alignment,
MemoryLayout<Struct>.size == MemoryLayout<Values>.size
else {
throw ConvertingError(
"""
memberwise: Can't convert \(Values.self) type \(Struct.self) as their memory layouts \
differ. This conversion should only be used with a memberwise initializer matching the \
memory layout of the struct. The "memberwise" initializer is the internal, \
compiler-generated initializer that specifies its arguments in the same order as the \
struct specifies its properties.
"""
)
}
return unsafeBitCast(output, to: Values.self)
}
}
}
65 changes: 65 additions & 0 deletions Tests/ParsingTests/MemberwiseTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import Parsing
import XCTest

final class MemberwiseTests: XCTestCase {
func testNonThrowingMemberwise() throws {
struct Point {
let x: Double
let y: Double
}

let parser = ParsePrint(.memberwise(Point.init(x:y:))) {
"("
Double.parser()
","
Double.parser()
")"
}

let result = try parser.parse("(1.5,-2.3)")
XCTAssertEqual(result.x, 1.5)
XCTAssertEqual(result.y, -2.3)

let printed = try parser.print(Point(x: 3.0, y: 4.0))
XCTAssertEqual(printed, "(3.0,4.0)")
}

func testThrowingMemberwise() throws {
struct ValidatedPoint {
let x: Double
let y: Double

init(x: Double, y: Double) throws {
guard x.isFinite && y.isFinite else {
throw ValidationError.invalidCoordinate
}
self.x = x
self.y = y
}
}

enum ValidationError: Error {
case invalidCoordinate
}

let parser = ParsePrint(.memberwise(ValidatedPoint.init(x:y:))) {
"("
Double.parser()
","
Double.parser()
")"
}

// Test successful parsing
let result = try parser.parse("(1.5,-2.3)")
XCTAssertEqual(result.x, 1.5)
XCTAssertEqual(result.y, -2.3)

// Test successful printing
let printed = try parser.print(ValidatedPoint(x: 3.0, y: 4.0))
XCTAssertEqual(printed, "(3.0,4.0)")

// Test throwing during parsing - should propagate the ValidationError
XCTAssertThrowsError(try parser.parse("(inf,-2.3)"))
}
}