parity-common icon indicating copy to clipboard operation
parity-common copied to clipboard

Custom bound for parity-util-mem-derive

Open NikVolf opened this issue 4 years ago • 7 comments

Imagine the struct:

struct Transaction<B> {
  hash: B::Hash,
}

#[parity_util_mem::MallocSizeOf] will fail miserably there, because it will add something like that:

impl<B> MallocSizeOf for Transaction<B> where B: MallocSizeOf { ... }

what instead would be cool is be able to declare this:

#[derive(parity_util_mem::MallocSizeOf)]
struct Transaction<B> {
  #[custom_ty_bound]
  hash: B::Hash,
}

which expands to:

impl MallocSizeOf for Transaction<B> where B::Hash: MallocSizeOf { .. }

(so that concrete type used in structure got bounded, not generic argument)

if any #[custom_ty_bound] declared in the structure, default ones (B: MallocSizeOf) should not be added

NikVolf avatar Feb 05 '20 13:02 NikVolf

Not sure I follow, what does the derived impl look like with #[custom_ty_bound] in your example?

dvdplm avatar Feb 05 '20 13:02 dvdplm

You mean something like #255?

ordian avatar Feb 05 '20 14:02 ordian

Not sure I follow, what does the derived impl look like with #[custom_ty_bound] in your example?

Yep sorry, forgot to expand. Obviously, actual type in structure gets bounded, not generic argument. So:

impl<B> MallocSizeOf for Transaction<B> where B::Hash: MallocSizeOf { .. }

NikVolf avatar Feb 05 '20 14:02 NikVolf

You mean something like #255?

Not really. It has nothing to do with actual calculations, just with where section bounds generation.

NikVolf avatar Feb 05 '20 14:02 NikVolf

Maybe we can have something like this https://github.com/paritytech/parity-scale-codec/pull/40?

ordian avatar Feb 05 '20 14:02 ordian

Maybe we can have something like this paritytech/parity-scale-codec#40?

Yes, seems like the right direction. I don't really see the point of making stupid bounds on all generic arguments.

For example, this thing:

#[derive(MallocSizeOf)]
struct Foo<B> {
  member: PhantomData<B>
}

could go just without any bounds on B, because MallocSizeOf is implemented for any PhantomData

NikVolf avatar Feb 05 '20 14:02 NikVolf

I don't really see the point of making stupid bounds on all generic arguments.

This is a long-standing issue in rustc https://github.com/rust-lang/rust/issues/26925 that will hopefully be resolved with chalk, but it may take a while (as you can see the issue is 5 years old).

ordian avatar Feb 05 '20 15:02 ordian