It seems like if you have a working generic function, you ought to be able to manually substitute in a type and get a working less-generic function. But in this case it doesn't work.
Below, query_generic
compiles fine. But when I manually substitute the specific type dyn U
for T
, the resulting less-generic function query_dyn
does not compile. I don't understand the error message. What's going on here?
trait Table<K> {
fn lookup<'view>(&'view self, key: &K) -> &'view Vec<String>;
}
trait U: Table<i32> + Table<u32> {}
fn query_generic<'view, A, T>(table: &'view T, key: &A) -> &'view Vec<String>
where
T: Table<A> + ?Sized,
{
<T as Table<A>>::lookup(table, key) // OK
}
fn query_dyn<'view, A>(table: &'view dyn U, key: &A) -> &'view Vec<String>
where
dyn U: Table<A>,
{
<dyn U as Table<A>>::lookup(table, key) // ERROR
}
Here's the full error message:
error[E0759]: `table` has lifetime `'view` but it needs to satisfy a `'static` lifetime requirement
--> src/lib.rs:11:58
|
7 | fn query_dyn<'view, A>(table: &'view dyn U, key: &A) -> &'view Vec<String>
| ------------ this data with lifetime `'view`...
...
11 | let f: fn(&'view dyn U, &A) -> &'view Vec<String> = <dyn U as Table<A>>::lookup;
| -^^^^^--------------------- ...is captured and required to live as long as `'static` here
error[E0759]: `table` has lifetime `'view` but it needs to satisfy a `'static` lifetime requirement
--> src/lib.rs:13:24
|
7 | fn query_dyn<'view, A>(table: &'view dyn U, key: &A) -> &'view Vec<String>
| ------------ this data with lifetime `'view`...
...
13 | Table::<A>::lookup(table, key) // ERROR
| ^^^^^ ...is captured here...
|
note: ...and is required to live as long as `'static` here
--> src/lib.rs:13:5
|
13 | Table::<A>::lookup(table, key) // ERROR
| ^^^^^^^^^^^^^^^^^^
error: aborting due to 2 previous errors
For more information about this error, try `rustc --explain E0759`.
error: could not compile `play-2021-08-17`