Closed
Description
Given the following code: Playground
struct PgConnection;
pub trait PgMetadataLookup {
fn cast_with_callback<R: ?Sized>(&mut self, c: &dyn Fn(&mut Self) -> &mut R) -> &mut R
where Self: Sized;
}
pub trait MultiMetadataLookup {
}
impl PgMetadataLookup for PgConnection {
fn cast_with_callback<R: ?Sized>(&mut self, c: &dyn Fn(&mut Self) -> &mut R) -> &mut R where Self: Sized{
c(self)
}
}
impl<T> MultiMetadataLookup for T where T: PgMetadataLookup {
}
fn bar(_a: &mut dyn MultiMetadataLookup) {
}
fn foo(conn: &mut PgConnection) -> &mut dyn PgMetadataLookup {
conn
}
fn main() {
let mut conn = PgConnection;
let mut pg_lookup = foo(&mut conn);
bar(pg_lookup.cast_with_callback(&|s| s))
}
The current output is:
error: the `cast_with_callback` method cannot be invoked on a trait object
--> src/main.rs:34:19
|
5 | where Self: Sized;
| ----- this has a `Sized` requirement
...
34 | bar(pg_lookup.cast_with_callback(&|s| s))
| ^^^^^^^^^^^^^^^^^^
|
= note: you need `&dyn PgMetadataLookup` instead of `&mut dyn PgMetadataLookup`
Ideally the output should not include a not telling me to use &dyn PgMetadataLookup
instead of a &mut dyn PgMetadataLookup
as this is misleading. In addition following this suggestion only changes the note of the error message such that it now says "note: you need &mut dyn PgMetadataLookup
instead of &dyn PgMetadataLookup
" which is also wrong.