24
submitted 8 months ago by Zistack@lemmy.sdf.org to c/rust@programming.dev

I'm building up to a new idiom for dealing with generic numeric types and traits, but this is the first step. I made it more general than I probably needed for my purposes, but it seemed like a good idea to just solve the whole problem given that I was going to do much of the work anyways.

Credit to the hereditary crate for inspiration on how to do it.

I'm very new at this software publishing thing, so lemme know if I messed something up.

you are viewing a single comment's thread
view the rest of the comments
[-] Zistack@lemmy.sdf.org 3 points 8 months ago

I was actually not aware of the RFC or those other crates. Ambassador and portrait seem kindof similar in their overall approach to how they accomplish things, though portrait appears to be solving a very different problem. My crate allows for forwarding based on conversions and not just delegation to members, which appears to be new (and this turns out to be important for my use-case.). It doesn't have anything for dealing with code that isn't in traits, but I wasn't intending to solve that problem.

I absolutely see why it would be nice to put delegation in at the language level. Most libraries aren't going to want to annotate their trait definitions just for this (std included), and having the compiler take care of things solves not only that, but also the annotation data format version issues that come with using proc-macros to do it.

It's a bit weird, though, because my conversion forwarding is actually strictly more powerful than delegation in some ways (but a little less flexible - mixing them grants the best of both techniques). Conversion forwarding allows for traits like FromIterator to be forwarded automatically for wrappers on containers, for example. You can't do that with delegates. It feels to me like you'd want both if you added either one of them in. The issue with trying to put something like conversion forwarding in is that the compiler either needs to know about the conversion traits (From, Into, AsRef, and AsRefMut)(As I write this, I realize I may have made a mistake in my crate... can guess what it is?) or it would need to be told how to do the conversions, complete with all additional generic parameters and trait bounds that would be required in the trait implementation. That's either violating some important abstraction boundaries in the language tools, or just extremely verbose.

[-] BB_C@programming.dev 2 points 8 months ago

In case you missed it like me yesterday, a new RFC with an initial experimental implementation (tracking issue) is where things at now.

And we can add a third word to delegate and forward, which is reuse ;)

this post was submitted on 26 Feb 2024
24 points (100.0% liked)

Rust

5980 readers
79 users here now

Welcome to the Rust community! This is a place to discuss about the Rust programming language.

Wormhole

!performance@programming.dev

Credits

  • The icon is a modified version of the official rust logo (changing the colors to a gradient and black background)

founded 1 year ago
MODERATORS