Skip to content
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

Custom bound for parity-util-mem-derive #331

Closed
NikVolf opened this issue Feb 5, 2020 · 7 comments
Closed

Custom bound for parity-util-mem-derive #331

NikVolf opened this issue Feb 5, 2020 · 7 comments

Comments

@NikVolf
Copy link
Contributor

NikVolf commented Feb 5, 2020

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

@dvdplm
Copy link
Contributor

dvdplm commented Feb 5, 2020

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

@ordian
Copy link
Member

ordian commented Feb 5, 2020

You mean something like #255?

@NikVolf
Copy link
Contributor Author

NikVolf commented Feb 5, 2020

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
Copy link
Contributor Author

NikVolf commented Feb 5, 2020

You mean something like #255?

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

@ordian
Copy link
Member

ordian commented Feb 5, 2020

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

@NikVolf
Copy link
Contributor Author

NikVolf commented Feb 5, 2020

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

@ordian
Copy link
Member

ordian commented Feb 5, 2020

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

This is a long-standing issue in rustc rust-lang/rust#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 ordian closed this as completed Nov 29, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants