feat: add get_opt and take_opt to row
fix: remove unused import
feat: add take_opt to row
fix: do not clone x in take_opt method
@@ -659,6 +659,18 @@ impl Row {
})
}
+ /// Will copy value at index `index` if it was not taken by `Row::take` earlier,
+ /// then will convert it to `T`. Returns `None` if the value was taken taken by
+ /// `Row::take` or was not able to be converted to `T`.
+ pub fn get_opt<T, I>(&mut self, index: I) -> Option<T>
+ where T: FromValue,
+ I: ColumnIndex {
+ index.idx(&*self.columns)
+ .and_then(|idx| self.values.get(idx))
+ .and_then(|x| x.as_ref())
+ .and_then(|x| from_value_opt::<T>(x.clone()).ok())
+ }
+
/// Will take value of a column with index `index` if it exists and wasn't taken earlier then
/// will converts it to `T`.
pub fn take<T, I>(&mut self, index: I) -> Option<T>
@@ -669,6 +681,18 @@ impl Row {
})
}
+ /// Will take value of a column with index `index` if it exists and wasn't taken earlier then
+ /// will converts it to `T`. Returns `None` if the value was taken earlier or was not able
+ /// to be converted to `T`.
+ pub fn take_opt<T, I>(&mut self, index: I) -> Option<T>
+ where T: FromValue,
+ I: ColumnIndex {
+ index.idx(&*self.columns)
+ .and_then(|idx| self.values.get_mut(idx))
+ .and_then(|x| x.take())
+ .and_then(|x| from_value_opt::<T>(x).ok())
+ }
+
/// Unwraps values of a row.
///
/// # Panics
