Skip to content

Commit bd34bed

Browse files
committed
glib: VariantType::new_tuple
Also impl AsRef for VariantTy{pe,}
1 parent ada6d5c commit bd34bed

File tree

2 files changed

+32
-10
lines changed

2 files changed

+32
-10
lines changed

glib/src/variant.rs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1605,16 +1605,11 @@ macro_rules! tuple_impls {
16051605
$($name: StaticVariantType,)+
16061606
{
16071607
fn static_variant_type() -> Cow<'static, VariantTy> {
1608-
let mut builder = crate::GStringBuilder::new("(");
1609-
1610-
$(
1611-
let t = $name::static_variant_type();
1612-
builder.append(t.as_str());
1613-
)+
1614-
1615-
builder.append_c(')');
1616-
1617-
Cow::Owned(VariantType::from_string(builder.into_string()).unwrap())
1608+
Cow::Owned(VariantType::new_tuple(&[
1609+
$(
1610+
$name::static_variant_type(),
1611+
)+
1612+
]))
16181613
}
16191614
}
16201615

glib/src/variant_type.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,21 @@ impl VariantType {
6464
unsafe { from_glib_full(ffi::g_variant_type_new_maybe(child_type.to_glib_none().0)) }
6565
}
6666

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+
6782
// rustdoc-stripper-ignore-next
6883
/// Tries to create a `VariantType` from an owned string.
6984
///
@@ -91,6 +106,12 @@ impl Drop for VariantType {
91106
}
92107
}
93108

109+
impl AsRef<VariantTy> for VariantType {
110+
fn as_ref(&self) -> &VariantTy {
111+
self
112+
}
113+
}
114+
94115
impl Borrow<VariantTy> for VariantType {
95116
fn borrow(&self) -> &VariantTy {
96117
self
@@ -631,6 +652,12 @@ impl<'a> From<&'a VariantTy> for Cow<'a, VariantTy> {
631652
}
632653
}
633654

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

0 commit comments

Comments
 (0)