i’ve got a type called InlineBuffer
defined as:
@frozen public
struct InlineBuffer<Storage>:Sendable where Storage:Sendable
{
public
var storage:Storage
@inlinable public
init(storage:Storage)
{
self.storage = storage
}
}
the idea is to use it to wrap some tuple storage, such as:
@frozen public
struct MD5:Sendable
{
@usableFromInline internal
typealias Storage = (UInt32, UInt32, UInt32, UInt32)
@usableFromInline internal
var buffer:InlineBuffer<Storage>
@inlinable internal
init(buffer:InlineBuffer<Storage>)
{
self.buffer = buffer
}
}
but now i am running into trouble with the Equatable
conformance, since Storage
is a tuple, and cannot itself be constrained to conform to Equatable
. the best i could come up with is a really hacky UnsafeRawBufferPointer.elementsEqual(_:)
-based comparison that looks like:
extension InlineBuffer:Equatable
{
@inlinable public static
func == (lhs:Self, rhs:Self) -> Bool
{
withUnsafeBytes(of: lhs.storage)
{
(lhs:UnsafeRawBufferPointer) in
withUnsafeBytes(of: rhs.storage)
{
(rhs:UnsafeRawBufferPointer) in
lhs.elementsEqual(rhs)
}
}
}
}
but this compares the bytes one at a time, and i suspect it might be faster to compare the tuple directly, where is it statically known to have the same length and can go four bytes at a time.