gio/
resource.rs

1// Take a look at the license at the top of the repository in the LICENSE file.
2
3use std::{mem, ptr};
4
5use glib::translate::*;
6
7use crate::{resources_register, Resource};
8
9impl Resource {
10    /// Creates a [`Resource`][crate::Resource] from a reference to the binary resource bundle.
11    ///
12    /// This will keep a reference to @data while the resource lives, so
13    /// the data should not be modified or freed.
14    ///
15    /// If you want to use this resource in the global resource namespace you need
16    /// to register it with [`resources_register()`][crate::resources_register()].
17    ///
18    /// Note: @data must be backed by memory that is at least pointer aligned.
19    /// Otherwise this function will internally create a copy of the memory since
20    /// GLib 2.56, or in older versions fail and exit the process.
21    ///
22    /// If @data is empty or corrupt, [`ResourceError::Internal`][crate::ResourceError::Internal] will be returned.
23    /// ## `data`
24    /// A [`glib::Bytes`][crate::glib::Bytes]
25    ///
26    /// # Returns
27    ///
28    /// a new [`Resource`][crate::Resource], or `NULL` on error
29    #[doc(alias = "g_resource_new_from_data")]
30    pub fn from_data(data: &glib::Bytes) -> Result<Resource, glib::Error> {
31        unsafe {
32            let mut error = ptr::null_mut();
33
34            // Create a copy of data if it is not pointer-aligned
35            // https://bugzilla.gnome.org/show_bug.cgi?id=790030
36            let mut data = data.clone();
37            let data_ptr = glib::ffi::g_bytes_get_data(data.to_glib_none().0, ptr::null_mut());
38            if data_ptr as usize % mem::align_of::<*const u8>() != 0 {
39                data = glib::Bytes::from(&*data);
40            }
41
42            let ret = crate::ffi::g_resource_new_from_data(data.to_glib_none().0, &mut error);
43            if error.is_null() {
44                Ok(from_glib_full(ret))
45            } else {
46                Err(from_glib_full(error))
47            }
48        }
49    }
50}
51
52#[doc(hidden)]
53pub fn resources_register_include_impl(bytes: &'static [u8]) -> Result<(), glib::Error> {
54    let bytes = glib::Bytes::from_static(bytes);
55    let resource = Resource::from_data(&bytes)?;
56    resources_register(&resource);
57    Ok(())
58}
59
60// rustdoc-stripper-ignore-next
61/// Include gresources generated with `glib_build_tools::compile_resources` and register with glib. `path` is
62/// relative to `OUTDIR`.
63///
64/// ```ignore
65/// gio::resources_register_include!("compiled.gresource").unwrap();
66/// ```
67#[macro_export]
68macro_rules! resources_register_include {
69    ($path:expr) => {
70        $crate::resources_register_include_impl(include_bytes!(concat!(
71            env!("OUT_DIR"),
72            "/",
73            $path
74        )))
75    };
76}