@@ -12,21 +12,20 @@ mod _bisect {
12
12
13
13
#[ derive( FromArgs ) ]
14
14
struct BisectArgs {
15
- #[ pyarg( any, name = "a" ) ]
15
+ #[ pyarg( any) ]
16
16
a : PyObjectRef ,
17
- #[ pyarg( any, name = "x" ) ]
17
+ #[ pyarg( any) ]
18
18
x : PyObjectRef ,
19
- #[ pyarg( any, optional, name = "lo" ) ]
19
+ #[ pyarg( any, optional) ]
20
20
lo : OptionalArg < PyObjectRef > ,
21
- #[ pyarg( any, optional, name = "hi" ) ]
21
+ #[ pyarg( any, optional) ]
22
22
hi : OptionalArg < PyObjectRef > ,
23
23
}
24
24
25
25
// Handles objects that implement __index__ and makes sure index fits in needed isize.
26
26
#[ inline]
27
27
fn handle_default (
28
28
arg : OptionalArg < PyObjectRef > ,
29
- default : Option < isize > ,
30
29
vm : & VirtualMachine ,
31
30
) -> PyResult < Option < isize > > {
32
31
Ok ( match arg {
@@ -35,7 +34,7 @@ mod _bisect {
35
34
vm. new_index_error ( "cannot fit 'int' into an index-sized integer" . to_owned ( ) )
36
35
} ) ?)
37
36
}
38
- OptionalArg :: Missing => default ,
37
+ OptionalArg :: Missing => None ,
39
38
} )
40
39
}
41
40
@@ -55,29 +54,29 @@ mod _bisect {
55
54
) -> PyResult < ( usize , usize ) > {
56
55
// We only deal with positives for lo, try_from can't fail.
57
56
// Default is always a Some so we can safely unwrap.
58
- let lo = handle_default ( lo, Some ( 0 ) , vm) ?
57
+ let lo = handle_default ( lo, vm) ?
59
58
. map ( |value| {
60
- if value < 0 {
61
- return Err ( vm. new_value_error ( "lo must be non-negative" . to_owned ( ) ) ) ;
62
- }
63
- Ok ( usize:: try_from ( value) . unwrap ( ) )
64
- } )
65
- . unwrap ( ) ?;
66
- let hi = handle_default ( hi, None , vm) ?
67
- . map ( |value| {
68
- if value < 0 {
69
- 0
70
- } else {
71
- // Can't fail.
72
- usize:: try_from ( value) . unwrap ( )
73
- }
59
+ usize:: try_from ( value)
60
+ . map_err ( |_| vm. new_value_error ( "lo must be non-negative" . to_owned ( ) ) )
74
61
} )
62
+ . unwrap_or ( Ok ( 0 ) ) ?;
63
+ let hi = handle_default ( hi, vm) ?
64
+ . map ( |value| usize:: try_from ( value) . unwrap_or ( 0 ) )
75
65
. unwrap_or ( seq_len) ;
76
66
Ok ( ( lo, hi) )
77
67
}
78
68
69
+ /// Return the index where to insert item x in list a, assuming a is sorted.
70
+ ///
71
+ /// The return value i is such that all e in a[:i] have e < x, and all e in
72
+ /// a[i:] have e >= x. So if x already appears in the list, a.insert(x) will
73
+ /// insert just before the leftmost x already there.
74
+ ///
75
+ /// Optional args lo (default 0) and hi (default len(a)) bound the
76
+ /// slice of a to be searched.
79
77
#[ inline]
80
- fn bisect_left_impl (
78
+ #[ pyfunction]
79
+ fn bisect_left (
81
80
BisectArgs { a, x, lo, hi } : BisectArgs ,
82
81
vm : & VirtualMachine ,
83
82
) -> PyResult < usize > {
@@ -95,8 +94,17 @@ mod _bisect {
95
94
Ok ( lo)
96
95
}
97
96
97
+ /// Return the index where to insert item x in list a, assuming a is sorted.
98
+ ///
99
+ /// The return value i is such that all e in a[:i] have e <= x, and all e in
100
+ /// a[i:] have e > x. So if x already appears in the list, a.insert(x) will
101
+ /// insert just after the rightmost x already there.
102
+ ///
103
+ /// Optional args lo (default 0) and hi (default len(a)) bound the
104
+ /// slice of a to be searched.
98
105
#[ inline]
99
- fn bisect_right_impl (
106
+ #[ pyfunction]
107
+ fn bisect_right (
100
108
BisectArgs { a, x, lo, hi } : BisectArgs ,
101
109
vm : & VirtualMachine ,
102
110
) -> PyResult < usize > {
@@ -114,32 +122,6 @@ mod _bisect {
114
122
Ok ( lo)
115
123
}
116
124
117
- /// Return the index where to insert item x in list a, assuming a is sorted.
118
- ///
119
- /// The return value i is such that all e in a[:i] have e < x, and all e in
120
- /// a[i:] have e >= x. So if x already appears in the list, a.insert(x) will
121
- /// insert just before the leftmost x already there.
122
- ///
123
- /// Optional args lo (default 0) and hi (default len(a)) bound the
124
- /// slice of a to be searched.
125
- #[ pyfunction]
126
- fn bisect_left ( args : BisectArgs , vm : & VirtualMachine ) -> PyResult < usize > {
127
- bisect_left_impl ( args, vm)
128
- }
129
-
130
- /// Return the index where to insert item x in list a, assuming a is sorted.
131
- ///
132
- /// The return value i is such that all e in a[:i] have e <= x, and all e in
133
- /// a[i:] have e > x. So if x already appears in the list, a.insert(x) will
134
- /// insert just after the rightmost x already there.
135
- ///
136
- /// Optional args lo (default 0) and hi (default len(a)) bound the
137
- /// slice of a to be searched.
138
- #[ pyfunction]
139
- fn bisect_right ( args : BisectArgs , vm : & VirtualMachine ) -> PyResult < usize > {
140
- bisect_right_impl ( args, vm)
141
- }
142
-
143
125
/// Insert item x in list a, and keep it sorted assuming a is sorted.
144
126
///
145
127
/// If x is already in a, insert it to the left of the leftmost x.
@@ -148,7 +130,7 @@ mod _bisect {
148
130
/// slice of a to be searched.
149
131
#[ pyfunction]
150
132
fn insort_left ( BisectArgs { a, x, lo, hi } : BisectArgs , vm : & VirtualMachine ) -> PyResult {
151
- let index = bisect_left_impl (
133
+ let index = bisect_left (
152
134
BisectArgs {
153
135
a : a. clone ( ) ,
154
136
x : x. clone ( ) ,
@@ -168,7 +150,7 @@ mod _bisect {
168
150
/// slice of a to be searched
169
151
#[ pyfunction]
170
152
fn insort_right ( BisectArgs { a, x, lo, hi } : BisectArgs , vm : & VirtualMachine ) -> PyResult {
171
- let index = bisect_right_impl (
153
+ let index = bisect_right (
172
154
BisectArgs {
173
155
a : a. clone ( ) ,
174
156
x : x. clone ( ) ,
0 commit comments