Files
scylladb/utils/bit_cast.hh
Avi Kivity 0ae22a09d4 LICENSE: Update to version 1.1
Updated terms of non-commercial use (must be a never-customer).
2026-04-12 19:46:33 +03:00

39 lines
929 B
C++

/*
* Copyright (C) 2021-present ScyllaDB
*/
/*
* SPDX-License-Identifier: LicenseRef-ScyllaDB-Source-Available-1.1
*/
#pragma once
#include <cstring>
#include <type_traits>
#include <span>
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));
}
template <TriviallyCopyable From>
std::byte* write_unaligned(std::byte* dst, const From& src) {
std::memcpy(dst, &src, sizeof(From));
return dst + sizeof(From);
}
std::span<const std::byte> object_representation(const TriviallyCopyable auto& x) {
return {reinterpret_cast<const std::byte*>(&x), sizeof(x)};
}