@@ -3,44 +3,45 @@ use num_bigint::{BigInt, ToBigInt};
3
3
use num_traits:: { One , Signed , ToPrimitive , Zero } ;
4
4
use std:: ops:: Range ;
5
5
6
- use super :: objbytearray:: { PyByteArray , PyByteArrayRef } ;
7
- use super :: objbytes:: { PyBytes , PyBytesRef } ;
8
- use super :: objint:: { self , PyInt , PyIntRef } ;
9
- use super :: objlist:: PyList ;
10
- use super :: objmemory:: PyMemoryView ;
11
- use super :: objnone:: PyNoneRef ;
12
- use super :: objsequence:: { PySliceableSequence , SequenceIndex } ;
13
- use super :: objslice:: PySliceRef ;
14
- use super :: objstr:: { self , PyString , PyStringRef } ;
15
- use super :: pystr:: { self , PyCommonString , PyCommonStringContainer , PyCommonStringWrapper } ;
6
+ use crate :: byteslike:: PyBytesLike ;
16
7
use crate :: function:: { OptionalArg , OptionalOption } ;
8
+ use crate :: obj:: objbytearray:: PyByteArray ;
9
+ use crate :: obj:: objbytes:: PyBytes ;
10
+ use crate :: obj:: objint:: { self , PyInt , PyIntRef } ;
11
+ use crate :: obj:: objlist:: PyList ;
12
+ use crate :: obj:: objmemory:: PyMemoryView ;
13
+ use crate :: obj:: objnone:: PyNoneRef ;
14
+ use crate :: obj:: objsequence:: { PySliceableSequence , SequenceIndex } ;
15
+ use crate :: obj:: objslice:: PySliceRef ;
16
+ use crate :: obj:: objstr:: { self , PyString , PyStringRef } ;
17
+ use crate :: obj:: pystr:: { self , PyCommonString , PyCommonStringContainer , PyCommonStringWrapper } ;
17
18
use crate :: pyobject:: {
18
19
Either , PyComparisonValue , PyIterable , PyObjectRef , PyResult , TryFromObject , TypeProtocol ,
19
20
} ;
20
21
use crate :: vm:: VirtualMachine ;
21
22
use rustpython_common:: hash;
22
23
23
24
#[ derive( Debug , Default , Clone ) ]
24
- pub struct PyByteInner {
25
+ pub struct PyBytesInner {
25
26
pub elements : Vec < u8 > ,
26
27
}
27
28
28
- impl From < Vec < u8 > > for PyByteInner {
29
- fn from ( elements : Vec < u8 > ) -> PyByteInner {
29
+ impl From < Vec < u8 > > for PyBytesInner {
30
+ fn from ( elements : Vec < u8 > ) -> PyBytesInner {
30
31
Self { elements }
31
32
}
32
33
}
33
34
34
- impl TryFromObject for PyByteInner {
35
+ impl TryFromObject for PyBytesInner {
35
36
fn try_from_object ( vm : & VirtualMachine , obj : PyObjectRef ) -> PyResult < Self > {
36
37
match_class ! ( match obj {
37
- i @ PyBytes => Ok ( PyByteInner {
38
+ i @ PyBytes => Ok ( PyBytesInner {
38
39
elements: i. get_value( ) . to_vec( )
39
40
} ) ,
40
- j @ PyByteArray => Ok ( PyByteInner {
41
+ j @ PyByteArray => Ok ( PyBytesInner {
41
42
elements: j. borrow_value( ) . elements. to_vec( )
42
43
} ) ,
43
- k @ PyMemoryView => Ok ( PyByteInner {
44
+ k @ PyMemoryView => Ok ( PyBytesInner {
44
45
elements: k. try_value( ) . unwrap( )
45
46
} ) ,
46
47
l @ PyList => l. get_byte_inner( vm) ,
@@ -49,7 +50,7 @@ impl TryFromObject for PyByteInner {
49
50
format!( "a bytes-like object is required, not {}" , obj. class( ) )
50
51
} ) ?;
51
52
let iter = PyIterable :: from_method( iter) ;
52
- Ok ( PyByteInner {
53
+ Ok ( PyBytesInner {
53
54
elements: iter. iter( vm) ?. collect:: <PyResult <_>>( ) ?,
54
55
} )
55
56
}
@@ -66,13 +67,13 @@ pub struct ByteInnerNewOptions {
66
67
}
67
68
68
69
impl ByteInnerNewOptions {
69
- pub fn get_value ( self , vm : & VirtualMachine ) -> PyResult < PyByteInner > {
70
+ pub fn get_value ( self , vm : & VirtualMachine ) -> PyResult < PyBytesInner > {
70
71
// First handle bytes(string, encoding[, errors])
71
72
if let OptionalArg :: Present ( enc) = self . encoding {
72
73
if let OptionalArg :: Present ( eval) = self . val_option {
73
74
if let Ok ( input) = eval. downcast :: < PyString > ( ) {
74
75
let bytes = objstr:: encode_string ( input, Some ( enc) , None , vm) ?;
75
- Ok ( PyByteInner {
76
+ Ok ( PyBytesInner {
76
77
elements : bytes. get_value ( ) . to_vec ( ) ,
77
78
} )
78
79
} else {
@@ -112,7 +113,7 @@ impl ByteInnerNewOptions {
112
113
// TODO: only support this method in the bytes() constructor
113
114
if let Some ( bytes_method) = vm. get_method( obj. clone( ) , "__bytes__" ) {
114
115
let bytes = vm. invoke( & bytes_method?, vec![ ] ) ?;
115
- return PyByteInner :: try_from_object( vm, bytes) ;
116
+ return PyBytesInner :: try_from_object( vm, bytes) ;
116
117
}
117
118
let elements = vm. extract_elements( & obj) . map_err( |_| {
118
119
vm. new_type_error( format!(
@@ -139,7 +140,7 @@ impl ByteInnerNewOptions {
139
140
Ok ( vec ! [ ] )
140
141
} ;
141
142
match value {
142
- Ok ( val) => Ok ( PyByteInner { elements : val } ) ,
143
+ Ok ( val) => Ok ( PyBytesInner { elements : val } ) ,
143
144
Err ( err) => Err ( err) ,
144
145
}
145
146
}
@@ -149,7 +150,7 @@ impl ByteInnerNewOptions {
149
150
#[ derive( FromArgs ) ]
150
151
pub struct ByteInnerFindOptions {
151
152
#[ pyarg( positional_only, optional = false ) ]
152
- sub : Either < PyByteInner , PyIntRef > ,
153
+ sub : Either < PyBytesInner , PyIntRef > ,
153
154
#[ pyarg( positional_only, default = "None" ) ]
154
155
start : Option < PyIntRef > ,
155
156
#[ pyarg( positional_only, default = "None" ) ]
@@ -202,9 +203,9 @@ impl ByteInnerPaddingOptions {
202
203
#[ derive( FromArgs ) ]
203
204
pub struct ByteInnerTranslateOptions {
204
205
#[ pyarg( positional_only, optional = false ) ]
205
- table : Either < PyByteInner , PyNoneRef > ,
206
+ table : Either < PyBytesInner , PyNoneRef > ,
206
207
#[ pyarg( positional_or_keyword, optional = true ) ]
207
- delete : OptionalArg < PyByteInner > ,
208
+ delete : OptionalArg < PyBytesInner > ,
208
209
}
209
210
210
211
impl ByteInnerTranslateOptions {
@@ -229,10 +230,10 @@ impl ByteInnerTranslateOptions {
229
230
}
230
231
}
231
232
232
- pub type ByteInnerSplitOptions < ' a > = pystr:: SplitArgs < ' a , PyByteInner , [ u8 ] , u8 > ;
233
+ pub type ByteInnerSplitOptions < ' a > = pystr:: SplitArgs < ' a , PyBytesInner , [ u8 ] , u8 > ;
233
234
234
235
#[ allow( clippy:: len_without_is_empty) ]
235
- impl PyByteInner {
236
+ impl PyBytesInner {
236
237
pub fn repr ( & self ) -> String {
237
238
let mut res = String :: with_capacity ( self . elements . len ( ) ) ;
238
239
for i in self . elements . iter ( ) {
@@ -287,13 +288,13 @@ impl PyByteInner {
287
288
hash:: hash_value ( & self . elements )
288
289
}
289
290
290
- pub fn add ( & self , other : PyByteInner ) -> Vec < u8 > {
291
+ pub fn add ( & self , other : PyBytesInner ) -> Vec < u8 > {
291
292
self . elements . py_add ( & other. elements )
292
293
}
293
294
294
295
pub fn contains (
295
296
& self ,
296
- needle : Either < PyByteInner , PyIntRef > ,
297
+ needle : Either < PyBytesInner , PyIntRef > ,
297
298
vm : & VirtualMachine ,
298
299
) -> PyResult < bool > {
299
300
Ok ( match needle {
@@ -698,7 +699,7 @@ impl PyByteInner {
698
699
699
700
pub fn join (
700
701
& self ,
701
- iterable : PyIterable < PyByteInner > ,
702
+ iterable : PyIterable < PyBytesInner > ,
702
703
vm : & VirtualMachine ,
703
704
) -> PyResult < Vec < u8 > > {
704
705
let iter = iterable. iter ( vm) ?;
@@ -719,7 +720,7 @@ impl PyByteInner {
719
720
Ok ( self . elements . py_find ( & needle, range, find) )
720
721
}
721
722
722
- pub fn maketrans ( from : PyByteInner , to : PyByteInner , vm : & VirtualMachine ) -> PyResult {
723
+ pub fn maketrans ( from : PyBytesInner , to : PyBytesInner , vm : & VirtualMachine ) -> PyResult {
723
724
let mut res = vec ! [ ] ;
724
725
725
726
for i in 0 ..=255 {
@@ -757,7 +758,7 @@ impl PyByteInner {
757
758
Ok ( res)
758
759
}
759
760
760
- pub fn strip ( & self , chars : OptionalOption < PyByteInner > ) -> Vec < u8 > {
761
+ pub fn strip ( & self , chars : OptionalOption < PyBytesInner > ) -> Vec < u8 > {
761
762
self . elements
762
763
. py_strip (
763
764
chars,
@@ -767,7 +768,7 @@ impl PyByteInner {
767
768
. to_vec ( )
768
769
}
769
770
770
- pub fn lstrip ( & self , chars : OptionalOption < PyByteInner > ) -> Vec < u8 > {
771
+ pub fn lstrip ( & self , chars : OptionalOption < PyBytesInner > ) -> Vec < u8 > {
771
772
self . elements
772
773
. py_strip (
773
774
chars,
@@ -777,7 +778,7 @@ impl PyByteInner {
777
778
. to_vec ( )
778
779
}
779
780
780
- pub fn rstrip ( & self , chars : OptionalOption < PyByteInner > ) -> Vec < u8 > {
781
+ pub fn rstrip ( & self , chars : OptionalOption < PyBytesInner > ) -> Vec < u8 > {
781
782
self . elements
782
783
. py_strip (
783
784
chars,
@@ -788,7 +789,7 @@ impl PyByteInner {
788
789
}
789
790
790
791
// new in Python 3.9
791
- pub fn removeprefix ( & self , prefix : PyByteInner ) -> Vec < u8 > {
792
+ pub fn removeprefix ( & self , prefix : PyBytesInner ) -> Vec < u8 > {
792
793
self . elements
793
794
. py_removeprefix ( & prefix. elements , prefix. elements . len ( ) , |s, p| {
794
795
s. starts_with ( p)
@@ -797,7 +798,7 @@ impl PyByteInner {
797
798
}
798
799
799
800
// new in Python 3.9
800
- pub fn removesuffix ( & self , suffix : PyByteInner ) -> Vec < u8 > {
801
+ pub fn removesuffix ( & self , suffix : PyBytesInner ) -> Vec < u8 > {
801
802
self . elements
802
803
. py_removesuffix ( & suffix. elements , suffix. elements . len ( ) , |s, p| {
803
804
s. ends_with ( p)
@@ -846,7 +847,7 @@ impl PyByteInner {
846
847
847
848
pub fn partition (
848
849
& self ,
849
- sub : & PyByteInner ,
850
+ sub : & PyBytesInner ,
850
851
vm : & VirtualMachine ,
851
852
) -> PyResult < ( Vec < u8 > , bool , Vec < u8 > ) > {
852
853
self . elements . py_partition (
@@ -858,7 +859,7 @@ impl PyByteInner {
858
859
859
860
pub fn rpartition (
860
861
& self ,
861
- sub : & PyByteInner ,
862
+ sub : & PyBytesInner ,
862
863
vm : & VirtualMachine ,
863
864
) -> PyResult < ( Vec < u8 > , bool , Vec < u8 > ) > {
864
865
self . elements . py_partition (
@@ -912,7 +913,7 @@ impl PyByteInner {
912
913
}
913
914
914
915
// len(self)>=1, from="", len(to)>=1, maxcount>=1
915
- fn replace_interleave ( & self , to : PyByteInner , maxcount : Option < usize > ) -> Vec < u8 > {
916
+ fn replace_interleave ( & self , to : PyBytesInner , maxcount : Option < usize > ) -> Vec < u8 > {
916
917
let place_count = self . elements . len ( ) + 1 ;
917
918
let count = maxcount. map_or ( place_count, |v| std:: cmp:: min ( v, place_count) ) - 1 ;
918
919
let capacity = self . elements . len ( ) + count * to. len ( ) ;
@@ -927,7 +928,7 @@ impl PyByteInner {
927
928
result
928
929
}
929
930
930
- fn replace_delete ( & self , from : PyByteInner , maxcount : Option < usize > ) -> Vec < u8 > {
931
+ fn replace_delete ( & self , from : PyBytesInner , maxcount : Option < usize > ) -> Vec < u8 > {
931
932
let count = count_substring ( self . elements . as_slice ( ) , from. elements . as_slice ( ) , maxcount) ;
932
933
if count == 0 {
933
934
// no matches
@@ -954,8 +955,8 @@ impl PyByteInner {
954
955
955
956
pub fn replace_in_place (
956
957
& self ,
957
- from : PyByteInner ,
958
- to : PyByteInner ,
958
+ from : PyBytesInner ,
959
+ to : PyBytesInner ,
959
960
maxcount : Option < usize > ,
960
961
) -> Vec < u8 > {
961
962
let len = from. len ( ) ;
@@ -986,8 +987,8 @@ impl PyByteInner {
986
987
987
988
fn replace_general (
988
989
& self ,
989
- from : PyByteInner ,
990
- to : PyByteInner ,
990
+ from : PyBytesInner ,
991
+ to : PyBytesInner ,
991
992
maxcount : Option < usize > ,
992
993
vm : & VirtualMachine ,
993
994
) -> PyResult < Vec < u8 > > {
@@ -1027,8 +1028,8 @@ impl PyByteInner {
1027
1028
1028
1029
pub fn replace (
1029
1030
& self ,
1030
- from : PyByteInner ,
1031
- to : PyByteInner ,
1031
+ from : PyBytesInner ,
1032
+ to : PyBytesInner ,
1032
1033
maxcount : OptionalArg < isize > ,
1033
1034
vm : & VirtualMachine ,
1034
1035
) -> PyResult < Vec < u8 > > {
@@ -1157,42 +1158,7 @@ pub trait ByteOr: ToPrimitive {
1157
1158
1158
1159
impl ByteOr for BigInt { }
1159
1160
1160
- pub enum PyBytesLike {
1161
- Bytes ( PyBytesRef ) ,
1162
- Bytearray ( PyByteArrayRef ) ,
1163
- }
1164
-
1165
- impl TryFromObject for PyBytesLike {
1166
- fn try_from_object ( vm : & VirtualMachine , obj : PyObjectRef ) -> PyResult < Self > {
1167
- match_class ! ( match obj {
1168
- b @ PyBytes => Ok ( PyBytesLike :: Bytes ( b) ) ,
1169
- b @ PyByteArray => Ok ( PyBytesLike :: Bytearray ( b) ) ,
1170
- obj => Err ( vm. new_type_error( format!(
1171
- "a bytes-like object is required, not {}" ,
1172
- obj. class( )
1173
- ) ) ) ,
1174
- } )
1175
- }
1176
- }
1177
-
1178
- impl PyBytesLike {
1179
- pub fn to_cow ( & self ) -> std:: borrow:: Cow < [ u8 ] > {
1180
- match self {
1181
- PyBytesLike :: Bytes ( b) => b. get_value ( ) . into ( ) ,
1182
- PyBytesLike :: Bytearray ( b) => b. borrow_value ( ) . elements . clone ( ) . into ( ) ,
1183
- }
1184
- }
1185
-
1186
- #[ inline]
1187
- pub fn with_ref < R > ( & self , f : impl FnOnce ( & [ u8 ] ) -> R ) -> R {
1188
- match self {
1189
- PyBytesLike :: Bytes ( b) => f ( b. get_value ( ) ) ,
1190
- PyBytesLike :: Bytearray ( b) => f ( & b. borrow_value ( ) . elements ) ,
1191
- }
1192
- }
1193
- }
1194
-
1195
- impl PyCommonStringWrapper < [ u8 ] > for PyByteInner {
1161
+ impl PyCommonStringWrapper < [ u8 ] > for PyBytesInner {
1196
1162
fn as_ref ( & self ) -> & [ u8 ] {
1197
1163
& self . elements
1198
1164
}
0 commit comments