derive_more icon indicating copy to clipboard operation
derive_more copied to clipboard

`derive(derive_more::From)` doesn't foward properly

Open CGMossa opened this issue 4 years ago • 2 comments

My case is that I want something equivalent to this:

#[derive(Debug, Clone, Copy)]
pub struct ReplacementParameter(Probability);

impl From<ReplacementParameter> for f64 {
    fn from(value: ReplacementParameter) -> Self {
        value.0.into()
    }
}

Since Probability implements TryFrom<f64>. But derive_more is failing on that, even if I use #[from(forward)].

What am I missing and maybe what is derive_more missing here?

CGMossa avatar Jun 17 '21 22:06 CGMossa

You're mixing From and TryFrom in this issue, which are different traits.

Can you share the a full code sample that you tried? (I don't see the definition of Probability) What do you want to be able to do/what exact implementation do you expect to be generated?

On Fri, 18 Jun 2021, 00:27 CGMossa, @.***> wrote:

My case is that I want something equivalent to this:

#[derive(Debug, Clone, Copy)]pub struct ReplacementParameter(Probability); impl From<ReplacementParameter> for f64 { fn from(value: ReplacementParameter) -> Self { value.0.into() } }

Since Probability implements TryFrom. But derive_more is failing on that, even if I use #[from(forward)].

What am I missing and maybe what is derive_more missing here?

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/JelteF/derive_more/issues/166, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAI3YJTZBZUJF4SM3JT3DLDTTJZD5ANCNFSM464RHMEQ .

JelteF avatar Jun 17 '21 22:06 JelteF

Also what's the compile error that you are getting?

On Fri, 18 Jun 2021, 00:27 CGMossa, @.***> wrote:

My case is that I want something equivalent to this:

#[derive(Debug, Clone, Copy)]pub struct ReplacementParameter(Probability); impl From<ReplacementParameter> for f64 { fn from(value: ReplacementParameter) -> Self { value.0.into() } }

Since Probability implements TryFrom. But derive_more is failing on that, even if I use #[from(forward)].

What am I missing and maybe what is derive_more missing here?

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/JelteF/derive_more/issues/166, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAI3YJTZBZUJF4SM3JT3DLDTTJZD5ANCNFSM464RHMEQ .

JelteF avatar Jun 17 '21 22:06 JelteF

@CGMossa forwarding is possible if f64 implements From<Probability>. The current syntax on master and 1.0.0-beta.* releases would be:

#[derive(Clone, Copy, Debug, Into)]
#[into(f64)]
pub struct ReplacementParameter(Probability);

However, if f64 implements only TryFrom<Probability>, you should use TryInto instead, which, at the moment, doesn't support granular types, though (tracked by https://github.com/JelteF/derive_more/issues/98).

I'm closing this for now. Feel free to reopen or ping, if we haven't understood your use case and the desired feature correctly.

tyranron avatar Dec 11 '23 13:12 tyranron

Err sorry for not following this or closing it. It has been too long since this, and I've recently recreated this example, without running into this hiccup.

CGMossa avatar Dec 11 '23 13:12 CGMossa