diff --git a/Libraries/LibWasm/Wasi.h b/Libraries/LibWasm/Wasi.h index fa9cd91361c..3bd219e1c0c 100644 --- a/Libraries/LibWasm/Wasi.h +++ b/Libraries/LibWasm/Wasi.h @@ -738,30 +738,31 @@ static_assert(sizeof(SockRecvResult) == 8); template struct Result { Result(TResult&& result) - : bits {} + : m_result(result) , tag(0) { - new (&bits) TResult(move(result)); } Result(Errno&& error) - : bits {} + : m_error(error) , tag(1) { - new (&bits) Errno(error); } - Optional result() const + static_assert(IsTriviallyDestructible); + static_assert(IsTriviallyDestructible); + + Optional result() const { if (tag == 0) - return *bit_cast(&bits[0]); + return m_result; return {}; } - Optional error() const + Optional error() const { if (tag == 1) - return *bit_cast(&bits[0]); + return m_error; return {}; } @@ -778,23 +779,25 @@ struct Result { } private: - alignas(max(alignof(TResult), alignof(Errno))) u8 bits[max(sizeof(TResult), sizeof(Errno))]; + union { + TResult m_result; + Errno m_error; + }; LittleEndian tag; }; template struct Result { Result() - : error_bits {} + : m_error() , tag(0) { } Result(Errno&& error) - : error_bits {} + : m_error(error) , tag(1) { - new (&error_bits) Errno(error); } Optional result() const @@ -803,16 +806,16 @@ struct Result { return { Empty {} }; return {}; } - Optional error() const + Optional error() const { if (tag == 1) - return *bit_cast(&error_bits[0]); + return m_error; return {}; } bool is_error() const { return tag == 1; } private: - alignas(Errno) u8 error_bits[sizeof(Errno)]; + Errno m_error; LittleEndian tag; };