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

Unable to use Self inside the struct definition for #[derive(Item)] #117

Open
Veetaha opened this issue Jul 14, 2020 · 1 comment
Open

Comments

@Veetaha
Copy link
Contributor

Veetaha commented Jul 14, 2020

🐛 Bug description

Given this code:

use dynomite::Item;
use std::marker::PhantomData;

#[derive(Item, Clone)]
struct Book {
    #[dynomite(partition_key)]
    pub partition_key: Id<Self>,
    pub name: String,
}

trait MyTrait {}
impl MyTrait for Book {}

#[derive(PartialOrd, PartialEq, Clone, Debug)]
struct Id<T: MyTrait>(String, PhantomData<T>);

impl<T: MyTrait> dynomite::Attribute for Id<T> {
    fn into_attr(self: Self) -> dynomite::dynamodb::AttributeValue {
        todo!()
    }
    fn from_attr(value: dynomite::dynamodb::AttributeValue) -> Result<Self, dynomite::AttributeError> {
        todo!()
    }
}

but this code gives the following error:

Log
~/junk/rust-sandbox $ cargo check -p crate_foo
    Checking crate_foo v0.1.0 (/home/veetaha/junk/rust-sandbox/crate_foo)
error[E0277]: the trait bound `BookKey: MyTrait` is not satisfied
  --> crate_foo/src/main.rs:7:5
   |
7  |     pub partition_key: Id<Self>,
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `MyTrait` is not implemented for `BookKey`
...
15 | struct Id<T: MyTrait>(String, PhantomData<T>);
   |              ------- required by this bound in `Id`

error: aborting due to previous error

For more information about this error, try `rustc --explain E0277`.
error: could not compile `crate_foo`.

To learn more, run the command again with --verbose.
~/junk/rust-sandbox $ 

It looks like Id<Self> is copied into struct BookKey { ... } body but instead we should replace all Self occurences with the struct #[derive(Item)] is placed onto before copying it into BookKey.

🤔 Expected Behavior

This should work out of the box without compile errors.

The workaround is to use Book literally instead of Self

🌍 Your environment

dynomite version:
0.8.2
rustc version:
rustc 1.44.1 (c7087fe00 2020-06-17)

@Veetaha Veetaha changed the title Unable to use Self in Unable to use Self inside the struct definition for #[derive(Item)] Jul 14, 2020
@softprops
Copy link
Owner

It looks like Id is copied into struct BookKey { ... } body but instead we should replace all Self occurences with the struct #[derive(Item)] is placed onto before copying it into BookKey.

I need to parse this problem a bit. This sounds a bit like an edge case to me. I'm the majority of cases when you declare a field is a partition key the item key struct simply represents a copy of that field.

At the macro level there isn't really type information only syntax and I'm not sure how I would be able to infer something like this from syntax alone.

I'll think on this one.

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

2 participants