Skip to content

Commit

Permalink
Improve AnySendableHashable to match Hashable (#191)
Browse files Browse the repository at this point in the history
  • Loading branch information
0xTim authored Nov 4, 2023
1 parent 21b72c5 commit cc584bd
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
14 changes: 0 additions & 14 deletions Sources/ConsoleKit/Terminal/Terminal.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,6 @@ import CRT
import Foundation
import NIOConcurrencyHelpers

/// A `Sendable` version of the standard library's `AnyHashable` type.
public struct AnySendableHashable: @unchecked Sendable, Hashable, ExpressibleByStringLiteral {
// Note: @unchecked Sendable since there's no way to express that `wrappedValue` is Sendable, even though we ensure that it is in the init.
let wrappedValue: AnyHashable

public init(_ wrappedValue: any Hashable & Sendable) {
self.wrappedValue = AnyHashable(wrappedValue)
}

public init(stringLiteral value: String) {
self.init(value)
}
}

/// Generic console that uses a mixture of Swift standard
/// library and Foundation code to fulfill protocol requirements.
public final class Terminal: Console, Sendable {
Expand Down
19 changes: 19 additions & 0 deletions Sources/ConsoleKit/Utilities/AnySendableHashable.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/// A `Sendable` version of the standard library's `AnyHashable` type.
public struct AnySendableHashable: @unchecked Sendable, Hashable, ExpressibleByStringLiteral {
// Note: @unchecked Sendable since there's no way to express that `wrappedValue` is Sendable, even though we ensure that it is in the init.
let wrappedValue: AnyHashable

public init(_ wrappedValue: some Hashable & Sendable) {
self.wrappedValue = AnyHashable(wrappedValue)
}

public init(stringLiteral value: String) {
self.init(value)
}
}

extension AnySendableHashable: CustomStringConvertible, CustomDebugStringConvertible, CustomReflectable {
public var description: String { self.wrappedValue.description }
public var debugDescription: String { self.wrappedValue.debugDescription }
public var customMirror: Mirror { self.wrappedValue.customMirror }
}

0 comments on commit cc584bd

Please sign in to comment.