-
Notifications
You must be signed in to change notification settings - Fork 31
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 DontImplCloneOnSqlType
attribute.
#94
base: master
Are you sure you want to change the base?
Changes from 2 commits
10f8e42
7c34a16
17b48c5
6ab8ee1
141d02c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -31,7 +31,14 @@ use syn::*; | |||||||||||||||||||||||||||||||||||||||||||
/// * `#[db_rename = "variant"]` specifies the db name for a specific variant. | ||||||||||||||||||||||||||||||||||||||||||||
#[proc_macro_derive( | ||||||||||||||||||||||||||||||||||||||||||||
DbEnum, | ||||||||||||||||||||||||||||||||||||||||||||
attributes(PgType, DieselType, ExistingTypePath, DbValueStyle, db_rename) | ||||||||||||||||||||||||||||||||||||||||||||
attributes( | ||||||||||||||||||||||||||||||||||||||||||||
PgType, | ||||||||||||||||||||||||||||||||||||||||||||
DieselType, | ||||||||||||||||||||||||||||||||||||||||||||
ExistingTypePath, | ||||||||||||||||||||||||||||||||||||||||||||
DbValueStyle, | ||||||||||||||||||||||||||||||||||||||||||||
db_rename, | ||||||||||||||||||||||||||||||||||||||||||||
DontImplCloneOnSqlType | ||||||||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||||||||
)] | ||||||||||||||||||||||||||||||||||||||||||||
pub fn derive(input: TokenStream) -> TokenStream { | ||||||||||||||||||||||||||||||||||||||||||||
let input: DeriveInput = parse_macro_input!(input as DeriveInput); | ||||||||||||||||||||||||||||||||||||||||||||
|
@@ -70,6 +77,9 @@ pub fn derive(input: TokenStream) -> TokenStream { | |||||||||||||||||||||||||||||||||||||||||||
.expect("ExistingTypePath is not a valid token") | ||||||||||||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||||||||||||
let new_diesel_mapping = Ident::new(new_diesel_mapping.as_ref(), Span::call_site()); | ||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||
let with_clone = !contains_in_attrs(&input.attrs, "DontImplCloneOnSqlType"); | ||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||
if let Data::Enum(syn::DataEnum { | ||||||||||||||||||||||||||||||||||||||||||||
variants: data_variants, | ||||||||||||||||||||||||||||||||||||||||||||
.. | ||||||||||||||||||||||||||||||||||||||||||||
|
@@ -81,6 +91,7 @@ pub fn derive(input: TokenStream) -> TokenStream { | |||||||||||||||||||||||||||||||||||||||||||
&pg_internal_type, | ||||||||||||||||||||||||||||||||||||||||||||
case_style, | ||||||||||||||||||||||||||||||||||||||||||||
&input.ident, | ||||||||||||||||||||||||||||||||||||||||||||
with_clone, | ||||||||||||||||||||||||||||||||||||||||||||
&data_variants, | ||||||||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||||||||
} else { | ||||||||||||||||||||||||||||||||||||||||||||
|
@@ -115,6 +126,10 @@ fn val_from_attrs(attrs: &[Attribute], attrname: &str) -> Option<String> { | |||||||||||||||||||||||||||||||||||||||||||
None | ||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||
fn contains_in_attrs(attrs: &[Attribute], attrname: &str) -> bool { | ||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How about just There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I imitated Lines 96 to 116 in 36c44a2
I will change it to |
||||||||||||||||||||||||||||||||||||||||||||
attrs.iter().any(|e| e.path().is_ident(attrname)) | ||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||
/// Defines the casing for the database representation. Follows serde naming convention. | ||||||||||||||||||||||||||||||||||||||||||||
#[derive(Copy, Clone, Debug, PartialEq)] | ||||||||||||||||||||||||||||||||||||||||||||
enum CaseStyle { | ||||||||||||||||||||||||||||||||||||||||||||
|
@@ -148,6 +163,7 @@ fn generate_derive_enum_impls( | |||||||||||||||||||||||||||||||||||||||||||
pg_internal_type: &str, | ||||||||||||||||||||||||||||||||||||||||||||
case_style: CaseStyle, | ||||||||||||||||||||||||||||||||||||||||||||
enum_ty: &Ident, | ||||||||||||||||||||||||||||||||||||||||||||
with_clone: bool, | ||||||||||||||||||||||||||||||||||||||||||||
variants: &syn::punctuated::Punctuated<Variant, syn::token::Comma>, | ||||||||||||||||||||||||||||||||||||||||||||
) -> TokenStream { | ||||||||||||||||||||||||||||||||||||||||||||
let modname = Ident::new(&format!("db_enum_impl_{}", enum_ty), Span::call_site()); | ||||||||||||||||||||||||||||||||||||||||||||
|
@@ -201,7 +217,7 @@ fn generate_derive_enum_impls( | |||||||||||||||||||||||||||||||||||||||||||
match existing_mapping_path { | ||||||||||||||||||||||||||||||||||||||||||||
Some(path) => { | ||||||||||||||||||||||||||||||||||||||||||||
let common_impls_on_existing_diesel_mapping = generate_common_impls(path, enum_ty); | ||||||||||||||||||||||||||||||||||||||||||||
let postgres_impl = generate_postgres_impl(path, enum_ty, true); | ||||||||||||||||||||||||||||||||||||||||||||
let postgres_impl = generate_postgres_impl(path, enum_ty, with_clone); | ||||||||||||||||||||||||||||||||||||||||||||
Some(quote! { | ||||||||||||||||||||||||||||||||||||||||||||
#common_impls_on_existing_diesel_mapping | ||||||||||||||||||||||||||||||||||||||||||||
#postgres_impl | ||||||||||||||||||||||||||||||||||||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
use diesel::prelude::*; | ||
|
||
#[cfg(feature = "postgres")] | ||
#[derive(diesel::sql_types::SqlType)] | ||
#[diesel(postgres_type(name = "my_remote_enum"))] | ||
pub struct MyRemoteEnumMapping; | ||
|
||
table! { | ||
use diesel::sql_types::Integer; | ||
use super::MyRemoteEnumMapping; | ||
test_remote { | ||
id -> Integer, | ||
my_enum -> MyRemoteEnumMapping, | ||
} | ||
} | ||
|
||
#[derive(Insertable, Queryable, Identifiable, Debug, Clone, PartialEq)] | ||
#[diesel(table_name = test_remote)] | ||
struct Data { | ||
id: i32, | ||
my_enum: MyRemoteEnum, | ||
} | ||
|
||
#[derive(Debug, PartialEq, Clone, diesel_derive_enum::DbEnum)] | ||
#[ExistingTypePath = "MyRemoteEnumMapping"] | ||
pub enum MyRemoteEnum { | ||
This, | ||
That, | ||
} | ||
|
||
#[test] | ||
fn clone_impl_on_sql_type() { | ||
let x = MyRemoteEnumMapping {}; | ||
let _ = x.clone(); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't understand why this change is necessary
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If
matrix.rust
is notstable
,rustc
is an old version.So the latest
diesel-cli
dependency packages cannot be compiled.e.g. serde_spanned@v0.6.5 needs rustc 1.67 or newer
Then, I changed it as follows.
If
matrix.rust
isstable
, use the latest dielse-cli.If it is not
stable
, use the older version of diesel-cli.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm I guess I'd want to just bump the MSRV then, but no strong opinion.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If I may, I would like to bump the MSRV.
However, I did this because I thought there might be some reason to keep the MSRV at 1.65.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's fine to bump it: https://github.com/diesel-rs/diesel/blob/2.1.x/rust-toolchain is at 1.68.0
:)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for your advice.
I will bump the MSRV to 1.68.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried to install diesel-cli@2.1.1 with rust:1.68.0.
Then it failed to compile.
Could I bump to
1.70
?I was able to compile it with 1.70.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No strong opinion here, I'm going to let @adwhit decide on this :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@adwhit What do you think about this topic?