Skip to content
29 changes: 29 additions & 0 deletions EmbeddedExample/EmbeddedParser.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Binary Parsing open source project
//
// Copyright (c) 2026 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
//
//===----------------------------------------------------------------------===//

import BinaryParsingEmbedded

@available(macOS 26.0, *)
@main
struct EmbeddedTest {
static func main() throws(ParsingError) {
let data: InlineArray<_, UInt8> = [0x0, 0x1, 0x2, 0x3, 0x4, 0x5]
var span = ParserSpan(data.span.bytes)

let a = try Int16(parsingBigEndian: &span)
let b = try Int16(parsingBigEndian: &span)
let c = try Int16(parsingBigEndian: &span)

precondition(a == 1)
precondition(b == 0x203)
print(c)
}
}
30 changes: 19 additions & 11 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ import PackageDescription
let package = Package(
name: "swift-binary-parsing",
platforms: [
.macOS(.v13), .iOS(.v16), .watchOS(.v9), .tvOS(.v16), .visionOS(.v1),
.macOS(.v14), .iOS(.v17), .watchOS(.v10), .tvOS(.v17), .visionOS(.v1),

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you need to bump these deployment targets back up? Were they wrong?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With the embedded target added I get this error when I build locally:

<unknown>:0: error: compiling for macOS 13.0, but module 'Swift' has a minimum deployment target of macOS 14: /Users/nate/Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2026-01-06-a.xctoolchain/usr/lib/swift/embedded/Swift.swiftmodule/arm64-apple-macos.swiftmodule

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Drat. This undoes some of the improvement from #47. I don't know of a good way around it, though

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ended up needing to conditionalize the embedded support anyway, so the platforms are reset in the default case! 🎉

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

],
products: [
.library(name: "BinaryParsing", targets: ["BinaryParsing"])
// .library(name: "BinaryParsingEmbedded", targets: ["BinaryParsingEmbedded"]),
.library(name: "BinaryParsing", targets: ["BinaryParsing"]),
.library(name: "BinaryParsingEmbedded", targets: ["BinaryParsingEmbedded"]),
],
dependencies: [
.package(
Expand All @@ -43,14 +43,14 @@ let package = Package(
.strictMemorySafety(),
]
),
// .target(
// name: "BinaryParsingEmbedded",
// dependencies: ["BinaryParsingMacros"],
// swiftSettings: [
// .enableExperimentalFeature("Embedded"),
// .enableExperimentalFeature("Lifetimes"),
// ]
// ),
.target(
name: "BinaryParsingEmbedded",
dependencies: ["BinaryParsingMacros"],
swiftSettings: [
.enableExperimentalFeature("Embedded"),
.enableExperimentalFeature("Lifetimes"),
]
),
.macro(
name: "BinaryParsingMacros",
dependencies: [
Expand Down Expand Up @@ -78,6 +78,14 @@ let package = Package(
"ParserTest",
.product(name: "ArgumentParser", package: "swift-argument-parser"),
]),
.executableTarget(
name: "EmbeddedExample",
dependencies: ["BinaryParsingEmbedded"],
path: "EmbeddedExample",
swiftSettings: [
.enableExperimentalFeature("Embedded")
]
),

.testTarget(
name: "BinaryParsingTests",
Expand Down
10 changes: 6 additions & 4 deletions Sources/BinaryParsing/Parser Types/ParserSource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
//
//===----------------------------------------------------------------------===//

#if canImport(Foundation)
#if !$Embedded && canImport(Foundation)
public import Foundation
#endif

Expand Down Expand Up @@ -65,7 +65,7 @@ extension RandomAccessCollection<UInt8> {
public func withParserSpanIfAvailable<T>(
_ body: (inout ParserSpan) throws(ThrownParsingError) -> T
) throws(ThrownParsingError) -> T? {
#if canImport(Foundation)
#if !$Embedded && canImport(Foundation)
if let data = self as? Foundation.Data {
let result = unsafe data.withUnsafeBytes { buffer in
var span = unsafe ParserSpan(_unsafeBytes: buffer)
Expand All @@ -81,7 +81,9 @@ extension RandomAccessCollection<UInt8> {
let result = self.withContiguousStorageIfAvailable { buffer in
let rawBuffer = UnsafeRawBufferPointer(buffer)
var span = unsafe ParserSpan(_unsafeBytes: rawBuffer)
return Result<T, ThrownParsingError> { try body(&span) }
return Result<T, ThrownParsingError> { () throws(ThrownParsingError) in
try body(&span)
}
}
switch result {
case .success(let t): return t
Expand Down Expand Up @@ -136,7 +138,7 @@ extension ParserSpanProvider {
}
}

#if canImport(Foundation)
#if !$Embedded && canImport(Foundation)
extension Data: ParserSpanProvider {
@inlinable
public func withParserSpan<T, E>(
Expand Down
2 changes: 1 addition & 1 deletion Sources/BinaryParsing/Parsers/Data.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
//
//===----------------------------------------------------------------------===//

#if canImport(Foundation)
#if !$Embedded && canImport(Foundation)
public import Foundation

extension Data {
Expand Down
Loading