Skip to content

Commit 84b5475

Browse files
committed
obj::objbyteinner::PyByteInner -> bytesinner::PyBytesInner
1 parent 5075749 commit 84b5475

19 files changed

+158
-153
lines changed

vm/src/builtins.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ mod decl {
1414
use rustpython_parser::parser;
1515

1616
use super::to_ascii;
17+
use crate::bytesinner::PyBytesInner;
1718
use crate::exceptions::PyBaseExceptionRef;
1819
use crate::function::{single_or_tuple_any, Args, KwArgs, OptionalArg, PyFuncArgs};
1920
use crate::obj::objbool::{self, IntoPyBool};
20-
use crate::obj::objbyteinner::PyByteInner;
2121
use crate::obj::objbytes::PyBytesRef;
2222
use crate::obj::objcode::PyCodeRef;
2323
use crate::obj::objdict::PyDictRef;
@@ -554,7 +554,7 @@ mod decl {
554554
}
555555

556556
#[pyfunction]
557-
fn ord(string: Either<PyByteInner, PyStringRef>, vm: &VirtualMachine) -> PyResult<u32> {
557+
fn ord(string: Either<PyBytesInner, PyStringRef>, vm: &VirtualMachine) -> PyResult<u32> {
558558
match string {
559559
Either::A(bytes) => {
560560
let bytes_len = bytes.elements.len();

vm/src/obj/objbyteinner.rs renamed to vm/src/bytesinner.rs

Lines changed: 48 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -3,44 +3,45 @@ use num_bigint::{BigInt, ToBigInt};
33
use num_traits::{One, Signed, ToPrimitive, Zero};
44
use std::ops::Range;
55

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;
167
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};
1718
use crate::pyobject::{
1819
Either, PyComparisonValue, PyIterable, PyObjectRef, PyResult, TryFromObject, TypeProtocol,
1920
};
2021
use crate::vm::VirtualMachine;
2122
use rustpython_common::hash;
2223

2324
#[derive(Debug, Default, Clone)]
24-
pub struct PyByteInner {
25+
pub struct PyBytesInner {
2526
pub elements: Vec<u8>,
2627
}
2728

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 {
3031
Self { elements }
3132
}
3233
}
3334

34-
impl TryFromObject for PyByteInner {
35+
impl TryFromObject for PyBytesInner {
3536
fn try_from_object(vm: &VirtualMachine, obj: PyObjectRef) -> PyResult<Self> {
3637
match_class!(match obj {
37-
i @ PyBytes => Ok(PyByteInner {
38+
i @ PyBytes => Ok(PyBytesInner {
3839
elements: i.get_value().to_vec()
3940
}),
40-
j @ PyByteArray => Ok(PyByteInner {
41+
j @ PyByteArray => Ok(PyBytesInner {
4142
elements: j.borrow_value().elements.to_vec()
4243
}),
43-
k @ PyMemoryView => Ok(PyByteInner {
44+
k @ PyMemoryView => Ok(PyBytesInner {
4445
elements: k.try_value().unwrap()
4546
}),
4647
l @ PyList => l.get_byte_inner(vm),
@@ -49,7 +50,7 @@ impl TryFromObject for PyByteInner {
4950
format!("a bytes-like object is required, not {}", obj.class())
5051
})?;
5152
let iter = PyIterable::from_method(iter);
52-
Ok(PyByteInner {
53+
Ok(PyBytesInner {
5354
elements: iter.iter(vm)?.collect::<PyResult<_>>()?,
5455
})
5556
}
@@ -66,13 +67,13 @@ pub struct ByteInnerNewOptions {
6667
}
6768

6869
impl ByteInnerNewOptions {
69-
pub fn get_value(self, vm: &VirtualMachine) -> PyResult<PyByteInner> {
70+
pub fn get_value(self, vm: &VirtualMachine) -> PyResult<PyBytesInner> {
7071
// First handle bytes(string, encoding[, errors])
7172
if let OptionalArg::Present(enc) = self.encoding {
7273
if let OptionalArg::Present(eval) = self.val_option {
7374
if let Ok(input) = eval.downcast::<PyString>() {
7475
let bytes = objstr::encode_string(input, Some(enc), None, vm)?;
75-
Ok(PyByteInner {
76+
Ok(PyBytesInner {
7677
elements: bytes.get_value().to_vec(),
7778
})
7879
} else {
@@ -112,7 +113,7 @@ impl ByteInnerNewOptions {
112113
// TODO: only support this method in the bytes() constructor
113114
if let Some(bytes_method) = vm.get_method(obj.clone(), "__bytes__") {
114115
let bytes = vm.invoke(&bytes_method?, vec![])?;
115-
return PyByteInner::try_from_object(vm, bytes);
116+
return PyBytesInner::try_from_object(vm, bytes);
116117
}
117118
let elements = vm.extract_elements(&obj).map_err(|_| {
118119
vm.new_type_error(format!(
@@ -139,7 +140,7 @@ impl ByteInnerNewOptions {
139140
Ok(vec![])
140141
};
141142
match value {
142-
Ok(val) => Ok(PyByteInner { elements: val }),
143+
Ok(val) => Ok(PyBytesInner { elements: val }),
143144
Err(err) => Err(err),
144145
}
145146
}
@@ -149,7 +150,7 @@ impl ByteInnerNewOptions {
149150
#[derive(FromArgs)]
150151
pub struct ByteInnerFindOptions {
151152
#[pyarg(positional_only, optional = false)]
152-
sub: Either<PyByteInner, PyIntRef>,
153+
sub: Either<PyBytesInner, PyIntRef>,
153154
#[pyarg(positional_only, default = "None")]
154155
start: Option<PyIntRef>,
155156
#[pyarg(positional_only, default = "None")]
@@ -202,9 +203,9 @@ impl ByteInnerPaddingOptions {
202203
#[derive(FromArgs)]
203204
pub struct ByteInnerTranslateOptions {
204205
#[pyarg(positional_only, optional = false)]
205-
table: Either<PyByteInner, PyNoneRef>,
206+
table: Either<PyBytesInner, PyNoneRef>,
206207
#[pyarg(positional_or_keyword, optional = true)]
207-
delete: OptionalArg<PyByteInner>,
208+
delete: OptionalArg<PyBytesInner>,
208209
}
209210

210211
impl ByteInnerTranslateOptions {
@@ -229,10 +230,10 @@ impl ByteInnerTranslateOptions {
229230
}
230231
}
231232

232-
pub type ByteInnerSplitOptions<'a> = pystr::SplitArgs<'a, PyByteInner, [u8], u8>;
233+
pub type ByteInnerSplitOptions<'a> = pystr::SplitArgs<'a, PyBytesInner, [u8], u8>;
233234

234235
#[allow(clippy::len_without_is_empty)]
235-
impl PyByteInner {
236+
impl PyBytesInner {
236237
pub fn repr(&self) -> String {
237238
let mut res = String::with_capacity(self.elements.len());
238239
for i in self.elements.iter() {
@@ -287,13 +288,13 @@ impl PyByteInner {
287288
hash::hash_value(&self.elements)
288289
}
289290

290-
pub fn add(&self, other: PyByteInner) -> Vec<u8> {
291+
pub fn add(&self, other: PyBytesInner) -> Vec<u8> {
291292
self.elements.py_add(&other.elements)
292293
}
293294

294295
pub fn contains(
295296
&self,
296-
needle: Either<PyByteInner, PyIntRef>,
297+
needle: Either<PyBytesInner, PyIntRef>,
297298
vm: &VirtualMachine,
298299
) -> PyResult<bool> {
299300
Ok(match needle {
@@ -698,7 +699,7 @@ impl PyByteInner {
698699

699700
pub fn join(
700701
&self,
701-
iterable: PyIterable<PyByteInner>,
702+
iterable: PyIterable<PyBytesInner>,
702703
vm: &VirtualMachine,
703704
) -> PyResult<Vec<u8>> {
704705
let iter = iterable.iter(vm)?;
@@ -719,7 +720,7 @@ impl PyByteInner {
719720
Ok(self.elements.py_find(&needle, range, find))
720721
}
721722

722-
pub fn maketrans(from: PyByteInner, to: PyByteInner, vm: &VirtualMachine) -> PyResult {
723+
pub fn maketrans(from: PyBytesInner, to: PyBytesInner, vm: &VirtualMachine) -> PyResult {
723724
let mut res = vec![];
724725

725726
for i in 0..=255 {
@@ -757,7 +758,7 @@ impl PyByteInner {
757758
Ok(res)
758759
}
759760

760-
pub fn strip(&self, chars: OptionalOption<PyByteInner>) -> Vec<u8> {
761+
pub fn strip(&self, chars: OptionalOption<PyBytesInner>) -> Vec<u8> {
761762
self.elements
762763
.py_strip(
763764
chars,
@@ -767,7 +768,7 @@ impl PyByteInner {
767768
.to_vec()
768769
}
769770

770-
pub fn lstrip(&self, chars: OptionalOption<PyByteInner>) -> Vec<u8> {
771+
pub fn lstrip(&self, chars: OptionalOption<PyBytesInner>) -> Vec<u8> {
771772
self.elements
772773
.py_strip(
773774
chars,
@@ -777,7 +778,7 @@ impl PyByteInner {
777778
.to_vec()
778779
}
779780

780-
pub fn rstrip(&self, chars: OptionalOption<PyByteInner>) -> Vec<u8> {
781+
pub fn rstrip(&self, chars: OptionalOption<PyBytesInner>) -> Vec<u8> {
781782
self.elements
782783
.py_strip(
783784
chars,
@@ -788,7 +789,7 @@ impl PyByteInner {
788789
}
789790

790791
// new in Python 3.9
791-
pub fn removeprefix(&self, prefix: PyByteInner) -> Vec<u8> {
792+
pub fn removeprefix(&self, prefix: PyBytesInner) -> Vec<u8> {
792793
self.elements
793794
.py_removeprefix(&prefix.elements, prefix.elements.len(), |s, p| {
794795
s.starts_with(p)
@@ -797,7 +798,7 @@ impl PyByteInner {
797798
}
798799

799800
// new in Python 3.9
800-
pub fn removesuffix(&self, suffix: PyByteInner) -> Vec<u8> {
801+
pub fn removesuffix(&self, suffix: PyBytesInner) -> Vec<u8> {
801802
self.elements
802803
.py_removesuffix(&suffix.elements, suffix.elements.len(), |s, p| {
803804
s.ends_with(p)
@@ -846,7 +847,7 @@ impl PyByteInner {
846847

847848
pub fn partition(
848849
&self,
849-
sub: &PyByteInner,
850+
sub: &PyBytesInner,
850851
vm: &VirtualMachine,
851852
) -> PyResult<(Vec<u8>, bool, Vec<u8>)> {
852853
self.elements.py_partition(
@@ -858,7 +859,7 @@ impl PyByteInner {
858859

859860
pub fn rpartition(
860861
&self,
861-
sub: &PyByteInner,
862+
sub: &PyBytesInner,
862863
vm: &VirtualMachine,
863864
) -> PyResult<(Vec<u8>, bool, Vec<u8>)> {
864865
self.elements.py_partition(
@@ -912,7 +913,7 @@ impl PyByteInner {
912913
}
913914

914915
// 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> {
916917
let place_count = self.elements.len() + 1;
917918
let count = maxcount.map_or(place_count, |v| std::cmp::min(v, place_count)) - 1;
918919
let capacity = self.elements.len() + count * to.len();
@@ -927,7 +928,7 @@ impl PyByteInner {
927928
result
928929
}
929930

930-
fn replace_delete(&self, from: PyByteInner, maxcount: Option<usize>) -> Vec<u8> {
931+
fn replace_delete(&self, from: PyBytesInner, maxcount: Option<usize>) -> Vec<u8> {
931932
let count = count_substring(self.elements.as_slice(), from.elements.as_slice(), maxcount);
932933
if count == 0 {
933934
// no matches
@@ -954,8 +955,8 @@ impl PyByteInner {
954955

955956
pub fn replace_in_place(
956957
&self,
957-
from: PyByteInner,
958-
to: PyByteInner,
958+
from: PyBytesInner,
959+
to: PyBytesInner,
959960
maxcount: Option<usize>,
960961
) -> Vec<u8> {
961962
let len = from.len();
@@ -986,8 +987,8 @@ impl PyByteInner {
986987

987988
fn replace_general(
988989
&self,
989-
from: PyByteInner,
990-
to: PyByteInner,
990+
from: PyBytesInner,
991+
to: PyBytesInner,
991992
maxcount: Option<usize>,
992993
vm: &VirtualMachine,
993994
) -> PyResult<Vec<u8>> {
@@ -1027,8 +1028,8 @@ impl PyByteInner {
10271028

10281029
pub fn replace(
10291030
&self,
1030-
from: PyByteInner,
1031-
to: PyByteInner,
1031+
from: PyBytesInner,
1032+
to: PyBytesInner,
10321033
maxcount: OptionalArg<isize>,
10331034
vm: &VirtualMachine,
10341035
) -> PyResult<Vec<u8>> {
@@ -1157,42 +1158,7 @@ pub trait ByteOr: ToPrimitive {
11571158

11581159
impl ByteOr for BigInt {}
11591160

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 {
11961162
fn as_ref(&self) -> &[u8] {
11971163
&self.elements
11981164
}

vm/src/byteslike.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
use crate::obj::objbytearray::{PyByteArray, PyByteArrayRef};
2+
use crate::obj::objbytes::{PyBytes, PyBytesRef};
3+
use crate::pyobject::PyObjectRef;
4+
use crate::pyobject::{PyResult, TryFromObject, TypeProtocol};
5+
use crate::vm::VirtualMachine;
6+
7+
pub enum PyBytesLike {
8+
Bytes(PyBytesRef),
9+
Bytearray(PyByteArrayRef),
10+
}
11+
12+
impl TryFromObject for PyBytesLike {
13+
fn try_from_object(vm: &VirtualMachine, obj: PyObjectRef) -> PyResult<Self> {
14+
match_class!(match obj {
15+
b @ PyBytes => Ok(PyBytesLike::Bytes(b)),
16+
b @ PyByteArray => Ok(PyBytesLike::Bytearray(b)),
17+
obj => Err(vm.new_type_error(format!(
18+
"a bytes-like object is required, not {}",
19+
obj.class()
20+
))),
21+
})
22+
}
23+
}
24+
25+
impl PyBytesLike {
26+
pub fn to_cow(&self) -> std::borrow::Cow<[u8]> {
27+
match self {
28+
PyBytesLike::Bytes(b) => b.get_value().into(),
29+
PyBytesLike::Bytearray(b) => b.borrow_value().elements.clone().into(),
30+
}
31+
}
32+
33+
#[inline]
34+
pub fn with_ref<R>(&self, f: impl FnOnce(&[u8]) -> R) -> R {
35+
match self {
36+
PyBytesLike::Bytes(b) => f(b.get_value()),
37+
PyBytesLike::Bytearray(b) => f(&b.borrow_value().elements),
38+
}
39+
}
40+
}

0 commit comments

Comments
 (0)