Skip to content

uucore: deduplicate CharByte From implementations using macro#11473

Open
ppmpreetham wants to merge 2 commits intouutils:mainfrom
ppmpreetham:main
Open

uucore: deduplicate CharByte From implementations using macro#11473
ppmpreetham wants to merge 2 commits intouutils:mainfrom
ppmpreetham:main

Conversation

@ppmpreetham
Copy link

Summary

Replace repetitive From implementations for CharByte with a reusable macro.

This introduces a new macro impl_from_for_enum! in uucore::macros and uses it
to generate the existing From<char>, From<u8>, and From<&u8> implementations.

Motivation

The previous implementation manually defined multiple From impls for CharByte
that followed an identical pattern. This change removes duplication while keeping
the behavior unchanged.

The macro is designed to be reusable for similar enum patterns elsewhere in the codebase.

Changes

  • Added impl_from_for_enum! macro to uucore::macros
  • Replaced manual From implementations for CharByte with macro usage
  • No functional changes

Example

Before:

impl From<char> for CharByte { ... }
impl From<u8> for CharByte { ... }
impl From<&u8> for CharByte { ... }

After:

impl_from_for_enum!(CharByte:
    char => Char,
    u8 => Byte, ref
);

Notes

  • The macro optionally supports From<&T> via , ref
  • Maintains full compatibility with existing iterator usage (.map(CharByte::from))

Testing

  • No behavior changes; existing tests continue to pass

Checklist

  • Code compiles without warnings
  • Follows rustfmt
  • No clippy warnings
  • No functional changes

@sylvestre
Copy link
Contributor

Sorry but why? :)

@ppmpreetham
Copy link
Author

ppmpreetham commented Mar 24, 2026

It makes the code more readable, and removes redundant impl codes :)

@sylvestre
Copy link
Contributor

sorry but i am not sure it is worse it ? :)

@ppmpreetham
Copy link
Author

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