khonsulabs / bonsaidb

A developer-friendly document database that grows with you, written in Rust
https://bonsaidb.io/
Apache License 2.0
1.01k stars 37 forks source link

Add Derive for Api #235

Closed ecton closed 2 years ago

ecton commented 2 years ago

The Api trait should be deriveable:

struct MyType;

impl Api for MyType {
  type Response = OtherType;
  type Error = bonsaidb::core::api::Infallible;

  fn name() -> ApiName {
    ApiName::new("authority", "name")
  }
}

Should be able to be derived as:

#[derive(Api)]
#[api(authority = "authority", name = "name", response = OtherType, error = Infallible]
struct MyType;

Response is required. Error should be optional. If not specified, it should use bonsaidb_core::api::Infallible. Authority can be optional the same way the other derives work.

@ModProg

ModProg commented 2 years ago

bonsaidb_core::api::Infallible

probably should remember to change that to ! whenever that comes around :laughing:

ecton commented 2 years ago

probably should remember to change that to ! whenever that comes around

Only if Serde supports ! with Serialize/Deserialize, which I don't anticipate based on this closed issue. That's the primary reason for having a different type than std::convert::Infallible -- due to orphan rules I can't implement Serialize/Deserialize for std::convert::Infallible, and similarly, I wouldn't be able to for the never type either.

ModProg commented 2 years ago

That makes sense, but when ! is stabilized, it would probably make sense to ask serde to add an implementation for it again.