-
-
Notifications
You must be signed in to change notification settings - Fork 114
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[BUG] <Variant as FromIterator<Item = T>>::from_iter
panics if T
contains Variant
#1498
Comments
No the problem is that your dict entries are
E.g. this change fixes it diff --git a/glib/src/variant.rs b/glib/src/variant.rs
index 14e73f797c5..2852bbe1ef8 100644
--- a/glib/src/variant.rs
+++ b/glib/src/variant.rs
@@ -451,7 +451,11 @@ impl Variant {
== ffi::GFALSE
{
ffi::g_variant_builder_clear(&mut builder);
- assert!(value.is_type(type_));
+ assert!(
+ value.is_type(type_),
+ "Expected type {type_} but got {}",
+ value.type_()
+ );
}
ffi::g_variant_builder_add_value(&mut builder, value.to_glib_none().0);
@@ -1839,9 +1843,9 @@ tuple_impls! {
16 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10 11 T11 12 T12 13 T13 14 T14 15 T15)
}
-impl<T: Into<Variant> + StaticVariantType> FromIterator<T> for Variant {
+impl<T: ToVariant + StaticVariantType> FromIterator<T> for Variant {
fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
- Variant::array_from_iter::<T>(iter.into_iter().map(|v| v.into()))
+ Variant::array_from_iter::<T>(iter.into_iter().map(|v| v.to_variant()))
}
}
@@ -2517,4 +2521,17 @@ mod tests {
let hashmap: Option<HashMap<u64, u64>> = FromVariant::from_variant(&variant);
assert!(hashmap.is_some());
}
+
+ #[test]
+ fn test_dict_array_collect() {
+ let a = [
+ // `DictEntry<i32, Variant>`
+ DictEntry::new(1, "foo".to_variant()),
+ DictEntry::new(2, 42.to_variant()),
+ ]
+ .into_iter()
+ .collect::<Variant>();
+ assert_eq!(a.type_().as_str(), "a{iv}");
+ assert_eq!(a.n_children(), 2);
+ }
} This needs some more thought, and it seems quite unexpected that |
Basically #[test]
fn test_dict_entry_types() {
let e = DictEntry::new(1, "foo".to_variant());
let v1 = e.to_variant();
let v2 = Variant::from(e);
assert_eq!(v1.type_(), v2.type_());
} Fails with
|
And the reason for that is that That seems like a problematic inconsistency 🙃 |
Bug description
<Variant as FromIterator<Item = T>>::from_iter
seems to assume that<T as Into<Variant>>::into
returns a variant of type<T as StaticVariantType>::static_variant_type()
, but this assumption does not hold ifT
or one of its constituents isVariant
.Backtrace
The text was updated successfully, but these errors were encountered: