Skip to content

Commit 64caf4c

Browse files
authored
Merge pull request #1101 from Lantern-chat/async_size
Shrink query_opt/query_one codegen size very slightly
2 parents e528e01 + 9d7c43c commit 64caf4c

File tree

1 file changed

+16
-20
lines changed

1 file changed

+16
-20
lines changed

tokio-postgres/src/client.rs

+16-20
Original file line numberDiff line numberDiff line change
@@ -274,19 +274,9 @@ impl Client {
274274
where
275275
T: ?Sized + ToStatement,
276276
{
277-
let stream = self.query_raw(statement, slice_iter(params)).await?;
278-
pin_mut!(stream);
279-
280-
let row = match stream.try_next().await? {
281-
Some(row) => row,
282-
None => return Err(Error::row_count()),
283-
};
284-
285-
if stream.try_next().await?.is_some() {
286-
return Err(Error::row_count());
287-
}
288-
289-
Ok(row)
277+
self.query_opt(statement, params)
278+
.await
279+
.and_then(|res| res.ok_or_else(Error::row_count))
290280
}
291281

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

313-
let row = match stream.try_next().await? {
314-
Some(row) => row,
315-
None => return Ok(None),
316-
};
303+
let mut first = None;
304+
305+
// Originally this was two calls to `try_next().await?`,
306+
// once for the first element, and second to error if more than one.
307+
//
308+
// However, this new form with only one .await in a loop generates
309+
// slightly smaller codegen/stack usage for the resulting future.
310+
while let Some(row) = stream.try_next().await? {
311+
if first.is_some() {
312+
return Err(Error::row_count());
313+
}
317314

318-
if stream.try_next().await?.is_some() {
319-
return Err(Error::row_count());
315+
first = Some(row);
320316
}
321317

322-
Ok(Some(row))
318+
Ok(first)
323319
}
324320

325321
/// The maximally flexible version of [`query`].

0 commit comments

Comments
 (0)