FileEnumeratorImpl

Trait FileEnumeratorImpl 

Source
pub trait FileEnumeratorImpl: ObjectImpl + ObjectSubclass<Type: IsA<FileEnumerator>> {
    // Provided methods
    fn next_file(
        &self,
        cancellable: Option<&Cancellable>,
    ) -> Result<Option<FileInfo>, Error> { ... }
    fn close(&self, cancellable: Option<&Cancellable>) -> (bool, Option<Error>) { ... }
}

Provided Methods§

Source

fn next_file( &self, cancellable: Option<&Cancellable>, ) -> Result<Option<FileInfo>, Error>

Returns information for the next file in the enumerated object. Will block until the information is available. The #GFileInfo returned from this function will contain attributes that match the attribute string that was passed when the #GFileEnumerator was created.

See the documentation of #GFileEnumerator for information about the order of returned files.

On error, returns None and sets @error to the error. If the enumerator is at the end, None will be returned and @error will be unset.

§cancellable

optional #GCancellable object, None to ignore.

§Returns

A #GFileInfo or None on error or end of enumerator. Free the returned object with g_object_unref() when no longer needed.

Source

fn close(&self, cancellable: Option<&Cancellable>) -> (bool, Option<Error>)

Closes the enumerator (see FileEnumeratorExt::close).

NOTE: If the enumerator has not been explicitly closed, GIO closes it when the object is dropped. But GIO does it by calling close vfunc in finalize, which is not safe and could lead to undefined behavior, such as accessing freed memory or resources, which can cause crashes or other unexpected behavior.

An issue has been opened in GLib to address this: https://gitlab.gnome.org/GNOME/glib/-/issues/3713 and a MR has been opened to fix it: https://gitlab.gnome.org/GNOME/glib/-/merge_requests/4672.

Until this is fixed, it is unsafe to rely on the enumerator being closed when the object is dropped. It is recommended to close the enumerator explicitly before dropping it, by calling FileEnumeratorExt::close, or to implement the ObjectImpl::dispose method and call FileEnumeratorExt::close there (it is safe to access the object there):

pub struct MyFileEnumerator();

#[glib::object_subclass]
impl ObjectSubclass for MyFileEnumerator { ... }

impl ObjectImpl for MyFileEnumerator {
    fn dispose(&self) {
        // close the enumerator here is safe and avoids `finalize` to call close.
        let _ = self.obj().close(Cancellable::NONE);
    }
}

impl FileEnumeratorImpl for MyFileEnumerator { ... }

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§