Skip to content

Commit

Permalink
Fix potential build errors from init(_:) overloading (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
davdroman authored Mar 24, 2023
1 parent f09e2a6 commit a8fd97e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
9 changes: 9 additions & 0 deletions Sources/Builders/_AppendableCollection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,18 @@ import Foundation
#endif

public protocol _EmptyInitializable {
@_spi(Internals)
init()
}

public protocol _AppendableCollection: Collection, _EmptyInitializable {
@_spi(Internals)
init(_ element: Element)
mutating func append<S: Sequence>(contentsOf newElements: S) where S.Element == Self.Element
}

extension RangeReplaceableCollection {
@_spi(Internals)
@inlinable
public init(_ element: Element) {
self.init(repeating: element, count: 1)
Expand All @@ -51,6 +54,7 @@ extension Data: _AppendableCollection {}
#endif

extension Dictionary: _AppendableCollection {
@_spi(Internals)
@inlinable
public init(_ element: Element) {
self = [element.key: element.value]
Expand All @@ -65,6 +69,7 @@ extension Dictionary: _AppendableCollection {
}

extension Set: _AppendableCollection {
@_spi(Internals)
@inlinable
public init(_ element: Element) {
self = [element]
Expand All @@ -83,11 +88,13 @@ extension String: _AppendableCollection {}
extension String.UnicodeScalarView: _AppendableCollection {}

extension String.UTF8View: _AppendableCollection {
@_spi(Internals)
@inlinable
public init() {
self = String().utf8
}

@_spi(Internals)
@inlinable
public init(_ element: Element) {
self.init()
Expand All @@ -112,11 +119,13 @@ extension Substring: _AppendableCollection {}
extension Substring.UnicodeScalarView: _AppendableCollection {}

extension Substring.UTF8View: _AppendableCollection {
@_spi(Internals)
@inlinable
public init() {
self = Substring().utf8
}

@_spi(Internals)
@inlinable
public init(_ element: Element) {
self.init()
Expand Down
6 changes: 1 addition & 5 deletions Sources/Builders/_AppendableCollectionBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
public struct _AppendableCollectionBuilder<Collection: _AppendableCollection> {
public typealias Element = Collection.Element

#if compiler(>=5.7)
#if swift(>=5.7)
@inlinable
public static func buildPartialBlock(first: Collection) -> Collection {
first
Expand All @@ -15,13 +15,11 @@ public struct _AppendableCollectionBuilder<Collection: _AppendableCollection> {
return accumulated
}
#else
@inlinable
public static func buildBlock(_ components: Collection...) -> Collection {
components.reduce(into: Collection()) { $0.append(contentsOf: $1) }
}
#endif

@inlinable
public static func buildArray(_ components: [Collection]) -> Collection {
components.reduce(into: Collection()) { $0.append(contentsOf: $1) }
}
Expand All @@ -36,7 +34,6 @@ public struct _AppendableCollectionBuilder<Collection: _AppendableCollection> {
component
}

@inlinable
public static func buildExpression(_ element: Element) -> Collection {
Collection(element)
}
Expand All @@ -51,7 +48,6 @@ public struct _AppendableCollectionBuilder<Collection: _AppendableCollection> {
component
}

@inlinable
public static func buildOptional(_ component: Collection?) -> Collection {
component ?? Collection()
}
Expand Down

0 comments on commit a8fd97e

Please sign in to comment.