Skip to content

Commit

Permalink
remove extension public/internal modifiers (#822)
Browse files Browse the repository at this point in the history
Motivation:

public/internal modifiers for `extension`s are confusing as a
`func foo` is public if within a `public extension`.

Modifications:

remove all `internal` (redundant) and `public` (dangerous) modifiers for
`extensions`.

Result:

- code easier to read
- code much easier to review in a small diff
  • Loading branch information
weissi authored Feb 14, 2019
1 parent f84b5b8 commit 0ac7e22
Show file tree
Hide file tree
Showing 18 changed files with 72 additions and 70 deletions.
4 changes: 2 additions & 2 deletions Sources/NIO/BlockingIOThreadPool.swift
Original file line number Diff line number Diff line change
Expand Up @@ -181,15 +181,15 @@ public final class BlockingIOThreadPool {
}
}

public extension BlockingIOThreadPool {
extension BlockingIOThreadPool {
/// Runs the submitted closure if the thread pool is still active, otherwise fails the promise.
/// The closure will be run on the thread pool so can do blocking work.
///
/// - parameters:
/// - eventLoop: The `EventLoop` the returned `EventLoopFuture` will fire on.
/// - body: The closure which performs some blocking work to be done on the thread pool.
/// - returns: The `EventLoopFuture` of `promise` fulfilled with the result (or error) of the passed closure.
func runIfActive<T>(eventLoop: EventLoop, _ body: @escaping () throws -> T) -> EventLoopFuture<T> {
public func runIfActive<T>(eventLoop: EventLoop, _ body: @escaping () throws -> T) -> EventLoopFuture<T> {
let promise = eventLoop.makePromise(of: T.self)
self.submit { shouldRun in
guard case shouldRun = BlockingIOThreadPool.WorkItemState.active else {
Expand Down
6 changes: 3 additions & 3 deletions Sources/NIO/ByteBuffer-views.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ public struct ByteBufferView: RandomAccessCollection {
}
}

public extension ByteBuffer {
extension ByteBuffer {
/// A view into the readable bytes of the `ByteBuffer`.
var readableBytesView: ByteBufferView {
public var readableBytesView: ByteBufferView {
return ByteBufferView(buffer: self, range: self.readerIndex ..< self.readerIndex + self.readableBytes)
}

Expand All @@ -73,7 +73,7 @@ public extension ByteBuffer {
/// - index: The index the view should start at
/// - length: The length of the view (in bytes)
/// - returns A view into a portion of a `ByteBuffer`.
func viewBytes(at index: Int, length: Int) -> ByteBufferView {
public func viewBytes(at index: Int, length: Int) -> ByteBufferView {
return ByteBufferView(buffer: self, range: index ..< index+length)
}
}
16 changes: 8 additions & 8 deletions Sources/NIO/Channel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -221,39 +221,39 @@ extension Channel {


/// Provides special extension to make writing data to the `Channel` easier by removing the need to wrap data in `NIOAny` manually.
public extension Channel {
extension Channel {

/// Write data into the `Channel`, automatically wrapping with `NIOAny`.
///
/// - seealso: `ChannelOutboundInvoker.write`.
func write<T>(_ any: T) -> EventLoopFuture<Void> {
public func write<T>(_ any: T) -> EventLoopFuture<Void> {
return self.write(NIOAny(any))
}

/// Write data into the `Channel`, automatically wrapping with `NIOAny`.
///
/// - seealso: `ChannelOutboundInvoker.write`.
func write<T>(_ any: T, promise: EventLoopPromise<Void>?) {
public func write<T>(_ any: T, promise: EventLoopPromise<Void>?) {
self.write(NIOAny(any), promise: promise)
}

/// Write and flush data into the `Channel`, automatically wrapping with `NIOAny`.
///
/// - seealso: `ChannelOutboundInvoker.writeAndFlush`.
func writeAndFlush<T>(_ any: T) -> EventLoopFuture<Void> {
public func writeAndFlush<T>(_ any: T) -> EventLoopFuture<Void> {
return self.writeAndFlush(NIOAny(any))
}


/// Write and flush data into the `Channel`, automatically wrapping with `NIOAny`.
///
/// - seealso: `ChannelOutboundInvoker.writeAndFlush`.
func writeAndFlush<T>(_ any: T, promise: EventLoopPromise<Void>?) {
public func writeAndFlush<T>(_ any: T, promise: EventLoopPromise<Void>?) {
self.writeAndFlush(NIOAny(any), promise: promise)
}
}

public extension ChannelCore {
extension ChannelCore {
/// Unwraps the given `NIOAny` as a specific concrete type.
///
/// This method is intended for use when writing custom `ChannelCore` implementations.
Expand All @@ -270,7 +270,7 @@ public extension ChannelCore {
/// - as: The type to extract from the `NIOAny`.
/// - returns: The content of the `NIOAny`.
@inlinable
func unwrapData<T>(_ data: NIOAny, as: T.Type = T.self) -> T {
public func unwrapData<T>(_ data: NIOAny, as: T.Type = T.self) -> T {
return data.forceAs()
}

Expand All @@ -283,7 +283,7 @@ public extension ChannelCore {
///
/// - parameters:
/// - channel: The `Channel` whose `ChannelPipeline` will be closed.
func removeHandlers(channel: Channel) {
public func removeHandlers(channel: Channel) {
channel.pipeline.removeHandlers()
}
}
Expand Down
4 changes: 2 additions & 2 deletions Sources/NIO/EventLoopFuture.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1101,7 +1101,7 @@ extension EventLoopFuture {
}
}

public extension EventLoopFuture {
extension EventLoopFuture {
/// Returns an `EventLoopFuture` that fires when this future completes, but executes its callbacks on the
/// target event loop instead of the original one.
///
Expand All @@ -1113,7 +1113,7 @@ public extension EventLoopFuture {
/// - parameters:
/// - to: The `EventLoop` that the returned `EventLoopFuture` will run on.
/// - returns: An `EventLoopFuture` whose callbacks run on `target` instead of the original loop.
func hop(to target: EventLoop) -> EventLoopFuture<Value> {
public func hop(to target: EventLoop) -> EventLoopFuture<Value> {
if target === self.eventLoop {
// We're already on that event loop, nothing to do here. Save an allocation.
return self
Expand Down
2 changes: 1 addition & 1 deletion Sources/NIO/LinuxCPUSet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ import CNIOLinux
extension LinuxCPUSet: Equatable {}

/// Linux specific extension to `Thread`.
internal extension Thread {
extension Thread {
/// Specify the thread-affinity of the `Thread` itself.
var affinity: LinuxCPUSet {
get {
Expand Down
14 changes: 7 additions & 7 deletions Sources/NIO/MulticastChannel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,34 +56,34 @@ public protocol MulticastChannel: Channel {


// MARK:- Default implementations for MulticastChannel
public extension MulticastChannel {
func joinGroup(_ group: SocketAddress, promise: EventLoopPromise<Void>?) {
extension MulticastChannel {
public func joinGroup(_ group: SocketAddress, promise: EventLoopPromise<Void>?) {
self.joinGroup(group, interface: nil, promise: promise)
}

func joinGroup(_ group: SocketAddress) -> EventLoopFuture<Void> {
public func joinGroup(_ group: SocketAddress) -> EventLoopFuture<Void> {
let promise = self.eventLoop.makePromise(of: Void.self)
self.joinGroup(group, promise: promise)
return promise.futureResult
}

func joinGroup(_ group: SocketAddress, interface: NIONetworkInterface?) -> EventLoopFuture<Void> {
public func joinGroup(_ group: SocketAddress, interface: NIONetworkInterface?) -> EventLoopFuture<Void> {
let promise = self.eventLoop.makePromise(of: Void.self)
self.joinGroup(group, interface: interface, promise: promise)
return promise.futureResult
}

func leaveGroup(_ group: SocketAddress, promise: EventLoopPromise<Void>?) {
public func leaveGroup(_ group: SocketAddress, promise: EventLoopPromise<Void>?) {
self.leaveGroup(group, interface: nil, promise: promise)
}

func leaveGroup(_ group: SocketAddress) -> EventLoopFuture<Void> {
public func leaveGroup(_ group: SocketAddress) -> EventLoopFuture<Void> {
let promise = self.eventLoop.makePromise(of: Void.self)
self.leaveGroup(group, promise: promise)
return promise.futureResult
}

func leaveGroup(_ group: SocketAddress, interface: NIONetworkInterface?) -> EventLoopFuture<Void> {
public func leaveGroup(_ group: SocketAddress, interface: NIONetworkInterface?) -> EventLoopFuture<Void> {
let promise = self.eventLoop.makePromise(of: Void.self)
self.leaveGroup(group, interface: interface, promise: promise)
return promise.futureResult
Expand Down
2 changes: 1 addition & 1 deletion Sources/NIO/PriorityQueue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ extension PriorityQueue: Sequence {
}
}

internal extension PriorityQueue {
extension PriorityQueue {
var count: Int {
return self.heap.count
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/NIO/Selector.swift
Original file line number Diff line number Diff line change
Expand Up @@ -657,7 +657,7 @@ struct SelectorEvent<R> {
}
}

internal extension Selector where R == NIORegistration {
extension Selector where R == NIORegistration {
/// Gently close the `Selector` after all registered `Channel`s are closed.
func closeGently(eventLoop: EventLoop) -> EventLoopFuture<Void> {
guard self.lifecycleState == .open else {
Expand Down
34 changes: 17 additions & 17 deletions Sources/NIO/SocketOptionProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -81,22 +81,22 @@ public protocol SocketOptionProvider {
// around. As a result, if you change one, you should probably change them all.
//
// You are welcome to add more helper methods here, but each helper method you add must be tested.
public extension SocketOptionProvider {
extension SocketOptionProvider {
/// Sets the socket option SO_LINGER to `value`.
///
/// - parameters:
/// - value: The value to set SO_LINGER to.
/// - returns: An `EventLoopFuture` that fires when the option has been set,
/// or if an error has occurred.
func setSoLinger(_ value: linger) -> EventLoopFuture<Void> {
public func setSoLinger(_ value: linger) -> EventLoopFuture<Void> {
return self.unsafeSetSocketOption(level: SocketOptionLevel(SOL_SOCKET), name: SO_LINGER, value: value)
}

/// Gets the value of the socket option SO_LINGER.
///
/// - returns: An `EventLoopFuture` containing the value of the socket option, or
/// any error that occurred while retrieving the socket option.
func getSoLinger() -> EventLoopFuture<linger> {
public func getSoLinger() -> EventLoopFuture<linger> {
return self.unsafeGetSocketOption(level: SocketOptionLevel(SOL_SOCKET), name: SO_LINGER)
}

Expand All @@ -106,15 +106,15 @@ public extension SocketOptionProvider {
/// - value: The value to set IP_MULTICAST_IF to.
/// - returns: An `EventLoopFuture` that fires when the option has been set,
/// or if an error has occurred.
func setIPMulticastIF(_ value: in_addr) -> EventLoopFuture<Void> {
public func setIPMulticastIF(_ value: in_addr) -> EventLoopFuture<Void> {
return self.unsafeSetSocketOption(level: IPPROTO_IP, name: IP_MULTICAST_IF, value: value)
}

/// Gets the value of the socket option IP_MULTICAST_IF.
///
/// - returns: An `EventLoopFuture` containing the value of the socket option, or
/// any error that occurred while retrieving the socket option.
func getIPMulticastIF() -> EventLoopFuture<in_addr> {
public func getIPMulticastIF() -> EventLoopFuture<in_addr> {
return self.unsafeGetSocketOption(level: IPPROTO_IP, name: IP_MULTICAST_IF)
}

Expand All @@ -124,15 +124,15 @@ public extension SocketOptionProvider {
/// - value: The value to set IP_MULTICAST_TTL to.
/// - returns: An `EventLoopFuture` that fires when the option has been set,
/// or if an error has occurred.
func setIPMulticastTTL(_ value: CUnsignedChar) -> EventLoopFuture<Void> {
public func setIPMulticastTTL(_ value: CUnsignedChar) -> EventLoopFuture<Void> {
return self.unsafeSetSocketOption(level: IPPROTO_IP, name: IP_MULTICAST_TTL, value: value)
}

/// Gets the value of the socket option IP_MULTICAST_TTL.
///
/// - returns: An `EventLoopFuture` containing the value of the socket option, or
/// any error that occurred while retrieving the socket option.
func getIPMulticastTTL() -> EventLoopFuture<CUnsignedChar> {
public func getIPMulticastTTL() -> EventLoopFuture<CUnsignedChar> {
return self.unsafeGetSocketOption(level: IPPROTO_IP, name: IP_MULTICAST_TTL)
}

Expand All @@ -142,15 +142,15 @@ public extension SocketOptionProvider {
/// - value: The value to set IP_MULTICAST_LOOP to.
/// - returns: An `EventLoopFuture` that fires when the option has been set,
/// or if an error has occurred.
func setIPMulticastLoop(_ value: CUnsignedChar) -> EventLoopFuture<Void> {
public func setIPMulticastLoop(_ value: CUnsignedChar) -> EventLoopFuture<Void> {
return self.unsafeSetSocketOption(level: IPPROTO_IP, name: IP_MULTICAST_LOOP, value: value)
}

/// Gets the value of the socket option IP_MULTICAST_LOOP.
///
/// - returns: An `EventLoopFuture` containing the value of the socket option, or
/// any error that occurred while retrieving the socket option.
func getIPMulticastLoop() -> EventLoopFuture<CUnsignedChar> {
public func getIPMulticastLoop() -> EventLoopFuture<CUnsignedChar> {
return self.unsafeGetSocketOption(level: IPPROTO_IP, name: IP_MULTICAST_LOOP)
}

Expand All @@ -160,15 +160,15 @@ public extension SocketOptionProvider {
/// - value: The value to set IPV6_MULTICAST_IF to.
/// - returns: An `EventLoopFuture` that fires when the option has been set,
/// or if an error has occurred.
func setIPv6MulticastIF(_ value: CUnsignedInt) -> EventLoopFuture<Void> {
public func setIPv6MulticastIF(_ value: CUnsignedInt) -> EventLoopFuture<Void> {
return self.unsafeSetSocketOption(level: IPPROTO_IPV6, name: IPV6_MULTICAST_IF, value: value)
}

/// Gets the value of the socket option IPV6_MULTICAST_IF.
///
/// - returns: An `EventLoopFuture` containing the value of the socket option, or
/// any error that occurred while retrieving the socket option.
func getIPv6MulticastIF() -> EventLoopFuture<CUnsignedInt> {
public func getIPv6MulticastIF() -> EventLoopFuture<CUnsignedInt> {
return self.unsafeGetSocketOption(level: IPPROTO_IPV6, name: IPV6_MULTICAST_IF)
}

Expand All @@ -178,15 +178,15 @@ public extension SocketOptionProvider {
/// - value: The value to set IPV6_MULTICAST_HOPS to.
/// - returns: An `EventLoopFuture` that fires when the option has been set,
/// or if an error has occurred.
func setIPv6MulticastHops(_ value: CInt) -> EventLoopFuture<Void> {
public func setIPv6MulticastHops(_ value: CInt) -> EventLoopFuture<Void> {
return self.unsafeSetSocketOption(level: IPPROTO_IPV6, name: IPV6_MULTICAST_HOPS, value: value)
}

/// Gets the value of the socket option IPV6_MULTICAST_HOPS.
///
/// - returns: An `EventLoopFuture` containing the value of the socket option, or
/// any error that occurred while retrieving the socket option.
func getIPv6MulticastHops() -> EventLoopFuture<CInt> {
public func getIPv6MulticastHops() -> EventLoopFuture<CInt> {
return self.unsafeGetSocketOption(level: IPPROTO_IPV6, name: IPV6_MULTICAST_HOPS)
}

Expand All @@ -196,15 +196,15 @@ public extension SocketOptionProvider {
/// - value: The value to set IPV6_MULTICAST_LOOP to.
/// - returns: An `EventLoopFuture` that fires when the option has been set,
/// or if an error has occurred.
func setIPv6MulticastLoop(_ value: CUnsignedInt) -> EventLoopFuture<Void> {
public func setIPv6MulticastLoop(_ value: CUnsignedInt) -> EventLoopFuture<Void> {
return self.unsafeSetSocketOption(level: IPPROTO_IPV6, name: IPV6_MULTICAST_LOOP, value: value)
}

/// Gets the value of the socket option IPV6_MULTICAST_LOOP.
///
/// - returns: An `EventLoopFuture` containing the value of the socket option, or
/// any error that occurred while retrieving the socket option.
func getIPv6MulticastLoop() -> EventLoopFuture<CUnsignedInt> {
public func getIPv6MulticastLoop() -> EventLoopFuture<CUnsignedInt> {
return self.unsafeGetSocketOption(level: IPPROTO_IPV6, name: IPV6_MULTICAST_LOOP)
}

Expand All @@ -215,7 +215,7 @@ public extension SocketOptionProvider {
///
/// - returns: An `EventLoopFuture` containing the value of the socket option, or
/// any error that occurred while retrieving the socket option.
func getTCPInfo() -> EventLoopFuture<tcp_info> {
public func getTCPInfo() -> EventLoopFuture<tcp_info> {
return self.unsafeGetSocketOption(level: IPPROTO_TCP, name: TCP_INFO)
}
#endif
Expand All @@ -227,7 +227,7 @@ public extension SocketOptionProvider {
///
/// - returns: An `EventLoopFuture` containing the value of the socket option, or
/// any error that occurred while retrieving the socket option.
func getTCPConnectionInfo() -> EventLoopFuture<tcp_connection_info> {
public func getTCPConnectionInfo() -> EventLoopFuture<tcp_connection_info> {
return self.unsafeGetSocketOption(level: IPPROTO_TCP, name: TCP_CONNECTION_INFO)
}
#endif
Expand Down
13 changes: 7 additions & 6 deletions Sources/NIOHTTP1/HTTPPipelineSetup.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@ import NIO
/// properties.
public typealias HTTPUpgradeConfiguration = (upgraders: [HTTPServerProtocolUpgrader], completionHandler: (ChannelHandlerContext) -> Void)

public extension ChannelPipeline {
extension ChannelPipeline {
/// Configure a `ChannelPipeline` for use as a HTTP client.
///
/// - parameters:
/// - first: Whether to add the HTTP client at the head of the channel pipeline,
/// or at the tail.
/// - returns: An `EventLoopFuture` that will fire when the pipeline is configured.
func addHTTPClientHandlers(first: Bool = false, leftOverBytesStrategy: RemoveAfterUpgradeStrategy = .dropBytes) -> EventLoopFuture<Void> {
public func addHTTPClientHandlers(first: Bool = false,
leftOverBytesStrategy: RemoveAfterUpgradeStrategy = .dropBytes) -> EventLoopFuture<Void> {
return addHandlers(HTTPRequestEncoder(), HTTPResponseDecoder(leftOverBytesStrategy: leftOverBytesStrategy), first: first)
}

Expand Down Expand Up @@ -57,10 +58,10 @@ public extension ChannelPipeline {
/// - errorHandling: Whether to provide assistance handling protocol errors (e.g.
/// failure to parse the HTTP request) by sending 400 errors. Defaults to `true`.
/// - returns: An `EventLoopFuture` that will fire when the pipeline is configured.
func configureHTTPServerPipeline(first: Bool = false,
withPipeliningAssistance pipelining: Bool = true,
withServerUpgrade upgrade: HTTPUpgradeConfiguration? = nil,
withErrorHandling errorHandling: Bool = true) -> EventLoopFuture<Void> {
public func configureHTTPServerPipeline(first: Bool = false,
withPipeliningAssistance pipelining: Bool = true,
withServerUpgrade upgrade: HTTPUpgradeConfiguration? = nil,
withErrorHandling errorHandling: Bool = true) -> EventLoopFuture<Void> {
let responseEncoder = HTTPResponseEncoder()
let requestDecoder = HTTPRequestDecoder(leftOverBytesStrategy: upgrade == nil ? .dropBytes : .forwardBytes)

Expand Down
2 changes: 1 addition & 1 deletion Sources/NIOHTTP1/HTTPResponseCompressor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import CNIOZlib
import NIO

internal extension String {
extension String {
/// Test if this `Collection` starts with the unicode scalars of `needle`.
///
/// - note: This will be faster than `String.startsWith` as no unicode normalisations are performed.
Expand Down
Loading

0 comments on commit 0ac7e22

Please sign in to comment.