Skip to content

Commit 82eec23

Browse files
committed
cxx-qt-lib: use get_or_default for associated types QMap or QHash
Also rename value to get so that we match Rust API.
1 parent 33868ba commit 82eec23

9 files changed

Lines changed: 86 additions & 62 deletions

File tree

crates/cxx-qt-lib-headers/include/core/qhash.h

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,15 @@ template<typename K, typename V>
3030
::rust::isize
3131
qhashLen(const QHash<K, V>& h) noexcept;
3232

33+
template<typename K, typename V>
34+
V
35+
qhashGetOrDefault(const QHash<K, V>& h, const K& key) noexcept
36+
{
37+
// Qt 6 returns a T and Qt 5 returns an const T
38+
// so we need to define our own method here for CXX
39+
return h.value(key);
40+
}
41+
3342
template<typename K, typename V>
3443
const K&
3544
qhashGetUncheckedKey(const QHash<K, V>& h, ::rust::isize pos) noexcept
@@ -79,15 +88,6 @@ qhashRemove(QHash<K, V>& h, const K& key) noexcept
7988
#endif
8089
}
8190

82-
template<typename K, typename V>
83-
V
84-
qhashValue(const QHash<K, V>& h, const K& key) noexcept
85-
{
86-
// Qt 6 returns a T and Qt 5 returns an const T
87-
// so we need to define our own method here for CXX
88-
return h.value(key);
89-
}
90-
9191
}
9292
}
9393
}

crates/cxx-qt-lib-headers/include/core/qmap.h

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,15 @@ template<typename K, typename V>
2727
::rust::isize
2828
qmapLen(const QMap<K, V>& m) noexcept;
2929

30+
template<typename K, typename V>
31+
V
32+
qmapGetOrDefault(const QMap<K, V>& m, const K& key) noexcept
33+
{
34+
// Qt 6 returns a T and Qt 5 returns an const T
35+
// so we need to define our own method here for CXX
36+
return m.value(key);
37+
}
38+
3039
template<typename K, typename V>
3140
const K&
3241
qmapGetUncheckedKey(const QMap<K, V>& m, ::rust::isize pos) noexcept
@@ -71,15 +80,6 @@ qmapRemove(QMap<K, V>& m, const K& key) noexcept
7180
return m.remove(key) >= 1;
7281
}
7382

74-
template<typename K, typename V>
75-
V
76-
qmapValue(const QMap<K, V>& m, const K& key) noexcept
77-
{
78-
// Qt 6 returns a T and Qt 5 returns an const T
79-
// so we need to define our own method here for CXX
80-
return m.value(key);
81-
}
82-
8383
}
8484
}
8585
}

crates/cxx-qt-lib/src/core/qhash/mod.rs

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ where
6262
T::Value: PartialEq,
6363
{
6464
fn eq(&self, other: &Self) -> bool {
65-
self.len() == other.len() && self.iter().all(|(k, v)| &T::value(other, k) == v)
65+
self.len() == other.len() && self.iter().all(|(k, v)| other.get(k).as_ref() == Some(v))
6666
}
6767
}
6868

@@ -87,6 +87,20 @@ where
8787
T::contains(self, key)
8888
}
8989

90+
/// Returns the value associated with the key if it exists.
91+
pub fn get(&self, key: &T::Key) -> Option<T::Value> {
92+
if self.contains(key) {
93+
Some(T::get_or_default(self, key))
94+
} else {
95+
None
96+
}
97+
}
98+
99+
/// Returns the value associated with the key or a default value.
100+
pub fn get_or_default(&self, key: &T::Key) -> T::Value {
101+
T::get_or_default(self, key)
102+
}
103+
90104
/// Inserts a new item with the key and a value of value.
91105
///
92106
/// The key and value is a reference here so it can be opaque or trivial but
@@ -120,11 +134,6 @@ where
120134
pub fn remove(&mut self, key: &T::Key) -> bool {
121135
T::remove(self, key)
122136
}
123-
124-
/// Returns the value associated with the key.
125-
pub fn value(&self, key: &T::Key) -> T::Value {
126-
T::value(self, key)
127-
}
128137
}
129138

