Files
purchases-ios/Sources/Misc/Codable/IgnoreHashable.swift
T
Mark Villacampa 679767adf9 Enable explicit_init lint rule and fix issues (#3418)
This rule will warn when calling `init` with the explicit type where it
can be inferred, or where the `.init` part can be omitted.
2023-11-16 00:20:00 +01:00

55 lines
1.2 KiB
Swift

//
// Copyright RevenueCat Inc. All Rights Reserved.
//
// Licensed under the MIT License (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://opensource.org/licenses/MIT
//
// IgnoreHashable.swift
//
// Created by Nacho Soto on 5/24/22.
import Foundation
/// A property wrapper that allows ignoring a value from the `Hashable` / `Equatable` implementation
/// - Example:
/// ```
/// struct Data {
/// var string1: String // Data equality / hash only uses this value
/// @IgnoreHashable var string2: String
/// }
/// ```
@propertyWrapper
struct IgnoreHashable<Value> {
var wrappedValue: Value
}
extension IgnoreHashable: Hashable {
static func == (lhs: Self, rhs: Self) -> Bool { return true }
func hash(into hasher: inout Hasher) {}
}
extension IgnoreHashable: Decodable where Value: Decodable {
init(from decoder: Decoder) throws {
self.init(wrappedValue: try .init(from: decoder))
}
}
extension IgnoreHashable: Encodable where Value: Encodable {
func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
try container.encode(self.wrappedValue)
}
}