diff --git a/vm/src/builtins/bytearray.rs b/vm/src/builtins/bytearray.rs index ff45b9018..3ca0b1873 100644 --- a/vm/src/builtins/bytearray.rs +++ b/vm/src/builtins/bytearray.rs @@ -442,7 +442,7 @@ impl PyByteArray { affix, "endswith", "bytes", - |s, x: &PyBytesInner| s.ends_with(&x.elements[..]), + |s, x: &PyBytesInner| s.ends_with(x.as_bytes()), vm, ) } @@ -463,7 +463,7 @@ impl PyByteArray { affix, "startswith", "bytes", - |s, x: &PyBytesInner| s.starts_with(&x.elements[..]), + |s, x: &PyBytesInner| s.starts_with(x.as_bytes()), vm, ) } diff --git a/vm/src/builtins/bytes.rs b/vm/src/builtins/bytes.rs index 41742c196..d0d41bccf 100644 --- a/vm/src/builtins/bytes.rs +++ b/vm/src/builtins/bytes.rs @@ -61,18 +61,18 @@ impl Deref for PyBytes { type Target = [u8]; fn deref(&self) -> &[u8] { - &self.inner.elements + self.as_bytes() } } impl AsRef<[u8]> for PyBytes { fn as_ref(&self) -> &[u8] { - &self.inner.elements + self.as_bytes() } } impl AsRef<[u8]> for PyBytesRef { fn as_ref(&self) -> &[u8] { - &self.inner.elements + self.as_bytes() } } @@ -133,7 +133,7 @@ impl PyBytes { #[inline] pub fn as_bytes(&self) -> &[u8] { - &self.inner.elements + self.inner.as_bytes() } #[pymethod(magic)] @@ -147,7 +147,7 @@ impl PyBytes { #[pymethod(magic)] fn sizeof(&self) -> usize { - size_of::() + self.inner.elements.len() * size_of::() + size_of::() + self.len() * size_of::() } #[pymethod(magic)] @@ -172,13 +172,9 @@ impl PyBytes { fn _getitem(&self, needle: &PyObject, vm: &VirtualMachine) -> PyResult { match SequenceIndex::try_from_borrowed_object(vm, needle, "byte")? { SequenceIndex::Int(i) => self - .inner - .elements .getitem_by_index(vm, i) .map(|x| vm.ctx.new_int(x).into()), SequenceIndex::Slice(slice) => self - .inner - .elements .getitem_by_slice(vm, slice) .map(|x| vm.ctx.new_bytes(x).into()), } @@ -294,7 +290,7 @@ impl PyBytes { #[pymethod] fn endswith(&self, options: anystr::StartsEndsWithArgs, vm: &VirtualMachine) -> PyResult { let (affix, substr) = - match options.prepare(&self.inner.elements[..], self.len(), |s, r| s.get_bytes(r)) { + match options.prepare(self.as_bytes(), self.len(), |s, r| s.get_bytes(r)) { Some(x) => x, None => return Ok(false), }; @@ -302,7 +298,7 @@ impl PyBytes { affix, "endswith", "bytes", - |s, x: &PyBytesInner| s.ends_with(&x.elements[..]), + |s, x: &PyBytesInner| s.ends_with(x.as_bytes()), vm, ) } @@ -314,7 +310,7 @@ impl PyBytes { vm: &VirtualMachine, ) -> PyResult { let (affix, substr) = - match options.prepare(&self.inner.elements[..], self.len(), |s, r| s.get_bytes(r)) { + match options.prepare(self.as_bytes(), self.len(), |s, r| s.get_bytes(r)) { Some(x) => x, None => return Ok(false), }; @@ -322,7 +318,7 @@ impl PyBytes { affix, "startswith", "bytes", - |s, x: &PyBytesInner| s.starts_with(&x.elements[..]), + |s, x: &PyBytesInner| s.starts_with(x.as_bytes()), vm, ) } @@ -539,12 +535,7 @@ impl PyBytes { #[pymethod(magic)] fn getnewargs(&self, vm: &VirtualMachine) -> PyTupleRef { - let param: Vec = self - .inner - .elements - .iter() - .map(|x| x.to_pyobject(vm)) - .collect(); + let param: Vec = self.elements().map(|x| x.to_pyobject(vm)).collect(); PyTuple::new_ref(param, &vm.ctx) } @@ -562,7 +553,7 @@ impl PyBytes { zelf: PyRef, vm: &VirtualMachine, ) -> (PyTypeRef, PyTupleRef, Option) { - let bytes = PyBytes::from(zelf.inner.elements.clone()).to_pyobject(vm); + let bytes = PyBytes::from(zelf.to_vec()).to_pyobject(vm); ( zelf.class().to_owned(), PyTuple::new_ref(vec![bytes], &vm.ctx), @@ -620,8 +611,7 @@ impl AsSequence for PyBytes { }), item: atomic_func!(|seq, i, vm| { PyBytes::sequence_downcast(seq) - .inner - .elements + .as_bytes() .getitem_by_index(vm, i) .map(|x| vm.ctx.new_bytes(vec![x]).into()) }), diff --git a/vm/src/builtins/int.rs b/vm/src/builtins/int.rs index a9b6e846f..07c53a8b1 100644 --- a/vm/src/builtins/int.rs +++ b/vm/src/builtins/int.rs @@ -606,11 +606,11 @@ impl PyInt { ) -> PyResult> { let signed = args.signed.map_or(false, Into::into); let value = match (args.byteorder, signed) { - (ArgByteOrder::Big, true) => BigInt::from_signed_bytes_be(&args.bytes.elements), - (ArgByteOrder::Big, false) => BigInt::from_bytes_be(Sign::Plus, &args.bytes.elements), - (ArgByteOrder::Little, true) => BigInt::from_signed_bytes_le(&args.bytes.elements), + (ArgByteOrder::Big, true) => BigInt::from_signed_bytes_be(args.bytes.as_bytes()), + (ArgByteOrder::Big, false) => BigInt::from_bytes_be(Sign::Plus, args.bytes.as_bytes()), + (ArgByteOrder::Little, true) => BigInt::from_signed_bytes_le(args.bytes.as_bytes()), (ArgByteOrder::Little, false) => { - BigInt::from_bytes_le(Sign::Plus, &args.bytes.elements) + BigInt::from_bytes_le(Sign::Plus, args.bytes.as_bytes()) } }; Self::with_value(cls, value, vm) diff --git a/vm/src/bytesinner.rs b/vm/src/bytesinner.rs index 5deae941b..5689d395a 100644 --- a/vm/src/bytesinner.rs +++ b/vm/src/bytesinner.rs @@ -20,7 +20,7 @@ use rustpython_common::hash; #[derive(Debug, Default, Clone)] pub struct PyBytesInner { - pub(crate) elements: Vec, + pub(super) elements: Vec, } impl From> for PyBytesInner { @@ -241,6 +241,11 @@ impl ByteInnerTranslateOptions { pub type ByteInnerSplitOptions<'a> = anystr::SplitArgs<'a, PyBytesInner>; impl PyBytesInner { + #[inline] + pub fn as_bytes(&self) -> &[u8] { + &self.elements + } + pub fn repr(&self, class_name: Option<&str>) -> String { if let Some(class_name) = class_name { rustpython_common::bytes::repr_with(&self.elements, &[class_name, "("], ")")