dhardy
July 27, 2021, 10:57am
1
We can already use macros for a "dummy implementation" of an expression:
fn do_something_complex() -> T {
todo!()
}
Now and again I've wanted a "dummy trait object". So:
trait T {
type Q;
fn ask(q: &Q) -> bool;
}
struct S;
dummy_impl!(T<Q = ()> for S);
// generates:
// impl T for S {
// type Q = ();
// fn ask(q: &Q) -> bool { todo!() }
// }
Here we specify Q = (); in this example it is not strictly necessary but in cases where the associated type has trait bounds it may be.
The biggest issue: macros can't do this due to lack of introspection for the trait T (even with proc-macros).
So, questions:
is such a thing possible with compiler built-ins?
is it desirable enough to be worth it?
should it be exposed in std?
Motivation:
writing tests on bounded generic functions may require a concrete type
writing the impl by hand can be tedious, especially if the trait is frequently modified
While I don't usually suggest "just use an IDE", it seems like, in this case, this might be the right solution?
3 Likes
H2CO3
July 27, 2021, 12:35pm
3
Yes, probably.
No, probably.
Doesn't copy-pasting solve this, even if you don't use an IDE?
1 Like
dhardy
July 27, 2021, 12:39pm
4
In this particular case, I wanted to generate a dummy trait object for use in a doc test demonstrating usage that would be completely unrelated if not for a type bound. Wasting (hidden) lines on the doc-test and having to update them should the underlying trait change (likely) is undesirable.
Not a big use-case, but copy+paste / IDE auto-complete don't actually solve it.
For that sepecific case, the following works
/// ```
/// # fn give_me_T<S: T>(S: S) {
/// your docs here
/// # }
/// ```
3 Likes
system
Closed
October 25, 2021, 2:55pm
6
This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.