Skip to content

Commit

Permalink
Merge pull request #1497 from kawadakk/patch/fix-variant-dict-into-va…
Browse files Browse the repository at this point in the history
…riant

glib: Do not use `end_unsafe` in `<Variant as From<VariantDict>>::from`
  • Loading branch information
sdroege authored Sep 1, 2024
2 parents 7c3e879 + 72a74ab commit 7638251
Showing 1 changed file with 29 additions and 1 deletion.
30 changes: 29 additions & 1 deletion glib/src/variant_dict.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,9 +213,15 @@ impl ToVariant for VariantDict {
}

impl From<VariantDict> for Variant {
// rustdoc-stripper-ignore-next
/// Consume a given `VariantDict` and call [`VariantDict::end`] on it.
///
/// Note: While this method consumes the `VariantDict`, the underlying
/// object could still be accessed through other clones because of the
/// reference counted clone semantics.
#[inline]
fn from(d: VariantDict) -> Self {
unsafe { d.end_unsafe() }
d.end()
}
}

Expand Down Expand Up @@ -251,6 +257,28 @@ mod test {
let _dict2: VariantDict = var.into();
}

#[test]
fn into_variant_roundtrip() {
let dict1 = VariantDict::default();
let dict2 = dict1.clone();
dict1.insert_value("one", &(1u8.to_variant()));

assert_eq!(dict1.lookup::<u8>("one").unwrap(), Some(1u8));
assert_eq!(dict2.lookup::<u8>("one").unwrap(), Some(1u8));

// Convert it into `Variant`
let dict: Variant = dict1.into();

// While we can still access the `VariantDict` via `dict2`,
// it should be empty now
assert_eq!(dict2.lookup::<u8>("one").unwrap(), None);

// Convert it back
let dict3: VariantDict = dict.into();

assert_eq!(dict3.lookup::<u8>("one").unwrap(), Some(1u8));
}

#[test]
fn create_populate_destroy() {
let dict = VariantDict::default();
Expand Down

0 comments on commit 7638251

Please sign in to comment.