Do not understand this error

I've implemented a set of personal finance tools in Rust. In the main application, I use the gtk crate to provide a gui for editing the sqlite financial database.

This has worked just fine for a year or so. Then I noted that the gtk crate had been updated and I edited my cargo.toml files to allow the new version to be used. Bad idea. In the main application, I have a line in the initialization code:

    let scrolled_window = ScrolledWindow::new(None, None);

This line provokes the following error:

error[E0283]: type annotations required: cannot resolve `_: gtk::IsA<gtk::Adjustment>`
   --> src/main.rs:159:42
    |
159 |     let scrolled_window:ScrolledWindow = ScrolledWindow::new(None, None);
    |                                          ^^^^^^^^^^^^^^^^^^^
    |
    = note: required by `gtk::ScrolledWindow::new`

From the gtk (crate) documentation:

#### `pub fn new<P: IsA<Adjustment>, Q: IsA<Adjustment>>(    hadjustment: Option<&P>,         vadjustment: Option<&Q>) -> ScrolledWindow` 

Creates a new scrolled window.

The two arguments are the scrolled window’s adjustments; these will be shared with the scrollbars and 
the child widget to keep the bars in sync with the child. Usually you want to pass `None` for the
adjustments, which will cause the scrolled window to create them for you.

This code worked in the previous version and furthermore is doing what is suggested in the documentation -- passing None for the two adjustment arguments. Why is there a complaint about IsA when those argument types are both Option<&P>, where P implements the IsA trait, when None is an Option? That's where I run out of ideas.

Help would be appreciated.

/Don Allen

I believe you're getting this error because the compiler needs to know exactly what the type of those Nones are - remember, Option<T> is generic over T, whether or not it actually contains a value. I suspect a change to the gtk-rs API has made it so the compiler is no longer able to infer the type.

In one of the examples, they use gtk::NONE_ADJUSTMENT, which is a constant None with a type annotation. Because the type is specified, this compiles fine.

Alternatively, I think you could do None as Option<&Adjustment>, but that's just as much typing!

Thank you.

1 Like

Yuck. I'd call that an API bug in GTK. You should never take an Option<A> for a generic type parameter A, due to exactly this issue.

Edit: Okay, so gtk::NONE_ADJUSTMENT exists. At least they're aware of the issue...

1 Like

Except the documentation is misleading, suggesting you usually want to pass 'None' for the adjustment arguments, which is what I did. No mention of NONE_ADJUSTMENT, which is what the documentation ought to suggest, to keep people from encountering this issue. Their being a "aware of the issue" doesn't help users. I will post an issue with them.

1 Like

The fact that it used to work makes me think this is just an instance of docs being out of date, rather than outright bad. I'm sure they'll appreciate being made aware of it!

1 Like

Thank you! Was stuck on this for too long...