Hey thanks for the suggestion. When I try to implement however, I get:
error[E0599]: no method named `iter` found for struct `std::collections::btree_map::Values<'_, std::string::String, f32>` in the current scope
--> src/score_map.rs:17:33
|
17 | self.our_tests.values().iter().copied().chain(
| ^^^^ method not found in `std::collections::btree_map::Values<'_, std::string::String, f32>`
I tried removing .iter
, since I believe BTreeMap::values()
returns a Value<K, V>
which implements Iterator
:
pub fn values(&self) -> impl Iterator<Item = f32> {
self.our_tests.values().copied().chain(
[self.line_coverage, self.branch_coverage, self.their_tests]
.iter()
.copied(),
)
}
Now I get
error: cannot infer an appropriate lifetime
--> src/score_map.rs:17:24
|
16 | pub fn values(&self) -> impl Iterator<Item = f32> {
| ------------------------- this return type evaluates to the `'static` lifetime...
17 | self.our_tests.values().copied().chain(
| -------------- ^^^^^^
| |
| ...but this borrow...
|
note: ...can't outlive the anonymous lifetime #1 defined on the method body at 16:5
--> src/score_map.rs:16:5
|
16 | / pub fn values(&self) -> impl Iterator<Item = f32> {
17 | | self.our_tests.values().copied().chain(
18 | | [self.line_coverage, self.branch_coverage, self.their_tests]
19 | | .iter()
20 | | .copied(),
21 | | )
22 | | }
| |_____^
help: you can add a bound to the return type to make it last less than `'static` and match the anonymous lifetime #1 defined on the method body at 16:5
|
16 | pub fn values(&self) -> impl Iterator<Item = f32> + '_ {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
In an effort to implement the suggestion of the compiler I changed the implementation to
pub fn values<'a>(&'a self) -> impl Iterator<Item = f32> + 'a {
self.our_tests.values().copied().chain(
[self.line_coverage, self.branch_coverage, self.their_tests]
.iter()
.copied(),
)
}
I thought this would mean that the returned Iterator
would have lifetime 'a
, same as self
. However, I get:
error[E0515]: cannot return value referencing temporary value
--> src/score_map.rs:17:9
|
17 | / self.our_tests.values().copied().chain(
18 | | [self.line_coverage, self.branch_coverage, self.their_tests]
| | ------------------------------------------------------------ temporary value created here
19 | | .iter()
20 | | .copied(),
21 | | )
| |_________^ returns a value referencing data owned by the current function
I think I am out of my depth at this point.