Skip to content

Commit 41d0dab

Browse files
authored
Merge pull request #564 from arcnmx/variant-dictentry
Variant::from_dict_entry
2 parents 976e84a + bd34bed commit 41d0dab

File tree

2 files changed

+79
-31
lines changed

2 files changed

+79
-31
lines changed

glib/src/variant.rs

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,20 @@ impl Variant {
493493
}
494494
}
495495

496+
// rustdoc-stripper-ignore-next
497+
/// Creates a new dictionary entry Variant.
498+
///
499+
/// [DictEntry] should be preferred over this when the types are known statically.
500+
#[doc(alias = "g_variant_new_dict_entry")]
501+
pub fn from_dict_entry(key: &Variant, value: &Variant) -> Self {
502+
unsafe {
503+
from_glib_none(ffi::g_variant_new_dict_entry(
504+
key.to_glib_none().0,
505+
value.to_glib_none().0,
506+
))
507+
}
508+
}
509+
496510
// rustdoc-stripper-ignore-next
497511
/// Creates a new maybe Variant.
498512
#[doc(alias = "g_variant_new_maybe")]
@@ -1232,10 +1246,7 @@ impl ToVariant for std::ffi::OsStr {
12321246

12331247
impl<T: StaticVariantType> StaticVariantType for Option<T> {
12341248
fn static_variant_type() -> Cow<'static, VariantTy> {
1235-
unsafe {
1236-
let ptr = ffi::g_variant_type_new_maybe(T::static_variant_type().to_glib_none().0);
1237-
Cow::Owned(from_glib_full(ptr))
1238-
}
1249+
Cow::Owned(VariantType::new_maybe(&T::static_variant_type()))
12391250
}
12401251
}
12411252

@@ -1521,12 +1532,7 @@ where
15211532
V: StaticVariantType + ToVariant,
15221533
{
15231534
fn to_variant(&self) -> Variant {
1524-
unsafe {
1525-
from_glib_none(ffi::g_variant_new_dict_entry(
1526-
self.key.to_variant().to_glib_none().0,
1527-
self.value.to_variant().to_glib_none().0,
1528-
))
1529-
}
1535+
Variant::from_dict_entry(&self.key.to_variant(), &self.value.to_variant())
15301536
}
15311537
}
15321538

@@ -1544,13 +1550,10 @@ impl FromVariant for Variant {
15441550

15451551
impl<K: StaticVariantType, V: StaticVariantType> StaticVariantType for DictEntry<K, V> {
15461552
fn static_variant_type() -> Cow<'static, VariantTy> {
1547-
unsafe {
1548-
let ptr = ffi::g_variant_type_new_dict_entry(
1549-
K::static_variant_type().to_glib_none().0,
1550-
V::static_variant_type().to_glib_none().0,
1551-
);
1552-
Cow::Owned(from_glib_full(ptr))
1553-
}
1553+
Cow::Owned(VariantType::new_dict_entry(
1554+
&K::static_variant_type(),
1555+
&V::static_variant_type(),
1556+
))
15541557
}
15551558
}
15561559

@@ -1609,16 +1612,11 @@ macro_rules! tuple_impls {
16091612
$($name: StaticVariantType,)+
16101613
{
16111614
fn static_variant_type() -> Cow<'static, VariantTy> {
1612-
let mut builder = crate::GStringBuilder::new("(");
1613-
1614-
$(
1615-
let t = $name::static_variant_type();
1616-
builder.append(t.as_str());
1617-
)+
1618-
1619-
builder.append_c(')');
1620-
1621-
Cow::Owned(VariantType::from_string(builder.into_string()).unwrap())
1615+
Cow::Owned(VariantType::new_tuple(&[
1616+
$(
1617+
$name::static_variant_type(),
1618+
)+
1619+
]))
16221620
}
16231621
}
16241622

glib/src/variant_type.rs

Lines changed: 54 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,47 @@ impl VariantType {
3838
VariantTy::new(type_string).map(ToOwned::to_owned)
3939
}
4040

41+
// rustdoc-stripper-ignore-next
42+
/// Creates a `VariantType` from a key and value type.
43+
#[doc(alias = "g_variant_type_new_dict_entry")]
44+
pub fn new_dict_entry(key_type: &VariantTy, value_type: &VariantTy) -> VariantType {
45+
unsafe {
46+
from_glib_full(ffi::g_variant_type_new_dict_entry(
47+
key_type.to_glib_none().0,
48+
value_type.to_glib_none().0,
49+
))
50+
}
51+
}
52+
53+
// rustdoc-stripper-ignore-next
54+
/// Creates a `VariantType` from an array element type.
55+
#[doc(alias = "g_variant_type_new_array")]
56+
pub fn new_array(elem_type: &VariantTy) -> VariantType {
57+
unsafe { from_glib_full(ffi::g_variant_type_new_array(elem_type.to_glib_none().0)) }
58+
}
59+
60+
// rustdoc-stripper-ignore-next
61+
/// Creates a `VariantType` from a maybe element type.
62+
#[doc(alias = "g_variant_type_new_maybe")]
63+
pub fn new_maybe(child_type: &VariantTy) -> VariantType {
64+
unsafe { from_glib_full(ffi::g_variant_type_new_maybe(child_type.to_glib_none().0)) }
65+
}
66+
67+
// rustdoc-stripper-ignore-next
68+
/// Creates a `VariantType` from a maybe element type.
69+
#[doc(alias = "g_variant_type_new_tuple")]
70+
pub fn new_tuple<T: AsRef<VariantTy>, I: IntoIterator<Item = T>>(items: I) -> VariantType {
71+
let mut builder = crate::GStringBuilder::new("(");
72+
73+
for ty in items {
74+
builder.append(ty.as_ref().as_str());
75+
}
76+
77+
builder.append_c(')');
78+
79+
VariantType::from_string(builder.into_string()).unwrap()
80+
}
81+
4182
// rustdoc-stripper-ignore-next
4283
/// Tries to create a `VariantType` from an owned string.
4384
///
@@ -65,6 +106,12 @@ impl Drop for VariantType {
65106
}
66107
}
67108

109+
impl AsRef<VariantTy> for VariantType {
110+
fn as_ref(&self) -> &VariantTy {
111+
self
112+
}
113+
}
114+
68115
impl Borrow<VariantTy> for VariantType {
69116
fn borrow(&self) -> &VariantTy {
70117
self
@@ -577,10 +624,7 @@ impl VariantTy {
577624
} else if self == VariantTy::DICT_ENTRY {
578625
Cow::Borrowed(VariantTy::DICTIONARY)
579626
} else {
580-
unsafe {
581-
let ptr = ffi::g_variant_type_new_array(self.to_glib_none().0);
582-
Cow::Owned(VariantType::from_glib_full(ptr))
583-
}
627+
Cow::Owned(VariantType::new_array(self))
584628
}
585629
}
586630
}
@@ -608,6 +652,12 @@ impl<'a> From<&'a VariantTy> for Cow<'a, VariantTy> {
608652
}
609653
}
610654

655+
impl AsRef<VariantTy> for VariantTy {
656+
fn as_ref(&self) -> &Self {
657+
self
658+
}
659+
}
660+
611661
impl ToOwned for VariantTy {
612662
type Owned = VariantType;
613663

0 commit comments

Comments
 (0)