You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository has been archived by the owner on Jun 8, 2021. It is now read-only.
Now I would like to see an example for getting the content of a notebook page, changing it and adding it back to the notebook all in a type safe manner. The problem is that I get back a Option<Widget>, instead of whatever type I say I'll add to the Notebook. When I try to get back what I put in, obviously I know what it should be, but Rust seems not to know:
let the_tab: Option<Widget> = notebook.get_nth_page(4); // just get the widget which was added in the fifth tab of the notebook
Then I could to the following:
let label = match the_tab {
Some(widget) => widget.downcast::<Label>(),
None => // what to do here?
};
I found downcast in the autogenerated docs from cargo doc in the Cast trait, after searching for an hour or more how to cast something, but still the problem is not solved, because I don't know how to handle the None case. The label must have some simple type like Label, so that I can call all the functions which are implemented for that type and work with what I got from the Notebook.
I am still a beginner in Rust, but I don't know how to proceed here. I think I would have to pass a type at creation time of the Notebook, in order to let Rust know what comes back, but that would not change the interface to suddenly give me not only Option<Widget>, but the actual more specific contained type.
An example of how to deal with such things in gtk-rs would be of great use.
The text was updated successfully, but these errors were encountered:
As I understand you get None only if you try access to page that not exist,
so reaction only depend on what you wan't,
IMHO as this normally don't happened,
you can use .unwrap() or better .expect("message") instead match.
Same with result of .downcast::<Label>():
or you know what type you inserted here and use .expect,
or you need decide that to do.
If you adding different types to pages,
then you can check type with http://gtk-rs.org/docs/gtk/trait.Cast.html#method.is beforehand.
@EPashkin Thanks for mentioning the Cast is thingy. I'll probably use that somewhere and use expect at other times.
An idea is to give the notebook a generic type, which tells it what stuff one is adding as pages and then return that as pages, but for now the API does not have that.
You can add generic type that contains Notebook (and deref) and add your variant append_page and get_nth_page for it,
but IMHO when all pages same type is rare and other cases can be statically checked.
I saw the example at https://github.com/gtk-rs/examples/blob/master/src/bin/notebook.rs and it is great. I am using it in my own project and it showed me how to customize "Types" of Widgets.
Now I would like to see an example for getting the content of a notebook page, changing it and adding it back to the notebook all in a type safe manner. The problem is that I get back a
Option<Widget>
, instead of whatever type I say I'll add to the Notebook. When I try to get back what I put in, obviously I know what it should be, but Rust seems not to know:Then I could to the following:
I found
downcast
in the autogenerated docs fromcargo doc
in theCast
trait, after searching for an hour or more how to cast something, but still the problem is not solved, because I don't know how to handle theNone
case. Thelabel
must have some simple type likeLabel
, so that I can call all the functions which are implemented for that type and work with what I got from theNotebook
.I am still a beginner in Rust, but I don't know how to proceed here. I think I would have to pass a type at creation time of the
Notebook
, in order to let Rust know what comes back, but that would not change the interface to suddenly give me not onlyOption<Widget>
, but the actual more specific contained type.An example of how to deal with such things in
gtk-rs
would be of great use.The text was updated successfully, but these errors were encountered: