Skip to content
This repository has been archived by the owner on Jun 8, 2021. It is now read-only.

Commit

Permalink
Allow creating Array type Variant
Browse files Browse the repository at this point in the history
  • Loading branch information
zeenix committed Jun 6, 2020
1 parent 97689e7 commit 66f21ae
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/variant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@
//! let variant = Variant::new_variant(&hello);
//! let variant = variant.get_variant().unwrap();
//! assert_eq!(variant.get_str(), Some("Hello!"));
//!
//! // Variant carrying an array
//! let array = [Variant::from("Hello"), Variant::from("there!")];
//! let variant = Variant::new_array::<&str>(&array);
//! assert_eq!(variant.n_children(), 2);
//! assert_eq!(variant.get_child_value(0).get_str(), Some("Hello"));
//! assert_eq!(variant.get_child_value(1).get_str(), Some("there!"));
//! ```

use bytes::Bytes;
Expand Down Expand Up @@ -183,6 +190,17 @@ impl Variant {
}
}

/// Creates a new GVariant array from children.
pub fn new_array<T: StaticVariantType>(children: &[Variant]) -> Self {
unsafe {
from_glib_none(glib_sys::g_variant_new_array(
T::static_variant_type().as_ptr() as *const _,
children.to_glib_none().0,
children.len(),
))
}
}

/// Constructs a new serialised-mode GVariant instance.
pub fn new_from_bytes<T: StaticVariantType>(bytes: &Bytes) -> Self {
unsafe {
Expand Down

0 comments on commit 66f21ae

Please sign in to comment.