diff --git a/stdlib/public/core/Dictionary.swift b/stdlib/public/core/Dictionary.swift index 6e867aedec522..ef67c73bb9b14 100644 --- a/stdlib/public/core/Dictionary.swift +++ b/stdlib/public/core/Dictionary.swift @@ -2154,3 +2154,43 @@ extension Dictionary.Index: @unchecked Sendable where Key: Sendable, Value: Sendable {} extension Dictionary.Iterator: @unchecked Sendable where Key: Sendable, Value: Sendable {} + +extension Dictionary { + /// Returns a boolean value indicating whether this dictionary is identical to + /// `other`. + /// + /// Two dictionary values are identical if there is no way to distinguish + /// between them. + /// + /// Comparing dictionaries this way includes comparing (normally) hidden + /// implementation details such as the memory location of any underlying + /// dictionary storage object. Therefore, identical dictionaries are + /// guaranteed to compare equal with `==`, but not all equal dictionaries are + /// considered identical. + /// + /// - Performance: O(1) + @_alwaysEmitIntoClient + public func isIdentical(to other: Self) -> Bool { +#if _runtime(_ObjC) + if + self._variant.isNative, + other._variant.isNative, + unsafe (self._variant.asNative._storage === other._variant.asNative._storage) + { + return true + } + if + !self._variant.isNative, + !other._variant.isNative, + self._variant.asCocoa.object === other._variant.asCocoa.object + { + return true + } +#else + if unsafe (self._variant.asNative._storage === other._variant.asNative._storage) { + return true + } +#endif + return false + } +} diff --git a/stdlib/public/core/Set.swift b/stdlib/public/core/Set.swift index fa79ca2858d0a..90ff27f5d0443 100644 --- a/stdlib/public/core/Set.swift +++ b/stdlib/public/core/Set.swift @@ -1658,3 +1658,42 @@ extension Set.Index: @unchecked Sendable where Element: Sendable { } extension Set.Iterator: @unchecked Sendable where Element: Sendable { } + +extension Set { + /// Returns a boolean value indicating whether this set is identical to + /// `other`. + /// + /// Two set values are identical if there is no way to distinguish between + /// them. + /// + /// Comparing sets this way includes comparing (normally) hidden + /// implementation details such as the memory location of any underlying set + /// storage object. Therefore, identical sets are guaranteed to compare equal + /// with `==`, but not all equal sets are considered identical. + /// + /// - Performance: O(1) + @_alwaysEmitIntoClient + public func isIdentical(to other: Self) -> Bool { +#if _runtime(_ObjC) + if + self._variant.isNative, + other._variant.isNative, + unsafe (self._variant.asNative._storage === other._variant.asNative._storage) + { + return true + } + if + !self._variant.isNative, + !other._variant.isNative, + self._variant.asCocoa.object === other._variant.asCocoa.object + { + return true + } +#else + if unsafe (self._variant.asNative._storage === other._variant.asNative._storage) { + return true + } +#endif + return false + } +}