Skip to content

Commit aaa364d

Browse files
committed
narrow publicity of BytesInner::elements
1 parent a0c34da commit aaa364d

File tree

4 files changed

+24
-29
lines changed

4 files changed

+24
-29
lines changed

vm/src/builtins/bytearray.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,7 @@ impl PyByteArray {
442442
affix,
443443
"endswith",
444444
"bytes",
445-
|s, x: &PyBytesInner| s.ends_with(&x.elements[..]),
445+
|s, x: &PyBytesInner| s.ends_with(x.as_bytes()),
446446
vm,
447447
)
448448
}
@@ -463,7 +463,7 @@ impl PyByteArray {
463463
affix,
464464
"startswith",
465465
"bytes",
466-
|s, x: &PyBytesInner| s.starts_with(&x.elements[..]),
466+
|s, x: &PyBytesInner| s.starts_with(x.as_bytes()),
467467
vm,
468468
)
469469
}

vm/src/builtins/bytes.rs

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -61,18 +61,18 @@ impl Deref for PyBytes {
6161
type Target = [u8];
6262

6363
fn deref(&self) -> &[u8] {
64-
&self.inner.elements
64+
self.as_bytes()
6565
}
6666
}
6767

6868
impl AsRef<[u8]> for PyBytes {
6969
fn as_ref(&self) -> &[u8] {
70-
&self.inner.elements
70+
self.as_bytes()
7171
}
7272
}
7373
impl AsRef<[u8]> for PyBytesRef {
7474
fn as_ref(&self) -> &[u8] {
75-
&self.inner.elements
75+
self.as_bytes()
7676
}
7777
}
7878

@@ -133,7 +133,7 @@ impl PyBytes {
133133

134134
#[inline]
135135
pub fn as_bytes(&self) -> &[u8] {
136-
&self.inner.elements
136+
self.inner.as_bytes()
137137
}
138138

139139
#[pymethod(magic)]
@@ -147,7 +147,7 @@ impl PyBytes {
147147

148148
#[pymethod(magic)]
149149
fn sizeof(&self) -> usize {
150-
size_of::<Self>() + self.inner.elements.len() * size_of::<u8>()
150+
size_of::<Self>() + self.len() * size_of::<u8>()
151151
}
152152

153153
#[pymethod(magic)]
@@ -172,13 +172,9 @@ impl PyBytes {
172172
fn _getitem(&self, needle: &PyObject, vm: &VirtualMachine) -> PyResult {
173173
match SequenceIndex::try_from_borrowed_object(vm, needle, "byte")? {
174174
SequenceIndex::Int(i) => self
175-
.inner
176-
.elements
177175
.getitem_by_index(vm, i)
178176
.map(|x| vm.ctx.new_int(x).into()),
179177
SequenceIndex::Slice(slice) => self
180-
.inner
181-
.elements
182178
.getitem_by_slice(vm, slice)
183179
.map(|x| vm.ctx.new_bytes(x).into()),
184180
}
@@ -294,15 +290,15 @@ impl PyBytes {
294290
#[pymethod]
295291
fn endswith(&self, options: anystr::StartsEndsWithArgs, vm: &VirtualMachine) -> PyResult<bool> {
296292
let (affix, substr) =
297-
match options.prepare(&self.inner.elements[..], self.len(), |s, r| s.get_bytes(r)) {
293+
match options.prepare(self.as_bytes(), self.len(), |s, r| s.get_bytes(r)) {
298294
Some(x) => x,
299295
None => return Ok(false),
300296
};
301297
substr.py_startsendswith(
302298
affix,
303299
"endswith",
304300
"bytes",
305-
|s, x: &PyBytesInner| s.ends_with(&x.elements[..]),
301+
|s, x: &PyBytesInner| s.ends_with(x.as_bytes()),
306302
vm,
307303
)
308304
}
@@ -314,15 +310,15 @@ impl PyBytes {
314310
vm: &VirtualMachine,
315311
) -> PyResult<bool> {
316312
let (affix, substr) =
317-
match options.prepare(&self.inner.elements[..], self.len(), |s, r| s.get_bytes(r)) {
313+
match options.prepare(self.as_bytes(), self.len(), |s, r| s.get_bytes(r)) {
318314
Some(x) => x,
319315
None => return Ok(false),
320316
};
321317
substr.py_startsendswith(
322318
affix,
323319
"startswith",
324320
"bytes",
325-
|s, x: &PyBytesInner| s.starts_with(&x.elements[..]),
321+
|s, x: &PyBytesInner| s.starts_with(x.as_bytes()),
326322
vm,
327323
)
328324
}
@@ -539,12 +535,7 @@ impl PyBytes {
539535

540536
#[pymethod(magic)]
541537
fn getnewargs(&self, vm: &VirtualMachine) -> PyTupleRef {
542-
let param: Vec<PyObjectRef> = self
543-
.inner
544-
.elements
545-
.iter()
546-
.map(|x| x.to_pyobject(vm))
547-
.collect();
538+
let param: Vec<PyObjectRef> = self.elements().map(|x| x.to_pyobject(vm)).collect();
548539
PyTuple::new_ref(param, &vm.ctx)
549540
}
550541

@@ -562,7 +553,7 @@ impl PyBytes {
562553
zelf: PyRef<Self>,
563554
vm: &VirtualMachine,
564555
) -> (PyTypeRef, PyTupleRef, Option<PyDictRef>) {
565-
let bytes = PyBytes::from(zelf.inner.elements.clone()).to_pyobject(vm);
556+
let bytes = PyBytes::from(zelf.to_vec()).to_pyobject(vm);
566557
(
567558
zelf.class().to_owned(),
568559
PyTuple::new_ref(vec![bytes], &vm.ctx),
@@ -620,8 +611,7 @@ impl AsSequence for PyBytes {
620611
}),
621612
item: atomic_func!(|seq, i, vm| {
622613
PyBytes::sequence_downcast(seq)
623-
.inner
624-
.elements
614+
.as_bytes()
625615
.getitem_by_index(vm, i)
626616
.map(|x| vm.ctx.new_bytes(vec![x]).into())
627617
}),

vm/src/builtins/int.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -606,11 +606,11 @@ impl PyInt {
606606
) -> PyResult<PyRef<Self>> {
607607
let signed = args.signed.map_or(false, Into::into);
608608
let value = match (args.byteorder, signed) {
609-
(ArgByteOrder::Big, true) => BigInt::from_signed_bytes_be(&args.bytes.elements),
610-
(ArgByteOrder::Big, false) => BigInt::from_bytes_be(Sign::Plus, &args.bytes.elements),
611-
(ArgByteOrder::Little, true) => BigInt::from_signed_bytes_le(&args.bytes.elements),
609+
(ArgByteOrder::Big, true) => BigInt::from_signed_bytes_be(args.bytes.as_bytes()),
610+
(ArgByteOrder::Big, false) => BigInt::from_bytes_be(Sign::Plus, args.bytes.as_bytes()),
611+
(ArgByteOrder::Little, true) => BigInt::from_signed_bytes_le(args.bytes.as_bytes()),
612612
(ArgByteOrder::Little, false) => {
613-
BigInt::from_bytes_le(Sign::Plus, &args.bytes.elements)
613+
BigInt::from_bytes_le(Sign::Plus, args.bytes.as_bytes())
614614
}
615615
};
616616
Self::with_value(cls, value, vm)

vm/src/bytesinner.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use rustpython_common::hash;
2020

2121
#[derive(Debug, Default, Clone)]
2222
pub struct PyBytesInner {
23-
pub(crate) elements: Vec<u8>,
23+
pub(super) elements: Vec<u8>,
2424
}
2525

2626
impl From<Vec<u8>> for PyBytesInner {
@@ -241,6 +241,11 @@ impl ByteInnerTranslateOptions {
241241
pub type ByteInnerSplitOptions<'a> = anystr::SplitArgs<'a, PyBytesInner>;
242242

243243
impl PyBytesInner {
244+
#[inline]
245+
pub fn as_bytes(&self) -> &[u8] {
246+
&self.elements
247+
}
248+
244249
pub fn repr(&self, class_name: Option<&str>) -> String {
245250
if let Some(class_name) = class_name {
246251
rustpython_common::bytes::repr_with(&self.elements, &[class_name, "("], ")")

0 commit comments

Comments
 (0)