From 6d5ed7288d36e33e37937dbf43fcf8928b22b91c Mon Sep 17 00:00:00 2001 From: Luke Howard Date: Tue, 9 Jun 2026 13:50:24 +1000 Subject: [PATCH] Prefer FoundationEssentials over Foundation for the Data bridges MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The optional `Data` conveniences in `ParserSource.swift` and `Data.swift` used `public import Foundation` guarded on `canImport(Foundation)`. Because these are *public* (re-exported) imports, every downstream consumer of BinaryParsing inherits a link dependency on full Foundation — which on non-Darwin platforms pulls in FoundationInternationalization and the ICU data blob — even though the only symbol used is `Data`, which is available in FoundationEssentials. Prefer FoundationEssentials when available, falling back to Foundation, and de-qualify `Foundation.Data` to `Data` so it resolves from whichever module is imported. No API change: the `Data` initializers and `ParserSpanProvider` conformance are unchanged; this only narrows the transitive link dependency. On Linux this removes libFoundation, libFoundationInternationalization and the ICU data library from the link closure of any executable that uses BinaryParsing but not full Foundation. Co-Authored-By: Claude Opus 4.8 (1M context) --- Sources/BinaryParsing/Parser Types/ParserSource.swift | 10 ++++++---- Sources/BinaryParsing/Parsers/Data.swift | 6 +++++- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/Sources/BinaryParsing/Parser Types/ParserSource.swift b/Sources/BinaryParsing/Parser Types/ParserSource.swift index d6e02fc..4afe91c 100644 --- a/Sources/BinaryParsing/Parser Types/ParserSource.swift +++ b/Sources/BinaryParsing/Parser Types/ParserSource.swift @@ -9,7 +9,9 @@ // //===----------------------------------------------------------------------===// -#if !$Embedded && canImport(Foundation) +#if !$Embedded && canImport(FoundationEssentials) +public import FoundationEssentials +#elseif !$Embedded && canImport(Foundation) public import Foundation #endif @@ -65,8 +67,8 @@ extension RandomAccessCollection { public func withParserSpanIfAvailable( _ body: (inout ParserSpan) throws(ThrownParsingError) -> T ) throws(ThrownParsingError) -> T? { - #if !$Embedded && canImport(Foundation) - if let data = self as? Foundation.Data { + #if !$Embedded && (canImport(FoundationEssentials) || canImport(Foundation)) + if let data = self as? Data { let result = unsafe data.withUnsafeBytes { buffer in var span = unsafe ParserSpan(_unsafeBytes: buffer) return Result { try body(&span) } @@ -138,7 +140,7 @@ extension ParserSpanProvider { } } -#if !$Embedded && canImport(Foundation) +#if !$Embedded && (canImport(FoundationEssentials) || canImport(Foundation)) extension Data: ParserSpanProvider { @inlinable public func withParserSpan( diff --git a/Sources/BinaryParsing/Parsers/Data.swift b/Sources/BinaryParsing/Parsers/Data.swift index fabf1dd..1c90302 100644 --- a/Sources/BinaryParsing/Parsers/Data.swift +++ b/Sources/BinaryParsing/Parsers/Data.swift @@ -9,9 +9,13 @@ // //===----------------------------------------------------------------------===// -#if !$Embedded && canImport(Foundation) +#if !$Embedded && canImport(FoundationEssentials) +public import FoundationEssentials +#elseif !$Embedded && canImport(Foundation) public import Foundation +#endif +#if !$Embedded && (canImport(FoundationEssentials) || canImport(Foundation)) extension Data { /// Creates a new data instance by copying the remaining bytes from the /// given parser span.