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

Commit 67caba9

Browse files
authored
Merge pull request #588 from zeenix/variant-data
Bind API to convert from/to bytes to/from Variant
2 parents 535ed31 + 975e960 commit 67caba9

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

src/variant.rs

+27
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,32 @@ impl Variant {
141142
}
142143
}
143144
}
145+
146+
/// Constructs a new serialised-mode GVariant instance.
147+
pub fn new_from_bytes<T: StaticVariantType>(bytes: &Bytes) -> 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+
false.to_glib(),
153+
))
154+
}
155+
}
156+
157+
/// Same as `new_from_bytes`, except checks on the passed data are skipped, which makes it a
158+
/// potentially unsafe function. You should not use this function on data from external sources.
159+
pub unsafe fn new_from_bytes_trusted<T: StaticVariantType>(bytes: &Bytes) -> Self {
160+
from_glib_none(glib_sys::g_variant_new_from_bytes(
161+
T::static_variant_type().as_ptr() as *const _,
162+
bytes.to_glib_none().0,
163+
true.to_glib(),
164+
))
165+
}
166+
167+
/// Returns the serialised form of a GVariant instance.
168+
pub fn get_data_as_bytes(&self) -> Bytes {
169+
unsafe { from_glib_full(glib_sys::g_variant_get_data_as_bytes(self.to_glib_none().0)) }
170+
}
144171
}
145172

146173
unsafe impl Send for Variant {}

0 commit comments

Comments
 (0)