diff --git a/Sources/NIO/TypeAssistedChannelHandler.swift b/Sources/NIO/TypeAssistedChannelHandler.swift index ecfde94dc..ce55acf2e 100644 --- a/Sources/NIO/TypeAssistedChannelHandler.swift +++ b/Sources/NIO/TypeAssistedChannelHandler.swift @@ -26,18 +26,10 @@ extension _EmittingChannelHandler { public protocol ChannelInboundHandler: _ChannelInboundHandler, _EmittingChannelHandler { associatedtype InboundIn - associatedtype InboundUserEventIn = Never - associatedtype InboundOut = Never - associatedtype OutboundUserEventOut = Never - associatedtype InboundUserEventOut = Never func unwrapInboundIn(_ value: NIOAny) -> InboundIn - func tryUnwrapInboundIn(_ value: NIOAny) -> InboundIn? func wrapInboundOut(_ value: InboundOut) -> NIOAny - func unwrapInboundUserEventIn(_ value: Any) -> InboundUserEventIn - func tryUnwrapInboundUserEventIn(_ value: Any) -> InboundUserEventIn? - func wrapInboundUserEventOut(_ value: InboundUserEventOut) -> Any } extension ChannelInboundHandler { @@ -45,61 +37,20 @@ extension ChannelInboundHandler { return value.forceAs() } - public func tryUnwrapInboundIn(_ value: NIOAny) -> InboundIn? { - return value.tryAs() - } - public func wrapInboundOut(_ value: InboundOut) -> NIOAny { return NIOAny(value) } - - public func unwrapInboundUserEventIn(_ value: Any) -> InboundUserEventIn { - return value as! InboundUserEventIn - } - - public func tryUnwrapInboundUserEventIn(_ value: Any) -> InboundUserEventIn? { - return value as? InboundUserEventIn - } - - public func wrapInboundUserEventOut(_ value: InboundUserEventOut) -> Any { - return value - } } public protocol ChannelOutboundHandler: _ChannelOutboundHandler, _EmittingChannelHandler { associatedtype OutboundIn - associatedtype OutboundUserEventIn = Never - associatedtype InboundOut = Never - associatedtype OutboundUserEventOut = Never - associatedtype InboundUserEventOut = Never func unwrapOutboundIn(_ value: NIOAny) -> OutboundIn - func tryUnwrapOutboundIn(_ value: NIOAny) -> OutboundIn? - - func unwrapOutboundUserEventIn(_ value: Any) -> OutboundUserEventIn - func tryUnwrapOutboundUserEventIn(_ value: Any) -> OutboundUserEventIn? - func wrapOutboundUserEventOut(_ value: OutboundUserEventOut) -> Any } extension ChannelOutboundHandler { public func unwrapOutboundIn(_ value: NIOAny) -> OutboundIn { return value.forceAs() } - - public func tryUnwrapOutboundIn(_ value: NIOAny) -> OutboundIn? { - return value.tryAs() - } - - public func unwrapOutboundUserEventIn(_ value: Any) -> OutboundUserEventIn { - return value as! OutboundUserEventIn - } - - public func tryUnwrapOutboundUserEventIn(_ value: Any) -> OutboundUserEventIn? { - return value as? OutboundUserEventIn - } - - public func wrapOutboundUserEventOut(_ value: OutboundUserEventOut) -> Any { - return value - } } diff --git a/Sources/NIOHTTP1/HTTPEncoder.swift b/Sources/NIOHTTP1/HTTPEncoder.swift index bdf1be10e..e6e300939 100644 --- a/Sources/NIOHTTP1/HTTPEncoder.swift +++ b/Sources/NIOHTTP1/HTTPEncoder.swift @@ -118,8 +118,8 @@ public final class HTTPRequestEncoder : ChannelOutboundHandler { public init () { } public func write(ctx: ChannelHandlerContext, data: NIOAny, promise: EventLoopPromise?) { - switch self.tryUnwrapOutboundIn(data) { - case .some(.head(var request)): + switch self.unwrapOutboundIn(data) { + case .head(var request): sanitizeTransportHeaders(mayHaveBody: request.method.mayHaveRequestBody, headers: &request.headers, version: request.version) self.isChunked = isChunkedPart(request.headers) @@ -132,12 +132,10 @@ public final class HTTPRequestEncoder : ChannelOutboundHandler { request.version.write(buffer: &buffer) buffer.write(staticString: "\r\n") }, ctx: ctx, headers: request.headers, promise: promise) - case .some(.body(let bodyPart)): + case .body(let bodyPart): writeChunk(wrapOutboundOut: self.wrapOutboundOut, ctx: ctx, isChunked: self.isChunked, chunk: bodyPart, promise: promise) - case .some(.end(let trailers)): + case .end(let trailers): writeTrailers(wrapOutboundOut: self.wrapOutboundOut, ctx: ctx, isChunked: self.isChunked, trailers: trailers, promise: promise) - case .none: - ctx.write(data: data, promise: promise) } } } @@ -155,8 +153,8 @@ public final class HTTPResponseEncoder : ChannelOutboundHandler { public init () { } public func write(ctx: ChannelHandlerContext, data: NIOAny, promise: EventLoopPromise?) { - switch self.tryUnwrapOutboundIn(data) { - case .some(.head(var response)): + switch self.unwrapOutboundIn(data) { + case .head(var response): sanitizeTransportHeaders(mayHaveBody: response.status.mayHaveResponseBody, headers: &response.headers, version: response.version) self.isChunked = isChunkedPart(response.headers) @@ -166,12 +164,10 @@ public final class HTTPResponseEncoder : ChannelOutboundHandler { response.status.write(buffer: &buffer) buffer.write(staticString: "\r\n") }, ctx: ctx, headers: response.headers, promise: promise) - case .some(.body(let bodyPart)): + case .body(let bodyPart): writeChunk(wrapOutboundOut: self.wrapOutboundOut, ctx: ctx, isChunked: self.isChunked, chunk: bodyPart, promise: promise) - case .some(.end(let trailers)): + case .end(let trailers): writeTrailers(wrapOutboundOut: self.wrapOutboundOut, ctx: ctx, isChunked: self.isChunked, trailers: trailers, promise: promise) - case .none: - ctx.write(data: data, promise: promise) } } } diff --git a/Sources/NIOOpenSSL/OpenSSLHandler.swift b/Sources/NIOOpenSSL/OpenSSLHandler.swift index 8547192c4..75257cd9d 100644 --- a/Sources/NIOOpenSSL/OpenSSLHandler.swift +++ b/Sources/NIOOpenSSL/OpenSSLHandler.swift @@ -29,7 +29,6 @@ public class OpenSSLHandler : ChannelInboundHandler, ChannelOutboundHandler { public typealias OutboundOut = ByteBuffer public typealias InboundIn = ByteBuffer public typealias InboundOut = ByteBuffer - public typealias InboundUserEventOut = TLSUserEvent private enum ConnectionState { case idle @@ -199,7 +198,7 @@ public class OpenSSLHandler : ChannelInboundHandler, ChannelOutboundHandler { // TODO(cory): This event should probably fire out of the OpenSSL info callback. let negotiatedProtocol = connection.getAlpnProtocol() - ctx.fireUserInboundEventTriggered(event: wrapInboundUserEventOut(TLSUserEvent.handshakeCompleted(negotiatedProtocol: negotiatedProtocol))) + ctx.fireUserInboundEventTriggered(event: TLSUserEvent.handshakeCompleted(negotiatedProtocol: negotiatedProtocol)) // We need to unbuffer any pending writes. We will have pending writes if the user attempted to write // before we completed the handshake. @@ -236,7 +235,7 @@ public class OpenSSLHandler : ChannelInboundHandler, ChannelOutboundHandler { writeDataToNetwork(ctx: ctx, promise: nil) // TODO(cory): This should probably fire out of the OpenSSL info callback. - ctx.fireUserInboundEventTriggered(event: wrapInboundUserEventOut(TLSUserEvent.shutdownCompleted)) + ctx.fireUserInboundEventTriggered(event: TLSUserEvent.shutdownCompleted) channelClose(ctx: ctx) case .failed(let err): // TODO(cory): This should probably fire out of the OpenSSL info callback. diff --git a/Sources/NIOTLS/ApplicationProtocolNegotiationHandler.swift b/Sources/NIOTLS/ApplicationProtocolNegotiationHandler.swift index 6196af5b9..04e86ddfe 100644 --- a/Sources/NIOTLS/ApplicationProtocolNegotiationHandler.swift +++ b/Sources/NIOTLS/ApplicationProtocolNegotiationHandler.swift @@ -70,7 +70,6 @@ public enum ALPNResult: Equatable { public class ApplicationProtocolNegotiationHandler: ChannelInboundHandler { public typealias InboundIn = Any public typealias InboundOut = Any - public typealias InboundUserEventIn = TLSUserEvent private let completionHandler: (ALPNResult) -> EventLoopFuture private var waitingForUser: Bool @@ -88,7 +87,7 @@ public class ApplicationProtocolNegotiationHandler: ChannelInboundHandler { } public func userInboundEventTriggered(ctx: ChannelHandlerContext, event: Any) { - guard let tlsEvent = tryUnwrapInboundUserEventIn(event) else { + guard let tlsEvent = event as? TLSUserEvent else { ctx.fireUserInboundEventTriggered(event: event) return } diff --git a/Tests/NIOHTTP1Tests/HTTPUpgradeTests.swift b/Tests/NIOHTTP1Tests/HTTPUpgradeTests.swift index 2e9b0130e..e3601de56 100644 --- a/Tests/NIOHTTP1Tests/HTTPUpgradeTests.swift +++ b/Tests/NIOHTTP1Tests/HTTPUpgradeTests.swift @@ -143,11 +143,10 @@ private class SuccessfulUpgrader: HTTPProtocolUpgrader { private class UserEventSaver: ChannelInboundHandler { public typealias InboundIn = Any - public typealias InboundUserEventIn = EventType public var events: [EventType] = [] public func userInboundEventTriggered(ctx: ChannelHandlerContext, event: Any) { - events.append(unwrapInboundUserEventIn(event)) + events.append(event as! EventType) ctx.fireUserInboundEventTriggered(event: event) } } diff --git a/Tests/NIOOpenSSLTests/OpenSSLIntegrationTest.swift b/Tests/NIOOpenSSLTests/OpenSSLIntegrationTest.swift index 16519deb2..39be77b8e 100644 --- a/Tests/NIOOpenSSLTests/OpenSSLIntegrationTest.swift +++ b/Tests/NIOOpenSSLTests/OpenSSLIntegrationTest.swift @@ -54,7 +54,6 @@ private func interactInMemory(clientChannel: EmbeddedChannel, serverChannel: Emb private final class SimpleEchoServer: ChannelInboundHandler { public typealias InboundIn = ByteBuffer public typealias OutboundOut = ByteBuffer - public typealias InboundUserEventIn = TLSUserEvent public func channelRead(ctx: ChannelHandlerContext, data: NIOAny) { ctx.write(data: data, promise: nil) @@ -70,7 +69,6 @@ private final class SimpleEchoServer: ChannelInboundHandler { internal final class PromiseOnReadHandler: ChannelInboundHandler { public typealias InboundIn = ByteBuffer public typealias OutboundOut = ByteBuffer - public typealias InboundUserEventIn = TLSUserEvent private let promise: EventLoopPromise private var data: NIOAny? = nil @@ -104,7 +102,6 @@ private final class WriteCountingHandler: ChannelOutboundHandler { public final class EventRecorderHandler: ChannelInboundHandler where UserEventType: Equatable { public typealias InboundIn = ByteBuffer - public typealias InboundUserEventIn = UserEventType public enum RecordedEvents: Equatable { case Registered @@ -175,7 +172,7 @@ public final class EventRecorderHandler: ChannelInboundHandler wh } public func userInboundEventTriggered(ctx: ChannelHandlerContext, event: Any) { - guard let ourEvent = tryUnwrapInboundUserEventIn(event) else { + guard let ourEvent = event as? UserEventType else { ctx.fireUserInboundEventTriggered(event: event) return } diff --git a/Tests/NIOTests/IdleStateHandlerTest.swift b/Tests/NIOTests/IdleStateHandlerTest.swift index 9471a9d27..73eb0b351 100644 --- a/Tests/NIOTests/IdleStateHandlerTest.swift +++ b/Tests/NIOTests/IdleStateHandlerTest.swift @@ -42,7 +42,6 @@ class IdleStateHandlerTest : XCTestCase { class TestWriteHandler: ChannelInboundHandler { typealias InboundIn = ByteBuffer typealias OutboundOut = ByteBuffer - typealias InboundUserEventIn = IdleStateHandler.IdleStateEvent private var read = false private let writeToChannel: Bool @@ -62,7 +61,7 @@ class IdleStateHandlerTest : XCTestCase { XCTAssertTrue(self.read) } - XCTAssertTrue(assertEventFn(self.unwrapInboundUserEventIn(event))) + XCTAssertTrue(assertEventFn(event as! IdleStateHandler.IdleStateEvent)) ctx.close(promise: nil) } diff --git a/Tests/NIOTests/TypeAssistedChannelHandlerTests+XCTest.swift b/Tests/NIOTests/TypeAssistedChannelHandlerTests+XCTest.swift index 4ec22d5af..026d1f95b 100644 --- a/Tests/NIOTests/TypeAssistedChannelHandlerTests+XCTest.swift +++ b/Tests/NIOTests/TypeAssistedChannelHandlerTests+XCTest.swift @@ -26,7 +26,6 @@ extension TypeAssistedChannelHandlerTest { static var allTests : [(String, (TypeAssistedChannelHandlerTest) -> () throws -> Void)] { return [ - ("testOptionalInboundIn", testOptionalInboundIn), ("testCanDefineBothInboundAndOutbound", testCanDefineBothInboundAndOutbound), ] } diff --git a/Tests/NIOTests/TypeAssistedChannelHandlerTests.swift b/Tests/NIOTests/TypeAssistedChannelHandlerTests.swift index 5c60f106e..0e50c83a0 100644 --- a/Tests/NIOTests/TypeAssistedChannelHandlerTests.swift +++ b/Tests/NIOTests/TypeAssistedChannelHandlerTests.swift @@ -16,22 +16,6 @@ import XCTest @testable import NIO class TypeAssistedChannelHandlerTest: XCTestCase { - func testOptionalInboundIn() throws { - class TestClass: ChannelInboundHandler { - public typealias InboundIn = String - - func canUnwrap(_ data: NIOAny) -> Bool { - return tryUnwrapInboundIn(data) != nil - } - } - - let c = TestClass() - let goodAny = NIOAny("Hello, world!") - let badAny = NIOAny(42) - XCTAssertTrue(c.canUnwrap(goodAny)) - XCTAssertFalse(c.canUnwrap(badAny)) - } - func testCanDefineBothInboundAndOutbound() throws { class TestClass: ChannelInboundHandler, ChannelOutboundHandler { public typealias OutboundIn = ByteBuffer