Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions crates/cxx-qt-lib/src/core/qhash/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
// SPDX-License-Identifier: MIT OR Apache-2.0
use core::{marker::PhantomData, mem::MaybeUninit};
use cxx::{type_id, ExternType};
use std::borrow::Borrow;
use std::fmt;

/// The `QHash` class is a template class that provides a hash-table-based dictionary.
Expand Down Expand Up @@ -225,6 +226,32 @@ where
}
}

impl<T, K, V> Extend<(K, V)> for QHash<T>
where
T: QHashPair,
K: Borrow<T::Key>,
V: Borrow<T::Value>,
{
fn extend<I: IntoIterator<Item = (K, V)>>(&mut self, iter: I) {
for (key, value) in iter {
self.insert_clone(key.borrow(), value.borrow());
}
}
}

impl<T, K, V> FromIterator<(K, V)> for QHash<T>
where
T: QHashPair,
K: Borrow<T::Key>,
V: Borrow<T::Value>,
{
fn from_iter<I: IntoIterator<Item = (K, V)>>(iter: I) -> Self {
let mut qhash = Self::default();
qhash.extend(iter);
qhash
}
}

/// Trait implementation for a pair in a [`QHash`].
pub trait QHashPair: Sized {
type Key;
Expand Down
27 changes: 27 additions & 0 deletions crates/cxx-qt-lib/src/core/qmap/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
// SPDX-License-Identifier: MIT OR Apache-2.0
use core::{marker::PhantomData, mem::MaybeUninit};
use cxx::{type_id, ExternType};
use std::borrow::Borrow;
use std::fmt;

/// The `QMap` class is a template class that provides an associative array.
Expand Down Expand Up @@ -221,6 +222,32 @@ where
}
}

impl<T, K, V> Extend<(K, V)> for QMap<T>
where
T: QMapPair,
K: Borrow<T::Key>,
V: Borrow<T::Value>,
{
fn extend<I: IntoIterator<Item = (K, V)>>(&mut self, iter: I) {
for (key, value) in iter {
self.insert_clone(key.borrow(), value.borrow());
}
}
}

impl<T, K, V> FromIterator<(K, V)> for QMap<T>
where
T: QMapPair,
K: Borrow<T::Key>,
V: Borrow<T::Value>,
{
fn from_iter<I: IntoIterator<Item = (K, V)>>(iter: I) -> Self {
let mut qmap = Self::default();
qmap.extend(iter);
qmap
}
}

/// Trait implementation for a pair in a [`QMap`].
pub trait QMapPair: Sized {
type Key;
Expand Down
16 changes: 16 additions & 0 deletions crates/cxx-qt-lib/src/core/qstringlist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,22 @@ impl From<&QStringList> for QList<QString> {
}
}

impl<'a> FromIterator<&'a QString> for QStringList {
fn from_iter<I: IntoIterator<Item = &'a QString>>(iter: I) -> Self {
let mut qstringlist = Self::default();
qstringlist.extend(iter);
qstringlist
}
}

impl FromIterator<QString> for QStringList {
fn from_iter<I: IntoIterator<Item = QString>>(iter: I) -> Self {
let mut qstringlist = Self::default();
qstringlist.extend(iter);
qstringlist
}
}

impl Deref for QStringList {
type Target = QList<QString>;

Expand Down
13 changes: 2 additions & 11 deletions crates/cxx-qt-lib/src/core/qvector/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,11 +196,7 @@ where
/// Convert a reference to a [`QVector`] into a [`Vec`] by making a deep copy of the data.
/// The original `QVector` can still be used after constructing the `Vec`.
fn from(qvec: &QVector<T>) -> Self {
let mut vec = Vec::with_capacity(qvec.len().try_into().unwrap());
for element in qvec.iter() {
vec.push(element.clone());
}
vec
qvec.iter().cloned().collect()
}
}

Expand All @@ -213,12 +209,7 @@ where
/// by making a deep copy of the data.
/// The original slice can still be used after constructing the `QVector`.
fn from(vec: S) -> Self {
let mut qvec = Self::default();
qvec.reserve_usize(vec.as_ref().len());
for element in vec.as_ref() {
qvec.append_clone(element);
}
qvec
vec.as_ref().iter().collect()
}
}
impl<'a, T> Extend<&'a T> for QVector<T>
Expand Down
Loading