Skip to content

Commit

Permalink
Shrink query_opt/query_one codegen size very slightly
Browse files Browse the repository at this point in the history
  • Loading branch information
novacrazy committed Feb 11, 2024
1 parent e8f44ec commit a800b86
Showing 1 changed file with 11 additions and 20 deletions.
31 changes: 11 additions & 20 deletions tokio-postgres/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,19 +274,9 @@ impl Client {
where
T: ?Sized + ToStatement,
{
let stream = self.query_raw(statement, slice_iter(params)).await?;
pin_mut!(stream);

let row = match stream.try_next().await? {
Some(row) => row,
None => return Err(Error::row_count()),
};

if stream.try_next().await?.is_some() {
return Err(Error::row_count());
}

Ok(row)
self.query_opt(statement, params)
.await
.and_then(|res| res.ok_or_else(Error::row_count))
}

/// Executes a statements which returns zero or one rows, returning it.
Expand All @@ -310,16 +300,17 @@ impl Client {
let stream = self.query_raw(statement, slice_iter(params)).await?;
pin_mut!(stream);

let row = match stream.try_next().await? {
Some(row) => row,
None => return Ok(None),
};
let mut first = None;

while let Some(row) = stream.try_next().await? {
if first.is_some() {
return Err(Error::row_count());
}

if stream.try_next().await?.is_some() {
return Err(Error::row_count());
first = Some(row);
}

Ok(Some(row))
Ok(first)
}

/// The maximally flexible version of [`query`].
Expand Down

0 comments on commit a800b86

Please sign in to comment.