Like std::bit_cast (https://en.cppreference.com/w/cpp/numeric/bit_cast) we only require To to be trivially copyable. There's no need for it to be a trivial type. Signed-off-by: Benny Halevy <bhalevy@scylladb.com>
30 lines
601 B
C++
30 lines
601 B
C++
/*
|
|
* Copyright (C) 2021-present ScyllaDB
|
|
*/
|
|
|
|
/*
|
|
* SPDX-License-Identifier: AGPL-3.0-or-later
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
#include <bit>
|
|
#include <cstring>
|
|
#include <cstddef>
|
|
#include <type_traits>
|
|
|
|
template <class T> concept Trivial = std::is_trivial_v<T>;
|
|
template <class T> concept TriviallyCopyable = std::is_trivially_copyable_v<T>;
|
|
|
|
template <TriviallyCopyable To>
|
|
To read_unaligned(const void* src) {
|
|
To dst;
|
|
std::memcpy(&dst, src, sizeof(To));
|
|
return dst;
|
|
}
|
|
|
|
template <TriviallyCopyable From>
|
|
void write_unaligned(void* dst, const From& src) {
|
|
std::memcpy(dst, &src, sizeof(From));
|
|
}
|