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

Commit

Permalink
Bind API to convert from/to bytes to/from Variant
Browse files Browse the repository at this point in the history
Bind Variant::new_from_bytes & Variant::get_data_as_bytes().
  • Loading branch information
zeenix committed Feb 12, 2020
1 parent 535ed31 commit 975e960
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/variant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
//! assert_eq!(num.get_str(), None);
//! ```
use bytes::Bytes;
use glib_sys;
use gobject_sys;
use gstring::GString;
Expand Down Expand Up @@ -141,6 +142,32 @@ impl Variant {
}
}
}

/// Constructs a new serialised-mode GVariant instance.
pub fn new_from_bytes<T: StaticVariantType>(bytes: &Bytes) -> Self {
unsafe {
from_glib_none(glib_sys::g_variant_new_from_bytes(
T::static_variant_type().as_ptr() as *const _,
bytes.to_glib_none().0,
false.to_glib(),
))
}
}

/// Same as `new_from_bytes`, except checks on the passed data are skipped, which makes it a
/// potentially unsafe function. You should not use this function on data from external sources.
pub unsafe fn new_from_bytes_trusted<T: StaticVariantType>(bytes: &Bytes) -> Self {
from_glib_none(glib_sys::g_variant_new_from_bytes(
T::static_variant_type().as_ptr() as *const _,
bytes.to_glib_none().0,
true.to_glib(),
))
}

/// Returns the serialised form of a GVariant instance.
pub fn get_data_as_bytes(&self) -> Bytes {
unsafe { from_glib_full(glib_sys::g_variant_get_data_as_bytes(self.to_glib_none().0)) }
}
}

unsafe impl Send for Variant {}
Expand Down

0 comments on commit 975e960

Please sign in to comment.