-
-
Notifications
You must be signed in to change notification settings - Fork 14
Add support for derive Into<Target>
#164
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
base: main
Are you sure you want to change the base?
Conversation
It is supported. I think it just needs to do the same thing as any other core::convert traits: https://github.com/taiki-e/auto_enums/blob/main/src/derive/core/convert.rs |
|
You're right, I was too focused on doing I tried that approach with great success regarding codegen. but no success regarding compilation #[auto_enum(Into)]
fn a(x: u8) -> impl Into<u64> {
match x {
0 => 8_u8,
1 => 16_u16,
2 => 32_u32,
_ => false
}
}generates fn a(x: u8) -> impl Into<u64> {
#[allow(non_camel_case_types)]
enum __Enum6219237852123227426<__Variant0, __Variant1, __Variant2, __Variant3> {
__Variant0(__Variant0),
__Variant1(__Variant1),
__Variant2(__Variant2),
__Variant3(__Variant3),
}
#[automatically_derived]
impl<__Variant0, __Variant1, __Variant2, __Variant3, __T> ::core::convert::Into<__T>
for __Enum6219237852123227426<__Variant0, __Variant1, __Variant2, __Variant3>
where
__Variant0: ::core::convert::Into<__T>,
__Variant1: ::core::convert::Into<__T>,
__Variant2: ::core::convert::Into<__T>,
__Variant3: ::core::convert::Into<__T>,
{
#[inline]
fn into(self) -> __T {
match self {
__Enum6219237852123227426::__Variant0(x) => {
<__Variant0 as ::core::convert::Into<__T>>::into(x)
}
__Enum6219237852123227426::__Variant1(x) => {
<__Variant1 as ::core::convert::Into<__T>>::into(x)
}
__Enum6219237852123227426::__Variant2(x) => {
<__Variant2 as ::core::convert::Into<__T>>::into(x)
}
__Enum6219237852123227426::__Variant3(x) => {
<__Variant3 as ::core::convert::Into<__T>>::into(x)
}
}
}
}
match x {
0 => __Enum6219237852123227426::__Variant0(8_u8),
1 => __Enum6219237852123227426::__Variant1(16_u16),
2 => __Enum6219237852123227426::__Variant2(32_u32),
_ => __Enum6219237852123227426::__Variant3(false),
}
}Which triggers a compile error Because the enum itself could replace On a nicer note this experimentation gave me hope for a used code genpub(crate) mod into {
use crate::derive::prelude::*;
pub(crate) const NAME: &[&str] = &["Into"];
pub(crate) fn derive(_cx: &Context, data: &Data) -> Result<TokenStream> {
Ok(derive_trait(data, &parse_quote!(::core::convert::Into), None, parse_quote! {
trait Into<__T> {
#[inline]
fn into(self) -> __T;
}
}))
}
} |
89732f8 to
7a1ac6a
Compare
7a1ac6a to
d25c91a
Compare
84d6d31 to
8754724
Compare
|
Still need to update the changelog and see if i missed some docs but the implementation looks good to me |
|
Out of curiosity I tried a Detailsfn a(x: u8) -> impl Into<u64> {
enum __Enum6219237852123227426<
__Variant0,
__Variant1,
__Variant2,
__Variant3,
> {
__Variant0(__Variant0),
__Variant1(__Variant1),
__Variant2(__Variant2),
__Variant3(__Variant3),
}
impl<__Variant0, __Variant1, __Variant2, __Variant3, __T>
::core::convert::From<
__Enum6219237852123227426<
__Variant0,
__Variant1,
__Variant2,
__Variant3,
>,
> for __T
where
__T: ::core::convert::From<__Variant0>,
__T: ::core::convert::From<__Variant1>,
__T: ::core::convert::From<__Variant2>,
__T: ::core::convert::From<__Variant3>,
{
#[inline]
fn from(
enum_value: __Enum6219237852123227426<
__Variant0,
__Variant1,
__Variant2,
__Variant3,
>,
) -> __T {
match enum_value {
__Enum6219237852123227426::__Variant0(x) => x.into(),
__Enum6219237852123227426::__Variant1(x) => x.into(),
__Enum6219237852123227426::__Variant2(x) => x.into(),
__Enum6219237852123227426::__Variant3(x) => x.into(),
}
}
}
match x {
0 => __Enum6219237852123227426::__Variant0(8_u8),
1 => __Enum6219237852123227426::__Variant1(16_u16),
2 => __Enum6219237852123227426::__Variant2(32_u32),
_ => __Enum6219237852123227426::__Variant3(false),
}
}
fn main() {
let a: u64 = a(12).into();
println!("{a}");
}But this seems to make rustc go crazy as I never get any compile error but also no program, no compilation, rust-analyzer is also unresponsive xD |
411b468 to
52983b6
Compare
1a35043 to
e51d38a
Compare
|
Hello @taiki-e I rebased the PR, I think it's ready to be reviewed and merged 👍 |
taiki-e
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, could you add a test case to tests/auto_enum.rs to check that this is actually working?
fb7b2ca to
a960b06
Compare
|
We good 👍 @taiki-e |
|
Could you add a test case to tests/auto_enum.rs to check that this is actually working? This PR currently has tests for cases where compilation fails, but not for cases where compilation succeeds. |
af71d88 to
b28c0ab
Compare
|
Sorry about that, took care of the other comments but managed to forgot that one. |
|
Comes from the |
cae6bf7 to
60bb130
Compare
|
Ok, I think I got it, I encourage you to double/triple check the |
60bb130 to
c60cddf
Compare
|
@taiki-e ? |
c60cddf to
232d026
Compare
|
Just saw the clippy error, should be fixed |
Fixes #163
Currently a WIP, the implementation works but isn't pretty nor clean.
Refactor in progress, mostly moving code around, adding the feature gate, add tests, remove the
'convert'as a default featureetc...
If you already have suggestions @taiki-e I'll gladly read them !
Especially around the manual use of
derive_utils::EnumImpl, I thinkderive_utilsdoesn't handle generic traits for now right ?