Skip to content

Commit 241aed7

Browse files
authored
Restore unary initializer for HTTPHeaders (#301)
Motivation: The Swift compiler seems to get very nervous when variadic inits are used for types that have constructors with optional values. In general we're not worried about breaking downstream consumers' extensions when we update our code, but in this case we break the ability to conform HTTPHeaders to ExpressibleByDictionaryLiteral, which is a bit annoying. See https://bugs.swift.org/browse/SR-7415 for more details. For 1.5.0 we should conform HTTPHeaders ourselves, but until that time we can restore anyone who was conforming HTTPHeaders to ExpressibleByDictionaryLiteral by restoring the old unary initializer and delegating it to the new one. This presents an effect that is equivalent to the old behaviour, but is new. As a side note, in general it's a bad idea to conform types that you do not own to standard library protocols. NIO reserves the right to add conformances to our types in any Semver Minor release, so having that conformance in your own code risks breakage without a Semver Major patch bump. Please be aware. Modifications: Restored the unary initializer for HTTPHeaders. Result: Less breakage, more happiness.
1 parent d94ab56 commit 241aed7

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

Sources/NIOHTTP1/HTTPTypes.swift

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,18 @@ public struct HTTPHeaders: CustomStringConvertible {
257257
/// - parameters
258258
/// - headers: An initial set of headers to use to populate the header block.
259259
/// - allocator: The allocator to use to allocate the underlying storage.
260-
public init(_ headers: [(String, String)] = [], allocator: ByteBufferAllocator = ByteBufferAllocator()) {
260+
public init(_ headers: [(String, String)] = []) {
261+
// Note: this initializer exists becuase of https://bugs.swift.org/browse/SR-7415.
262+
// Otherwise we'd only have the one below with a default argument for `allocator`.
263+
self.init(headers, allocator: ByteBufferAllocator())
264+
}
265+
266+
/// Construct a `HTTPHeaders` structure.
267+
///
268+
/// - parameters
269+
/// - headers: An initial set of headers to use to populate the header block.
270+
/// - allocator: The allocator to use to allocate the underlying storage.
271+
public init(_ headers: [(String, String)] = [], allocator: ByteBufferAllocator) {
261272
// Reserve enough space in the array to hold all indices.
262273
var array: [HTTPHeader] = []
263274
array.reserveCapacity(headers.count)

0 commit comments

Comments
 (0)