Skip to content

Commit 66d41a9

Browse files
JabobKrauskopftaiki-e
authored andcommitted
channel: Add new_biased constructor for biased channel selection (#1150)
1 parent d0d0a80 commit 66d41a9

File tree

1 file changed

+33
-7
lines changed

1 file changed

+33
-7
lines changed

crossbeam-channel/src/select.rs

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -611,6 +611,9 @@ pub struct Select<'a> {
611611

612612
/// The next index to assign to an operation.
613613
next_index: usize,
614+
615+
/// Whether to use the index of handles as bias for selecting ready operations.
616+
biased: bool,
614617
}
615618

616619
unsafe impl Send for Select<'_> {}
@@ -633,6 +636,28 @@ impl<'a> Select<'a> {
633636
Select {
634637
handles: Vec::with_capacity(4),
635638
next_index: 0,
639+
biased: false,
640+
}
641+
}
642+
643+
/// Creates an empty list of channel operations with biased selection.
644+
///
645+
/// When multiple handles are ready, this will select the operation with the lowest index.
646+
///
647+
/// # Examples
648+
///
649+
/// ```
650+
/// use crossbeam_channel::Select;
651+
///
652+
/// let mut sel = Select::new_biased();
653+
///
654+
/// // The list of operations is empty, which means no operation can be selected.
655+
/// assert!(sel.try_select().is_err());
656+
/// ```
657+
pub fn new_biased() -> Self {
658+
Self {
659+
biased: true,
660+
..Default::default()
636661
}
637662
}
638663

@@ -774,7 +799,7 @@ impl<'a> Select<'a> {
774799
/// }
775800
/// ```
776801
pub fn try_select(&mut self) -> Result<SelectedOperation<'a>, TrySelectError> {
777-
try_select(&mut self.handles, false)
802+
try_select(&mut self.handles, self.biased)
778803
}
779804

780805
/// Blocks until one of the operations becomes ready and selects it.
@@ -821,7 +846,7 @@ impl<'a> Select<'a> {
821846
/// }
822847
/// ```
823848
pub fn select(&mut self) -> SelectedOperation<'a> {
824-
select(&mut self.handles, false)
849+
select(&mut self.handles, self.biased)
825850
}
826851

827852
/// Blocks for a limited time until one of the operations becomes ready and selects it.
@@ -871,7 +896,7 @@ impl<'a> Select<'a> {
871896
&mut self,
872897
timeout: Duration,
873898
) -> Result<SelectedOperation<'a>, SelectTimeoutError> {
874-
select_timeout(&mut self.handles, timeout, false)
899+
select_timeout(&mut self.handles, timeout, self.biased)
875900
}
876901

877902
/// Blocks until a given deadline, or until one of the operations becomes ready and selects it.
@@ -923,7 +948,7 @@ impl<'a> Select<'a> {
923948
&mut self,
924949
deadline: Instant,
925950
) -> Result<SelectedOperation<'a>, SelectTimeoutError> {
926-
select_deadline(&mut self.handles, deadline, false)
951+
select_deadline(&mut self.handles, deadline, self.biased)
927952
}
928953

929954
/// Attempts to find a ready operation without blocking.
@@ -962,7 +987,7 @@ impl<'a> Select<'a> {
962987
/// }
963988
/// ```
964989
pub fn try_ready(&mut self) -> Result<usize, TryReadyError> {
965-
match run_ready(&mut self.handles, Timeout::Now, false) {
990+
match run_ready(&mut self.handles, Timeout::Now, self.biased) {
966991
None => Err(TryReadyError),
967992
Some(index) => Ok(index),
968993
}
@@ -1015,7 +1040,7 @@ impl<'a> Select<'a> {
10151040
panic!("no operations have been added to `Select`");
10161041
}
10171042

1018-
run_ready(&mut self.handles, Timeout::Never, false).unwrap()
1043+
run_ready(&mut self.handles, Timeout::Never, self.biased).unwrap()
10191044
}
10201045

10211046
/// Blocks for a limited time until one of the operations becomes ready.
@@ -1108,7 +1133,7 @@ impl<'a> Select<'a> {
11081133
/// }
11091134
/// ```
11101135
pub fn ready_deadline(&mut self, deadline: Instant) -> Result<usize, ReadyTimeoutError> {
1111-
match run_ready(&mut self.handles, Timeout::At(deadline), false) {
1136+
match run_ready(&mut self.handles, Timeout::At(deadline), self.biased) {
11121137
None => Err(ReadyTimeoutError),
11131138
Some(index) => Ok(index),
11141139
}
@@ -1120,6 +1145,7 @@ impl<'a> Clone for Select<'a> {
11201145
Select {
11211146
handles: self.handles.clone(),
11221147
next_index: self.next_index,
1148+
biased: self.biased,
11231149
}
11241150
}
11251151
}

0 commit comments

Comments
 (0)