Skip to content

Commit 85d0ddd

Browse files
committed
Refactoring some import conditions
1 parent 5627897 commit 85d0ddd

32 files changed

+264
-92
lines changed

Sources/FoundationEssentials/Calendar/Calendar_Cache.swift

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,9 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13-
1413
#if FOUNDATION_FRAMEWORK
1514
@_implementationOnly import _ForSwiftFoundation
1615
import CoreFoundation
17-
#else
1816
#endif
1917

2018
/// Singleton which listens for notifications about preference changes for Calendar and holds cached singletons for the current locale, calendar, and time zone.
@@ -24,7 +22,7 @@ struct CalendarCache : Sendable {
2422

2523
// _CalendarICU, if present
2624
static var calendarICUClass: _CalendarProtocol.Type = {
27-
#if FOUNDATION_FRAMEWORK
25+
#if FOUNDATION_FRAMEWORK && canImport(FoundationICU)
2826
_CalendarICU.self
2927
#else
3028
if let name = _typeByName("FoundationInternationalization._CalendarICU"), let t = name as? _CalendarProtocol.Type {

Sources/FoundationEssentials/Data/Data.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ private func malloc_good_size(_ size: Int) -> Int {
3737
return size
3838
}
3939

40+
#elseif canImport(C)
41+
42+
private func malloc_good_size(_ size: Int) -> Int {
43+
return size
44+
}
45+
4046
#endif
4147

4248
#if os(Windows)
@@ -46,6 +52,8 @@ import func WinSDK.UnmapViewOfFile
4652
internal func __DataInvokeDeallocatorUnmap(_ mem: UnsafeMutableRawPointer, _ length: Int) {
4753
#if os(Windows)
4854
_ = UnmapViewOfFile(mem)
55+
#elseif canImport(C)
56+
free(mem)
4957
#else
5058
munmap(mem, length)
5159
#endif

Sources/FoundationEssentials/Data/DataProtocol.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ extension DataProtocol {
210210
extension DataProtocol where Self : ContiguousBytes {
211211
public func copyBytes<DestinationType, R: RangeExpression>(to ptr: UnsafeMutableBufferPointer<DestinationType>, from range: R) where R.Bound == Index {
212212
precondition(ptr.baseAddress != nil)
213-
213+
214214
let concreteRange = range.relative(to: self)
215215
withUnsafeBytes { fullBuffer in
216216
let adv = distance(from: startIndex, to: concreteRange.lowerBound)

Sources/FoundationEssentials/Date.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ extension NSDate : _HasCustomAnyHashableRepresentation {
358358
#endif // FOUNDATION_FRAMEWORK
359359

360360
// MARK: - Playground Support
361-
#if FOUNDATION_FRAMEWORK
361+
#if FOUNDATION_FRAMEWORK && !NO_FORMATTERS
362362
@available(macOS 10.10, iOS 8.0, watchOS 2.0, tvOS 9.0, *)
363363
extension Date : _CustomPlaygroundQuickLookable {
364364
var summary: String {

Sources/FoundationEssentials/ErrorCodes+POSIX.swift

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,27 +9,7 @@
99
//
1010
//===----------------------------------------------------------------------===//
1111

12-
13-
#if FOUNDATION_FRAMEWORK
14-
/// Describes an error in the POSIX error domain.
15-
@available(macOS 10.10, iOS 8.0, watchOS 2.0, tvOS 9.0, *)
16-
public struct POSIXError : _BridgedStoredNSError {
17-
public let _nsError: NSError
18-
19-
public init(_nsError error: NSError) {
20-
precondition(error.domain == NSPOSIXErrorDomain)
21-
self._nsError = error
22-
}
23-
24-
public static var errorDomain: String { return NSPOSIXErrorDomain }
25-
26-
public var hashValue: Int {
27-
return _nsError.hashValue
28-
}
29-
30-
public typealias Code = POSIXErrorCode
31-
}
32-
#else
12+
#if !FOUNDATION_FRAMEWORK || canImport(C)
3313
// Define the POSIXErrorCode for all platforms here.
3414
public enum POSIXErrorCode : Int32 {
3515
/// Operation not permitted.
@@ -366,6 +346,28 @@ public enum POSIXErrorCode : Int32 {
366346

367347
extension POSIXErrorCode : Equatable, Hashable, RawRepresentable {
368348
}
349+
#endif
350+
351+
#if FOUNDATION_FRAMEWORK
352+
/// Describes an error in the POSIX error domain.
353+
@available(macOS 10.10, iOS 8.0, watchOS 2.0, tvOS 9.0, *)
354+
public struct POSIXError : _BridgedStoredNSError {
355+
public let _nsError: NSError
356+
357+
public init(_nsError error: NSError) {
358+
precondition(error.domain == NSPOSIXErrorDomain)
359+
self._nsError = error
360+
}
361+
362+
public static var errorDomain: String { return NSPOSIXErrorDomain }
363+
364+
public var hashValue: Int {
365+
return _nsError.hashValue
366+
}
367+
368+
public typealias Code = POSIXErrorCode
369+
}
370+
#else
369371

370372
/// Describes an error in the POSIX error domain.
371373
@available(macOS 10.10, iOS 8.0, watchOS 2.0, tvOS 9.0, *)

Sources/FoundationEssentials/JSON/JSONDecoder.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ open class JSONDecoder {
6262
/// Decode the `Date` as UNIX millisecond timestamp from a JSON number.
6363
case millisecondsSince1970
6464

65-
#if FOUNDATION_FRAMEWORK
65+
#if FOUNDATION_FRAMEWORK && !NO_FORMATTERS
6666
// TODO: Reenable once DateFormatStyle has been moved: https://github.com/apple/swift-foundation/issues/46
6767
/// Decode the `Date` as an ISO-8601-formatted string (in RFC 3339 format).
6868
@available(macOS 10.12, iOS 10.0, watchOS 3.0, tvOS 10.0, *)
@@ -647,7 +647,8 @@ extension JSONDecoderImpl: Decoder {
647647
case .millisecondsSince1970:
648648
let double = try self.unwrapFloatingPoint(from: mapValue, as: Double.self, for: codingPathNode, additionalKey)
649649
return Date(timeIntervalSince1970: double / 1000.0)
650-
#if FOUNDATION_FRAMEWORK // TODO: Reenable once DateFormatStyle has been moved
650+
#if FOUNDATION_FRAMEWORK && !NO_FORMATTERS
651+
// TODO: Reenable once DateFormatStyle has been moved
651652
case .iso8601:
652653
let string = try self.unwrapString(from: mapValue, for: codingPathNode, additionalKey)
653654
guard let date = try? Date.ISO8601FormatStyle().parse(string) else {

Sources/FoundationEssentials/JSON/JSONEncoder.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ open class JSONEncoder {
6666
/// Encode the `Date` as UNIX millisecond timestamp (as a JSON number).
6767
case millisecondsSince1970
6868

69-
#if FOUNDATION_FRAMEWORK
69+
#if FOUNDATION_FRAMEWORK && !NO_FORMATTERS
7070
// TODO: Reenable once DateFormatStyle has been ported: https://github.com/apple/swift-foundation/issues/46
7171
/// Encode the `Date` as an ISO-8601-formatted string (in RFC 3339 format).
7272
@available(macOS 10.12, iOS 10.0, watchOS 3.0, tvOS 10.0, *)
@@ -998,7 +998,7 @@ private extension __JSONEncoder {
998998
case .millisecondsSince1970:
999999
return try .number(from: 1000.0 * date.timeIntervalSince1970, with: .throw, for: codingPathNode, additionalKey)
10001000

1001-
#if FOUNDATION_FRAMEWORK
1001+
#if FOUNDATION_FRAMEWORK && !NO_FORMATTERS
10021002
case .iso8601:
10031003
return self.wrap(date.formatted(.iso8601))
10041004

Sources/FoundationEssentials/JSON/JSONWriter.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ internal struct JSONWriter {
349349
pretty ? writer(" : ") : writer(":")
350350
try serializeJSON(value, depth: depth)
351351
}
352-
#if FOUNDATION_FRAMEWORK
352+
#if FOUNDATION_FRAMEWORK && !NO_LOCALIZATION
353353
if sortedKeys {
354354
// TODO: Until we have a solution for sorting like Locale.system in FoundationEssentials or with the help of FoundationLocalization, this comparison requires bridging back to NSString. To avoid the extreme overhead of bridging the strings on every comparison, we'll do it up front instead.
355355
let nsKeysAndValues = dict.map {

Sources/FoundationEssentials/Locale/Locale+Language.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ extension Locale {
5959

6060
/// Returns a list of system languages, includes the languages of all product localization for the current platform
6161
public static var systemLanguages: [Language] {
62-
#if FOUNDATION_FRAMEWORK
62+
#if FOUNDATION_FRAMEWORK && canImport(FoundationICU)
6363
NSLocale.systemLanguages().map {
6464
let comp = Components(identifier: $0 as! String)
6565
return Language(components: comp)

Sources/FoundationEssentials/Locale/Locale.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public struct Locale : Hashable, Equatable, Sendable {
7878
Locale(inner: LocaleCache.cache.system)
7979
}
8080

81-
#if FOUNDATION_FRAMEWORK
81+
#if FOUNDATION_FRAMEWORK && canImport(FoundationICU)
8282
/// This returns an instance of `Locale` that's set up exactly like it would be if the user changed the current locale to that identifier, set the preferences keys in the overrides dictionary, then called `current`.
8383
internal static func localeAsIfCurrent(name: String?, cfOverrides: CFDictionary? = nil, disableBundleMatching: Bool = false) -> Locale {
8484
return LocaleCache.cache.localeAsIfCurrent(name: name, cfOverrides: cfOverrides, disableBundleMatching: disableBundleMatching)
@@ -483,7 +483,7 @@ public struct Locale : Hashable, Equatable, Sendable {
483483
_locale.forceMinDaysInFirstWeek(calendar)
484484
}
485485

486-
#if FOUNDATION_FRAMEWORK
486+
#if FOUNDATION_FRAMEWORK && !NO_FORMATTERS
487487
// This is framework-only because Date.FormatStyle.DateStyle is Internationalization-only.
488488
package func customDateFormat(_ style: Date.FormatStyle.DateStyle) -> String? {
489489
_locale.customDateFormat(style)

0 commit comments

Comments
 (0)