Change column_name to &str to avoid double allocation

This commit is contained in:
Jayson Reis 2018-06-25 17:56:05 +02:00
parent fce61e13ad
commit beda4c82c8
No known key found for this signature in database
GPG Key ID: 32CAA472083D88E2
2 changed files with 8 additions and 6 deletions

View File

@ -57,16 +57,16 @@ impl<'l> Statement<'l> {
/// Return the name of a column in the statement /// Return the name of a column in the statement
#[inline] #[inline]
pub fn column_name(&self, i: usize) -> String { pub fn column_name(&self, i: usize) -> &str {
unsafe { unsafe {
let ret = ffi::sqlite3_column_name(self.raw.0, i as c_int); let ret = ffi::sqlite3_column_name(self.raw.0, i as c_int);
c_str_to_string!(ret) c_str_to_str!(ret).unwrap()
} }
} }
/// Return column names in the statement /// Return column names in the statement
#[inline] #[inline]
pub fn column_names(&self) -> Vec<String> { pub fn column_names(&self) -> Vec<&str> {
(0..self.columns()).map(|i| self.column_name(i)).collect() (0..self.columns()).map(|i| self.column_name(i)).collect()
} }

View File

@ -142,9 +142,11 @@ fn statement_columns() {
assert_eq!(statement.columns(), 4); assert_eq!(statement.columns(), 4);
let column_names = statement.column_names(); {
assert_eq!(column_names, vec!["id", "name", "age", "user_photo"]); let column_names = statement.column_names();
assert_eq!("user_photo", statement.column_name(3)); assert_eq!(column_names, vec!["id", "name", "age", "user_photo"]);
assert_eq!("user_photo", statement.column_name(3));
}
assert_eq!(ok!(statement.next()), State::Row); assert_eq!(ok!(statement.next()), State::Row);