Skip to main content

Resource

Struct Resource 

Source
pub struct Resource { /* private fields */ }
Expand description

s more common to use the --generate-source and --generate-header arguments to create a source file and header to link directly into your application. This will generate get_resource(), register_resource() and unregister_resource() functions, prefixed by the --c-name argument passed to glib-compile-resources. get_resource() returns the generated GResource object. The register and unregister functions register the resource so its files can be accessed using resources_lookup_data().

Once a GResource has been created and registered all the data in it can be accessed globally in the process by using API calls like resources_open_stream() to stream the data or resources_lookup_data() to get a direct pointer to the data. You can also use URIs like resource:///org/gtk/Example/data/splashscreen.png with File to access the resource data.

Some higher-level APIs, such as GtkApplication, will automatically load resources from certain well-known paths in the resource namespace as a convenience. See the documentation for those APIs for details.

There are two forms of the generated source, the default version uses the compiler support for constructor and destructor functions (where available) to automatically create and register the GResource on startup or library load time. If you pass --manual-register, two functions to register/unregister the resource are created instead. This requires an explicit initialization call in your application/library, but it works on all platforms, even on the minor ones where constructors are not supported. (Constructor support is available for at least Win32, Mac OS and Linux.)

Note that resource data can point directly into the data segment of e.g. a library, so if you are unloading libraries during runtime you need to be very careful with keeping around pointers to data from a resource, as this goes away when the library is unloaded. However, in practice this is not generally a problem, since most resource accesses are for your own resources, and resource data is often used once, during parsing, and then released.

§Overlays

When debugging a program or testing a change to an installed version, it is often useful to be able to replace resources in the program or library, without recompiling, for debugging or quick hacking and testing purposes. Since GLib 2.50, it is possible to use the G_RESOURCE_OVERLAYS environment variable to selectively overlay resources with replacements from the filesystem. It is a G_SEARCHPATH_SEPARATOR-separated list of substitutions to perform during resource lookups. It is ignored when running in a setuid process.

A substitution has the form

/org/gtk/libgtk=/home/desrt/gtk-overlay

The part before the = is the resource subpath for which the overlay applies. The part after is a filesystem path which contains files and subdirectories as you would like to be loaded as resources with the equivalent names.

In the example above, if an application tried to load a resource with the resource path /org/gtk/libgtk/ui/gtkdialog.ui then GResource would check the filesystem path /home/desrt/gtk-overlay/ui/gtkdialog.ui. If a file was found there, it would be used instead. This is an overlay, not an outright replacement, which means that if a file is not found at that path, the built-in version will be used instead. Whiteouts are not currently supported.

Substitutions must start with a slash, and must not contain a trailing slash before the =. The filesystem path after the = should ideally be absolute, but this is not strictly required. It is possible to overlay the location of a single resource with an individual file.

GLib type: Shared boxed type with reference counted clone semantics.

Implementations§

Source§

impl Resource

Source

pub fn from_data(data: &Bytes) -> Result<Resource, Error>

Creates a Resource 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 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 NULL on error

Source§

impl Resource

Source

pub fn as_ptr(&self) -> *mut GResource

Return the inner pointer to the underlying C value.

Source

pub unsafe fn from_glib_ptr_borrow(ptr: &*mut GResource) -> &Self

Borrows the underlying C value.

Source§

impl Resource

Source

pub fn enumerate_children( &self, path: &str, lookup_flags: ResourceLookupFlags, ) -> Result<Vec<GString>, Error>

Returns all the names of children at the specified @path in the resource.

The return result is a NULL terminated list of strings which should be released with strfreev().

If @path is invalid or does not exist in the Resource, ResourceError::NotFound will be returned.

@lookup_flags controls the behaviour of the lookup.

§path

A path name inside the resource

§lookup_flags

A ResourceLookupFlags

§Returns

an array of constant strings

Source

pub fn info( &self, path: &str, lookup_flags: ResourceLookupFlags, ) -> Result<(usize, u32), Error>

Looks for a file at the specified @path in the resource and if found returns information about it.

@lookup_flags controls the behaviour of the lookup.

The only error this can return is ResourceError::NotFound, if @path was not found in @self.

§path

A path name inside the resource

§lookup_flags

A ResourceLookupFlags

§Returns

TRUE if the file was found, FALSE if there were errors

§size

a location to place the length of the contents of the file, or NULL if the length is not needed

§flags

a location to place the flags about the file, or NULL if the length is not needed

Source

pub fn has_children(&self, path: &str) -> bool

Available on crate feature v2_84 only.

Returns whether the specified @path in the resource has children.

§path

A pathname inside the resource

§Returns

true if @path has children

Source

pub fn lookup_data( &self, path: &str, lookup_flags: ResourceLookupFlags, ) -> Result<Bytes, Error>

Looks for a file at the specified @path in the resource and returns a glib::Bytes that lets you directly access the data in memory.

The data is always followed by a zero byte, so you can safely use the data as a C string. However, that byte is not included in the size of the glib::Bytes.

