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

Bind API to convert from/to bytes to/from Variant #588

Merged
merged 1 commit into from
Feb 12, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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