Skip to content

Commit b7a0070

Browse files
cratelynmaminrayej
andcommitted
review: use let else binding in is_empty()
this commit uses a `let else` binding to early return when the channel has already terminated. it then, additionally, removes a redundant `else if` branch. Co-authored-by: M.Amin Rayej <[email protected]> Signed-off-by: katelyn martin <[email protected]>
1 parent abcff8f commit b7a0070

File tree

1 file changed

+16
-18
lines changed

1 file changed

+16
-18
lines changed

tokio/src/sync/oneshot.rs

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1014,25 +1014,23 @@ impl<T> Receiver<T> {
10141014
/// }
10151015
/// ```
10161016
pub fn is_empty(&self) -> bool {
1017-
if let Some(inner) = self.inner.as_ref() {
1018-
let state = State::load(&inner.state, Acquire);
1019-
if state.is_complete() {
1020-
// SAFETY: If `state.is_complete()` returns true, then the
1021-
// `VALUE_SENT` bit has been set and the sender side of the
1022-
// channel will no longer attempt to access the inner
1023-
// `UnsafeCell`. Therefore, it is now safe for us to access the
1024-
// cell.
1025-
//
1026-
// The channel is empty if it does not have a value.
1027-
unsafe { !inner.has_value() }
1028-
} else if state.is_closed() {
1029-
// The receiver closed the channel...
1030-
true
1031-
} else {
1032-
// No value has been sent yet.
1033-
true
1034-
}
1017+
let Some(inner) = self.inner.as_ref() else {
1018+
// The channel has already terminated.
1019+
return true;
1020+
};
1021+
1022+
let state = State::load(&inner.state, Acquire);
1023+
if state.is_complete() {
1024+
// SAFETY: If `state.is_complete()` returns true, then the
1025+
// `VALUE_SENT` bit has been set and the sender side of the
1026+
// channel will no longer attempt to access the inner
1027+
// `UnsafeCell`. Therefore, it is now safe for us to access the
1028+
// cell.
1029+
//
1030+
// The channel is empty if it does not have a value.
1031+
unsafe { !inner.has_value() }
10351032
} else {
1033+
// The receiver closed the channel or no value has been sent yet.
10361034
true
10371035
}
10381036
}

0 commit comments

Comments
 (0)