1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
// Copyright 2017, The Gtk-rs Project Developers. // See the COPYRIGHT file at the top-level directory of this distribution. // Licensed under the MIT license, see the LICENSE file or <http://opensource.org/licenses/MIT> use gio_sys; use glib; use glib::translate::*; use glib_sys; use std::mem; use std::ptr; use Resource; impl Resource { /// Creates a GResource from a reference to the binary resource bundle. /// This will keep a reference to `data` while the resource lives, so /// the data should not be modified or freed. /// /// If you want to use this resource in the global resource namespace you need /// to register it with `g_resources_register`. /// /// Note: `data` must be backed by memory that is at least pointer aligned. /// Otherwise this function will internally create a copy of the memory since /// GLib 2.56, or in older versions fail and exit the process. /// /// If `data` is empty or corrupt, `ResourceError::Internal` will be returned. /// ## `data` /// A `glib::Bytes` /// /// # Returns /// /// a new `Resource`, or `None` on error pub fn new_from_data(data: &glib::Bytes) -> Result<Resource, glib::Error> { unsafe { let mut error = ptr::null_mut(); // Create a copy of data if it is not pointer-aligned // https://bugzilla.gnome.org/show_bug.cgi?id=790030 let mut data = data.clone(); let data_ptr = glib_sys::g_bytes_get_data(data.to_glib_none().0, ptr::null_mut()); if data_ptr as usize % mem::align_of::<*const u8>() != 0 { data = glib::Bytes::from(&*data); } let ret = gio_sys::g_resource_new_from_data(data.to_glib_none().0, &mut error); if error.is_null() { Ok(from_glib_full(ret)) } else { Err(from_glib_full(error)) } } } }