Skip to content

Commit

Permalink
Merge pull request #104 from zacharygolba/feat-row-opt
Browse files Browse the repository at this point in the history
feat: add get_opt and take_opt to row
  • Loading branch information
blackbeam authored Jul 1, 2017
2 parents 598e43b + a0fcd32 commit e31ea34
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/conn/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,18 @@ impl Row {
})
}

/// Will copy value at index `index` if it was not taken by `Row::take` or `Row::take_opt`
/// earlier, then will attempt convert it to `T`. Unlike `Row::get`, `Row::get_opt` will
/// allow you to directly handle errors if the value could not be converted to `T`.
pub fn get_opt<T, I>(&mut self, index: I) -> Option<MyResult<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| Some(from_value_opt::<T>(x.clone())))
}

/// 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>
Expand All @@ -537,6 +549,18 @@ impl Row {
})
}

/// Will take value of a column with index `index` if it exists and wasn't taken earlier then
/// will attempt to convert it to `T`. Unlike `Row::take`, `Row::take_opt` will allow you to
/// directly handle errors if the value could not be converted to `T`.
pub fn take_opt<T, I>(&mut self, index: I) -> Option<MyResult<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| Some(from_value_opt::<T>(x)))
}

/// Unwraps values of a row.
///
/// # Panics
Expand Down

0 comments on commit e31ea34

Please sign in to comment.