Comments (2)
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 anError
Something like #[strum(try_from_repr(MyError, my_error_func))]
from strum.
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 ifstrum
supportsnostd
, 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)
- EnumDiscriminants: can't infer type HOT 3
- Getting the discriminant from an enum variant HOT 3
- Publish new release. HOT 4
- AsRefStr documentation incorrectly states 'static ref is returned HOT 2
- Publish Git tags HOT 2
- Implement `Iterator` for EnumTable HOT 6
- Customize error message for FromStr
- Documentation for `VariantArray` and `VariantNames` say their `VARIANTS` constants are arrays, but they're actually slices HOT 2
- Support for const_into_str to Enable Static String Conversion in Const Context
- Accessing the EnumTable type through the enum HOT 1
- Error updating to 0.26 - trait bound `std::iter::FusedIterator` is not satisfied HOT 1
- Itertools 0.13.0 and strum derive macro EnumIter break if both are used at the same time HOT 2
- strum-macros 0.26.3 breaks `no_std` build HOT 5
- Round tripping "{{}}" doesn't work with Display + EnumString in 0.26 HOT 2
- EnumProperty must be defined only once in the macro namespace of this module HOT 1
- Using `#[strum(to_string = "}")]` results in macro error HOT 3
- Fix itertools compatibility part 2. Release strum 0.26.3
- Is this expected behavior (combining EnumStrings with multiple serializes and ascii_case_insensitive with VariantNames)?
- `IntoStaticStr` interprets `to_string` literally HOT 2
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 strum.