130139
impl<T> QHash<T>
@@ -202,6 +211,7 @@ pub trait QHashPair: Sized {
202211
fn contains(hash: &QHash<Self>, key: &Self::Key) -> bool;
203212
fn default() -> QHash<Self>;
204213
fn drop(hash: &mut QHash<Self>);
214+
fn get_or_default(hash: &QHash<Self>, key: &Self::Key) -> Self::Value;
205215
/// # Safety
206216
///
207217
/// Calling this method with an out-of-bounds index is undefined behavior
@@ -219,7 +229,6 @@ pub trait QHashPair: Sized {
219229
fn insert_clone(hash: &mut QHash<Self>, key: &Self::Key, value: &Self::Value);
220230
fn len(hash: &QHash<Self>) -> isize;
221231
fn remove(hash: &mut QHash<Self>, key: &Self::Key) -> bool;
222-
fn value(hash: &QHash<Self>, key: &Self::Key) -> Self::Value;
223232
}
224233

225234
macro_rules! impl_qhash_pair {
@@ -249,6 +258,10 @@ macro_rules! impl_qhash_pair {
249258
$module::drop(hash);
250259
}
251260

261+
fn get_or_default(hash: &QHash<Self>, key: &$keyTypeName) -> $valueTypeName {
262+
$module::get_or_default(hash, key)
263+
}
264+
252265
unsafe fn get_unchecked_key(hash: &QHash<Self>, pos: isize) -> &$keyTypeName {
253266
$module::get_unchecked_key(hash, pos)
254267
}
@@ -272,10 +285,6 @@ macro_rules! impl_qhash_pair {
272285
fn remove(hash: &mut QHash<Self>, key: &$keyTypeName) -> bool {
273286
$module::remove(hash, key)
274287
}
275-
276-
fn value(hash: &QHash<Self>, key: &$keyTypeName) -> $valueTypeName {
277-
$module::value(hash, key)
278-
}
279288
}
280289
};
281290
}

crates/cxx-qt-lib/src/core/qhash/qhash_i32_qbytearray.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ pub mod ffi {
3636

3737
#[namespace = "rust::cxxqtlib1::qhash"]
3838
unsafe extern "C++" {
39+
#[rust_name = "get_or_default_i32_QByteArray"]
40+
fn qhashGetOrDefault(_: &QHash_i32_QByteArray, key: &i32) -> QByteArray;
3941
#[rust_name = "get_unchecked_key_i32_QByteArray"]
4042
#[allow(clippy::needless_lifetimes)]
4143
unsafe fn qhashGetUncheckedKey<'a>(_: &'a QHash_i32_QByteArray, pos: isize) -> &'a i32;
@@ -47,8 +49,6 @@ pub mod ffi {
4749
fn qhashLen(_: &QHash_i32_QByteArray) -> isize;
4850
#[rust_name = "remove_i32_QByteArray"]
4951
fn qhashRemove(_: &mut QHash_i32_QByteArray, key: &i32) -> bool;
50-
#[rust_name = "value_i32_QByteArray"]
51-
fn qhashValue(_: &QHash_i32_QByteArray, key: &i32) -> QByteArray;
5252
}
5353
}
5454

@@ -64,6 +64,10 @@ pub(crate) fn drop(hash: &mut ffi::QHash_i32_QByteArray) {
6464
ffi::qhash_drop_i32_QByteArray(hash);
6565
}
6666

67+
pub(crate) fn get_or_default(hash: &ffi::QHash_i32_QByteArray, key: &i32) -> ffi::QByteArray {
68+
ffi::get_or_default_i32_QByteArray(hash, key)
69+
}
70+
6771
pub(crate) unsafe fn get_unchecked_key(hash: &ffi::QHash_i32_QByteArray, pos: isize) -> &i32 {
6872
ffi::get_unchecked_key_i32_QByteArray(hash, pos)
6973
}
@@ -87,10 +91,6 @@ pub(crate) fn remove(hash: &mut ffi::QHash_i32_QByteArray, key: &i32) -> bool {
8791
ffi::remove_i32_QByteArray(hash, key)
8892
}
8993

90-
pub(crate) fn value(hash: &ffi::QHash_i32_QByteArray, key: &i32) -> ffi::QByteArray {
91-
ffi::value_i32_QByteArray(hash, key)
92-
}
93-
9494
#[allow(non_camel_case_types)]
9595
pub struct QHashPair_i32_QByteArray;
9696

