From e3fba4ec5e7ffcea0915732be469b799d21c253f Mon Sep 17 00:00:00 2001 From: Philippe Weidmann Date: Thu, 21 Mar 2024 15:20:14 +0100 Subject: [PATCH] feat: EnsureFrozen propertyWrapper --- .../Extensions/EnsureFrozen.swift | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 Sources/InfomaniakCore/Extensions/EnsureFrozen.swift diff --git a/Sources/InfomaniakCore/Extensions/EnsureFrozen.swift b/Sources/InfomaniakCore/Extensions/EnsureFrozen.swift new file mode 100644 index 0000000..29ede8a --- /dev/null +++ b/Sources/InfomaniakCore/Extensions/EnsureFrozen.swift @@ -0,0 +1,48 @@ +/* + Infomaniak Core - iOS + Copyright (C) 2024 Infomaniak Network SA + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + */ + +import Foundation +import RealmSwift + + +/// This property wrapper ensures that the passed object is frozen. In case the given object isn't frozen an assert is raised. +@propertyWrapper +public struct EnsureFrozen { + public let wrappedValue: Value + + public init(wrappedValue: Value) { + #if DEBUG + assert(wrappedValue.isFrozen, "Object should be frozen") + #endif + self.wrappedValue = wrappedValue + } +} + +/// This property wrapper ensures that the passed object collection is frozen. In case all the objects in the given collection +/// aren't frozen an assert is raised. +@propertyWrapper +public struct EnsureFrozenCollection> { + public let wrappedValue: Value + + public init(wrappedValue: Value) { + #if DEBUG + assert(wrappedValue.allSatisfy { $0.isFrozen }, "All objects in collection should be frozen") + #endif + self.wrappedValue = wrappedValue + } +}