Rename get_raw to get_ref_unwrap and get_raw_checked to get_ref (#838)

* test `From<FromSqlError> for Error`
* Rename get_raw to get_ref_unwrap and get_raw_checked to get_ref
This commit is contained in:
gwenn 2020-12-22 21:34:30 +01:00 committed by GitHub
parent 73f59a3a38
commit e154ccb606
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 8 deletions

View File

@ -1614,11 +1614,21 @@ mod test {
let mut rows = query.query([])?; let mut rows = query.query([])?;
while let Some(row) = rows.next()? { while let Some(row) = rows.next()? {
let i = row.get_raw(0).as_i64()?; let i = row.get_ref(0)?.as_i64()?;
let expect = vals[i as usize]; let expect = vals[i as usize];
let x = row.get_raw("x").as_str()?; let x = row.get_ref("x")?.as_str()?;
assert_eq!(x, expect); assert_eq!(x, expect);
} }
let mut query = db.prepare("SELECT x FROM foo")?;
let rows = query.query_map([], |row| {
let x = row.get_ref(0)?.as_str()?; // check From<FromSqlError> for Error
Ok(x[..].to_owned())
})?;
for (i, row) in rows.enumerate() {
assert_eq!(row?, vals[i]);
}
Ok(()) Ok(())
} }

View File

@ -241,7 +241,7 @@ impl<'stmt> Row<'stmt> {
/// ///
/// ## Failure /// ## Failure
/// ///
/// Panics if calling `row.get(idx)` would return an error, /// Panics if calling [`row.get(idx)`](Row::get) would return an error,
/// including: /// including:
/// ///
/// * If the underlying SQLite column type is not a valid type as a source /// * If the underlying SQLite column type is not a valid type as a source
@ -303,7 +303,7 @@ impl<'stmt> Row<'stmt> {
/// This `ValueRef` is valid only as long as this Row, which is enforced by /// This `ValueRef` is valid only as long as this Row, which is enforced by
/// it's lifetime. This means that while this method is completely safe, /// it's lifetime. This means that while this method is completely safe,
/// it can be somewhat difficult to use, and most callers will be better /// it can be somewhat difficult to use, and most callers will be better
/// served by `get` or `get`. /// served by [`get`](Row::get) or [`get_unwrap`](Row::get_unwrap).
/// ///
/// ## Failure /// ## Failure
/// ///
@ -312,7 +312,7 @@ impl<'stmt> Row<'stmt> {
/// ///
/// Returns an `Error::InvalidColumnName` if `idx` is not a valid column /// Returns an `Error::InvalidColumnName` if `idx` is not a valid column
/// name for this row. /// name for this row.
pub fn get_raw_checked<I: RowIndex>(&self, idx: I) -> Result<ValueRef<'_>> { pub fn get_ref<I: RowIndex>(&self, idx: I) -> Result<ValueRef<'_>> {
let idx = idx.idx(self.stmt)?; let idx = idx.idx(self.stmt)?;
// Narrowing from `ValueRef<'stmt>` (which `self.stmt.value_ref(idx)` // Narrowing from `ValueRef<'stmt>` (which `self.stmt.value_ref(idx)`
// returns) to `ValueRef<'a>` is needed because it's only valid until // returns) to `ValueRef<'a>` is needed because it's only valid until
@ -327,17 +327,31 @@ impl<'stmt> Row<'stmt> {
/// This `ValueRef` is valid only as long as this Row, which is enforced by /// This `ValueRef` is valid only as long as this Row, which is enforced by
/// it's lifetime. This means that while this method is completely safe, /// it's lifetime. This means that while this method is completely safe,
/// it can be difficult to use, and most callers will be better served by /// it can be difficult to use, and most callers will be better served by
/// `get` or `get`. /// [`get`](Row::get) or [`get_unwrap`](Row::get_unwrap).
/// ///
/// ## Failure /// ## Failure
/// ///
/// Panics if calling `row.get_raw_checked(idx)` would return an error, /// Panics if calling [`row.get_ref(idx)`](Row::get_ref) would return an error,
/// including: /// including:
/// ///
/// * If `idx` is outside the range of columns in the returned query. /// * If `idx` is outside the range of columns in the returned query.
/// * If `idx` is not a valid column name for this row. /// * If `idx` is not a valid column name for this row.
pub fn get_ref_unwrap<I: RowIndex>(&self, idx: I) -> ValueRef<'_> {
self.get_ref(idx).unwrap()
}
/// Renamed to [`get_ref`](Row::get_ref).
#[deprecated = "Use [`get_ref`](Row::get_ref) instead."]
#[inline]
pub fn get_raw_checked<I: RowIndex>(&self, idx: I) -> Result<ValueRef<'_>> {
self.get_ref(idx)
}
/// Renamed to [`get_ref_unwrap`](Row::get_ref_unwrap).
#[deprecated = "Use [`get_ref_unwrap`](Row::get_ref_unwrap) instead."]
#[inline]
pub fn get_raw<I: RowIndex>(&self, idx: I) -> ValueRef<'_> { pub fn get_raw<I: RowIndex>(&self, idx: I) -> ValueRef<'_> {
self.get_raw_checked(idx).unwrap() self.get_ref_unwrap(idx)
} }
} }