I've been trying many things from nested loops and sequential duplicated nested loops to this:
pub fn values_to_references<'a>(values_vector: Vec<String>) -> Vec<&'a gtk::ToValue> {
values_vector
.into_iter()
.map(| item: String | &item.to_value() as &ToValue)
.collect()
}
But whatever I try, the compiler always tells me that the lifetime of item
is too short:
borrowed value must be valid for the lifetime 'a as defined on the body at 87:89...
borrowed value does not live long enough (does not live long enough)
I need to convert to those references in order to add String
s to a gtk::TreeModel
(gtk::TreeStore
), which will balk at anything other than that it seems. All examples I could find use references to static strings, which are known at compile time. That's not very useful. My strings come from a JSON file and cannot be known at compile time.
I want to be able to do the following with the gtk::TreeModel
:
model.insert_with_values(
None,
None,
&[0, 1, 2, 3],
to_value_items.as_slice());
How can I achieve this?
Example Code
More code from my attempts to add stuff to a gtk::TreeStore
or gtk::TreeModel
:
pub fn add_to_tree_store(tree_store: &TreeStore, row: Vec<String>) {
tree_store.insert_with_values(
None,
None,
&[0, 1, 2, 3],
VocTreeView::strings_to_ampersand_str(row)
.iter()
.map(|x| x as &ToValue)
.collect());
}
pub fn strings_to_ampersand_str<'res_ref>(values_vector: Vec<String>) -> Vec<&'res_ref str> {
let append_values: Vec<_> = values_vector.iter().map(|x| &x[..]).collect();
append_values
}
This fails at collect()
:
a collection of type `&[>k::ToValue]` cannot be built from an iterator over elements of type `>k::ToValue` [E0277]
the trait bound `&[>k::ToValue]: std::iter::FromIterator<>k::ToValue>` is not satisfied (the trait `std::iter::FromIterator<>k::ToValue>` is not implemented for `&[>k::ToValue]`) [E0277]
Seems the same problem is again biting me.
Example Code 2
pub fn add_to_tree_store(tree_store: &TreeStore, row: Vec<String>) {
tree_store.insert_with_values(
None,
None,
&[0, 1, 2, 3],
VocTreeView::values_to_references(&row)
.iter()
.map(|x| x as >k::ToValue)
.collect());
}
pub fn values_to_references(values_vector: &[String]) -> Vec<>k::ToValue> {
values_vector
.into_iter()
.map(|item| item as >k::ToValue)
.collect()
}
This runs into an error at x
inside map and collect
as well:
At x
:
required for the cast to the object type `gtk::ToValue` [E0277]
required because of the requirements on the impl of `gtk::ToValue` for `>k::ToValue` [E0277]
required because of the requirements on the impl of `glib::value::SetValue` for `>k::ToValue` [E0277]
the trait bound `gtk::ToValue: glib::value::SetValue` is not satisfied (the trait `glib::value::SetValue` is not implemented for `gtk::ToValue`) [E0277]
At collect
:
a collection of type `&[>k::ToValue]` cannot be built from an iterator over elements of type `>k::ToValue` [E0277]
the trait bound `&[>k::ToValue]: std::iter::FromIterator<>k::ToValue>` is not satisfied (the trait `std::iter::FromIterator<>k::ToValue>` is not implemented for `&[>k::ToValue]`) [E0277]