crates/cxx-qt-lib/src/core/qhash/qhash_qstring_qvariant.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ pub mod ffi {
3838

3939
#[namespace = "rust::cxxqtlib1::qhash"]
4040
unsafe extern "C++" {
41+
#[rust_name = "get_or_default_QString_QVariant"]
42+
fn qhashGetOrDefault(_: &QHash_QString_QVariant, key: &QString) -> QVariant;
4143
#[rust_name = "get_unchecked_key_QString_QVariant"]
4244
unsafe fn qhashGetUncheckedKey(_: &QHash_QString_QVariant, pos: isize) -> &QString;
4345
#[rust_name = "get_unchecked_value_QString_QVariant"]
@@ -48,8 +50,6 @@ pub mod ffi {
4850
fn qhashLen(_: &QHash_QString_QVariant) -> isize;
4951
#[rust_name = "remove_QString_QVariant"]
5052
fn qhashRemove(_: &mut QHash_QString_QVariant, key: &QString) -> bool;
51-
#[rust_name = "value_QString_QVariant"]
52-
fn qhashValue(_: &QHash_QString_QVariant, key: &QString) -> QVariant;
5353
}
5454
}
5555

@@ -65,6 +65,13 @@ pub(crate) fn drop(hash: &mut ffi::QHash_QString_QVariant) {
6565
ffi::qhash_drop_QString_QVariant(hash);
6666
}
6767

68+
pub(crate) fn get_or_default(
69+
hash: &ffi::QHash_QString_QVariant,
70+
key: &ffi::QString,
71+
) -> ffi::QVariant {
72+
ffi::get_or_default_QString_QVariant(hash, key)
73+
}
74+
6875
pub(crate) unsafe fn get_unchecked_key(
6976
hash: &ffi::QHash_QString_QVariant,
7077
pos: isize,
@@ -95,10 +102,6 @@ pub(crate) fn remove(hash: &mut ffi::QHash_QString_QVariant, key: &ffi::QString)
95102
ffi::remove_QString_QVariant(hash, key)
96103
}
97104

98-
pub(crate) fn value(hash: &ffi::QHash_QString_QVariant, key: &ffi::QString) -> ffi::QVariant {
99-
ffi::value_QString_QVariant(hash, key)
100-
}
101-
102105
#[allow(non_camel_case_types)]
103106
pub struct QHashPair_QString_QVariant;
104107

crates/cxx-qt-lib/src/core/qmap/mod.rs

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ where
5757
{
5858
/// Returns true if both maps contain the same key value pairs
5959
fn eq(&self, other: &Self) -> bool {
60-
self.len() == other.len() && self.iter().all(|(k, v)| &T::value(other, k) == v)
60+
self.len() == other.len() && self.iter().all(|(k, v)| other.get(k).as_ref() == Some(v))
6161
}
6262
}
6363

@@ -82,6 +82,20 @@ where
8282
T::contains(self, key)
8383
}
8484

85+
/// Returns the value associated with the key if it exists.
86+
pub fn get(&self, key: &T::Key) -> Option<T::Value> {
87+
if self.contains(key) {
88+
Some(T::get_or_default(self, key))
89+
} else {
90+
None
91+
}
92+
}
93+
94+
/// Returns the value associated with the key or a default value.
95+
pub fn get_or_default(&self, key: &T::Key) -> T::Value {
96+
T::get_or_default(self, key)
97+
}
98+
8599
/// Inserts a new item with the key and a value of value.
86100
///
87101
/// The key and value are references here so they can be opaque or trivial.
@@ -113,11 +127,6 @@ where
113127
pub fn remove(&mut self, key: &T::Key) -> bool {
114128
T::remove(self, key)
115129
}
116-
117-
/// Returns the value associated with the key.
118-
pub fn value(&self, key: &T::Key) -> T::Value {
119-
T::value(self, key)
120-
}
121130
}
122131

123132
impl<T> QMap<T>
@@ -195,6 +204,7 @@ pub trait QMapPair: Sized {
195204
fn contains(map: &QMap<Self>, key: &Self::Key) -> bool;
196205
fn default() -> QMap<Self>;
197206
fn drop(map: &mut QMap<Self>);
207+
fn get_or_default(map: &QMap<Self>, key: &Self::Key) -> Self::Value;
198208
/// # Safety
199209
///
200210
/// Calling this method with an out-of-bounds index is undefined behavior
@@ -212,7 +222,6 @@ pub trait QMapPair: Sized {
212222
fn insert_clone(map: &mut QMap<Self>, key: &Self::Key, value: &Self::Value);
213223
fn len(map: &QMap<Self>) -> isize;
214224
fn remove(map: &mut QMap<Self>, key: &Self::Key) -> bool;
215-
fn value(map: &QMap<Self>, key: &Self::Key) -> Self::Value;
216225
}
217226

218227
macro_rules! impl_qmap_pair {
@@ -242,6 +251,10 @@ macro_rules! impl_qmap_pair {
242251
$module::drop(map);
243252
}
244253

254+
fn get_or_default(map: &QMap<Self>, key: &$keyTypeName) -> $valueTypeName {
255+
$module::get_or_default(map, key)
256+
}
257+
245258
unsafe fn get_unchecked_key(map: &QMap<Self>, pos: isize) -> &$keyTypeName {
246259
$module::get_unchecked_key(map, pos)
247260
}
@@ -265,10 +278,6 @@ macro_rules! impl_qmap_pair {
265278
fn remove(map: &mut QMap<Self>, key: &$keyTypeName) -> bool {
266279
$module::remove(map, key)
267280
}
268-
269-
fn value(map: &QMap<Self>, key: &$keyTypeName) -> $valueTypeName {
270-
$module::value(map, key)
271-
}
272281
}
273282
};
274283
}

crates/cxx-qt-lib/src/core/qmap/qmap_qstring_qvariant.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ pub mod ffi {
3838

3939
#[namespace = "rust::cxxqtlib1::qmap"]
4040
unsafe extern "C++" {
41+
#[rust_name = "get_or_default_QString_QVariant"]
42+
fn qmapGetOrDefault(_: &QMap_QString_QVariant, key: &QString) -> QVariant;
4143
#[rust_name = "get_unchecked_key_QString_QVariant"]
4244
unsafe fn qmapGetUncheckedKey(_: &QMap_QString_QVariant, pos: isize) -> &QString;
4345
#[rust_name = "get_unchecked_value_QString_QVariant"]
@@ -48,8 +50,6 @@ pub mod ffi {
4850
fn qmapLen(_: &QMap_QString_QVariant) -> isize;
4951
#[rust_name = "remove_QString_QVariant"]
5052
fn qmapRemove(_: &mut QMap_QString_QVariant, key: &QString) -> bool;
51-
#[rust_name = "value_QString_QVariant"]
52-
fn qmapValue(_: &QMap_QString_QVariant, key: &QString) -> QVariant;
5353
}
5454
}
5555

@@ -65,6 +65,13 @@ pub(crate) fn drop(map: &mut ffi::QMap_QString_QVariant) {
6565
ffi::qmap_drop_QString_QVariant(map);
6666
}
6767

68+
pub(crate) fn get_or_default(
69+
map: &ffi::QMap_QString_QVariant,
70+
key: &ffi::QString,
71+
) -> ffi::QVariant {
72+
ffi::get_or_default_QString_QVariant(map, key)
73+
}
74+
6875
pub(crate) unsafe fn get_unchecked_key(
6976
map: &ffi::QMap_QString_QVariant,
7077
pos: isize,
@@ -95,10 +102,6 @@ pub(crate) fn remove(map: &mut ffi::QMap_QString_QVariant, key: &ffi::QString) -
95102
ffi::remove_QString_QVariant(map, key)
96103
}
97104

98-
pub(crate) fn value(map: &ffi::QMap_QString_QVariant, key: &ffi::QString) -> ffi::QVariant {
99-
ffi::value_QString_QVariant(map, key)
100-
}
101-
102105
#[allow(non_camel_case_types)]
103106
pub struct QMapPair_QString_QVariant;
104107

tests/qt_types_standalone/rust/src/qhash.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@ fn read_qhash_qstring_qvariant(h: &QHash<QHashPair_QString_QVariant>) -> bool {
3535
}
3636

3737
// Check that value method works
38-
let value_kdab = match h.value(&QString::from("kdab")).value::<i32>() {
38+
let value_kdab = match h.get_or_default(&QString::from("kdab")).value::<i32>() {
3939
Some(value) => value == 10,
4040
None => false,
4141
};
42-
let value_qt = match h.value(&QString::from("Qt")).value::<QString>() {
42+
let value_qt = match h.get_or_default(&QString::from("Qt")).value::<QString>() {
4343
Some(value) => value.to_string() == "Rust",
4444
_ => false,
4545
};

tests/qt_types_standalone/rust/src/qmap.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,11 @@ fn read_qmap_qstring_qvariant(h: &QMap<QMapPair_QString_QVariant>) -> bool {
3636

3737
// Check that that contains and value method works
3838
let value_kdab = h
39-
.value(&QString::from("kdab"))
39+
.get_or_default(&QString::from("kdab"))
4040
.value::<i32>()
4141
.map_or_else(|| false, |value| value == 10);
4242
let value_qt = h
43-
.value(&QString::from("Qt"))
43+
.get_or_default(&QString::from("Qt"))
4444
.value::<QString>()
4545
.map_or_else(|| false, |value| value.to_string() == "Rust");
4646

0 commit comments

Comments
 (0)