@@ -611,6 +611,9 @@ pub struct Select<'a> {
611
611
612
612
/// The next index to assign to an operation.
613
613
next_index : usize ,
614
+
615
+ /// Whether to use the index of handles as bias for selecting ready operations.
616
+ biased : bool ,
614
617
}
615
618
616
619
unsafe impl Send for Select < ' _ > { }
@@ -633,6 +636,28 @@ impl<'a> Select<'a> {
633
636
Select {
634
637
handles : Vec :: with_capacity ( 4 ) ,
635
638
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 ( )
636
661
}
637
662
}
638
663
@@ -774,7 +799,7 @@ impl<'a> Select<'a> {
774
799
/// }
775
800
/// ```
776
801
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 )
778
803
}
779
804
780
805
/// Blocks until one of the operations becomes ready and selects it.
@@ -821,7 +846,7 @@ impl<'a> Select<'a> {
821
846
/// }
822
847
/// ```
823
848
pub fn select ( & mut self ) -> SelectedOperation < ' a > {
824
- select ( & mut self . handles , false )
849
+ select ( & mut self . handles , self . biased )
825
850
}
826
851
827
852
/// Blocks for a limited time until one of the operations becomes ready and selects it.
@@ -871,7 +896,7 @@ impl<'a> Select<'a> {
871
896
& mut self ,
872
897
timeout : Duration ,
873
898
) -> Result < SelectedOperation < ' a > , SelectTimeoutError > {
874
- select_timeout ( & mut self . handles , timeout, false )
899
+ select_timeout ( & mut self . handles , timeout, self . biased )
875
900
}
876
901
877
902
/// Blocks until a given deadline, or until one of the operations becomes ready and selects it.
@@ -923,7 +948,7 @@ impl<'a> Select<'a> {
923
948
& mut self ,
924
949
deadline : Instant ,
925
950
) -> Result < SelectedOperation < ' a > , SelectTimeoutError > {
926
- select_deadline ( & mut self . handles , deadline, false )
951
+ select_deadline ( & mut self . handles , deadline, self . biased )
927
952
}
928
953
929
954
/// Attempts to find a ready operation without blocking.
@@ -962,7 +987,7 @@ impl<'a> Select<'a> {
962
987
/// }
963
988
/// ```
964
989
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 ) {
966
991
None => Err ( TryReadyError ) ,
967
992
Some ( index) => Ok ( index) ,
968
993
}
@@ -1015,7 +1040,7 @@ impl<'a> Select<'a> {
1015
1040
panic ! ( "no operations have been added to `Select`" ) ;
1016
1041
}
1017
1042
1018
- run_ready ( & mut self . handles , Timeout :: Never , false ) . unwrap ( )
1043
+ run_ready ( & mut self . handles , Timeout :: Never , self . biased ) . unwrap ( )
1019
1044
}
1020
1045
1021
1046
/// Blocks for a limited time until one of the operations becomes ready.
@@ -1108,7 +1133,7 @@ impl<'a> Select<'a> {
1108
1133
/// }
1109
1134
/// ```
1110
1135
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 ) {
1112
1137
None => Err ( ReadyTimeoutError ) ,
1113
1138
Some ( index) => Ok ( index) ,
1114
1139
}
@@ -1120,6 +1145,7 @@ impl<'a> Clone for Select<'a> {
1120
1145
Select {
1121
1146
handles : self . handles . clone ( ) ,
1122
1147
next_index : self . next_index ,
1148
+ biased : self . biased ,
1123
1149
}
1124
1150
}
1125
1151
}
0 commit comments