From 7c647ab8f821c88c4002a28e4dd1b2a7540b0847 Mon Sep 17 00:00:00 2001 From: Zachary Golba Date: Fri, 2 Jun 2017 12:26:54 -0400 Subject: [PATCH 1/3] 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 --- src/conn/mod.rs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/conn/mod.rs b/src/conn/mod.rs index ad96760..bc83684 100644 --- a/src/conn/mod.rs +++ b/src/conn/mod.rs @@ -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(&mut self, index: I) -> Option + 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::(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(&mut self, index: I) -> Option @@ -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(&mut self, index: I) -> Option + 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::(x).ok()) + } + /// Unwraps values of a row. /// /// # Panics From 9be65b6adf08d0b2039831eca400e3731de02465 Mon Sep 17 00:00:00 2001 From: Zachary Golba Date: Fri, 30 Jun 2017 18:42:51 -0400 Subject: [PATCH 2/3] fix: do not swallow errors in (get|take)_opt methods --- src/conn/mod.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/conn/mod.rs b/src/conn/mod.rs index bc83684..f678d18 100644 --- a/src/conn/mod.rs +++ b/src/conn/mod.rs @@ -662,13 +662,13 @@ 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(&mut self, index: I) -> Option + pub fn get_opt(&mut self, index: I) -> Option> 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::(x.clone()).ok()) + .and_then(|x| Some(from_value_opt::(x.clone()))) } /// Will take value of a column with index `index` if it exists and wasn't taken earlier then @@ -684,13 +684,13 @@ 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(&mut self, index: I) -> Option + pub fn take_opt(&mut self, index: I) -> Option> 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::(x).ok()) + .and_then(|x| Some(from_value_opt::(x))) } /// Unwraps values of a row. From a0fcd324769e98ee7d846c83c72ccab5c6bea7c3 Mon Sep 17 00:00:00 2001 From: Zachary Golba Date: Sat, 1 Jul 2017 08:35:47 -0400 Subject: [PATCH 3/3] docs: update docs to reflect new fn signatures --- src/conn/mod.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/conn/mod.rs b/src/conn/mod.rs index f678d18..aaead85 100644 --- a/src/conn/mod.rs +++ b/src/conn/mod.rs @@ -659,9 +659,9 @@ 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`. + /// 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(&mut self, index: I) -> Option> where T: FromValue, I: ColumnIndex { @@ -682,8 +682,8 @@ 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`. + /// 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(&mut self, index: I) -> Option> where T: FromValue, I: ColumnIndex {