From 226a1fc4827eaad96e244fe369704a83b7ef2308 Mon Sep 17 00:00:00 2001 From: PJB3005 Date: Sat, 25 Jul 2026 15:18:37 +0200 Subject: [PATCH] read_unaligned helper "Things C++ should've had built-in 2 decades ago" --- include/helpers/alignment.hpp | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 include/helpers/alignment.hpp diff --git a/include/helpers/alignment.hpp b/include/helpers/alignment.hpp new file mode 100644 index 0000000000..7ce0ead69a --- /dev/null +++ b/include/helpers/alignment.hpp @@ -0,0 +1,18 @@ +#pragma once + +namespace dusk::helpers::alignment { + +/** + * Read data from an address that may not be aligned properly. + * @tparam T Type of data to read. + * @param ptr Address to read from. + * @return The copied value. + */ +template requires std::is_trivially_copyable_v +[[nodiscard]] constexpr T read_unaligned(u8 const* ptr) { + T copy; + memcpy(©, ptr, sizeof(T)); + return copy; +} + +}