Skip to content

Commit

Permalink
Merge pull request #129 from jpsim/update-hashable
Browse files Browse the repository at this point in the history
Update `Hashable` conformance
  • Loading branch information
norio-nomura authored Jun 17, 2018
2 parents 64bceaa + 7f9eff9 commit c64a5f2
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 0 deletions.
16 changes: 16 additions & 0 deletions Sources/Yams/Node.Mapping.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,16 @@ extension Node.Mapping: Equatable {
}
}

#if swift(>=4.1.50)
extension Node.Mapping: Hashable {
/// :nodoc:
public func hash(into hasher: inout Hasher) {
hasher.combine(pairs)
hasher.combine(resolvedTag)
}
}
#endif

extension Node.Mapping: ExpressibleByDictionaryLiteral {
/// :nodoc:
public init(dictionaryLiteral elements: (Node, Node)...) {
Expand Down Expand Up @@ -187,9 +197,11 @@ private struct Pair<Value: Comparable & Equatable>: Comparable, Equatable {
self.value = value
}

#if !swift(>=4.1.50)
static func == (lhs: Pair, rhs: Pair) -> Bool {
return lhs.key == rhs.key && lhs.value == rhs.value
}
#endif

static func < (lhs: Pair<Value>, rhs: Pair<Value>) -> Bool {
return lhs.key < rhs.key
Expand All @@ -199,3 +211,7 @@ private struct Pair<Value: Comparable & Equatable>: Comparable, Equatable {
return (key: pair.key, value: pair.value)
}
}

#if swift(>=4.1.50)
extension Pair: Hashable where Value: Hashable {}
#endif
10 changes: 10 additions & 0 deletions Sources/Yams/Node.Scalar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,16 @@ extension Node.Scalar: Equatable {
}
}

#if swift(>=4.1.50)
extension Node.Scalar: Hashable {
/// :nodoc:
public func hash(into hasher: inout Hasher) {
hasher.combine(string)
hasher.combine(resolvedTag)
}
}
#endif

extension Node.Scalar: TagResolvable {
static let defaultTagName = Tag.Name.str
func resolveTag(using resolver: Resolver) -> Tag.Name {
Expand Down
10 changes: 10 additions & 0 deletions Sources/Yams/Node.Sequence.swift
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,16 @@ extension Node.Sequence: Equatable {
}
}

#if swift(>=4.1.50)
extension Node.Sequence: Hashable {
/// :nodoc:
public func hash(into hasher: inout Hasher) {
hasher.combine(nodes)
hasher.combine(resolvedTag)
}
}
#endif

extension Node.Sequence: ExpressibleByArrayLiteral {
/// :nodoc:
public init(arrayLiteral elements: Node...) {
Expand Down
2 changes: 2 additions & 0 deletions Sources/Yams/Node.swift
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ extension Node {
// MARK: Hashable

extension Node: Hashable {
#if !swift(>=4.1.50)
/// This `Node`'s Hashable `hashValue`.
public var hashValue: Int {
switch self {
Expand Down Expand Up @@ -216,6 +217,7 @@ extension Node: Hashable {
return false
}
}
#endif
}

// MARK: Comparable
Expand Down
9 changes: 9 additions & 0 deletions Sources/Yams/Tag.swift
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,17 @@ extension Tag: CustomStringConvertible {
}

extension Tag: Hashable {
#if swift(>=4.1.50)
/// :nodoc:
public func hash(into hasher: inout Hasher) {
hasher.combine(name)
}
#else
/// :nodoc:
public var hashValue: Int {
return name.hashValue
}
#endif

/// :nodoc:
public static func == (lhs: Tag, rhs: Tag) -> Bool {
Expand All @@ -98,6 +105,7 @@ extension Tag.Name: ExpressibleByStringLiteral {
}

extension Tag.Name: Hashable {
#if !swift(>=4.1.50)
/// :nodoc:
public var hashValue: Int {
return rawValue.hashValue
Expand All @@ -107,6 +115,7 @@ extension Tag.Name: Hashable {
public static func == (lhs: Tag.Name, rhs: Tag.Name) -> Bool {
return lhs.rawValue == rhs.rawValue
}
#endif
}

// http://www.yaml.org/spec/1.2/spec.html#Schema
Expand Down

0 comments on commit c64a5f2

Please sign in to comment.