SE-0185: let Swift 4.1 generate as much Equatable and Hashable conformance as possible

This commit is contained in:
Gwendal Roué committed 2018-02-25 09:24:23 +01:00
1 parent b0e126f29d
commit b4922f60a2
8 files changed
+63 -26

No files matched your search

+6
View File
@@ -841,11 +841,14 @@ extension Database {
self.rawValue = rawValue
}
#if swift(>=4.1)
#else
/// The hash value
/// :nodoc:
public var hashValue: Int {
return rawValue.hashValue
}
#endif
/// The `BINARY` built-in SQL collation
public static let binary = CollationName("BINARY")
@@ -878,11 +881,14 @@ extension Database {
self.rawValue = rawValue
}
#if swift(>=4.1)
#else
/// The hash value
/// :nodoc:
public var hashValue: Int {
return rawValue.hashValue
}
#endif
/// The `TEXT` SQL column type
public static let text = ColumnType("TEXT")
+3
View File
@@ -139,6 +139,8 @@ public struct ResultCode : RawRepresentable, Equatable, CustomStringConvertible
public static let SQLITE_OK_LOAD_PERMANENTLY = ResultCode(rawValue: (SQLITE_OK.rawValue | (1<<8)))
}
#if swift(>=4.1)
#else
// Equatable
extension ResultCode {
/// :nodoc:
@@ -146,6 +148,7 @@ extension ResultCode {
return lhs.rawValue == rhs.rawValue
}
}
#endif
// CustomStringConvertible
extension ResultCode {
+29 -15
View File
@@ -5,12 +5,28 @@
#endif
/// An SQL function or aggregate.
public final class DatabaseFunction {
public let name: String
let argumentCount: Int32?
public final class DatabaseFunction: Hashable {
// SQLite identifies functions by (name + argument count)
private struct Identity: Hashable {
let name: String
let nArg: Int32 // -1 for variadic functions
#if swift(>=4.1)
#else
var hashValue: Int {
return name.hashValue ^ nArg.hashValue
}
static func == (lhs: Identity, rhs: Identity) -> Bool {
return lhs.name == rhs.name && lhs.nArg == rhs.nArg
}
#endif
}
public var name: String { return identity.name }
private let identity: Identity
let pure: Bool
private let kind: Kind
private var nArg: Int32 { return argumentCount ?? -1 }
private var eTextRep: Int32 { return (SQLITE_UTF8 | (pure ? SQLITE_DETERMINISTIC : 0)) }
/// Returns an SQL function.
@@ -38,8 +54,7 @@ public final class DatabaseFunction {
/// exactly *argumentCount* elements, provided *argumentCount* is
/// not nil.
public init(_ name: String, argumentCount: Int32? = nil, pure: Bool = false, function: @escaping ([DatabaseValue]) throws -> DatabaseValueConvertible?) {
self.name = name
self.argumentCount = argumentCount
self.identity = Identity(name: name, nArg: argumentCount ?? -1)
self.pure = pure
self.kind = .function{ (argc, argv) in
let arguments = (0..<Int(argc)).map { index in
@@ -89,8 +104,7 @@ public final class DatabaseFunction {
/// have exactly *argumentCount* elements, provided *argumentCount* is
/// not nil.
public init<Aggregate: DatabaseAggregate>(_ name: String, argumentCount: Int32? = nil, pure: Bool = false, aggregate: Aggregate.Type) {
self.name = name
self.argumentCount = argumentCount
self.identity = Identity(name: name, nArg: argumentCount ?? -1)
self.pure = pure
self.kind = .aggregate { return Aggregate() }
}
@@ -104,8 +118,8 @@ public final class DatabaseFunction {
let code = sqlite3_create_function_v2(
db.sqliteConnection,
name,
nArg,
identity.name,
identity.nArg,
eTextRep,
definitionP,
kind.xFunc,
@@ -127,8 +141,8 @@ public final class DatabaseFunction {
func uninstall(in db: Database) {
let code = sqlite3_create_function_v2(
db.sqliteConnection,
name,
nArg,
identity.name,
identity.nArg,
eTextRep,
nil, nil, nil, nil, nil)
@@ -309,17 +323,17 @@ public final class DatabaseFunction {
}
}
extension DatabaseFunction : Hashable {
extension DatabaseFunction {
/// The hash value
/// :nodoc:
public var hashValue: Int {
return name.hashValue ^ nArg.hashValue
return identity.hashValue
}
/// Two functions are equal if they share the same name and arity.
/// :nodoc:
public static func == (lhs: DatabaseFunction, rhs: DatabaseFunction) -> Bool {
return lhs.name == rhs.name && lhs.nArg == rhs.nArg
return lhs.identity == rhs.identity
}
}
+3
View File
@@ -255,11 +255,14 @@ private struct TableRegion: Equatable {
return false
}
#if swift(>=4.1)
#else
static func == (lhs: TableRegion, rhs: TableRegion) -> Bool {
if lhs.columns != rhs.columns { return false }
if lhs.rowIds != rhs.rowIds { return false }
return true
}
#endif
func intersection(_ other: TableRegion) -> TableRegion {
let columnsIntersection: Set<String>?
+3
View File
@@ -911,6 +911,8 @@ extension StatementArguments {
}
}
#if swift(>=4.1)
#else
// Equatable
extension StatementArguments {
/// :nodoc:
@@ -920,6 +922,7 @@ extension StatementArguments {
return true
}
}
#endif
/// A thread-unsafe statement cache
struct StatementCache {
@@ -486,6 +486,8 @@ public struct SQLFunctionName : Hashable {
self.sql = sql
}
#if swift(>=4.1)
#else
/// The hash value
///
/// :nodoc:
@@ -499,6 +501,7 @@ public struct SQLFunctionName : Hashable {
public static func == (lhs: SQLFunctionName, rhs: SQLFunctionName) -> Bool {
return lhs.sql == rhs.sql
}
#endif
}
/// [**Experimental**](http://github.com/groue/GRDB.swift#what-are-experimental-features)
+4 -3
View File
@@ -1050,8 +1050,9 @@ private final class Item<T: RowConvertible> : RowConvertible, Equatable {
init(row: Row) {
self.row = row.copy()
}
static func ==<T> (lhs: Item<T>, rhs: Item<T>) -> Bool {
return lhs.row == rhs.row
}
}
private func ==<T> (lhs: Item<T>, rhs: Item<T>) -> Bool {
return lhs.row == rhs.row
}
+12 -8
View File
@@ -977,20 +977,22 @@ final class DAO {
// MARK: - InsertQuery
private struct InsertQuery {
private struct InsertQuery: Hashable {
let onConflict: Database.ConflictResolution
let tableName: String
let insertedColumns: [String]
}
extension InsertQuery : Hashable {
// Not generated by Swift 4.1
var hashValue: Int { return tableName.hashValue }
#if swift(>=4.1)
#else
static func == (lhs: InsertQuery, rhs: InsertQuery) -> Bool {
if lhs.tableName != rhs.tableName { return false }
if lhs.onConflict != rhs.onConflict { return false }
return lhs.insertedColumns == rhs.insertedColumns
}
#endif
}
extension InsertQuery {
@@ -1016,22 +1018,24 @@ extension InsertQuery {
// MARK: - UpdateQuery
private struct UpdateQuery {
private struct UpdateQuery: Hashable {
let onConflict: Database.ConflictResolution
let tableName: String
let updatedColumns: [String]
let conditionColumns: [String]
}
extension UpdateQuery : Hashable {
// Not generated by Swift 4.1
var hashValue: Int { return tableName.hashValue }
#if swift(>=4.1)
#else
static func == (lhs: UpdateQuery, rhs: UpdateQuery) -> Bool {
if lhs.tableName != rhs.tableName { return false }
if lhs.onConflict != rhs.onConflict { return false }
if lhs.updatedColumns != rhs.updatedColumns { return false }
return lhs.conditionColumns == rhs.conditionColumns
}
#endif
}
extension UpdateQuery {