Code Monkey home page Code Monkey logo

Comments (2)

Peternator7 avatar Peternator7 commented on July 20, 2024

I appreciate the sentiment; however, I am hesitant to make a change like this. I get a lot of issues asking for the same implementation of an existing macro, but with a different trait, or a different error type. They're all valid, but it makes configuration confusing, and it's difficult to pick one error type in particular that will make everyone happy.

Here's a playground link that shows why it's hard. The error type being anything other than zero-sized changes the size of the returned Result from 2 bytes to 3 (which gets padded up to 4). That's not much, and it might get inlined, but it is an observable difference that someone might care about so it's easier to generate the optimal code and then allow users to define a wrapper impl if they want to change the error handling.

If you're interested on working on this, I would accept a PR if the following were configurable.

  • The error type
  • The function/callback that turns a repr into an Error

Something like #[strum(try_from_repr(MyError, my_error_func))]

from strum.

jaskij avatar jaskij commented on July 20, 2024

I see what you mean about sizes. I personally don't see caring about the size of a Result<> as opposed to the enum itself, but someone might.

I would like to work on this, seems like the type of thing to get started with proc macros. Unfortunately, I do not have the capability to commit to working on it.

In the interest of further discussion (in case I or someone else does end up writing a PR in the future), some questions:

  • What about the actual implementation? Should it be separate, or depend on FromRepr? I don't remember if strum supports nostd, but if it does, those codebases are quite sensitive to code size.
  • If the implementations are to be separated, would you want common parts factored out of strum_macros::from_repr::from_repr_inner()? At a glance, at least getting the repr could be used in both macros.

Thinking on it further, while it does not align with the current API of strum, and would be a bit hacky, I'm willing to bet this could actually be done with a declarative macro.

from strum.

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.