Unable to use Self inside the struct definition for #[derive(Item)]
๐ 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)
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.
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.