libQuotient
A Qt library for building matrix clients
Loading...
Searching...
No Matches
rust_util.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: The Quotient Project Contributors
2// SPDX-License-Identifier: LGPL-3.0-or-later
3
4#pragma once
5
6#include "lib.rs.h"
7#include "util.h"
8
9#include <QtCore/QJsonObject>
10
11namespace Quotient {
12
13inline QUOTIENT_API QByteArray bytesFromRust(const auto &bytes)
14{
15 return {std::bit_cast<const char *>(bytes.data()), std::ssize(bytes)};
16}
17
19{
21}
22
24
25//! \brief Loads a C++ object from its JSON representation stored in a Rust string
26//! \note JsonConverter or JsonObjectConverter specialisation for \p PodT has to be defined prior to
27//! calling this function.
28template <typename PodT>
29inline QUOTIENT_API auto fromRustJson(const rust::String& s)
30{
31 return fromJson<PodT>(jsonFromRust(s));
32}
33
35{
37}
38
40{
41 return bytesToRust(string.toUtf8());
42}
43
45
46template <typename OverrideSliceValueT = void, typename R>
47inline QUOTIENT_API auto toRustSlice(const R &r)
49{
50 // The = void hack is needed because we cannot take R to the first place without losing ability
51 // to override SliceValueT at invocation point, and we cannot use std::ranges::range_value_t<R>
52 // as default SliceValueT before R is defined
55 static_assert(sizeof(SliceValueT) == sizeof(std::ranges::range_value_t<R>)
56 && sizeof(unsignedSize(r))
59}
60
61} // namespace Quotient
QUOTIENT_API auto toRustSlice(const R &r)
Definition rust_util.h:47
QUOTIENT_API auto fromRustJson(const rust::String &s)
Loads a C++ object from its JSON representation stored in a Rust string.
Definition rust_util.h:29
#define QUOTIENT_API