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

Commit 4593064

Browse files
committed
Bind API to convert from/to bytes to/from Variant
Bind Variant::new_from_bytes & Variant::get_data_as_bytes().
1 parent 535ed31 commit 4593064

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

src/variant.rs

+17
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
//! assert_eq!(num.get_str(), None);
3939
//! ```
4040
41+
use bytes::Bytes;
4142
use glib_sys;
4243
use gobject_sys;
4344
use gstring::GString;
@@ -141,6 +142,22 @@ impl Variant {
141142
}
142143
}
143144
}
145+
146+
/// Constructs a new serialised-mode GVariant instance.
147+
pub fn new_from_bytes<T: StaticVariantType>(bytes: &Bytes, trusted: bool) -> Self {
148+
unsafe {
149+
from_glib_none(glib_sys::g_variant_new_from_bytes(
150+
T::static_variant_type().as_ptr() as *const _,
151+
bytes.to_glib_none().0,
152+
trusted.to_glib(),
153+
))
154+
}
155+
}
156+
157+
/// Returns the serialised form of a GVariant instance.
158+
pub fn get_data_as_bytes(&self) -> Bytes {
159+
unsafe { from_glib_full(glib_sys::g_variant_get_data_as_bytes(self.to_glib_none().0)) }
160+
}
144161
}
145162

146163
unsafe impl Send for Variant {}

0 commit comments

Comments
 (0)