You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Currently, converting a string-like value (e.g., String, &str) into an enum requires either manual matching or custom implementations in each use case. To enhance usability and provide a consistent conversion mechanism, I propose implementing From<T> for enums where T: AsRef<str>.
Motivation
Improved Developer Experience: This enhancement will allow developers to seamlessly convert string-like values into enum variants using the .into() method.
Flexibility: By utilizing T: AsRef<str>, the implementation will support multiple input types (String, &str, etc.) without duplicating code.
Standardization: It will encourage a consistent approach to enum conversion, reducing boilerplate code and improving maintainability.
Proposed Solution
Implement the From<T> trait for enums using the AsRef<str> constraint.
Provide a default matching mechanism that maps specific strings to enum variants.
For unsupported strings, the implementation should either:
(Option A) Panic with a clear error message (if using From).
(Option B) Return a Result<Self, Error> (if using TryFrom, which supports error handling).
Example
With From<T>:
#[derive(Debug)]enumMyEnum{VariantA,VariantB,VariantC,}impl<T>From<T>forMyEnumwhereT:AsRef<str>,{fnfrom(value:T) -> Self{match value.as_ref(){"VariantA" => MyEnum::VariantA,"VariantB" => MyEnum::VariantB,"VariantC" => MyEnum::VariantC,
_ => panic!("Invalid value for MyEnum"),}}}
Usage:
let value:MyEnum = "VariantA".into();println!("{:?}", value);// Output: VariantA
With TryFrom<T> (for safer conversion):
use std::convert::TryFrom;impl<T>TryFrom<T>forMyEnumwhereT:AsRef<str>,{typeError = &'staticstr;fntry_from(value:T) -> Result<Self,Self::Error>{match value.as_ref(){"VariantA" => Ok(MyEnum::VariantA),"VariantB" => Ok(MyEnum::VariantB),"VariantC" => Ok(MyEnum::VariantC),
_ => Err("Invalid value for MyEnum"),}}}
Usage:
let result:Result<MyEnum,_> = MyEnum::try_from("VariantA");match result {Ok(value) => println!("Converted: {:?}", value),Err(e) => println!("Error: {}", e),}
Impact
This implementation will significantly simplify the process of converting strings into enums, especially in projects where enums are frequently used for configuration or input validation.
Open Questions
Should the default implementation use From (panic on invalid input) or TryFrom (return an error)?
Should this feature include a macro for generating implementations across multiple enums?
The text was updated successfully, but these errors were encountered:
Description
Currently, converting a string-like value (e.g.,
String
,&str
) into anenum
requires either manual matching or custom implementations in each use case. To enhance usability and provide a consistent conversion mechanism, I propose implementingFrom<T>
for enums whereT: AsRef<str>
.Motivation
.into()
method.T: AsRef<str>
, the implementation will support multiple input types (String
,&str
, etc.) without duplicating code.Proposed Solution
From<T>
trait for enums using theAsRef<str>
constraint.From
).Result<Self, Error>
(if usingTryFrom
, which supports error handling).Example
With
From<T>
:Usage:
With
TryFrom<T>
(for safer conversion):Usage:
Impact
This implementation will significantly simplify the process of converting strings into enums, especially in projects where enums are frequently used for configuration or input validation.
Open Questions
From
(panic on invalid input) orTryFrom
(return an error)?The text was updated successfully, but these errors were encountered: