#deriving Custom trait for wrapper newtype

9 messages · Page 1 of 1 (latest)

lyric pawn
#

Hi there! I need a little help with something

I'm coding a game with godot-rust (gdnative 0.11.0) and i have a workspace with a rust struct

pub struct MyStruct {...}

a workspace with a godot interface

pub struct Interface;
pub struct Wrapper(MyStruct);

impl Interface {
pub fn leak_the_wrapper() -> WrapperType {...}
}

The problem I have is i want to derive a trait for my wrapper that i dont want to derive for MyStruct because better compilation time basically

but rust wont let me write this

#[derive(MyTrait)]
pub struct Wrapper(MyStruct);

Complaining that MyStruct doesn't implement the trait.

What's odd is that I could just Dervive the trait for my struct directly but i dont wanna do that (because i'm not sure how to) :/

Is there a nice solution to this that preferably doesn't involve cfg! ?

Thanks ❤️

cursive parcel
#

by "better compilation type"... what do you mean by that?

lyric pawn
#

better compilation time* sorry typo

cursive parcel
#

eh, it shouldnt matter

lyric pawn
#

it does for multiple reason,

  1. Sometimes i code on a potato (and it crashes if i use gdnative bc its a potato)
  2. The members of that workspace can be used without gdnative so there is no reason for me to derive the type there, i like to view the glue code to godot to something external to that workspace
proud musk
#

Well, this is complicated and depends on the trait you are trying to implement, its derive macro and the type.
Usually, most derive macros implement the trait for the outer type by using the implementation of the inner types. (Example Clone, Debug).
Sometimes, depending on the trait and type, there are a few hacks, like Deriving clone on Rc<T> even if T isn't clone works as Rc<T> implements Clone itself.
So as far as I know, you'd need a InnerWrapper<T> : MyStruct and then Wrapper<InnerWrapper<MyStruct>>, which doesn't sound too appealing (and pretty much the same thing you are trying to do right now).
So, yeah, your best bet, as far as I can tell, is to implement the trait by hand for MyStruct by hand. Looking at implementation for MyTrait for other types should help.

lyric pawn
#

Okay thanks ! Well I really dont feel confident enough to derive the trait by hand because another one of my structs is really complicated but I'll try using features to derive the trait conditionally , thank you !

proud musk
#

You could also look at the proc macro for #[derive(MyTrait)], that should help too.

#

Best of luck!