Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve enum conversions #4397

Merged

Conversation

pwojcikdev
Copy link
Contributor

@pwojcikdev pwojcikdev commented Jan 25, 2024

There was a lot of handwritten code doing simple enum conversions, and thanks to magic enum library this is unnecessary. Both magic_enum::enum_cast (...) and magic_enum::enum_name (...) are constexpr, so depending on how good the compiler is, the code shouldn't be slower than what we had before.


debug_assert (false, "unknown election behavior");
return {};
auto value = magic_enum::enum_cast<nano::stat::detail> (magic_enum::enum_name (behavior));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not convinced this is an improvement overall, although the code brevity is a nice thing.
If a new behaviour is added that doesn't exist as a detail then the problem will be silently ignored whereas, in the previous case, the compiler would complain that not all values were handled by the switch case statement.
I am not objecting to the change, just wondering what this gives us, other than a few less lines of very obvious code.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I hope you're joking... This used to be and still is one on the most annoying and unnecessary things to do in this codebase.

nano/node/network.cpp Show resolved Hide resolved
nano/node/transport/tcp_server.cpp Show resolved Hide resolved
dsiganos
dsiganos previously approved these changes Jan 25, 2024
ASSERT_FALSE (nano::to_string (nano::stat::type::_last).empty ());
ASSERT_NO_THROW (std::string{ nano::to_string (nano::stat::type::_last) });
ASSERT_EQ (nano::to_string (nano::stat::type::_last), "_last");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't this line do the job of the both lines above it?

Copy link
Contributor Author

@pwojcikdev pwojcikdev Jan 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if nano::to_string (nano::stat::type::_last) returns a malformed string view with size set to a random value? This seems to be fixed in the latest magic enum version but was a problem before. If assert_eq fails it's most likely going to print this large, malformed string. Byt here it will get caught sooner. Also, I added those lines so they follow a pattern.

@pwojcikdev pwojcikdev merged commit 9e23032 into nanocurrency:develop Jan 26, 2024
18 of 19 checks passed
@pwojcikdev pwojcikdev deleted the enum-conversion-improvements branch January 26, 2024 17:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants