From 848d191515cb2b67737916c9faeb7cc298438957 Mon Sep 17 00:00:00 2001 From: Sean Griffin Date: Fri, 8 Apr 2016 15:31:10 -0400 Subject: [PATCH] Bump to the latest nightly We were affected by breaking changes with how struct fields are represented. Now that I can get the tests compiling, I noticed that I implemented the `Option` impl for specialization incorrectly. --- .travis.yml | 2 +- diesel/src/types/specialization_impls.rs | 1 + diesel_codegen/src/associations/belongs_to.rs | 4 +++- diesel_codegen/src/attr.rs | 6 +++--- diesel_codegen/src/model.rs | 12 +++++++++--- diesel_codegen/src/update.rs | 4 ++-- diesel_tests/tests/types.rs | 1 - 7 files changed, 19 insertions(+), 11 deletions(-) diff --git a/.travis.yml b/.travis.yml index 6c623a892e..c9b1fe9758 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,7 +4,7 @@ dist: trusty rust: - stable - beta - - nightly-2016-04-01 + - nightly-2016-04-07 - nightly addons: postgresql: '9.4' diff --git a/diesel/src/types/specialization_impls.rs b/diesel/src/types/specialization_impls.rs index a500e5dbc1..7a1791bcb9 100644 --- a/diesel/src/types/specialization_impls.rs +++ b/diesel/src/types/specialization_impls.rs @@ -5,6 +5,7 @@ impl FromSqlRow, DB> for Option where { default fn build_from_row>(row: &mut R) -> Result> { if row.next_is_null(1) { + row.take(); Ok(None) } else { T::build_from_row(row).map(Some) diff --git a/diesel_codegen/src/associations/belongs_to.rs b/diesel_codegen/src/associations/belongs_to.rs index 0d2f2476a6..ec02a31af5 100644 --- a/diesel_codegen/src/associations/belongs_to.rs +++ b/diesel_codegen/src/associations/belongs_to.rs @@ -80,7 +80,9 @@ impl<'a, 'b> BelongsToAssociationBuilder<'a, 'b> { } fn foreign_key_type(&self) -> P { - self.model.attr_named(self.foreign_key_name()) + let name = self.foreign_key_name(); + self.model.attr_named(name) + .expect(&format!("Couldn't find an attr named {}", name)) .ty.clone() } diff --git a/diesel_codegen/src/attr.rs b/diesel_codegen/src/attr.rs index 7a305fad99..ff4ea7d74f 100644 --- a/diesel_codegen/src/attr.rs +++ b/diesel_codegen/src/attr.rs @@ -14,10 +14,10 @@ pub struct Attr { impl Attr { pub fn from_struct_field(cx: &mut ExtCtxt, field: &ast::StructField) -> Option { - let field_name = field.node.ident(); + let field_name = field.ident; let column_name = - str_value_of_attr_with_name(cx, &field.node.attrs, "column_name"); - let ty = field.node.ty.clone(); + str_value_of_attr_with_name(cx, &field.attrs, "column_name"); + let ty = field.ty.clone(); match (column_name, field_name) { (Some(column_name), f) => Some(Attr { diff --git a/diesel_codegen/src/model.rs b/diesel_codegen/src/model.rs index f75feb45bf..d81c66919a 100644 --- a/diesel_codegen/src/model.rs +++ b/diesel_codegen/src/model.rs @@ -47,10 +47,16 @@ impl Model { }) } - pub fn attr_named(&self, name: ast::Ident) -> &Attr { + pub fn attr_named(&self, name: ast::Ident) -> Option<&Attr> { self.attrs.iter().find(|attr| { - attr.field_name == Some(name) - }).expect(&format!("Couldn't find an attr named {}", name)) + attr.field_name.map(|f| f.name) == Some(name.name) + }) + } + + pub fn attr_for_column(&self, name: ast::Ident) -> Option<&Attr> { + self.attrs.iter().find(|attr| { + attr.column_name.name == name.name + }) } } diff --git a/diesel_codegen/src/update.rs b/diesel_codegen/src/update.rs index 1e1065080e..c3a3604215 100644 --- a/diesel_codegen/src/update.rs +++ b/diesel_codegen/src/update.rs @@ -91,7 +91,7 @@ fn changeset_impl( let ref struct_name = model.ty; let pk = model.primary_key_name(); let table_name = options.table_name; - let attrs_for_changeset = model.attrs.iter().filter(|a| a.column_name != pk) + let attrs_for_changeset = model.attrs.iter().filter(|a| a.column_name.name != pk.name) .collect::>(); let changeset_ty = cx.ty(span, TyKind::Tup( attrs_for_changeset.iter() @@ -127,7 +127,7 @@ fn save_changes_impl( let sql_type = cx.path(span, vec![options.table_name, str_to_ident("SqlType")]); let table = cx.path(span, vec![options.table_name, str_to_ident("table")]); let mut result = Vec::new(); - if let Some(pk) = model.attrs.iter().find(|a| a.column_name == pk) { + if let Some(pk) = model.attr_for_column(pk) { let pk_field = pk.field_name.unwrap(); if cfg!(feature = "postgres") { result.push(quote_item!(cx, diff --git a/diesel_tests/tests/types.rs b/diesel_tests/tests/types.rs index ba9fb0bb67..01235b6064 100644 --- a/diesel_tests/tests/types.rs +++ b/diesel_tests/tests/types.rs @@ -377,7 +377,6 @@ fn third_party_crates_can_add_new_types() { } } - #[cfg(not(feature = "unstable"))] impl FromSqlRow for i32 { fn build_from_row>(row: &mut R) -> Result> { FromSql::::from_sql(row.take())