cxx_qt_lib/core/
qanystringview.rs

1// SPDX-FileCopyrightText: 2024 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
2// SPDX-FileContributor: Joshua Goins <josh@redstrate.com>
3//
4// SPDX-License-Identifier: MIT OR Apache-2.0
5use crate::{QByteArray, QString};
6use core::ffi::c_void;
7use core::marker::PhantomData;
8use core::mem::MaybeUninit;
9use cxx::{type_id, ExternType};
10
11#[cxx::bridge]
12mod ffi {
13    unsafe extern "C++" {
14        include!("cxx-qt-lib/qanystringview.h");
15        type QAnyStringView<'a> = super::QAnyStringView<'a>;
16
17        include!("cxx-qt-lib/qbytearray.h");
18        type QByteArray = crate::QByteArray;
19
20        include!("cxx-qt-lib/qstring.h");
21        type QString = crate::QString;
22
23        /// Returns true if the string has no characters; otherwise returns false.
24        #[rust_name = "is_empty"]
25        fn isEmpty(self: &QAnyStringView) -> bool;
26
27        /// Returns true if this string is null; otherwise returns false.
28        #[rust_name = "is_null"]
29        fn isNull(self: &QAnyStringView) -> bool;
30
31        /// Returns a deep copy of this string view's data as a QString.
32        #[rust_name = "to_qstring"]
33        fn toString(self: &QAnyStringView) -> QString;
34    }
35
36    #[namespace = "rust::cxxqtlib1"]
37    unsafe extern "C++" {
38        include!("cxx-qt-lib/common.h");
39
40        #[doc(hidden)]
41        #[rust_name = "QAnyStringView_init_default"]
42        fn construct() -> QAnyStringView<'static>;
43        #[doc(hidden)]
44        #[rust_name = "QAnyStringView_init_from_rust_string"]
45        fn qanystringviewInitFromRustString<'a>(string: &'a str) -> QAnyStringView<'a>;
46        #[doc(hidden)]
47        #[rust_name = "QAnyStringView_init_from_qbytearray"]
48        fn construct<'a>(bytes: &'a QByteArray) -> QAnyStringView<'a>;
49        #[doc(hidden)]
50        #[rust_name = "QAnyStringView_init_from_qstring"]
51        fn construct<'a>(string: &'a QString) -> QAnyStringView<'a>;
52        #[doc(hidden)]
53        #[rust_name = "QAnyStringView_init_from_QAnyStringView"]
54        fn construct<'a>(string: &QAnyStringView<'a>) -> QAnyStringView<'a>;
55
56        #[doc(hidden)]
57        #[rust_name = "QAnyStringView_eq"]
58        fn operatorEq(a: &QAnyStringView, b: &QAnyStringView) -> bool;
59
60        #[doc(hidden)]
61        #[rust_name = "QAnyStringView_len"]
62        fn qanystringviewLen(string: &QAnyStringView) -> isize;
63    }
64}
65
66/// The QAnyStringView class provides a unified view of a Latin-1, UTF-8, or UTF-16 string.
67#[repr(C)]
68pub struct QAnyStringView<'a> {
69    /// QAnyStringView has two members, a pointer and a size_t
70    _space: MaybeUninit<[usize; 1]>,
71    _space2: MaybeUninit<[c_void; 1]>,
72
73    /// Needed to keep the lifetime in check
74    _phantom: PhantomData<&'a usize>,
75}
76
77impl<'a> Clone for QAnyStringView<'a> {
78    /// Constructs a copy of other.
79    ///
80    /// This operation takes constant time, because QAnyStringView is a view-only string.
81    fn clone(&self) -> QAnyStringView<'a> {
82        ffi::QAnyStringView_init_from_QAnyStringView(self)
83    }
84}
85
86impl Default for QAnyStringView<'_> {
87    /// Constructs a null string. Null strings are also empty.
88    fn default() -> Self {
89        ffi::QAnyStringView_init_default()
90    }
91}
92
93impl PartialEq for QAnyStringView<'_> {
94    fn eq(&self, other: &Self) -> bool {
95        ffi::QAnyStringView_eq(self, other)
96    }
97}
98
99impl Eq for QAnyStringView<'_> {}
100
101impl<'a> From<&'a str> for QAnyStringView<'a> {
102    /// Constructs a QAnyStringView from a Rust string
103    fn from(str: &'a str) -> Self {
104        ffi::QAnyStringView_init_from_rust_string(str)
105    }
106}
107
108impl<'a> From<&'a QByteArray> for QAnyStringView<'a> {
109    /// Constructs a QAnyStringView from a QByteArray
110    fn from(bytes: &'a QByteArray) -> Self {
111        ffi::QAnyStringView_init_from_qbytearray(bytes)
112    }
113}
114
115impl<'a> From<&'a QString> for QAnyStringView<'a> {
116    /// Constructs a QAnyStringView from a QString
117    fn from(string: &'a QString) -> Self {
118        ffi::QAnyStringView_init_from_qstring(string)
119    }
120}
121
122impl QAnyStringView<'_> {
123    /// Returns the number of characters in this string.
124    pub fn len(&self) -> isize {
125        ffi::QAnyStringView_len(self)
126    }
127}
128
129// Safety:
130//
131// Static checks on the C++ side to ensure the size is the same.
132unsafe impl ExternType for QAnyStringView<'_> {
133    type Id = type_id!("QAnyStringView");
134    type Kind = cxx::kind::Trivial;
135}