gio/
resource.rs

1// Copyright 2017, The Gtk-rs Project Developers.
2// See the COPYRIGHT file at the top-level directory of this distribution.
3// Licensed under the MIT license, see the LICENSE file or <http://opensource.org/licenses/MIT>
4
5use gio_sys;
6use glib;
7use glib::translate::*;
8use glib_sys;
9use std::mem;
10use std::ptr;
11use Resource;
12
13impl Resource {
14    pub fn new_from_data(data: &glib::Bytes) -> Result<Resource, glib::Error> {
15        unsafe {
16            let mut error = ptr::null_mut();
17
18            // Create a copy of data if it is not pointer-aligned
19            // https://bugzilla.gnome.org/show_bug.cgi?id=790030
20            let mut data = data.clone();
21            let data_ptr = glib_sys::g_bytes_get_data(data.to_glib_none().0, ptr::null_mut());
22            if data_ptr as usize % mem::align_of::<*const u8>() != 0 {
23                data = glib::Bytes::from(&*data);
24            }
25
26            let ret = gio_sys::g_resource_new_from_data(data.to_glib_none().0, &mut error);
27            if error.is_null() {
28                Ok(from_glib_full(ret))
29            } else {
30                Err(from_glib_full(error))
31            }
32        }
33    }
34}