diff --git a/stdlib/public/core/Array.swift b/stdlib/public/core/Array.swift index 46066791213e0..dbb71e14c311c 100644 --- a/stdlib/public/core/Array.swift +++ b/stdlib/public/core/Array.swift @@ -2136,3 +2136,33 @@ internal struct _ArrayAnyHashableBox } extension Array: @unchecked Sendable where Element: Sendable { } + +extension Array { + /// Returns a boolean value indicating whether this array is identical to + /// `other`. + /// + /// Two array values are identical if there is no way to distinguish between + /// them. + /// + /// Comparing arrays this way includes comparing (normally) hidden + /// implementation details such as the memory location of any underlying + /// array storage object. Therefore, identical arrays are guaranteed to + /// compare equal with `==`, but not all equal arrays are considered + /// identical. + /// + /// - Performance: O(1) + @_alwaysEmitIntoClient + public func isIdentical(to other: Self) -> Bool { + let lhsCount = self.count + if lhsCount != other.count { + return false + } + + // Test referential equality. + if unsafe lhsCount == 0 || self._buffer.identity == other._buffer.identity { + return true + } + + return false + } +} diff --git a/stdlib/public/core/ArraySlice.swift b/stdlib/public/core/ArraySlice.swift index 9b278648dbc28..e3f2b43d3ffda 100644 --- a/stdlib/public/core/ArraySlice.swift +++ b/stdlib/public/core/ArraySlice.swift @@ -1586,3 +1586,33 @@ extension ArraySlice { } } #endif + +extension ArraySlice { + /// Returns a boolean value indicating whether this array is identical to + /// `other`. + /// + /// Two array values are identical if there is no way to distinguish between + /// them. + /// + /// Comparing arrays this way includes comparing (normally) hidden + /// implementation details such as the memory location of any underlying + /// array storage object. Therefore, identical arrays are guaranteed to + /// compare equal with `==`, but not all equal arrays are considered + /// identical. + /// + /// - Performance: O(1) + @_alwaysEmitIntoClient + public func isIdentical(to other: Self) -> Bool { + let lhsCount = self.count + if lhsCount != other.count { + return false + } + + // Test referential equality. + if unsafe lhsCount == 0 || self._buffer.identity == other._buffer.identity { + return true + } + + return false + } +} diff --git a/stdlib/public/core/ContiguousArray.swift b/stdlib/public/core/ContiguousArray.swift index 19b5daf8e9193..41a4a4e37771a 100644 --- a/stdlib/public/core/ContiguousArray.swift +++ b/stdlib/public/core/ContiguousArray.swift @@ -1495,3 +1495,33 @@ extension ContiguousArray { extension ContiguousArray: @unchecked Sendable where Element: Sendable { } + +extension ContiguousArray { + /// Returns a boolean value indicating whether this array is identical to + /// `other`. + /// + /// Two array values are identical if there is no way to distinguish between + /// them. + /// + /// Comparing arrays this way includes comparing (normally) hidden + /// implementation details such as the memory location of any underlying + /// array storage object. Therefore, identical arrays are guaranteed to + /// compare equal with `==`, but not all equal arrays are considered + /// identical. + /// + /// - Performance: O(1) + @_alwaysEmitIntoClient + public func isIdentical(to other: Self) -> Bool { + let lhsCount = self.count + if lhsCount != other.count { + return false + } + + // Test referential equality. + if unsafe lhsCount == 0 || self._buffer.identity == other._buffer.identity { + return true + } + + return false + } +}