rust-unofficial / patterns

A catalogue of Rust design patterns, anti-patterns and idioms
https://rust-unofficial.github.io/patterns/
Mozilla Public License 2.0
7.84k stars 352 forks source link

Add Inner Trait pattern #355

Closed rbran closed 1 year ago

simonsan commented 1 year ago

I also like this one, I skimmed over it and it looks like a good addition to this repository. Maybe others, e.g. @neithernut and/or @pickfire can give it a read as well to leave some always valued feedback. So we can merge it next week. (Sorry for C&P, not much time right now.)

burdges commented 1 year ago

You'd avoid re-implementing the exposed methods if you require InnerCar : Car btw.

It's a useful pattern if you've very large crates or maybe some dyn InnerCar patterns. It'll also become more useful whenever specialization arrives.

Yet, if your project has many small crates, then you'd typically achieve this using free fns instead.

fn set_speed<C: Car>(car: &mut C, new_speed: f64) { ... }
fn get_acceleration<C: Car>(car: &mut C) -> f64 { ... }
simonsan commented 1 year ago

@rbran Could you please rebase on main, we just merged our refactoring efforts to support translations. Basically SUMMARY.md and the other markdown files are moved into ./src/**/{file}. Cheers.

rbran commented 1 year ago

The more that I think about this pattern, the less I like it. It fells too complex/edge-case for a pattern, or maybe is a simple adaptation/equivalent for an "abstract-class" made by someone that is too stuck with the OOP mentality.

simonsan commented 1 year ago

The more that I think about this pattern, the less I like it. It fells too complex/edge-case for a pattern, or maybe is a simple adaptation/equivalent for an "abstract-class" made by someone that is too stuck with the OOP mentality.

I understand the hesitation and value that feedback. I would say, that there are use cases, where you would like to use it, though. Maybe, this thought you have there is really something you could bring into a sentence as a warning when not to use it and which problems it can bring. I would say, that it's always better to explain these kinda things and let people gain that knowledge, than not documenting it at all and have a mess all over the place.

rbran commented 1 year ago

I'll rethink this, if I got a more concise idea for a pattern, I'll create a new PR.