Skip to content

Commit

Permalink
Restore unary initializer for HTTPHeaders (#301)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Lukasa authored Apr 11, 2018
1 parent d94ab56 commit 241aed7
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion Sources/NIOHTTP1/HTTPTypes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,18 @@ public struct HTTPHeaders: CustomStringConvertible {
/// - parameters
/// - headers: An initial set of headers to use to populate the header block.
/// - allocator: The allocator to use to allocate the underlying storage.
public init(_ headers: [(String, String)] = [], allocator: ByteBufferAllocator = ByteBufferAllocator()) {
public init(_ headers: [(String, String)] = []) {
// Note: this initializer exists becuase of https://bugs.swift.org/browse/SR-7415.
// Otherwise we'd only have the one below with a default argument for `allocator`.
self.init(headers, allocator: ByteBufferAllocator())
}

/// Construct a `HTTPHeaders` structure.
///
/// - parameters
/// - headers: An initial set of headers to use to populate the header block.
/// - allocator: The allocator to use to allocate the underlying storage.
public init(_ headers: [(String, String)] = [], allocator: ByteBufferAllocator) {
// Reserve enough space in the array to hold all indices.
var array: [HTTPHeader] = []
array.reserveCapacity(headers.count)
Expand Down

0 comments on commit 241aed7

Please sign in to comment.