-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Add a proc macro derive for Encode and Decode supporting _only_ 1-arity tuple structs #34
Comments
This is considered a "good first issue" because it's conceptually very simple for a derive macro; it should only forward the A rundown of implementing a simple derive is available in the new Rust book here: https://doc.rust-lang.org/book/ch19-06-macros.html#how-to-write-a-custom-derive-macro However, instead of creating a new macro library, add These functions, however, should only be shims. Add a new module
mod derives;
#[proc_macro_derive(Encode)]
pub fn derive_encode(tokenstream: TokenStream) -> TokenStream {
let input = syn::parse_macro_input!(tokenstream as syn::DeriveInput);
match derives::expand_derive_encode(input) {
Ok(ts) => ts.into(),
Err(e) => e.to_compile_error(),
}
}
#[proc_macro_derive(Decode)]
pub fn derive_decode(tokenstream: TokenStream) -> TokenStream {
let input = syn::parse_macro_input!(tokenstream as syn::DeriveInput);
match derives::expand_derive_decode(input) {
Ok(ts) => ts.into(),
Err(e) => e.to_compile_error(),
}
} Then in Some recommendations: Here's the kind of expected input and output: use sqlx::{Encode, Decode};
#[derive(Decode, Encode)]
struct Foo(i32);
// expected output for the above derives:
impl<DB> Encode<DB> for Foo where DB: sqlx::Database, i32: sqlx::encode::Encode<DB> {
fn encode(&self, buf: &mut Vec<u8>) -> sqlx::encode::IsNull {
self.0.encode(buf)
}
}
impl<DB> Decode<DB> for Foo where DB: sqlx::Database, i32: sqlx::decode::Decode<DB> {
fn decode(raw: &[u8]) -> Result<Self, sqlx::decode::DecodeError> {
<i32 as Decode<DB>>::decode(raw).map(Self)
}
} To make the usage nice, you should change the When you're done with that, open a PR and ping me (@abonander) for review; I'll walk you through adding tests. Of course also if you get stuck, feel free to @ me. I wrote all the other macro code there. |
Closed by #71. |
I don't want to define what anything but 1-arity tuple structs means at this time. We are going to explore derives for custom types in the near future.
From #5
The text was updated successfully, but these errors were encountered: