From c6e148efcfbb88499a9a6eca6fdf0bcc77bef4dd Mon Sep 17 00:00:00 2001 From: Jeong YunWon Date: Fri, 10 Jan 2020 21:30:50 +0900 Subject: [PATCH] cleanup bytearray --- vm/src/obj/objbytearray.rs | 166 ++++++++++++++++++------------------- vm/src/obj/objbyteinner.rs | 10 +-- vm/src/stdlib/binascii.rs | 2 +- vm/src/stdlib/io.rs | 4 +- vm/src/stdlib/json.rs | 2 +- vm/src/stdlib/socket.rs | 2 +- 6 files changed, 89 insertions(+), 97 deletions(-) diff --git a/vm/src/obj/objbytearray.rs b/vm/src/obj/objbytearray.rs index d979efb44..e1514988d 100644 --- a/vm/src/obj/objbytearray.rs +++ b/vm/src/obj/objbytearray.rs @@ -38,7 +38,7 @@ use std::str::FromStr; #[pyclass(name = "bytearray")] #[derive(Clone, Debug)] pub struct PyByteArray { - pub inner: RefCell, + inner: RefCell, } pub type PyByteArrayRef = PyRef; @@ -49,19 +49,19 @@ impl PyByteArray { } } - pub fn from_inner(inner: PyByteInner) -> Self { + fn from_inner(inner: PyByteInner) -> Self { PyByteArray { inner: RefCell::new(inner), } } - // pub fn get_value(&self) -> Vec { - // self.inner.borrow().clone().elements - // } + pub fn borrow_value(&self) -> std::cell::Ref<'_, PyByteInner> { + self.inner.borrow() + } - // pub fn get_value_mut(&self) -> Vec { - // self.inner.borrow_mut().clone().elements - // } + pub fn borrow_value_mut(&self) -> std::cell::RefMut<'_, PyByteInner> { + self.inner.borrow_mut() + } } impl PyValue for PyByteArray { @@ -70,28 +70,19 @@ impl PyValue for PyByteArray { } } -// pub fn get_value(obj: &PyObjectRef) -> Vec { -// obj.payload::().unwrap().get_value() -// } - -// pub fn get_value_mut(obj: &PyObjectRef) -> Vec { -// obj.payload::().unwrap().get_value_mut() -// } - /// Fill bytearray class methods dictionary. -pub fn init(context: &PyContext) { - PyByteArrayRef::extend_class(context, &context.types.bytearray_type); +pub(crate) fn init(context: &PyContext) { + PyByteArray::extend_class(context, &context.types.bytearray_type); let bytearray_type = &context.types.bytearray_type; extend_class!(context, bytearray_type, { - "fromhex" => context.new_method(PyByteArrayRef::fromhex), - "maketrans" => context.new_method(PyByteInner::maketrans), + "maketrans" => context.new_method(PyByteInner::maketrans), }); PyByteArrayIterator::extend_class(context, &context.types.bytearrayiterator_type); } #[pyimpl] -impl PyByteArrayRef { +impl PyByteArray { #[pyslot(new)] fn tp_new( cls: PyClassRef, @@ -102,60 +93,60 @@ impl PyByteArrayRef { } #[pymethod(name = "__repr__")] - fn repr(self, _vm: &VirtualMachine) -> PyResult { + fn repr(&self, _vm: &VirtualMachine) -> PyResult { Ok(format!("bytearray(b'{}')", self.inner.borrow().repr()?)) } #[pymethod(name = "__len__")] - fn len(self, _vm: &VirtualMachine) -> usize { + fn len(&self, _vm: &VirtualMachine) -> usize { self.inner.borrow().len() } #[pymethod(name = "__sizeof__")] - fn sizeof(self, _vm: &VirtualMachine) -> usize { + fn sizeof(&self, _vm: &VirtualMachine) -> usize { size_of::() + self.inner.borrow().len() * size_of::() } #[pymethod(name = "__eq__")] - fn eq(self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult { + fn eq(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult { self.inner.borrow().eq(other, vm) } #[pymethod(name = "__ge__")] - fn ge(self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult { + fn ge(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult { self.inner.borrow().ge(other, vm) } #[pymethod(name = "__le__")] - fn le(self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult { + fn le(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult { self.inner.borrow().le(other, vm) } #[pymethod(name = "__gt__")] - fn gt(self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult { + fn gt(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult { self.inner.borrow().gt(other, vm) } #[pymethod(name = "__lt__")] - fn lt(self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult { + fn lt(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult { self.inner.borrow().lt(other, vm) } #[pymethod(name = "__hash__")] - fn hash(self, vm: &VirtualMachine) -> PyResult<()> { + fn hash(&self, vm: &VirtualMachine) -> PyResult<()> { Err(vm.new_type_error("unhashable type: bytearray".to_string())) } #[pymethod(name = "__iter__")] - fn iter(self, _vm: &VirtualMachine) -> PyByteArrayIterator { + fn iter(zelf: PyRef, _vm: &VirtualMachine) -> PyByteArrayIterator { PyByteArrayIterator { position: Cell::new(0), - bytearray: self, + bytearray: zelf, } } #[pymethod(name = "__add__")] - fn add(self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult { + fn add(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult { if let Ok(other) = PyByteInner::try_from_object(vm, other) { Ok(vm.ctx.new_bytearray(self.inner.borrow().add(other))) } else { @@ -165,7 +156,7 @@ impl PyByteArrayRef { #[pymethod(name = "__contains__")] fn contains( - self, + &self, needle: Either, vm: &VirtualMachine, ) -> PyResult { @@ -173,13 +164,13 @@ impl PyByteArrayRef { } #[pymethod(name = "__getitem__")] - fn getitem(self, needle: Either, vm: &VirtualMachine) -> PyResult { + fn getitem(&self, needle: Either, vm: &VirtualMachine) -> PyResult { self.inner.borrow().getitem(needle, vm) } #[pymethod(name = "__setitem__")] fn setitem( - self, + &self, needle: Either, value: PyObjectRef, vm: &VirtualMachine, @@ -188,75 +179,76 @@ impl PyByteArrayRef { } #[pymethod(name = "__delitem__")] - fn delitem(self, needle: Either, vm: &VirtualMachine) -> PyResult<()> { + fn delitem(&self, needle: Either, vm: &VirtualMachine) -> PyResult<()> { self.inner.borrow_mut().delitem(needle, vm) } #[pymethod(name = "isalnum")] - fn isalnum(self, vm: &VirtualMachine) -> bool { + fn isalnum(&self, vm: &VirtualMachine) -> bool { self.inner.borrow().isalnum(vm) } #[pymethod(name = "isalpha")] - fn isalpha(self, vm: &VirtualMachine) -> bool { + fn isalpha(&self, vm: &VirtualMachine) -> bool { self.inner.borrow().isalpha(vm) } #[pymethod(name = "isascii")] - fn isascii(self, vm: &VirtualMachine) -> bool { + fn isascii(&self, vm: &VirtualMachine) -> bool { self.inner.borrow().isascii(vm) } #[pymethod(name = "isdigit")] - fn isdigit(self, vm: &VirtualMachine) -> bool { + fn isdigit(&self, vm: &VirtualMachine) -> bool { self.inner.borrow().isdigit(vm) } #[pymethod(name = "islower")] - fn islower(self, vm: &VirtualMachine) -> bool { + fn islower(&self, vm: &VirtualMachine) -> bool { self.inner.borrow().islower(vm) } #[pymethod(name = "isspace")] - fn isspace(self, vm: &VirtualMachine) -> bool { + fn isspace(&self, vm: &VirtualMachine) -> bool { self.inner.borrow().isspace(vm) } #[pymethod(name = "isupper")] - fn isupper(self, vm: &VirtualMachine) -> bool { + fn isupper(&self, vm: &VirtualMachine) -> bool { self.inner.borrow().isupper(vm) } #[pymethod(name = "istitle")] - fn istitle(self, vm: &VirtualMachine) -> bool { + fn istitle(&self, vm: &VirtualMachine) -> bool { self.inner.borrow().istitle(vm) } #[pymethod(name = "lower")] - fn lower(self, vm: &VirtualMachine) -> PyResult { + fn lower(&self, vm: &VirtualMachine) -> PyResult { Ok(vm.ctx.new_bytearray(self.inner.borrow().lower(vm))) } #[pymethod(name = "upper")] - fn upper(self, vm: &VirtualMachine) -> PyResult { + fn upper(&self, vm: &VirtualMachine) -> PyResult { Ok(vm.ctx.new_bytearray(self.inner.borrow().upper(vm))) } #[pymethod(name = "capitalize")] - fn capitalize(self, vm: &VirtualMachine) -> PyResult { + fn capitalize(&self, vm: &VirtualMachine) -> PyResult { Ok(vm.ctx.new_bytearray(self.inner.borrow().capitalize(vm))) } #[pymethod(name = "swapcase")] - fn swapcase(self, vm: &VirtualMachine) -> PyResult { + fn swapcase(&self, vm: &VirtualMachine) -> PyResult { Ok(vm.ctx.new_bytearray(self.inner.borrow().swapcase(vm))) } #[pymethod(name = "hex")] - fn hex(self, vm: &VirtualMachine) -> String { + fn hex(&self, vm: &VirtualMachine) -> String { self.inner.borrow().hex(vm) } + #[pymethod] fn fromhex(string: PyStringRef, vm: &VirtualMachine) -> PyResult { Ok(vm .ctx @@ -264,39 +256,39 @@ impl PyByteArrayRef { } #[pymethod(name = "center")] - fn center(self, options: ByteInnerPaddingOptions, vm: &VirtualMachine) -> PyResult { + fn center(&self, options: ByteInnerPaddingOptions, vm: &VirtualMachine) -> PyResult { Ok(vm .ctx .new_bytearray(self.inner.borrow().center(options, vm)?)) } #[pymethod(name = "ljust")] - fn ljust(self, options: ByteInnerPaddingOptions, vm: &VirtualMachine) -> PyResult { + fn ljust(&self, options: ByteInnerPaddingOptions, vm: &VirtualMachine) -> PyResult { Ok(vm .ctx .new_bytearray(self.inner.borrow().ljust(options, vm)?)) } #[pymethod(name = "rjust")] - fn rjust(self, options: ByteInnerPaddingOptions, vm: &VirtualMachine) -> PyResult { + fn rjust(&self, options: ByteInnerPaddingOptions, vm: &VirtualMachine) -> PyResult { Ok(vm .ctx .new_bytearray(self.inner.borrow().rjust(options, vm)?)) } #[pymethod(name = "count")] - fn count(self, options: ByteInnerFindOptions, vm: &VirtualMachine) -> PyResult { + fn count(&self, options: ByteInnerFindOptions, vm: &VirtualMachine) -> PyResult { self.inner.borrow().count(options, vm) } #[pymethod(name = "join")] - fn join(self, iter: PyIterable, vm: &VirtualMachine) -> PyResult { + fn join(&self, iter: PyIterable, vm: &VirtualMachine) -> PyResult { self.inner.borrow().join(iter, vm) } #[pymethod(name = "endswith")] fn endswith( - self, + &self, suffix: Either, start: OptionalArg, end: OptionalArg, @@ -309,7 +301,7 @@ impl PyByteArrayRef { #[pymethod(name = "startswith")] fn startswith( - self, + &self, prefix: Either, start: OptionalArg, end: OptionalArg, @@ -321,12 +313,12 @@ impl PyByteArrayRef { } #[pymethod(name = "find")] - fn find(self, options: ByteInnerFindOptions, vm: &VirtualMachine) -> PyResult { + fn find(&self, options: ByteInnerFindOptions, vm: &VirtualMachine) -> PyResult { self.inner.borrow().find(options, false, vm) } #[pymethod(name = "index")] - fn index(self, options: ByteInnerFindOptions, vm: &VirtualMachine) -> PyResult { + fn index(&self, options: ByteInnerFindOptions, vm: &VirtualMachine) -> PyResult { let res = self.inner.borrow().find(options, false, vm)?; if res == -1 { return Err(vm.new_value_error("substring not found".to_string())); @@ -335,12 +327,12 @@ impl PyByteArrayRef { } #[pymethod(name = "rfind")] - fn rfind(self, options: ByteInnerFindOptions, vm: &VirtualMachine) -> PyResult { + fn rfind(&self, options: ByteInnerFindOptions, vm: &VirtualMachine) -> PyResult { self.inner.borrow().find(options, true, vm) } #[pymethod(name = "rindex")] - fn rindex(self, options: ByteInnerFindOptions, vm: &VirtualMachine) -> PyResult { + fn rindex(&self, options: ByteInnerFindOptions, vm: &VirtualMachine) -> PyResult { let res = self.inner.borrow().find(options, true, vm)?; if res == -1 { return Err(vm.new_value_error("substring not found".to_string())); @@ -349,7 +341,7 @@ impl PyByteArrayRef { } #[pymethod(name = "remove")] - fn remove(self, x: PyIntRef, vm: &VirtualMachine) -> PyResult<()> { + fn remove(&self, x: PyIntRef, vm: &VirtualMachine) -> PyResult<()> { let x = x.as_bigint().byte_or(vm)?; let bytes = &mut self.inner.borrow_mut().elements; @@ -364,12 +356,12 @@ impl PyByteArrayRef { } #[pymethod(name = "translate")] - fn translate(self, options: ByteInnerTranslateOptions, vm: &VirtualMachine) -> PyResult { + fn translate(&self, options: ByteInnerTranslateOptions, vm: &VirtualMachine) -> PyResult { self.inner.borrow().translate(options, vm) } #[pymethod(name = "strip")] - fn strip(self, chars: OptionalArg, vm: &VirtualMachine) -> PyResult { + fn strip(&self, chars: OptionalArg, vm: &VirtualMachine) -> PyResult { Ok(vm.ctx.new_bytes( self.inner .borrow() @@ -378,7 +370,7 @@ impl PyByteArrayRef { } #[pymethod(name = "lstrip")] - fn lstrip(self, chars: OptionalArg, vm: &VirtualMachine) -> PyResult { + fn lstrip(&self, chars: OptionalArg, vm: &VirtualMachine) -> PyResult { Ok(vm.ctx.new_bytes( self.inner .borrow() @@ -387,7 +379,7 @@ impl PyByteArrayRef { } #[pymethod(name = "rstrip")] - fn rstrip(self, chars: OptionalArg, vm: &VirtualMachine) -> PyResult { + fn rstrip(&self, chars: OptionalArg, vm: &VirtualMachine) -> PyResult { Ok(vm.ctx.new_bytes( self.inner .borrow() @@ -396,7 +388,7 @@ impl PyByteArrayRef { } #[pymethod(name = "split")] - fn split(self, options: ByteInnerSplitOptions, vm: &VirtualMachine) -> PyResult { + fn split(&self, options: ByteInnerSplitOptions, vm: &VirtualMachine) -> PyResult { let as_bytes = self .inner .borrow() @@ -408,7 +400,7 @@ impl PyByteArrayRef { } #[pymethod(name = "rsplit")] - fn rsplit(self, options: ByteInnerSplitOptions, vm: &VirtualMachine) -> PyResult { + fn rsplit(&self, options: ByteInnerSplitOptions, vm: &VirtualMachine) -> PyResult { let as_bytes = self .inner .borrow() @@ -420,7 +412,7 @@ impl PyByteArrayRef { } #[pymethod(name = "partition")] - fn partition(self, sep: PyByteInner, vm: &VirtualMachine) -> PyResult { + fn partition(&self, sep: PyByteInner, vm: &VirtualMachine) -> PyResult { // sep ALWAYS converted to bytearray even it's bytes or memoryview // so its ok to accept PyByteInner let (left, right) = self.inner.borrow().partition(&sep, false)?; @@ -432,7 +424,7 @@ impl PyByteArrayRef { } #[pymethod(name = "rpartition")] - fn rpartition(self, sep: PyByteInner, vm: &VirtualMachine) -> PyResult { + fn rpartition(&self, sep: PyByteInner, vm: &VirtualMachine) -> PyResult { let (left, right) = self.inner.borrow().partition(&sep, true)?; Ok(vm.ctx.new_tuple(vec![ vm.ctx.new_bytearray(left), @@ -442,14 +434,14 @@ impl PyByteArrayRef { } #[pymethod(name = "expandtabs")] - fn expandtabs(self, options: ByteInnerExpandtabsOptions, vm: &VirtualMachine) -> PyResult { + fn expandtabs(&self, options: ByteInnerExpandtabsOptions, vm: &VirtualMachine) -> PyResult { Ok(vm .ctx .new_bytearray(self.inner.borrow().expandtabs(options))) } #[pymethod(name = "splitlines")] - fn splitlines(self, options: ByteInnerSplitlinesOptions, vm: &VirtualMachine) -> PyResult { + fn splitlines(&self, options: ByteInnerSplitlinesOptions, vm: &VirtualMachine) -> PyResult { let as_bytes = self .inner .borrow() @@ -461,13 +453,13 @@ impl PyByteArrayRef { } #[pymethod(name = "zfill")] - fn zfill(self, width: PyIntRef, vm: &VirtualMachine) -> PyResult { + fn zfill(&self, width: PyIntRef, vm: &VirtualMachine) -> PyResult { Ok(vm.ctx.new_bytearray(self.inner.borrow().zfill(width))) } #[pymethod(name = "replace")] fn replace( - self, + &self, old: PyByteInner, new: PyByteInner, count: OptionalArg, @@ -479,17 +471,17 @@ impl PyByteArrayRef { } #[pymethod(name = "clear")] - fn clear(self, _vm: &VirtualMachine) { + fn clear(&self, _vm: &VirtualMachine) { self.inner.borrow_mut().elements.clear(); } #[pymethod(name = "copy")] - fn copy(self, vm: &VirtualMachine) -> PyResult { + fn copy(&self, vm: &VirtualMachine) -> PyResult { Ok(vm.ctx.new_bytearray(self.inner.borrow().elements.clone())) } #[pymethod(name = "append")] - fn append(self, x: PyIntRef, vm: &VirtualMachine) -> PyResult<()> { + fn append(&self, x: PyIntRef, vm: &VirtualMachine) -> PyResult<()> { self.inner .borrow_mut() .elements @@ -498,7 +490,7 @@ impl PyByteArrayRef { } #[pymethod(name = "extend")] - fn extend(self, iterable_of_ints: PyIterable, vm: &VirtualMachine) -> PyResult<()> { + fn extend(&self, iterable_of_ints: PyIterable, vm: &VirtualMachine) -> PyResult<()> { let mut inner = self.inner.borrow_mut(); for x in iterable_of_ints.iter(vm)? { @@ -512,7 +504,7 @@ impl PyByteArrayRef { } #[pymethod(name = "insert")] - fn insert(self, mut index: isize, x: PyIntRef, vm: &VirtualMachine) -> PyResult<()> { + fn insert(&self, mut index: isize, x: PyIntRef, vm: &VirtualMachine) -> PyResult<()> { let bytes = &mut self.inner.borrow_mut().elements; let len = isize::try_from(bytes.len()) .map_err(|_e| vm.new_overflow_error("bytearray too big".to_string()))?; @@ -538,7 +530,7 @@ impl PyByteArrayRef { } #[pymethod(name = "pop")] - fn pop(self, vm: &VirtualMachine) -> PyResult { + fn pop(&self, vm: &VirtualMachine) -> PyResult { let bytes = &mut self.inner.borrow_mut().elements; bytes .pop() @@ -546,22 +538,22 @@ impl PyByteArrayRef { } #[pymethod(name = "title")] - fn title(self, vm: &VirtualMachine) -> PyResult { + fn title(&self, vm: &VirtualMachine) -> PyResult { Ok(vm.ctx.new_bytearray(self.inner.borrow().title())) } #[pymethod(name = "__mul__")] - fn repeat(self, n: isize, vm: &VirtualMachine) -> PyResult { + fn repeat(&self, n: isize, vm: &VirtualMachine) -> PyResult { Ok(vm.ctx.new_bytearray(self.inner.borrow().repeat(n, vm)?)) } #[pymethod(name = "__rmul__")] - fn rmul(self, n: isize, vm: &VirtualMachine) -> PyResult { + fn rmul(&self, n: isize, vm: &VirtualMachine) -> PyResult { self.repeat(n, vm) } #[pymethod(name = "__imul__")] - fn irepeat(self, n: isize, vm: &VirtualMachine) -> PyResult<()> { + fn irepeat(&self, n: isize, vm: &VirtualMachine) -> PyResult<()> { self.inner.borrow_mut().irepeat(n, vm) } @@ -578,7 +570,7 @@ impl PyByteArrayRef { } #[pymethod(name = "__mod__")] - fn modulo(self, values: PyObjectRef, vm: &VirtualMachine) -> PyResult { + fn modulo(&self, values: PyObjectRef, vm: &VirtualMachine) -> PyResult { let format_string = CFormatString::from_str(std::str::from_utf8(&self.inner.borrow().elements).unwrap()) .map_err(|err| vm.new_value_error(err.to_string()))?; @@ -586,12 +578,12 @@ impl PyByteArrayRef { } #[pymethod(name = "__rmod__")] - fn rmod(self, _values: PyObjectRef, vm: &VirtualMachine) -> PyResult { + fn rmod(&self, _values: PyObjectRef, vm: &VirtualMachine) -> PyResult { Ok(vm.ctx.not_implemented()) } #[pymethod(name = "reverse")] - fn reverse(self, _vm: &VirtualMachine) -> PyResult<()> { + fn reverse(&self, _vm: &VirtualMachine) -> PyResult<()> { self.inner.borrow_mut().elements.reverse(); Ok(()) } diff --git a/vm/src/obj/objbyteinner.rs b/vm/src/obj/objbyteinner.rs index 666ed82d0..09cd00bc1 100644 --- a/vm/src/obj/objbyteinner.rs +++ b/vm/src/obj/objbyteinner.rs @@ -32,7 +32,7 @@ impl TryFromObject for PyByteInner { elements: i.get_value().to_vec() }), j @ PyByteArray => Ok(PyByteInner { - elements: j.inner.borrow().elements.to_vec() + elements: j.borrow_value().elements.to_vec() }), k @ PyMemoryView => Ok(PyByteInner { elements: k.get_obj_value().unwrap() @@ -86,7 +86,7 @@ impl ByteInnerNewOptions { ); } i @ PyBytes => Ok(i.get_value().to_vec()), - j @ PyByteArray => Ok(j.inner.borrow().elements.to_vec()), + j @ PyByteArray => Ok(j.borrow_value().elements.to_vec()), obj => { // TODO: only support this method in the bytes() constructor if let Some(bytes_method) = vm.get_method(obj.clone(), "__bytes__") { @@ -1214,7 +1214,7 @@ impl PyByteInner { pub fn try_as_byte(obj: &PyObjectRef) -> Option> { match_class!(match obj.clone() { i @ PyBytes => Some(i.get_value().to_vec()), - j @ PyByteArray => Some(j.inner.borrow().elements.to_vec()), + j @ PyByteArray => Some(j.borrow_value().elements.to_vec()), _ => None, }) } @@ -1439,7 +1439,7 @@ impl PyBytesLike { pub fn to_cow(&self) -> std::borrow::Cow<[u8]> { match self { PyBytesLike::Bytes(b) => b.get_value().into(), - PyBytesLike::Bytearray(b) => b.inner.borrow().elements.clone().into(), + PyBytesLike::Bytearray(b) => b.borrow_value().elements.clone().into(), } } @@ -1447,7 +1447,7 @@ impl PyBytesLike { pub fn with_ref(&self, f: impl FnOnce(&[u8]) -> R) -> R { match self { PyBytesLike::Bytes(b) => f(b.get_value()), - PyBytesLike::Bytearray(b) => f(&b.inner.borrow().elements), + PyBytesLike::Bytearray(b) => f(&b.borrow_value().elements), } } } diff --git a/vm/src/stdlib/binascii.rs b/vm/src/stdlib/binascii.rs index 5f402402d..782d902e0 100644 --- a/vm/src/stdlib/binascii.rs +++ b/vm/src/stdlib/binascii.rs @@ -42,7 +42,7 @@ impl SerializedData { pub fn with_ref(&self, f: impl FnOnce(&[u8]) -> R) -> R { match self { SerializedData::Bytes(b) => f(b.get_value()), - SerializedData::Buffer(b) => f(&b.inner.borrow().elements), + SerializedData::Buffer(b) => f(&b.borrow_value().elements), SerializedData::Ascii(a) => f(a.as_str().as_bytes()), } } diff --git a/vm/src/stdlib/io.rs b/vm/src/stdlib/io.rs index d55618ef7..ec71693ed 100644 --- a/vm/src/stdlib/io.rs +++ b/vm/src/stdlib/io.rs @@ -465,7 +465,7 @@ fn raw_io_base_read( vm.call_method(&instance, "readinto", vec![b.as_object().clone()])?, )?; if let Some(n) = n { - let bytes = &mut b.inner.borrow_mut().elements; + let bytes = &mut b.borrow_value_mut().elements; bytes.truncate(n); Ok(vm.ctx.new_bytes(bytes.clone())) } else { @@ -628,7 +628,7 @@ mod fileio { if let Some(bytes) = obj.payload::() { //TODO: Implement for MemoryView - let value_mut = &mut bytes.inner.borrow_mut().elements; + let value_mut = &mut bytes.borrow_value_mut().elements; value_mut.clear(); match f.read_to_end(value_mut) { Ok(_) => {} diff --git a/vm/src/stdlib/json.rs b/vm/src/stdlib/json.rs index 84cf15325..d72591cc6 100644 --- a/vm/src/stdlib/json.rs +++ b/vm/src/stdlib/json.rs @@ -28,7 +28,7 @@ pub fn json_loads(obj: PyObjectRef, vm: &VirtualMachine) -> PyResult { b @ PyBytes => py_serde::deserialize(vm, &mut serde_json::Deserializer::from_slice(&b)), ba @ PyByteArray => py_serde::deserialize( vm, - &mut serde_json::Deserializer::from_slice(&ba.inner.borrow().elements) + &mut serde_json::Deserializer::from_slice(&ba.borrow_value().elements) ), obj => { let msg = format!( diff --git a/vm/src/stdlib/socket.rs b/vm/src/stdlib/socket.rs index 9c8098e37..8dc0b5633 100644 --- a/vm/src/stdlib/socket.rs +++ b/vm/src/stdlib/socket.rs @@ -190,7 +190,7 @@ impl PySocket { #[pymethod] fn recv_into(&self, buf: PyByteArrayRef, vm: &VirtualMachine) -> PyResult { - let mut buffer = buf.inner.borrow_mut(); + let mut buffer = buf.borrow_value_mut(); self.sock() .recv(&mut buffer.elements) .map_err(|err| convert_sock_error(vm, err))