For uncompressed resource files this is a pointer directly into the resource bundle, which is typically in some read-only data section in the program binary. For compressed files, memory is allocated on the heap and the data is automatically uncompressed.

@lookup_flags controls the behaviour of the lookup.

This can return error ResourceError::NotFound if @path was not found in @self, or ResourceError::Internal if decompression of a compressed resource failed.

§path

A path name inside the resource

§lookup_flags

A ResourceLookupFlags

§Returns

glib::Bytes or NULL on error

Source

pub fn open_stream( &self, path: &str, lookup_flags: ResourceLookupFlags, ) -> Result<InputStream, Error>

Looks for a file at the specified @path in the resource and returns a InputStream that lets you read the data.

@lookup_flags controls the behaviour of the lookup.

The only error this can return is ResourceError::NotFound, if @path was not found in @self.

§path

A path name inside the resource

§lookup_flags

A ResourceLookupFlags

§Returns

InputStream or NULL on error

Source

pub fn load(filename: impl AsRef<Path>) -> Result<Resource, Error>

t exist, or there is an error in reading it, an error from GLib::MappedFile::new() will be returned.

§filename

the path of a filename to load, in the GLib filename encoding

§Returns

a new Resource, or NULL on error

Trait Implementations§

Source§

impl Clone for Resource

Source§

fn clone(&self) -> Self

Makes a clone of this shared reference.

This increments the strong reference count of the reference. Dropping the reference will decrement it again.

1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Resource

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Eq for Resource

Source§

impl From<Resource> for Value

Source§

fn from(s: Resource) -> Self

Converts to this type from the input type.
Source§

impl HasParamSpec for Resource

Source§

type ParamSpec = ParamSpecBoxed

Source§

type SetValue = Resource

Preferred value to be used as setter for the associated ParamSpec.
Source§

type BuilderFn = fn(&str) -> ParamSpecBoxedBuilder<'_, Resource>

Source§

fn param_spec_builder() -> Self::BuilderFn

Source§

impl Hash for Resource

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl Ord for Resource

Source§

fn cmp(&self, other: &Resource) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 (const: unstable) · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 (const: unstable) · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 (const: unstable) · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl PartialEq for Resource

Source§

fn eq(&self, other: &Resource) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialOrd for Resource

Source§

fn partial_cmp(&self, other: &Resource) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 (const: unstable) · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 (const: unstable) · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 (const: unstable) · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 (const: unstable) · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl StaticType for Resource

Source§

fn static_type() -> Type

Returns the type identifier of Self.
Source§

impl StructuralPartialEq for Resource

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> FromGlibContainerAsVec<<T as GlibPtrDefault>::GlibType, *const GList> for T

Source§

impl<T> FromGlibContainerAsVec<<T as GlibPtrDefault>::GlibType, *const GPtrArray> for T

Source§

impl<T> FromGlibContainerAsVec<<T as GlibPtrDefault>::GlibType, *const GSList> for T

Source§

impl<T> FromGlibContainerAsVec<<T as GlibPtrDefault>::GlibType, *mut GList> for T

Source§

impl<T> FromGlibContainerAsVec<<T as GlibPtrDefault>::GlibType, *mut GPtrArray> for T

Source§

impl<T> FromGlibContainerAsVec<<T as GlibPtrDefault>::GlibType, *mut GSList> for T

Source§

impl<T> FromGlibPtrArrayContainerAsVec<<T as GlibPtrDefault>::GlibType, *const GList> for T

Source§

impl<T> FromGlibPtrArrayContainerAsVec<<T as GlibPtrDefault>::GlibType, *const GPtrArray> for T

Source§

impl<T> FromGlibPtrArrayContainerAsVec<<T as GlibPtrDefault>::GlibType, *const GSList> for T

Source§

impl<T> FromGlibPtrArrayContainerAsVec<<T as GlibPtrDefault>::GlibType, *mut GList> for T

Source§

impl<T> FromGlibPtrArrayContainerAsVec<<T as GlibPtrDefault>::GlibType, *mut GPtrArray> for T

Source§

impl<T> FromGlibPtrArrayContainerAsVec<<T as GlibPtrDefault>::GlibType, *mut GSList> for T

Source§

impl<'a, T, C, E> FromValueOptional<'a> for T
where T: FromValue<'a, Checker = C>, C: ValueTypeChecker<Error = ValueTypeMismatchOrNoneError<E>>, E: Error + Send + 'static,

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoClosureReturnValue for T
where T: Into<Value>,

Source§

impl<T> Property for T
where T: HasParamSpec,

Source§

type Value = T

Source§

impl<T> PropertyGet for T
where T: HasParamSpec,

Source§

type Value = T

Source§

fn get<R, F>(&self, f: F) -> R
where F: Fn(&<T as PropertyGet>::Value) -> R,

Source§

impl<T> StaticTypeExt for T
where T: StaticType,

Source§

fn ensure_type()

Ensures that the type has been registered with the type system.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> TransparentType for T

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T> TryFromClosureReturnValue for T
where T: for<'a> FromValue<'a> + StaticType + 'static,

Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.