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

Adds safe signed->unsigned, unsigned->signed casts #188

Merged
merged 1 commit into from
Apr 9, 2025

Conversation

Ozaq
Copy link
Contributor

@Ozaq Ozaq commented Apr 2, 2025

Adds new 'into_unsigned()' and 'into_signed' type casts that throw on conversion from a unrepresentable value in the target representation.

@simondsmart
Copy link
Contributor

Looks nice. What is the use case you have in mind?

@codecov-commenter
Copy link

codecov-commenter commented Apr 2, 2025

Codecov Report

Attention: Patch coverage is 97.36842% with 2 lines in your changes missing coverage. Please review.

Project coverage is 64.69%. Comparing base (ac5300a) to head (1841cb3).

Files with missing lines Patch % Lines
src/eckit/utils/SafeCasts.h 75.00% 2 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           develop     #188      +/-   ##
===========================================
+ Coverage    64.64%   64.69%   +0.04%     
===========================================
  Files         1105     1107       +2     
  Lines        55988    56064      +76     
  Branches      4234     4235       +1     
===========================================
+ Hits         36193    36269      +76     
  Misses       19795    19795              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link

github-actions bot commented Apr 2, 2025

Private downstream CI failed.
Workflow name: private-downstream-ci
View the logs at https://github.com/ecmwf/private-downstream-ci/actions/runs/14230629088.

Copy link

github-actions bot commented Apr 2, 2025

Private downstream CI failed.
Workflow name: private-downstream-ci-hpc
View the logs at https://github.com/ecmwf/private-downstream-ci/actions/runs/14231269229.

@Ozaq
Copy link
Contributor Author

Ozaq commented Apr 3, 2025

Looks nice. What is the use case you have in mind?

I am reading a signed value in Hermes (which should have been a unsigned value), and since I am receiving the value form the network I want to ensure the service is safely converting the value, or raise an error that can be handled gracefully.

@wdeconinck wdeconinck changed the title Adds safe signed->unsigned, unsigne->signed casts Adds safe signed->unsigned, unsigned->signed casts Apr 3, 2025
Copy link
Member

@wdeconinck wdeconinck left a comment

Choose a reason for hiding this comment

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

I think this will be very useful!

@wdeconinck
Copy link
Member

wdeconinck commented Apr 3, 2025

Note, the unit test fails compilation for rocky-8.6.
https://github.com/ecmwf/eckit/actions/runs/14229901435/job/39878181222?pr=188#step:3:3872

/opt/actions-runner/work/_work/eckit/eckit/tests/utils/test_safe_casts.cc:20:51: error: expected ')'
     20 |     EXPECT_EQUAL(into_unsigned(int8_t{5}), uint8_t{5});
        |                                                   ^

Sometimes wrapping the argument in parentheses could work around in my experience. Worth a try:

EXPECT_EQUAL(into_unsigned(int8_t{5}), (uint8_t{5}));

@pmaciel
Copy link
Member

pmaciel commented Apr 3, 2025

i like this PR :-)

But I would like to raise the discussion, shouldn't this be integrated with eckit::Translator ? It serves a similar purpose, and we'de benefit from integrating this PR's more-natural interface into the more cumbersome Translator operator()(...)

@Ozaq
Copy link
Contributor Author

Ozaq commented Apr 3, 2025

i like this PR :-)

But I would like to raise the discussion, shouldn't this be integrated with eckit::Translator ? It serves a similar purpose, and we'de benefit from integrating this PR's more-natural interface into the more cumbersome Translator operator()(...)

I haven't even looked into translator... (or was particularly aware of its existence) Let me have a look and i'll get back to you.

@simondsmart
Copy link
Contributor

Note that we have already added the translate() function which somewhat improves the Translator functionality.

@Ozaq Ozaq force-pushed the feature/safe-casts branch from e2cb32a to ae4c9eb Compare April 4, 2025 20:15
@Ozaq
Copy link
Contributor Author

Ozaq commented Apr 4, 2025

i like this PR :-)

But I would like to raise the discussion, shouldn't this be integrated with eckit::Translator ? It serves a similar purpose, and we'de benefit from integrating this PR's more-natural interface into the more cumbersome Translator operator()(...)

Ok so I had some time to look into the translator. I think this should not become part of the translator. All specializations in Translator.h deal with serialization or deserialization (one of FROM/TO are std::string). While it could be integrated into translator just from looking at the interface I don't think it would make for nice semantics.

@Ozaq Ozaq force-pushed the feature/safe-casts branch from ae4c9eb to 4bdfd93 Compare April 4, 2025 20:34
Copy link

github-actions bot commented Apr 4, 2025

Private downstream CI succeeded.
Workflow name: private-downstream-ci
View the logs at https://github.com/ecmwf/private-downstream-ci/actions/runs/14274524976.

1 similar comment
Copy link

github-actions bot commented Apr 4, 2025

Private downstream CI succeeded.
Workflow name: private-downstream-ci
View the logs at https://github.com/ecmwf/private-downstream-ci/actions/runs/14274524976.

Copy link

github-actions bot commented Apr 4, 2025

Private downstream CI succeeded.
Workflow name: private-downstream-ci-hpc
View the logs at https://github.com/ecmwf/private-downstream-ci/actions/runs/14274896755.

@Ozaq
Copy link
Contributor Author

Ozaq commented Apr 6, 2025

