aikalant / capnp_conv

MIT License
11 stars 3 forks source link

Cannot use generics with capnp_conv due to trait bound errors #4

Open hqdncw opened 1 month ago

hqdncw commented 1 month ago

Description

I'm running into issues when trying to use generics with capnp_conv.

Schema

My schema is defined as follows:

struct Map(Key, Value) {
  entries @0 :List(Entry);

  struct Entry {
    key @0 :Key;
    value @1 :Value;
  }
}

struct ErrorInfo {
  data @0 :Map(Text, Text);
}

Attempted Rust implementation

Here's my attempt at converting the schema to Rust:

#[capnp_conv(schema_capnp::map::entry)]
pub struct MapEntry<K, V> {
    pub key: K,
    pub value: V,
}

#[capnp_conv(schema_capnp::map)]
pub struct Map<K, V> {
    pub entries: Vec<MapEntry<K, V>>,
}

#[capnp_conv(schema_capnp::error_info)]
pub struct ErrorInfo {
    pub data: Map<String, String>,
}

However, the compiler raises the following errors:

Diagnostics:
1. the method `write` exists for struct `Map<String, String>`, but its trait bounds were not satisfied
   items from traits can only be used if the trait is implemented and in scope
   the following traits define an item `write`, perhaps you need to implement one of them:
   candidate #1: `async_std::io::WriteExt`
   candidate #2: `capnp_conv::Writable`
   candidate #3: `futures_lite::AsyncWriteExt`
   candidate #4: `futures_lite::AsyncWriteExt`
   candidate #5: `futures_util::io::AsyncWriteExt`
   candidate #6: `itoa::private::Sealed`
   candidate #7: `std::hash::Hasher`
   candidate #8: `std::io::Write`
   candidate #9: `tokio::io::AsyncWriteExt` [E0599]
2. the function or associated item `read` exists for struct `Map<String, String>`, but its trait bounds were not satisfied
   items from traits can only be used if the trait is implemented and in scope
   the following traits define an item `read`, perhaps you need to implement one of them:
   candidate #1: `async_std::io::ReadExt`
   candidate #2: `capnp::io::Read`
   candidate #3: `capnp_conv::Readable`
   candidate #4: `futures_lite::AsyncReadExt`
   candidate #5: `futures_lite::AsyncReadExt`
   candidate #6: `futures_util::io::AsyncReadExt`
   candidate #7: `object::read::read_ref::ReadRef`
   candidate #8: `sha1::digest::XofReader`
   candidate #9: `std::io::Read`
   candidate #10: `tokio::io::AsyncReadExt` [E0599]
aikalant commented 4 weeks ago

It looks like Text/Data/List's are, though pointer types, and therefore valid capnp generic types, not supported currently by capnp_conv.

You can wrap your strings in structs as a workaround for now, but I think to fully support this will require implementing readable/writable for strings/vecs.