Problem with Rust bindings for TreeListModel

I am trying to convert Rust / GTK app from using TreeView to ColumnView but am stuck at creating the TreeListModel.

I have a rust struct that implements ListModel but can’t return that from create_func

My code is

            let model = TreeListModel::new(plan_object, false, true, |x| {
                ...
                ...
                let so = SectorObject::new(&sec.clone());  //This implements ListModel
                Some(so)  // This doesn't compile  - ^^ expected `ListModel`, found `SectorObject`
            });

This techniques works generally elsewhere in my code, but I believe the bindings are wrong.

The signature for TreeListModel::new is

pub fn new<P: Fn(&Object) -> Option<ListModel> + 'static>(
    root: impl IsA<ListModel>,
    passthrough: bool,
    autoexpand: bool,
    create_func: P,
) -> TreeListModel

This specifies the create_func returns Option<ListModel but I believe it should probably specified to return Option<IsA<ListModel>> which is the pattern used everywhere else.

The compiler suggests I correct the error by returning Some(so.into()) to coerce the value, but when I try that I get a null pointer error, specifically

thread 'main' panicked at /home/..../index.crates.io-6f17d22bba15001f/glib-0.20.4/src/object.rs:1371:1:
assertion failed: !ptr.is_null()

So that does not seem like the way to go.

I believe you are looking for upcast

Thanks @zbrown. That works. The null pointer problem was an error in my code.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.