Can we move on with this as it is now or do you have specific change requests?

@pmaciel
Copy link
Member

pmaciel commented Apr 6, 2025

Can we move on with this as it is now or do you have specific change requests?

Yes, please merge this with Translator (in the right place) -- otherwise we will start having multiple ways of doing the same thing

@wdeconinck
Copy link
Member

My guess is miscommunication. I suspect @pmaciel didn't mean to replace this functionality with eckit::Translator and he doesn't mean that you should be using eckit::Translator. Rather he means to refactor eckit::Translator to make use of this new functionality, if possible.

@pmaciel
Copy link
Member

pmaciel commented Apr 6, 2025

Yes indeed - to me the SafeCasts file is fine, it's just Translator should use this for its existing (and possibly missing) specialisations

@Ozaq
Copy link
Contributor Author

Ozaq commented Apr 7, 2025

+1 thank ms for the clarification! Will do!

Copy link
Member

@pmaciel pmaciel left a comment

Choose a reason for hiding this comment

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

After a call with @Ozaq we agreed to make a more modern solution, while providing a easy transition.

@pmaciel pmaciel force-pushed the feature/safe-casts branch from 4bdfd93 to 808aa73 Compare April 8, 2025 07:31
Copy link

github-actions bot commented Apr 8, 2025

Private downstream CI succeeded.
Workflow name: private-downstream-ci
View the logs at https://github.com/ecmwf/private-downstream-ci/actions/runs/14327896521.

1 similar comment
Copy link

github-actions bot commented Apr 8, 2025

Private downstream CI succeeded.
Workflow name: private-downstream-ci
View the logs at https://github.com/ecmwf/private-downstream-ci/actions/runs/14327896521.

Copy link

github-actions bot commented Apr 8, 2025

Private downstream CI succeeded.
Workflow name: private-downstream-ci-hpc
View the logs at https://github.com/ecmwf/private-downstream-ci/actions/runs/14329200187.

Copy link

github-actions bot commented Apr 8, 2025

Private downstream CI failed.
Workflow name: private-downstream-ci
View the logs at https://github.com/ecmwf/private-downstream-ci/actions/runs/14337360251.

Adds new 'into_unsigned()' and 'into_signed' type casts that throw on
conversion from a unrepresentable value in the target representation.
@Ozaq Ozaq force-pushed the feature/safe-casts branch from 808aa73 to 1841cb3 Compare April 9, 2025 11:49
Copy link

github-actions bot commented Apr 9, 2025

Private downstream CI succeeded.
Workflow name: private-downstream-ci
View the logs at https://github.com/ecmwf/private-downstream-ci/actions/runs/14357007979.

2 similar comments
Copy link

github-actions bot commented Apr 9, 2025

Private downstream CI succeeded.
Workflow name: private-downstream-ci
View the logs at https://github.com/ecmwf/private-downstream-ci/actions/runs/14357007979.

Copy link

github-actions bot commented Apr 9, 2025

Private downstream CI succeeded.
Workflow name: private-downstream-ci
View the logs at https://github.com/ecmwf/private-downstream-ci/actions/runs/14357007979.

Copy link

github-actions bot commented Apr 9, 2025

Private downstream CI succeeded.
Workflow name: private-downstream-ci-hpc
View the logs at https://github.com/ecmwf/private-downstream-ci/actions/runs/14358319108.

Copy link

github-actions bot commented Apr 9, 2025

Private downstream CI succeeded.
Workflow name: private-downstream-ci
View the logs at https://github.com/ecmwf/private-downstream-ci/actions/runs/14361076528.

2 similar comments
Copy link

github-actions bot commented Apr 9, 2025

Private downstream CI succeeded.
Workflow name: private-downstream-ci
View the logs at https://github.com/ecmwf/private-downstream-ci/actions/runs/14361076528.

Copy link

github-actions bot commented Apr 9, 2025

Private downstream CI succeeded.
Workflow name: private-downstream-ci
View the logs at https://github.com/ecmwf/private-downstream-ci/actions/runs/14361076528.

Copy link

github-actions bot commented Apr 9, 2025

Private downstream CI succeeded.
Workflow name: private-downstream-ci-hpc
View the logs at https://github.com/ecmwf/private-downstream-ci/actions/runs/14363166865.

Copy link

github-actions bot commented Apr 9, 2025

Private downstream CI succeeded.
Workflow name: private-downstream-ci-hpc
View the logs at https://github.com/ecmwf/private-downstream-ci/actions/runs/14364535722.

Copy link

github-actions bot commented Apr 9, 2025

Private downstream CI succeeded.
Workflow name: private-downstream-ci
View the logs at https://github.com/ecmwf/private-downstream-ci/actions/runs/14365064496.

2 similar comments
Copy link

github-actions bot commented Apr 9, 2025

Private downstream CI succeeded.
Workflow name: private-downstream-ci
View the logs at https://github.com/ecmwf/private-downstream-ci/actions/runs/14365064496.

Copy link

github-actions bot commented Apr 9, 2025

Private downstream CI succeeded.
Workflow name: private-downstream-ci
View the logs at https://github.com/ecmwf/private-downstream-ci/actions/runs/14365064496.

@pmaciel pmaciel merged commit 79796d6 into develop Apr 9, 2025
237 of 243 checks passed
@pmaciel pmaciel deleted the feature/safe-casts branch April 9, 2025 19:42
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.

5 participants