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
11
namespace
Quotient
{
12
13
inline
QUOTIENT_API
QByteArray
bytesFromRust
(
const
auto
&
bytes
)
14
{
15
return
{
std
::
bit_cast
<
const
char
*>(
bytes
.
data
()),
std
::
ssize
(
bytes
)};
16
}
17
18
inline
QUOTIENT_API
QString
stringFromRust
(
const
rust
::
String
&
string
)
19
{
20
return
QString
::
fromUtf8
(
bytesFromRust
(
string
));
21
}
22
23
QUOTIENT_API
QJsonObject
jsonFromRust
(
const
rust
::
String
&
string
);
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.
28
template
<
typename
PodT>
29
inline
QUOTIENT_API
auto
fromRustJson
(
const
rust::String& s)
30
{
31
return
fromJson<PodT>(jsonFromRust(s));
32
}
33
34
inline
QUOTIENT_API
rust
::
String
bytesToRust
(
const
QByteArray
&
bytes
)
35
{
36
return
rust
::
String
(
bytes
.
data
(),
unsignedSize
(
bytes
));
37
}
38
39
inline
QUOTIENT_API
rust
::
String
stringToRust
(
const
QString
&
string
)
40
{
41
return
bytesToRust
(
string
.
toUtf8
());
42
}
43
44
QUOTIENT_API
rust
::
Vec
<
rust
::
String
>
stringsToRust
(
const
QStringList
&
strings
);
45
46
template
<
typename
OverrideSliceValueT =
void
,
typename
R>
47
inline
QUOTIENT_API
auto
toRustSlice
(
const
R &r)
48
requires
std
::
ranges
::
contiguous_range
<
const
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
53
using
SliceValueT
=
std
::
conditional_t
<
std
::
is_void_v
<
OverrideSliceValueT
>,
54
std
::
ranges
::
range_value_t
<
R
>,
OverrideSliceValueT
>;
55
static_assert
(
sizeof
(
SliceValueT
) ==
sizeof
(
std
::
ranges
::
range_value_t
<
R
>)
56
&&
sizeof
(
unsignedSize
(
r
))
57
<=
sizeof
(
std
::
ranges
::
range_size_t
<
rust
::
Slice
<
SliceValueT
>>));
58
return
rust
::
Slice
<
SliceValueT
>(
std
::
bit_cast
<
SliceValueT
*>(
std
::
data
(
r
)),
unsignedSize
(
r
));
59
}
60
61
}
// namespace Quotient
Quotient
Definition
accountregistry.h:13
Quotient::toRustSlice
QUOTIENT_API auto toRustSlice(const R &r)
Definition
rust_util.h:47
Quotient::fromRustJson
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
QUOTIENT_API
#define QUOTIENT_API
Definition
quotient_export.h:22
Quotient
rust_util.h
Generated by
1.9.8