Repository: apple/swift-nio
File: Sources/NIOFoundationCompat/ByteBuffer-foundation.swift (Swift)

Code before:
    /// - returns: The number of bytes written.
    @inlinable
    @discardableResult
    public mutating func setData<D: DataProtocol>(_ data: D, at index: Int) -> Int {
        // DataProtocol refines RandomAccessCollection, so getting `count` must be O(1). This avoids
        // intermediate allocations in the awkward case by ensuring we definitely have sufficient
        // space for these writes.
        self.reserveCapacity(minimumWritableBytes: data.count)

        var written = 0
        for region in data.regions {
            written += self.setContiguousBytes(region, at: index + written)
        }
        return written
    }

    // MARK: - UUID

    /// Get a `UUID` from the 16 bytes starting at `index`. This will not change the reader index.
    /// If there are less than 16 bytes starting at `index` then `nil` will be returned.
    ///
    /// - Parameters:
    ///   - index: The starting index of the bytes of interest into the `ByteBuffer`.
    /// - Returns: A `UUID` value containing the bytes of interest or `nil` if the selected bytes
    ///            are not readable or there were not enough bytes.
    public func getUUID(at index: Int) -> UUID? {
        guard let chunk1 = self.getInteger(at: index, as: UInt64.self),
              let chunk2 = self.getInteger(at: index + 8, as: UInt64.self) else {
            return nil
        }

        let uuidBytes = (
            UInt8(truncatingIfNeeded: chunk1 >> 56),
            UInt8(truncatingIfNeeded: chunk1 >> 48),
            UInt8(truncatingIfNeeded: chunk1 >> 40),
            UInt8(truncatingIfNeeded: chunk1 >> 32),
            UInt8(truncatingIfNeeded: chunk1 >> 24),
            UInt8(truncatingIfNeeded: chunk1 >> 16),
            UInt8(truncatingIfNeeded: chunk1 >> 8),
            UInt8(truncatingIfNeeded: chunk1),
            UInt8(truncatingIfNeeded: chunk2 >> 56),
            UInt8(truncatingIfNeeded: chunk2 >> 48),
            UInt8(truncatingIfNeeded: chunk2 >> 40),
            UInt8(truncatingIfNeeded: chunk2 >> 32),
            UInt8(truncatingIfNeeded: chunk2 >> 24),
            UInt8(truncatingIfNeeded: chunk2 >> 16),
            UInt8(truncatingIfNeeded: chunk2 >> 8),
            UInt8(truncatingIfNeeded: chunk2)
        )

        return UUID(uuid: uuidBytes)

Review comment (suggestion):
> @glbrntt "maybe should be xxxUUIDBytes and xxxUUIDString"

I think I'd be in favor of this, as uuids are also often send in string representation over the wire.

Code after:
    /// - returns: The number of bytes written.
    @inlinable
    @discardableResult
    public mutating func setData<D: DataProtocol>(_ data: D, at index: Int) -> Int {
        // DataProtocol refines RandomAccessCollection, so getting `count` must be O(1). This avoids
        // intermediate allocations in the awkward case by ensuring we definitely have sufficient
        // space for these writes.
        self.reserveCapacity(minimumWritableBytes: data.count)

        var written = 0
        for region in data.regions {
            written += self.setContiguousBytes(region, at: index + written)
        }
        return written
    }

    // MARK: - UUID

    /// Get a `UUID` from the 16 bytes starting at `index`. This will not change the reader index.
    /// If there are less than 16 bytes starting at `index` then `nil` will be returned.
    ///
    /// - Parameters:
    ///   - index: The starting index of the bytes of interest into the `ByteBuffer`.
    /// - Returns: A `UUID` value containing the bytes of interest or `nil` if the selected bytes
    ///            are not readable or there were not enough bytes.
    public func getUUIDBytes(at index: Int) -> UUID? {
        guard let chunk1 = self.getInteger(at: index, as: UInt64.self),
              let chunk2 = self.getInteger(at: index + 8, as: UInt64.self) else {
            return nil
        }

        let uuidBytes = (
            UInt8(truncatingIfNeeded: chunk1 >> 56),
            UInt8(truncatingIfNeeded: chunk1 >> 48),
            UInt8(truncatingIfNeeded: chunk1 >> 40),
            UInt8(truncatingIfNeeded: chunk1 >> 32),
            UInt8(truncatingIfNeeded: chunk1 >> 24),
            UInt8(truncatingIfNeeded: chunk1 >> 16),
            UInt8(truncatingIfNeeded: chunk1 >> 8),
            UInt8(truncatingIfNeeded: chunk1),
            UInt8(truncatingIfNeeded: chunk2 >> 56),
            UInt8(truncatingIfNeeded: chunk2 >> 48),
            UInt8(truncatingIfNeeded: chunk2 >> 40),
            UInt8(truncatingIfNeeded: chunk2 >> 32),
            UInt8(truncatingIfNeeded: chunk2 >> 24),
            UInt8(truncatingIfNeeded: chunk2 >> 16),
            UInt8(truncatingIfNeeded: chunk2 >> 8),
            UInt8(truncatingIfNeeded: chunk2)
        )

        return UUID(uuid: uuidBytes)