Comments (9)
@dtolnay In #54 I basically implemented what we discussed here. Did you have a particular use case in mind? If so, could you test it with that patch? If it works, we can close this issue. If you didn't have any particular thing in mind, I would probably close it without further testing.
from auto_impl.
Nice idea! Implementing this is fairly easy. But I noticed a problem: what should happen if the trait has a method with self
receiver? Then the generated impl block with ?Sized
bounds causes an "the size for values of type T
cannot be known at compilation time" error.
So should we just add ?Sized
bounds whenever possible? And just don't add them if it's impossible due to a method or so? Or let the user configure that somehow? But configuration requires new syntax and makes the crate more complicated to use. Any ideas?
from auto_impl.
Good catch -- when there are self-by-value methods then the impl shouldn't have a ?Sized
bound.
from auto_impl.
My question was rather about: if we conditionally add the : ?Sized
bound, that might be surprising for the user of the library. As in: they use a trait with trait objects, add a "self by value" method to the trait and suddenly auto_impl
does something different and their stuff doesn't work anymore. Or is such a situation unlikely?
Also we should probably ignore "self by value" methods which are marked with where Self: Sized
.
from auto_impl.
So I guess there are a few different directions we could take:
- Silently add
?Sized
bounds when they're possible - Add another container type, like
&dyn
that implies&
and will fail if there are by-valueself
receivers - Allow custom bounds like
T: ?Sized
to be added by consumers
Personally, I think 1 (and maybe 2 as well?) would be ok. The argument about it being surprising is totally reasonable, but I think the fail wagon would ride like this: you define your trait as object safe and #[auto_impl(&)]
, you depend on it being object-safe and it works, you change your trait so that it's no longer object safe and you start getting compile errors. In that case I don't think the cause and effect are too far apart. But that's where 2 might help if you don't just want it to be a coincidence that your trait is auto-implemented for dyn Trait
.
from auto_impl.
(I'm kind of conflating object safety with ?Sized
bounds, which isn't totally accurate but I think you get what I mean 🙂)
from auto_impl.
+1 for @KodrAus's option 1. If my code asks for auto_impl(&, Box)
then I would expect those impls to apply whenever possible; for ?Sized if possible, and not for ?Sized if that wouldn't be possible.
from auto_impl.
Ok, I'm convinced! Will probably implement option 1 in the next days.
from auto_impl.
My use case looks like https://github.com/auto-impl-rs/auto_impl/blob/90f17b69350204689730de3a5c4cc17d038d8d94/tests/compile-pass/trait_obj_immutable_self.rs so I think we can close this out. Thanks!
from auto_impl.
Related Issues (20)
- Emit warning on empty proxy type list HOT 3
- Make `Fn*` types work with some generic methods HOT 1
- Relicense as MIT/Apache-2 HOT 4
- Newtype support HOT 9
- Add `!` (never) as proxy type HOT 3
- auto_impl for trait objects? HOT 3
- Compiler error running tests
- Add proper compile-fail tests HOT 2
- Investigate using `syn-mid` HOT 3
- Proposal: throw away diag.rs module and use proc-macro-error crate HOT 3
- Auto-impl trait objects? HOT 8
- Unable to use auto_impl with async-trait HOT 17
- Use `Span::mixed_site` HOT 3
- `no_std` support HOT 2
- Support mutable function arguments HOT 1
- const generics support HOT 1
- Errors when using an associated type defined in a supertrait HOT 6
- Doesn't work with generic associated types HOT 1
- request for crates.io minor release with syn 2 upgrade HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from auto_impl.