Skip to content

Commit ea0ba58

Browse files
committed
make PingResponse Hashable
1 parent fb1bb3d commit ea0ba58

File tree

1 file changed

+18
-11
lines changed

1 file changed

+18
-11
lines changed

Sources/SwiftyPing/SwiftyPing.swift

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public protocol PingDelegate {
2424
}
2525

2626
/// Describes all possible errors thrown within `SwiftyPing`
27-
public enum PingError: Error, Equatable {
27+
public enum PingError: Error, Equatable, Hashable {
2828
// Response errors
2929

3030
/// The response took longer to arrive than `configuration.timeoutInterval`.
@@ -712,14 +712,7 @@ public class SwiftyPing: NSObject {
712712

713713
// MARK: ICMP
714714

715-
public struct IPv4Address: Equatable {
716-
public static func == (lhs: IPv4Address, rhs: IPv4Address) -> Bool {
717-
return lhs.bytes.0 == rhs.bytes.0 &&
718-
lhs.bytes.1 == rhs.bytes.1 &&
719-
lhs.bytes.2 == rhs.bytes.2 &&
720-
lhs.bytes.3 == rhs.bytes.3
721-
}
722-
715+
public struct IPv4Address: Equatable, Hashable {
723716
public var bytes: (UInt8, UInt8, UInt8, UInt8)
724717

725718
init(a: UInt8, b: UInt8, c: UInt8, d: UInt8) {
@@ -728,10 +721,24 @@ public struct IPv4Address: Equatable {
728721
self.bytes.2 = c
729722
self.bytes.3 = d
730723
}
724+
725+
public static func == (lhs: IPv4Address, rhs: IPv4Address) -> Bool {
726+
return lhs.bytes.0 == rhs.bytes.0 &&
727+
lhs.bytes.1 == rhs.bytes.1 &&
728+
lhs.bytes.2 == rhs.bytes.2 &&
729+
lhs.bytes.3 == rhs.bytes.3
730+
}
731+
732+
public func hash(into hasher: inout Hasher) {
733+
hasher.combine(bytes.0)
734+
hasher.combine(bytes.1)
735+
hasher.combine(bytes.2)
736+
hasher.combine(bytes.3)
737+
}
731738
}
732739

733740
/// Format of IPv4 header
734-
public struct IPHeader: Equatable {
741+
public struct IPHeader: Equatable, Hashable {
735742
public var versionAndHeaderLength: UInt8
736743
public var differentiatedServices: UInt8
737744
public var totalLength: UInt16
@@ -770,7 +777,7 @@ public enum ICMPType: UInt8 {
770777
// MARK: - Helpers
771778

772779
/// A struct encapsulating a ping response.
773-
public struct PingResponse: Equatable {
780+
public struct PingResponse: Equatable, Hashable {
774781
/// The randomly generated identifier used in the ping header.
775782
public let identifier: UInt16
776783
/// The IP address of the host.

0 commit comments

Comments
 (0)