Skip to content

Commit 7cd6dd5

Browse files
committed
Ensure on_conflict_do_nothing works with slices
Due to the various signatures, the end result of `slice.on_conflict_do_nothing()` was `&OnConflictDoNothing<&&[T]>`, which doesn't implement the various traits. The solution is to remove the sized contraint from the extension method so that `[T]` implements it instead of `&[T]`.
1 parent 909a39b commit 7cd6dd5

2 files changed

Lines changed: 47 additions & 1 deletion

File tree

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ All user visible changes to this project will be documented in this file.
44
This project adheres to [Semantic Versioning](http://semver.org/), as described
55
for Rust libraries in [RFC #1105](https://github.com/rust-lang/rfcs/blob/master/text/1105-api-evolution.md)
66

7+
## Unreleased
8+
9+
### Fixed
10+
11+
* `.on_conflict_do_nothing()` now interacts with slices properly.
12+
713
## [0.11.0] - 2017-02-16
814

915
### Added

diesel/src/pg/upsert/on_conflict_extension.rs

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ pub trait OnConflictExtension {
77
///
88
/// # Examples
99
///
10+
/// ### Single Record
11+
///
1012
/// ```rust
1113
/// # #[macro_use] extern crate diesel;
1214
/// # #[macro_use] extern crate diesel_codegen;
@@ -44,6 +46,8 @@ pub trait OnConflictExtension {
4446
/// # }
4547
/// ```
4648
///
49+
/// ### Vec of Records
50+
///
4751
/// ```rust
4852
/// # #[macro_use] extern crate diesel;
4953
/// # #[macro_use] extern crate diesel_codegen;
@@ -76,10 +80,46 @@ pub trait OnConflictExtension {
7680
/// assert_eq!(Ok(1), inserted_row_count);
7781
/// # }
7882
/// ```
83+
///
84+
/// ### Slice of records
85+
///
86+
/// ```rust
87+
/// # #[macro_use] extern crate diesel;
88+
/// # #[macro_use] extern crate diesel_codegen;
89+
/// # include!("src/doctest_setup.rs");
90+
/// #
91+
/// # table! {
92+
/// # users {
93+
/// # id -> Integer,
94+
/// # name -> VarChar,
95+
/// # }
96+
/// # }
97+
/// #
98+
/// # #[derive(Clone, Copy, Insertable)]
99+
/// # #[table_name="users"]
100+
/// # struct User<'a> {
101+
/// # id: i32,
102+
/// # name: &'a str,
103+
/// # }
104+
/// #
105+
/// # fn main() {
106+
/// # use self::users::dsl::*;
107+
/// use self::diesel::pg::upsert::*;
108+
///
109+
/// # let conn = establish_connection();
110+
/// # conn.execute("TRUNCATE TABLE users").unwrap();
111+
/// let user = User { id: 1, name: "Sean", };
112+
///
113+
/// let new_users: &[User] = &[user, user];
114+
/// let inserted_row_count = diesel::insert(&new_users.on_conflict_do_nothing())
115+
/// .into(users).execute(&conn);
116+
/// assert_eq!(Ok(1), inserted_row_count);
117+
/// # }
118+
/// ```
79119
fn on_conflict_do_nothing(&self) -> OnConflictDoNothing<&Self> {
80120
OnConflictDoNothing::new(self)
81121
}
82122
}
83123

84-
impl<T> OnConflictExtension for T {
124+
impl<T: ?Sized> OnConflictExtension for T {
85125
}

0 commit comments

Comments
 (0)