You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

61 lines
1.6 KiB

2 years ago
  1. //
  2. // Copyright © 2019 Swinject Contributors. All rights reserved.
  3. //
  4. import Foundation
  5. // MARK: ServiceKeyOption
  6. public protocol ServiceKeyOption: CustomStringConvertible {
  7. func isEqualTo(_ another: ServiceKeyOption) -> Bool
  8. func hash(into: inout Hasher)
  9. }
  10. // MARK: - ServiceKey
  11. internal struct ServiceKey {
  12. internal let serviceType: Any.Type
  13. internal let argumentsType: Any.Type
  14. internal let name: String?
  15. internal let option: ServiceKeyOption? // Used for SwinjectStoryboard or other extensions.
  16. internal init(
  17. serviceType: Any.Type,
  18. argumentsType: Any.Type,
  19. name: String? = nil,
  20. option: ServiceKeyOption? = nil
  21. ) {
  22. self.serviceType = serviceType
  23. self.argumentsType = argumentsType
  24. self.name = name
  25. self.option = option
  26. }
  27. }
  28. // MARK: Hashable
  29. extension ServiceKey: Hashable {
  30. public func hash(into hasher: inout Hasher) {
  31. ObjectIdentifier(serviceType).hash(into: &hasher)
  32. ObjectIdentifier(argumentsType).hash(into: &hasher)
  33. name?.hash(into: &hasher)
  34. option?.hash(into: &hasher)
  35. }
  36. }
  37. // MARK: Equatable
  38. func == (lhs: ServiceKey, rhs: ServiceKey) -> Bool {
  39. return lhs.serviceType == rhs.serviceType
  40. && lhs.argumentsType == rhs.argumentsType
  41. && lhs.name == rhs.name
  42. && equalOptions(opt1: lhs.option, opt2: rhs.option)
  43. }
  44. private func equalOptions(opt1: ServiceKeyOption?, opt2: ServiceKeyOption?) -> Bool {
  45. switch (opt1, opt2) {
  46. case let (opt1?, opt2?): return opt1.isEqualTo(opt2)
  47. case (nil, nil): return true
  48. default: return false
  49. }
  50. }