1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// SPDX-FileCopyrightText: 2021 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
// SPDX-FileContributor: Andrew Hayzen <andrew.hayzen@kdab.com>
// SPDX-FileContributor: Gerhard de Clercq <gerhard.declercq@kdab.com>
//
// SPDX-License-Identifier: MIT OR Apache-2.0

#[cxx::bridge]
mod ffi {
    unsafe extern "C++" {
        include!("cxx-qt-lib/include/qt_types.h");

        type QString;

        #[namespace = "rust::cxxqtlib1"]
        #[rust_name = "qstring_init_from_rust_string"]
        fn qstringInitFromRustString(string: &str) -> UniquePtr<QString>;
        #[namespace = "rust::cxxqtlib1"]
        #[rust_name = "qstring_to_rust_string"]
        fn qstringToRustString(string: &QString) -> String;
    }

    impl UniquePtr<QString> {}
}

/// The QStringCpp class provides a Unicode character string.
///
/// Note that this is the C++ representation and String or &str should be used in Rust.
pub type QStringCpp = ffi::QString;

impl QStringCpp {
    /// Create a new Rust String from this QStringCpp. This operation
    /// needs to convert the UTF-16 data in the QString to UTF-8
    /// data and thus likely needs to an allocate. This is essentially
    /// a copy and so any changes will not propagate to the QStringCpp.
    pub fn to_rust(&self) -> String {
        ffi::qstring_to_rust_string(self)
    }
}

impl crate::ToUniquePtr for &String {
    type CppType = QStringCpp;

    /// Retrieve the UniquePtr to the Qt QStringCpp of this Rust String
    /// so that this object can be passed back to C++.
    fn to_unique_ptr(self) -> cxx::UniquePtr<QStringCpp> {
        ffi::qstring_init_from_rust_string(self.as_ref())
    }
}

impl crate::ToUniquePtr for &str {
    type CppType = QStringCpp;

    /// Retrieve the UniquePtr to the Qt QStringCpp of this Rust &str
    /// so that this object can be passed back to C++.
    fn to_unique_ptr(self) -> cxx::UniquePtr<QStringCpp> {
        ffi::qstring_init_from_rust_string(self)
    }
}

impl From<&QStringCpp> for String {
    fn from(qstring: &QStringCpp) -> Self {
        qstring.to_rust()
    }
}