Skip to content

Commit

Permalink
Introduce Row::take
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanUkhov committed Nov 8, 2023
1 parent 6d91e14 commit 629601d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,19 @@ impl Row {
self.try_read(column).unwrap()
}

/// Take the value from a column.
///
/// In case of integer indices, the first column has index 0. Any subsequent invocation will
/// result in `Value::Null`.
#[inline]
pub fn take<U>(&mut self, column: U) -> Value
where
U: RowIndex,
{
let index = column.index(self);
std::mem::take(&mut self.values[index])
}

/// Try to read the value in a column.
///
/// In case of integer indices, the first column has index 0.
Expand Down
11 changes: 11 additions & 0 deletions tests/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,17 @@ fn next_read_with_name_and_option() {
assert!(row.read::<Option<&str>, _>("email").is_none());
}

#[test]
fn next_take() {
let connection = setup_users(":memory:");
let query = "SELECT * FROM users";
let mut statement = ok!(connection.prepare(query));

let mut row = ok!(ok!(statement.iter().next()));
assert_eq!(row.take("name"), Value::String("Alice".into()));
assert_eq!(row.take("name"), Value::Null);
}

#[test]
fn next_try_read_with_index() {
let connection = setup_users(":memory:");
Expand Down

0 comments on commit 629601d

Please sign in to comment.