CasperLabs / datasize-rs

Heap memory usage estimation
Other
17 stars 13 forks source link

Derive fails for generic structure with trait bounds. #18

Closed alex-ozdemir closed 1 year ago

alex-ozdemir commented 1 year ago

First, thank you for building this library.

The following does not compile:

use datasize::DataSize;
#[derive(DataSize)]
struct WrapMap<K: Eq, V> {
    inner: std::collections::HashMap<K, V>
}

I get this compiler error (nightly 2023-03-17):

    Checking datasize-bug v0.1.0 (/home/aozdemir/repos/datasize-bug)
error: generic arguments must come before the first constraint
 --> src/lib.rs:3:23
  |
3 | struct WrapMap<K: Eq, V> {
  |                -----  ^ generic argument
  |                |
  |                constraint
  |
help: move the constraint after the generic argument
  |
3 | struct WrapMap<V, K : Eq> {
  |               ~~~~~~~~~~~

error[E0658]: associated type bounds are unstable
 --> src/lib.rs:3:16
  |
3 | struct WrapMap<K: Eq, V> {
  |                ^^^^^
  |
  = note: see issue #52662 <https://github.com/rust-lang/rust/issues/52662> for more information
  = help: add `#![feature(associated_type_bounds)]` to the crate attributes to enable

error[E0107]: struct takes 2 generic arguments but 1 generic argument was supplied
 --> src/lib.rs:3:8
  |
3 | struct WrapMap<K: Eq, V> {
  |        ^^^^^^^        - supplied 1 generic argument
  |        |
  |        expected 2 generic arguments
  |
note: struct defined here, with 2 generic parameters: `K`, `V`
 --> src/lib.rs:3:8
  |
3 | struct WrapMap<K: Eq, V> {
  |        ^^^^^^^ -      -
help: add missing generic argument
  |
3 | struct WrapMap<K: Eq, V, V> {
  |                        +++

error[E0229]: associated type bindings are not allowed here
 --> src/lib.rs:3:16
  |
3 | struct WrapMap<K: Eq, V> {
  |                ^^^^^ associated type not allowed here

Some errors have detailed explanations: E0107, E0229, E0658.
For more information about an error, try `rustc --explain E0107`.
error: could not compile `datasize-bug` (lib) due to 4 previous errors

Without the Eq bound, it does compile.

marc-casperlabs commented 1 year ago

Hello, thanks for the report. Could you try moving the trait bound to a where clause instead? IIRC that has been a workaround for us.

alex-ozdemir commented 1 year ago

Thanks, Marc.

That workaround does work for me. For posterity: deferring the trait bounds to the relevant impl also works.

marc-casperlabs commented 1 year ago

Glad to hear that :)