Repository: skyline-emu/skyline
File: app/src/main/cpp/skyline/common/uuid.cpp (C++)

Code before:
                struct {
                    std::array<u8, 6> nodeArray;
                };
            };
        };

        UUID Swap() {
            UuidLayout swappedLayout{*this};
            swappedLayout.timeLow = util::SwapEndianness(timeLow);
            swappedLayout.timeMid = util::SwapEndianness(timeMid);
            swappedLayout.timeHighAndVersion = util::SwapEndianness(timeHighAndVersion);
            swappedLayout.nodeArray = util::SwapEndianness(nodeArray);

            UUID out;
            std::memcpy(&out, &swappedLayout, sizeof(UUID));
            return out;
        }
    };
    static_assert(sizeof(UuidLayout) == 0x10);

    UUID UUID::GenerateUuidV4() {
        constexpr u8 reserved{0x1}; // RFC4122 variant
        constexpr u8 version{0x4}; // v4 UUIDs are generated entirely from random numbers

        std::random_device rd;
        std::mt19937_64 gen(rd());

        std::uniform_int_distribution<u64> dist;

        // Create an initial random UUID
        UuidLayout uuid{
            .low = dist(gen),
            .high = dist(gen),
        };

        // Set format bits
        uuid.reserved = reserved;
        uuid.version = version;

        return uuid.Swap();
    }

    UUID UUID::GenerateUuidV5(span<u8, 20> sha1) {
        constexpr u8 reserved{0x1}; // RFC4122 variant
        constexpr u8 version{0x5}; // v4 UUIDs are generated using SHA1 hashes

        UuidLayout uuid;
        std::memcpy(&uuid, sha1.data(), sizeof(UuidLayout));

        // Set format bits
        uuid.reserved = reserved;

Review comment (suggestion):
This is going to construct a `span` of `sizeof(UuidLayout)` `u64`s which would be way larger than you want. You'd need to do `span(&uuid, 1).cast<u64>()` as of now but if you do some template stuff for `FillRandomBytes` to make it automatically cast to the largest type based off `sizeof(T)` then you could do `span(&uuid, 1)`.

Code after:
                    std::array<u8, 6> nodeArray;
                };
            };
        };

        UUID Swap() {
            UuidLayout swappedLayout{*this};
            swappedLayout.timeLow = util::SwapEndianness(timeLow);
            swappedLayout.timeMid = util::SwapEndianness(timeMid);
            swappedLayout.timeHighAndVersion = util::SwapEndianness(timeHighAndVersion);
            swappedLayout.nodeArray = util::SwapEndianness(nodeArray);

            UUID out;
            std::memcpy(&out, &swappedLayout, sizeof(UUID));
            return out;
        }
    };
    static_assert(sizeof(UuidLayout) == 0x10);

    UUID UUID::GenerateUuidV4() {
        constexpr u8 reserved{0x1}; // RFC4122 variant
        constexpr u8 version{0x4}; // v4 UUIDs are generated entirely from random numbers

        // Create an initial random UUID
        UuidLayout uuid;
        util::FillRandomBytes(uuid);

        // Set format bits
        uuid.reserved = reserved;
        uuid.version = version;

        return uuid.Swap();
    }

    UUID UUID::GenerateUuidV5(span<u8, 20> sha1) {
        constexpr u8 reserved{0x1}; // RFC4122 variant
        constexpr u8 version{0x5}; // v4 UUIDs are generated using SHA1 hashes

        UuidLayout uuid;
        std::memcpy(&uuid, sha1.data(), sizeof(UuidLayout));

        // Set format bits
        uuid.reserved = reserved;
        uuid.version = version;

        return uuid.Swap();
    }
}