Skip to content

Instantly share code, notes, and snippets.

@marcusklaas
Created May 6, 2015 12:33
Show Gist options
  • Save marcusklaas/2f9f3a7d4ee78d1ee2d2 to your computer and use it in GitHub Desktop.
Save marcusklaas/2f9f3a7d4ee78d1ee2d2 to your computer and use it in GitHub Desktop.
weird conflicting implementation error
/// A trait for types that can be created from a SQLite value.
pub trait FromSql {
unsafe fn column_result(stmt: *mut sqlite3_stmt, col: c_int) -> SqliteResult<Self>;
}
impl<'a, T> FromSql for T where T: convert::From<&'a str> {
unsafe fn column_result(stmt: *mut sqlite3_stmt, col: c_int) -> SqliteResult<T> {
let c_text = ffi::sqlite3_column_text(stmt, col);
if c_text.is_null() {
Ok(T::from(""))
} else {
let c_slice = CStr::from_ptr(c_text as *const c_char).to_bytes();
let utf8_str = str::from_utf8(c_slice);
utf8_str
.map(|s| T::from(s) )
.map_err(|e| SqliteError{code: 0, message: e.to_string()})
}
}
}
impl FromSql for time::Timespec {
unsafe fn column_result(stmt: *mut sqlite3_stmt,
col: c_int) -> SqliteResult<time::Timespec> {
let col_str = FromSql::column_result(stmt, col);
col_str.and_then(|txt: String| {
time::strptime(&txt, SQLITE_DATETIME_FMT).map(|tm| {
tm.to_timespec()
}).map_err(|parse_error| {
SqliteError{ code: ffi::SQLITE_MISMATCH, message: format!("{}", parse_error) }
})
})
}
}
// Error:
src/types.rs:178:1: 191:2 error: conflicting implementations for trait `types::FromSql` [E0119]
src/types.rs:178 impl<'a, T> FromSql for T where T: convert::From<&'a str> {
src/types.rs:179 unsafe fn column_result(stmt: *mut sqlite3_stmt, col: c_int) -> SqliteResult<T> {
src/types.rs:180 let c_text = ffi::sqlite3_column_text(stmt, col);
src/types.rs:181 if c_text.is_null() {
src/types.rs:182 Ok(T::from(""))
src/types.rs:183 } else {
...
src/types.rs:208:1: 220:2 note: note conflicting implementation here
src/types.rs:208 impl FromSql for time::Timespec {
src/types.rs:209 unsafe fn column_result(stmt: *mut sqlite3_stmt,
src/types.rs:210 col: c_int) -> SqliteResult<time::Timespec> {
src/types.rs:211 let col_str = FromSql::column_result(stmt, col);
src/types.rs:212 col_str.and_then(|txt: String| {
src/types.rs:213 time::strptime(&txt, SQLITE_DATETIME_FMT).map(|tm| {
...
error: aborting due to previous error
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment