Skip to content

Instantly share code, notes, and snippets.

@sgrif
Last active January 3, 2018 22:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sgrif/63efe328cc5fbd69c40d905dd3726428 to your computer and use it in GitHub Desktop.
Save sgrif/63efe328cc5fbd69c40d905dd3726428 to your computer and use it in GitHub Desktop.
// This is the structure of the code #[derive(Queryable)]
// generates in diesel_derives. It's tricky, because we can't assume
// that you did `extern crate diesel;` or that anything named `diesel`
// is in scope. In a normal macro you have `$crate` for this, but
// custom derives don't have access to that so we have to do hacks to make this work.
const _IMPL_QUERYABLE_FOR_FOO: () {
// This is kinda like if we had access to $crate
// but this doesn't work if we try to use this code within diesel itself
extern crate diesel;
impl diesel::query_source::Queryable for Foo {
// ...
}
}
// This is in Diesel itself
#[macro_export]
#[doc(hidden)]
macro_rules! __diesel_use_everything {
() => {
pub use $crate::*;
};
}
// This is the generated code
const _IMPL_QUERYABLE_FOR_FOO {
mod diesel {
__diesel_use_everything!();
}
impl diesel::query_source::Queryable for Foo {
// ...
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment