Skip to main content

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::marker::PhantomData;
7use core::mem::MaybeUninit;
8use cxx::{type_id, ExternType};
9use std::fmt;
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///
68/// Introduced in Qt 6.0.
69///
70/// Qt Documentation: [QAnyStringView](https://doc.qt.io/qt/qanystringview.html#details)
71#[repr(C)]
72pub struct QAnyStringView<'a> {
73    /// `QAnyStringView` has two members, a pointer and a `size_t`
74    _data: MaybeUninit<usize>,
75    _size: MaybeUninit<isize>,
76
77    /// Needed to keep the lifetime in check
78    _phantom: PhantomData<&'a usize>,
79}
80
81impl<'a> Clone for QAnyStringView<'a> {
82    /// Constructs a copy of `self`.
83    ///
84    /// This operation takes constant time, because `QAnyStringView` is a view-only string.
85    fn clone(&self) -> QAnyStringView<'a> {
86        ffi::QAnyStringView_init_from_QAnyStringView(self)
87    }
88}
89
90impl Default for QAnyStringView<'_> {
91    /// Constructs a null string view.
92    fn default() -> Self {
93        ffi::QAnyStringView_init_default()
94    }
95}
96
97impl PartialEq for QAnyStringView<'_> {
98    fn eq(&self, other: &Self) -> bool {
99        ffi::QAnyStringView_eq(self, other)
100    }
101}
102
103impl Eq for QAnyStringView<'_> {}
104
105impl<'a> From<&'a str> for QAnyStringView<'a> {
106    /// Constructs a `QAnyStringView` from a Rust string.
107    fn from(str: &'a str) -> Self {
108        ffi::QAnyStringView_init_from_rust_string(str)
109    }
110}
111
112impl<'a> From<&'a QByteArray> for QAnyStringView<'a> {
113    /// Constructs a `QAnyStringView` from a `QByteArray`.
114    fn from(bytes: &'a QByteArray) -> Self {
115        ffi::QAnyStringView_init_from_qbytearray(bytes)
116    }
117}
118
119impl<'a> From<&'a QString> for QAnyStringView<'a> {
120    /// Constructs a `QAnyStringView` from a `QString`.
121    fn from(string: &'a QString) -> Self {
122        ffi::QAnyStringView_init_from_qstring(string)
123    }
124}
125
126impl QAnyStringView<'_> {
127    /// Returns the size of this string view, in the encoding's code points.
128    pub fn len(&self) -> isize {
129        ffi::QAnyStringView_len(self)
130    }
131}
132
133impl fmt::Display for QAnyStringView<'_> {
134    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
135        self.to_qstring().fmt(f)
136    }
137}
138
139impl fmt::Debug for QAnyStringView<'_> {
140    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
141        self.to_qstring().fmt(f)
142    }
143}
144
145// Safety:
146//
147// Static checks on the C++ side to ensure the size is the same.
148unsafe impl ExternType for QAnyStringView<'_> {
149    type Id = type_id!("QAnyStringView");
150    type Kind = cxx::kind::Trivial;
151}