Giter Club home page Giter Club logo

Comments (9)

LukasKalbertodt avatar LukasKalbertodt commented on July 19, 2024 1

@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.

LukasKalbertodt avatar LukasKalbertodt commented on July 19, 2024

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.

dtolnay avatar dtolnay commented on July 19, 2024

Good catch -- when there are self-by-value methods then the impl shouldn't have a ?Sized bound.

from auto_impl.

LukasKalbertodt avatar LukasKalbertodt commented on July 19, 2024

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.

KodrAus avatar KodrAus commented on July 19, 2024

So I guess there are a few different directions we could take:

  1. Silently add ?Sized bounds when they're possible
  2. Add another container type, like &dyn that implies & and will fail if there are by-value self receivers
  3. 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.

KodrAus avatar KodrAus commented on July 19, 2024

(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.

dtolnay avatar dtolnay commented on July 19, 2024

+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.

LukasKalbertodt avatar LukasKalbertodt commented on July 19, 2024

Ok, I'm convinced! Will probably implement option 1 in the next days.

from auto_impl.

dtolnay avatar dtolnay commented on July 19, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.