diff --git a/vm/src/builtins/bytearray.rs b/vm/src/builtins/bytearray.rs index cb9f20451..19dbb90bd 100644 --- a/vm/src/builtins/bytearray.rs +++ b/vm/src/builtins/bytearray.rs @@ -26,8 +26,8 @@ use crate::slots::{ use crate::utils::Either; use crate::vm::VirtualMachine; use crate::{ - BorrowValue, IdProtocol, IntoPyObject, PyClassImpl, PyComparisonValue, PyContext, PyIterable, - PyObjectRef, PyRef, PyResult, PyValue, TypeProtocol, + IdProtocol, IntoPyObject, PyClassImpl, PyComparisonValue, PyContext, PyIterable, PyObjectRef, + PyRef, PyResult, PyValue, TypeProtocol, }; use bstr::ByteSlice; use crossbeam_utils::atomic::AtomicCell; @@ -53,7 +53,7 @@ pub struct PyByteArray { pub type PyByteArrayRef = PyRef; -impl<'a> BorrowValue<'a> for PyByteArray { +impl<'a> rustpython_common::borrow::BorrowValue<'a> for PyByteArray { type Borrowed = PyMappedRwLockReadGuard<'a, [u8]>; fn borrow_value(&'a self) -> Self::Borrowed { @@ -194,7 +194,7 @@ impl PyByteArray { fn iadd(zelf: PyRef, other: PyBytesLike, vm: &VirtualMachine) -> PyResult> { zelf.try_resizable(vm)? .elements - .extend(&*other.borrow_value()); + .extend(&*other.borrow_buf()); Ok(zelf) } diff --git a/vm/src/builtins/bytes.rs b/vm/src/builtins/bytes.rs index fa6fb14d6..15c53d53b 100644 --- a/vm/src/builtins/bytes.rs +++ b/vm/src/builtins/bytes.rs @@ -21,8 +21,8 @@ use crate::slots::{BufferProtocol, Comparable, Hashable, Iterable, PyComparisonO use crate::utils::Either; use crate::vm::VirtualMachine; use crate::{ - BorrowValue, IntoPyObject, PyClassImpl, PyComparisonValue, PyContext, PyIterable, PyObjectRef, - PyRef, PyResult, PyValue, TryFromObject, TypeProtocol, + IntoPyObject, PyClassImpl, PyComparisonValue, PyContext, PyIterable, PyObjectRef, PyRef, + PyResult, PyValue, TryFromObject, TypeProtocol, }; use crate::builtins::memory::{Buffer, BufferOptions}; @@ -44,7 +44,7 @@ pub struct PyBytes { pub type PyBytesRef = PyRef; -impl<'a> BorrowValue<'a> for PyBytes { +impl<'a> rustpython_common::borrow::BorrowValue<'a> for PyBytes { type Borrowed = &'a [u8]; fn borrow_value(&'a self) -> Self::Borrowed { @@ -134,7 +134,7 @@ impl PyBytes { #[pymethod(name = "__add__")] fn add(&self, other: PyBytesLike, vm: &VirtualMachine) -> PyObjectRef { - vm.ctx.new_bytes(self.inner.add(&*other.borrow_value())) + vm.ctx.new_bytes(self.inner.add(&*other.borrow_buf())) } #[pymethod(name = "__contains__")] @@ -512,7 +512,7 @@ struct BytesBuffer { impl Buffer for BytesBuffer { fn obj_bytes(&self) -> BorrowedValue<[u8]> { - self.bytes.borrow_value().into() + self.bytes.as_bytes().into() } fn obj_bytes_mut(&self) -> BorrowedValueMut<[u8]> { @@ -585,7 +585,7 @@ impl PyBytesIterator {} impl PyIter for PyBytesIterator { fn next(zelf: &PyRef, vm: &VirtualMachine) -> PyResult { let pos = zelf.position.fetch_add(1); - if let Some(&ret) = zelf.bytes.borrow_value().get(pos) { + if let Some(&ret) = zelf.bytes.as_bytes().get(pos) { Ok(vm.ctx.new_int(ret)) } else { Err(vm.new_stop_iteration()) diff --git a/vm/src/builtins/code.rs b/vm/src/builtins/code.rs index 36e8f04eb..b241a05e3 100644 --- a/vm/src/builtins/code.rs +++ b/vm/src/builtins/code.rs @@ -47,7 +47,7 @@ fn borrow_obj_constant(obj: &PyObjectRef) -> BorrowedConstant { }, ref s @ super::pystr::PyStr => BorrowedConstant::Str { value: s.as_str() }, ref b @ super::bytes::PyBytes => BorrowedConstant::Bytes { - value: b.borrow_value() + value: b.as_bytes() }, ref c @ PyCode => { BorrowedConstant::Code { code: &c.code } diff --git a/vm/src/builtins/float.rs b/vm/src/builtins/float.rs index 27a295038..964d8364b 100644 --- a/vm/src/builtins/float.rs +++ b/vm/src/builtins/float.rs @@ -12,7 +12,7 @@ use crate::function::{OptionalArg, OptionalOption}; use crate::slots::{Comparable, Hashable, PyComparisonOp}; use crate::VirtualMachine; use crate::{ - BorrowValue, IdProtocol, IntoPyObject, + IdProtocol, IntoPyObject, PyArithmaticValue::{self, *}, PyClassImpl, PyComparisonValue, PyContext, PyObjectRef, PyRef, PyResult, PyValue, TryFromObject, TypeProtocol, @@ -188,7 +188,7 @@ impl PyFloat { vm.new_value_error(format!("could not convert string to float: '{}'", s)) })? } else if let Some(bytes) = val.payload_if_subclass::(vm) { - lexical_core::parse(bytes.borrow_value()).map_err(|_| { + lexical_core::parse(bytes.as_bytes()).map_err(|_| { vm.new_value_error(format!( "could not convert string to float: '{}'", bytes.repr() diff --git a/vm/src/builtins/int.rs b/vm/src/builtins/int.rs index bd2e494a6..504079d39 100644 --- a/vm/src/builtins/int.rs +++ b/vm/src/builtins/int.rs @@ -800,11 +800,11 @@ fn try_int_radix(obj: &PyObjectRef, base: u32, vm: &VirtualMachine) -> PyResult< bytes_to_int(s.as_bytes(), base) } bytes @ PyBytes => { - let bytes = bytes.borrow_value(); + let bytes = bytes.as_bytes(); bytes_to_int(bytes, base) } bytearray @ PyByteArray => { - let inner = bytearray.borrow_value(); + let inner = bytearray.borrow_buf(); bytes_to_int(&inner, base) } _ => { diff --git a/vm/src/bytesinner.rs b/vm/src/bytesinner.rs index 0c2f94218..93d94dfbb 100644 --- a/vm/src/bytesinner.rs +++ b/vm/src/bytesinner.rs @@ -18,8 +18,7 @@ use crate::slots::PyComparisonOp; use crate::utils::Either; use crate::vm::VirtualMachine; use crate::{ - BorrowValue, IdProtocol, PyComparisonValue, PyIterable, PyObjectRef, PyResult, PyValue, - TryFromObject, + IdProtocol, PyComparisonValue, PyIterable, PyObjectRef, PyResult, PyValue, TryFromObject, }; use rustpython_common::hash; @@ -61,7 +60,7 @@ impl ByteInnerNewOptions { let encoding = encoding .ok_or_else(|| vm.new_type_error("string argument without an encoding".to_owned()))?; let bytes = pystr::encode_string(s, Some(encoding), errors.into_option(), vm)?; - Ok(bytes.borrow_value().to_vec().into()) + Ok(bytes.as_bytes().to_vec().into()) } fn get_value_from_source(source: PyObjectRef, vm: &VirtualMachine) -> PyResult { @@ -917,8 +916,8 @@ where F: Fn(&[u8]) -> R, { match_class!(match obj { - i @ PyBytes => Some(f(i.borrow_value())), - j @ PyByteArray => Some(f(&j.borrow_value())), + i @ PyBytes => Some(f(i.as_bytes())), + j @ PyByteArray => Some(f(&j.borrow_buf())), _ => None, }) } @@ -1076,7 +1075,7 @@ pub fn bytes_decode( let DecodeArgs { encoding, errors } = args; let encoding = encoding .as_ref() - .map_or(crate::codecs::DEFAULT_ENCODING, |s| s.borrow_value()); + .map_or(crate::codecs::DEFAULT_ENCODING, |s| s.as_str()); vm.state .codec_registry .decode_text(zelf, encoding, errors, vm) @@ -1158,7 +1157,7 @@ pub fn bytes_to_hex( s_guard.as_bytes() } Either::B(bytes) => { - b_guard = bytes.borrow_value(); + b_guard = bytes.as_bytes(); b_guard } }; diff --git a/vm/src/byteslike.rs b/vm/src/byteslike.rs index 837bac8f6..9442a7647 100644 --- a/vm/src/byteslike.rs +++ b/vm/src/byteslike.rs @@ -2,7 +2,7 @@ use crate::builtins::memory::{try_buffer_from_object, BufferRef}; use crate::builtins::PyStrRef; use crate::common::borrow::{BorrowedValue, BorrowedValueMut}; use crate::vm::VirtualMachine; -use crate::{BorrowValue, PyObjectRef, PyResult, TryFromObject}; +use crate::{PyObjectRef, PyResult, TryFromObject}; #[derive(Debug)] pub struct PyBytesLike(BufferRef); @@ -15,19 +15,19 @@ impl PyBytesLike { where F: FnOnce(&[u8]) -> R, { - f(&*self.borrow_value()) + f(&*self.borrow_buf()) } pub fn len(&self) -> usize { - self.borrow_value().len() + self.borrow_buf().len() } pub fn is_empty(&self) -> bool { - self.borrow_value().is_empty() + self.borrow_buf().is_empty() } pub fn to_cow(&self) -> std::borrow::Cow<[u8]> { - self.borrow_value().to_vec().into() + self.borrow_buf().to_vec().into() } } @@ -36,15 +36,15 @@ impl PyRwBytesLike { where F: FnOnce(&mut [u8]) -> R, { - f(&mut *self.borrow_value()) + f(&mut *self.borrow_buf_mut()) } pub fn len(&self) -> usize { - self.borrow_value().len() + self.borrow_buf_mut().len() } pub fn is_empty(&self) -> bool { - self.borrow_value().is_empty() + self.borrow_buf_mut().is_empty() } } @@ -73,7 +73,7 @@ impl TryFromObject for PyBytesLike { } } -impl<'a> BorrowValue<'a> for PyBytesLike { +impl<'a> rustpython_common::borrow::BorrowValue<'a> for PyBytesLike { type Borrowed = BorrowedValue<'a, [u8]>; fn borrow_value(&'a self) -> Self::Borrowed { self.borrow_buf() @@ -131,7 +131,7 @@ impl TryFromObject for PyRwBytesLike { } } -impl<'a> BorrowValue<'a> for PyRwBytesLike { +impl<'a> rustpython_common::borrow::BorrowValue<'a> for PyRwBytesLike { type Borrowed = BorrowedValueMut<'a, [u8]>; fn borrow_value(&'a self) -> Self::Borrowed { self.borrow_buf_mut() @@ -155,13 +155,13 @@ impl TryFromObject for BufOrStr { impl BufOrStr { pub fn borrow_bytes(&self) -> BorrowedValue<'_, [u8]> { match self { - Self::Buf(b) => b.borrow_value(), + Self::Buf(b) => b.borrow_buf(), Self::Str(s) => s.as_str().as_bytes().into(), } } } -impl<'a> BorrowValue<'a> for BufOrStr { +impl<'a> rustpython_common::borrow::BorrowValue<'a> for BufOrStr { type Borrowed = BorrowedValue<'a, [u8]>; fn borrow_value(&'a self) -> Self::Borrowed { self.borrow_bytes() diff --git a/vm/src/cformat.rs b/vm/src/cformat.rs index 8c2d37ac2..b2af0eff5 100644 --- a/vm/src/cformat.rs +++ b/vm/src/cformat.rs @@ -389,7 +389,7 @@ impl CFormatSpec { })? .invoke((), vm)?; let bytes = PyBytes::try_from_object(vm, bytes)?; - Ok(self.format_bytes(bytes.borrow_value())) + Ok(self.format_bytes(bytes.as_bytes())) } } }, diff --git a/vm/src/py_io.rs b/vm/src/py_io.rs index 4fb8b4c28..ab8ae8c7d 100644 --- a/vm/src/py_io.rs +++ b/vm/src/py_io.rs @@ -2,7 +2,7 @@ use crate::builtins::bytes::PyBytes; use crate::builtins::pystr::PyStr; use crate::exceptions::PyBaseExceptionRef; use crate::VirtualMachine; -use crate::{BorrowValue, PyObjectRef, PyResult}; +use crate::{PyObjectRef, PyResult}; use std::{fmt, io, ops}; pub trait Write { @@ -81,7 +81,7 @@ pub fn file_readline(obj: &PyObjectRef, size: Option, vm: &VirtualMachine } } b @ PyBytes => { - let buf = b.borrow_value(); + let buf = b.as_bytes(); if buf.is_empty() { return Err(eof_err()); } diff --git a/vm/src/stdlib/array.rs b/vm/src/stdlib/array.rs index 8404d9a46..2abcc4d8b 100644 --- a/vm/src/stdlib/array.rs +++ b/vm/src/stdlib/array.rs @@ -586,7 +586,7 @@ impl PyArray { #[pymethod] fn frombytes(zelf: PyRef, b: PyBytesLike, vm: &VirtualMachine) -> PyResult<()> { - let b = b.borrow_value(); + let b = b.borrow_buf(); let itemsize = zelf.borrow_value().itemsize(); if b.len() % itemsize != 0 { return Err(vm.new_value_error("bytes length not a multiple of item size".to_owned())); diff --git a/vm/src/stdlib/ast.rs b/vm/src/stdlib/ast.rs index ac6591009..6a46ec996 100644 --- a/vm/src/stdlib/ast.rs +++ b/vm/src/stdlib/ast.rs @@ -231,7 +231,7 @@ impl Node for ast::Constant { } } ref s @ builtins::pystr::PyStr => ast::Constant::Str(s.as_str().to_owned()), - ref b @ builtins::bytes::PyBytes => ast::Constant::Bytes(b.borrow_value().to_owned()), + ref b @ builtins::bytes::PyBytes => ast::Constant::Bytes(b.as_bytes().to_owned()), ref t @ builtins::tuple::PyTuple => { ast::Constant::Tuple( t.borrow_value() diff --git a/vm/src/stdlib/fcntl.rs b/vm/src/stdlib/fcntl.rs index 42c15a408..25dfc86b1 100644 --- a/vm/src/stdlib/fcntl.rs +++ b/vm/src/stdlib/fcntl.rs @@ -78,7 +78,7 @@ mod fcntl { let buf_len = match buf_kind { Either::A(rw_arg) => { let mutate_flag = mutate_flag.unwrap_or(true); - let mut arg_buf = rw_arg.borrow_value(); + let mut arg_buf = rw_arg.borrow_buf_mut(); if mutate_flag { let ret = unsafe { libc::ioctl(fd, request as _, arg_buf.as_mut_ptr()) }; diff --git a/vm/src/stdlib/hashlib.rs b/vm/src/stdlib/hashlib.rs index ccf2a01ee..c95ca686e 100644 --- a/vm/src/stdlib/hashlib.rs +++ b/vm/src/stdlib/hashlib.rs @@ -8,7 +8,7 @@ mod hashlib { use crate::common::lock::{PyRwLock, PyRwLockReadGuard, PyRwLockWriteGuard}; use crate::function::{FuncArgs, OptionalArg}; use crate::vm::VirtualMachine; - use crate::{BorrowValue, PyResult, PyValue, StaticType}; + use crate::{PyResult, PyValue, StaticType}; use blake2::{Blake2b, Blake2s}; use digest::DynDigest; use md5::Md5; @@ -72,7 +72,7 @@ mod hashlib { #[pymethod(name = "update")] fn update(&self, data: PyBytesRef) { - self.borrow_value_mut().input(data.borrow_value()); + self.borrow_value_mut().input(data.as_bytes()); } #[pymethod(name = "digest")] diff --git a/vm/src/stdlib/io.rs b/vm/src/stdlib/io.rs index 4fb8bf89c..116f75a8b 100644 --- a/vm/src/stdlib/io.rs +++ b/vm/src/stdlib/io.rs @@ -91,8 +91,8 @@ mod _io { use crate::utils::Either; use crate::vm::{ReprGuard, VirtualMachine}; use crate::{ - BorrowValue, IdProtocol, IntoPyObject, PyContext, PyIterable, PyObjectRef, PyRef, PyResult, - PyValue, StaticType, TryFromObject, TypeProtocol, + IdProtocol, IntoPyObject, PyContext, PyIterable, PyObjectRef, PyRef, PyResult, PyValue, + StaticType, TryFromObject, TypeProtocol, }; fn validate_whence(whence: i32) -> bool { @@ -577,17 +577,17 @@ mod _io { break; } Some(b) => { - if b.borrow_value().is_empty() { + if b.as_bytes().is_empty() { break; } - total_len += b.borrow_value().len(); + total_len += b.as_bytes().len(); chunks.push(b) } } } let mut ret = Vec::with_capacity(total_len); for b in chunks { - ret.extend_from_slice(b.borrow_value()) + ret.extend_from_slice(b.as_bytes()) } Ok(Some(ret)) } @@ -619,9 +619,9 @@ mod _io { if data.is(&bufobj) { return Ok(l); } - let mut buf = b.borrow_value(); + let mut buf = b.borrow_buf_mut(); let data = PyBytesLike::try_from_object(vm, data)?; - let data = data.borrow_value(); + let data = data.borrow_buf(); match buf.get_mut(..data.len()) { Some(slice) => { slice.copy_from_slice(&data); @@ -912,7 +912,7 @@ mod _io { let avail = self.buffer.len() - self.pos as usize; let buf_len; { - let buf = obj.borrow_value(); + let buf = obj.borrow_buf(); buf_len = buf.len(); if buf.len() <= avail { self.buffer[self.pos as usize..][..buf.len()].copy_from_slice(&buf); @@ -1171,7 +1171,7 @@ mod _io { let res = >::try_from_object(vm, res)?; let ret = if let Some(mut data) = data { if let Some(bytes) = res { - data.extend_from_slice(bytes.borrow_value()); + data.extend_from_slice(bytes.as_bytes()); } Some(PyBytes::from(data).into_ref(vm)) } else { @@ -1188,8 +1188,8 @@ mod _io { let read_data = >::try_from_object(vm, read_data)?; match read_data { - Some(b) if !b.borrow_value().is_empty() => { - let l = b.borrow_value().len(); + Some(b) if !b.as_bytes().is_empty() => { + let l = b.as_bytes().len(); read_size += l; if self.abs_pos != -1 { self.abs_pos += l as Offset; @@ -1203,7 +1203,7 @@ mod _io { let mut data = data.unwrap_or_default(); data.reserve(read_size); for bytes in &chunks { - data.extend_from_slice(bytes.borrow_value()) + data.extend_from_slice(bytes.as_bytes()) } Some(PyBytes::from(data).into_ref(vm)) }; @@ -2000,7 +2000,7 @@ mod _io { fn as_bytes(&self) -> &[u8] { match self { Self::Utf8(s) => s.as_str().as_bytes(), - Self::Bytes(b) => b.borrow_value(), + Self::Bytes(b) => b.as_bytes(), } } } @@ -2170,10 +2170,7 @@ mod _io { let has_read1 = vm.get_attribute_opt(buffer.clone(), "read1")?.is_some(); let seekable = pybool::boolval(vm, vm.call_method(&buffer, "seekable", ())?)?; - let codec = vm - .state - .codec_registry - .lookup(encoding.borrow_value(), vm)?; + let codec = vm.state.codec_registry.lookup(encoding.as_str(), vm)?; let encoder = if pybool::boolval(vm, vm.call_method(&buffer, "writable", ())?)? { let incremental_encoder = @@ -2425,7 +2422,7 @@ mod _io { let mut skip_back = 1; while skip_bytes > 0 { cookie.set_decoder_state(decoder, vm)?; - let input = &next_input.borrow_value()[..skip_bytes as usize]; + let input = &next_input.as_bytes()[..skip_bytes as usize]; let (bytes_decoded, chars_decoded) = decoder_decode(input)?; if chars_decoded <= chars_to_skip { let (dec_buffer, dec_flags) = decoder_getstate()?; @@ -2455,7 +2452,7 @@ mod _io { if chars_to_skip != 0 { let mut chars_decoded = 0; let mut bytes_decoded = 0; - let mut input = next_input.borrow_value(); + let mut input = next_input.as_bytes(); input = &input[skip_bytes..]; while !input.is_empty() { let (byte1, rest) = input.split_at(1); @@ -2840,7 +2837,7 @@ mod _io { use crate::builtins::{int, PyTuple}; let state_err = || vm.new_type_error("illegal decoder state".to_owned()); let state = state.downcast::().map_err(|_| state_err())?; - match state.borrow_value() { + match state.as_slice() { [buf, flags] => { let buf = buf.clone().downcast::().map_err(|obj| { vm.new_type_error(format!( @@ -2895,7 +2892,7 @@ mod _io { input_chunk.class().name )) })?; - let nbytes = buf.borrow_value().len(); + let nbytes = buf.borrow_buf().len(); let eof = nbytes == 0; let decoded = vm.call_method(decoder, "decode", (input_chunk, eof))?; let decoded = check_decoded(decoded, vm)?; @@ -2911,8 +2908,8 @@ mod _io { if let Some((dec_buffer, dec_flags)) = dec_state { // TODO: inplace append to bytes when refcount == 1 - let mut next_input = dec_buffer.borrow_value().to_vec(); - next_input.extend_from_slice(&*buf.borrow_value()); + let mut next_input = dec_buffer.as_bytes().to_vec(); + next_input.extend_from_slice(&*buf.borrow_buf()); self.snapshot = Some((dec_flags, PyBytes::from(next_input).into_ref(vm))); } @@ -3171,7 +3168,7 @@ mod _io { ) -> PyResult { let raw_bytes = object .flatten() - .map_or_else(Vec::new, |input| input.borrow_value().to_vec()); + .map_or_else(Vec::new, |input| input.as_bytes().to_vec()); BytesIO { buffer: PyRwLock::new(BufferedIO::new(Cursor::new(raw_bytes))), @@ -3230,7 +3227,7 @@ mod _io { let mut buf = self.buffer(vm)?; let ret = buf .cursor - .read(&mut *obj.borrow_value()) + .read(&mut *obj.borrow_buf_mut()) .map_err(|_| vm.new_value_error("Error readinto from Take".to_owned()))?; Ok(ret) @@ -3667,9 +3664,7 @@ mod fileio { use crate::function::{FuncArgs, OptionalArg}; use crate::stdlib::os; use crate::vm::VirtualMachine; - use crate::{ - BorrowValue, PyObjectRef, PyRef, PyResult, PyValue, StaticType, TryFromObject, TypeProtocol, - }; + use crate::{PyObjectRef, PyRef, PyResult, PyValue, StaticType, TryFromObject, TypeProtocol}; use crossbeam_utils::atomic::AtomicCell; use std::io::{Read, Write}; @@ -3973,7 +3968,7 @@ mod fileio { let handle = self.get_fd(vm)?; - let mut buf = obj.borrow_value(); + let mut buf = obj.borrow_buf_mut(); let mut f = handle.take(buf.len() as _); let ret = f.read(&mut buf).map_err(|e| e.into_pyexception(vm))?; diff --git a/vm/src/stdlib/marshal.rs b/vm/src/stdlib/marshal.rs index 31d317150..8f176ac0a 100644 --- a/vm/src/stdlib/marshal.rs +++ b/vm/src/stdlib/marshal.rs @@ -6,7 +6,6 @@ mod decl { use crate::builtins::code::{PyCode, PyCodeRef}; use crate::bytecode; use crate::byteslike::PyBytesLike; - use crate::common::borrow::BorrowValue; use crate::vm::VirtualMachine; use crate::{PyObjectRef, PyResult, TryFromObject}; @@ -24,7 +23,7 @@ mod decl { #[pyfunction] fn loads(code_bytes: PyBytesLike, vm: &VirtualMachine) -> PyResult { let code = - bytecode::CodeObject::from_bytes(&*code_bytes.borrow_value()).map_err(|e| match e { + bytecode::CodeObject::from_bytes(&*code_bytes.borrow_buf()).map_err(|e| match e { bytecode::CodeDeserializeError::Eof => vm.new_exception_msg( vm.ctx.exceptions.eof_error.clone(), "end of file while deserializing bytecode".to_owned(), diff --git a/vm/src/stdlib/os.rs b/vm/src/stdlib/os.rs index 85a8f9e0a..1787796fe 100644 --- a/vm/src/stdlib/os.rs +++ b/vm/src/stdlib/os.rs @@ -691,11 +691,11 @@ mod _os { ) -> PyResult<()> { let key: &ffi::OsStr = match key { Either::A(ref s) => s.as_str().as_ref(), - Either::B(ref b) => bytes_as_osstr(b.borrow_value(), vm)?, + Either::B(ref b) => bytes_as_osstr(b.as_bytes(), vm)?, }; let value: &ffi::OsStr = match value { Either::A(ref s) => s.as_str().as_ref(), - Either::B(ref b) => bytes_as_osstr(b.borrow_value(), vm)?, + Either::B(ref b) => bytes_as_osstr(b.as_bytes(), vm)?, }; env::set_var(key, value); Ok(()) @@ -705,7 +705,7 @@ mod _os { fn unsetenv(key: Either, vm: &VirtualMachine) -> PyResult<()> { let key: &ffi::OsStr = match key { Either::A(ref s) => s.as_str().as_ref(), - Either::B(ref b) => bytes_as_osstr(b.borrow_value(), vm)?, + Either::B(ref b) => bytes_as_osstr(b.as_bytes(), vm)?, }; env::remove_var(key); Ok(()) diff --git a/vm/src/stdlib/pystruct.rs b/vm/src/stdlib/pystruct.rs index de1b8067e..1f467a4bb 100644 --- a/vm/src/stdlib/pystruct.rs +++ b/vm/src/stdlib/pystruct.rs @@ -709,7 +709,7 @@ pub(crate) mod _struct { fn pack_char(vm: &VirtualMachine, arg: PyObjectRef, data: &mut [u8]) -> PyResult<()> { let v = PyBytesRef::try_from_object(vm, arg)?; - let ch = *v.borrow_value().iter().exactly_one().map_err(|_| { + let ch = *v.as_bytes().iter().exactly_one().map_err(|_| { new_struct_error( vm, "char format requires a bytes object of length 1".to_owned(), @@ -894,7 +894,7 @@ pub(crate) mod _struct { let spec = FormatSpec::decode_and_parse(vm, &fmt)?; let fmt_str = match fmt { Either::A(s) => s, - Either::B(b) => PyStr::from(std::str::from_utf8(b.borrow_value()).unwrap()) + Either::B(b) => PyStr::from(std::str::from_utf8(b.as_bytes()).unwrap()) .into_ref_with_type(vm, vm.ctx.types.str_type.clone())?, }; PyStruct { spec, fmt_str }.into_ref_with_type(vm, cls) diff --git a/vm/src/stdlib/socket.rs b/vm/src/stdlib/socket.rs index e74baa65d..ba03280e5 100644 --- a/vm/src/stdlib/socket.rs +++ b/vm/src/stdlib/socket.rs @@ -470,7 +470,7 @@ impl PySocket { ) -> PyResult { let flags = flags.unwrap_or(0); let sock = self.sock(); - let mut buf = buf.borrow_value(); + let mut buf = buf.borrow_buf_mut(); let buf = &mut *buf; self.sock_op(vm, SelectKind::Read, || { sock.recv_with_flags(slice_as_uninit(buf), flags) @@ -505,7 +505,7 @@ impl PySocket { flags: OptionalArg, vm: &VirtualMachine, ) -> PyResult<(usize, PyObjectRef)> { - let mut buf = buf.borrow_value(); + let mut buf = buf.borrow_buf_mut(); let buf = &mut *buf; let buf = match nbytes { OptionalArg::Present(i) => { @@ -534,7 +534,7 @@ impl PySocket { vm: &VirtualMachine, ) -> PyResult { let flags = flags.unwrap_or(0); - let buf = bytes.borrow_value(); + let buf = bytes.borrow_buf(); let buf = &*buf; self.sock_op(vm, SelectKind::Write, || { self.sock().send_with_flags(buf, flags) @@ -554,7 +554,7 @@ impl PySocket { let deadline = timeout.map(Deadline::new); - let buf = bytes.borrow_value(); + let buf = bytes.borrow_buf(); let buf = &*buf; let mut buf_offset = 0; // now we have like 3 layers of interrupt loop :) @@ -595,7 +595,7 @@ impl PySocket { OptionalArg::Missing => (0, arg2), }; let addr = self.extract_address(address, "sendto", vm)?; - let buf = bytes.borrow_value(); + let buf = bytes.borrow_buf(); let buf = &*buf; self.sock_op(vm, SelectKind::Write, || { self.sock().send_to_with_flags(buf, &addr, flags) @@ -915,7 +915,7 @@ fn _socket_inet_aton(ip_string: PyStrRef, vm: &VirtualMachine) -> PyResult PyResult { - let packed_ip = packed_ip.borrow_value(); + let packed_ip = packed_ip.borrow_buf(); let packed_ip = <&[u8; 4]>::try_from(&*packed_ip) .map_err(|_| vm.new_os_error("packed IP wrong length for inet_ntoa".to_owned()))?; Ok(vm.ctx.new_str(Ipv4Addr::from(*packed_ip).to_string())) @@ -1165,7 +1165,7 @@ fn _socket_inet_ntop( packed_ip: PyBytesLike, vm: &VirtualMachine, ) -> PyResult { - let packed_ip = packed_ip.borrow_value(); + let packed_ip = packed_ip.borrow_buf(); match af_inet { c::AF_INET => { let packed_ip = <&[u8; 4]>::try_from(&*packed_ip).map_err(|_| { diff --git a/vm/src/stdlib/ssl.rs b/vm/src/stdlib/ssl.rs index 123a50ac7..3dc53302c 100644 --- a/vm/src/stdlib/ssl.rs +++ b/vm/src/stdlib/ssl.rs @@ -846,7 +846,7 @@ impl PySslSocket { #[pymethod] fn write(&self, data: PyBytesLike, vm: &VirtualMachine) -> PyResult { let mut stream = self.stream.write(); - let data = data.borrow_value(); + let data = data.borrow_buf(); let data = &*data; let timeout = SocketTimeout::get(stream.get_ref()); let state = ssl_select(stream.get_ref(), SslNeeds::Write, &timeout); @@ -889,7 +889,7 @@ impl PySslSocket { fn read(&self, n: usize, buffer: OptionalArg, vm: &VirtualMachine) -> PyResult { let mut stream = self.stream.write(); let mut inner_buffer = if let OptionalArg::Present(buffer) = &buffer { - Either::A(buffer.borrow_value()) + Either::A(buffer.borrow_buf_mut()) } else { Either::B(vec![0u8; n]) }; diff --git a/vm/src/stdlib/termios.rs b/vm/src/stdlib/termios.rs index f686f6218..64cf148e1 100644 --- a/vm/src/stdlib/termios.rs +++ b/vm/src/stdlib/termios.rs @@ -76,11 +76,8 @@ mod termios { )) })?; for (cc, x) in termios.c_cc.iter_mut().zip(cc.iter()) { - *cc = if let Some(c) = x - .payload::() - .filter(|b| b.borrow_value().len() == 1) - { - c.borrow_value()[0] as _ + *cc = if let Some(c) = x.payload::().filter(|b| b.as_bytes().len() == 1) { + c.as_bytes()[0] as _ } else if let Some(i) = x.payload::() { int::try_to_primitive(i.as_bigint(), vm)? } else { diff --git a/vm/src/stdlib/zlib.rs b/vm/src/stdlib/zlib.rs index d14dca04f..566d56c79 100644 --- a/vm/src/stdlib/zlib.rs +++ b/vm/src/stdlib/zlib.rs @@ -11,7 +11,7 @@ mod decl { use crate::function::OptionalArg; use crate::types::create_simple_type; use crate::vm::VirtualMachine; - use crate::{BorrowValue, IntoPyRef, PyResult, PyValue, StaticType}; + use crate::{IntoPyRef, PyResult, PyValue, StaticType}; use adler32::RollingAdler32 as Adler32; use crc32fast::Hasher as Crc32; @@ -310,7 +310,7 @@ mod decl { if stream_end && !leftover.is_empty() { let mut unused_data = self.unused_data.lock(); let unused: Vec<_> = unused_data - .borrow_value() + .as_bytes() .iter() .chain(leftover) .copied() @@ -326,7 +326,7 @@ mod decl { } else { Some(args.max_length) }; - let data = args.data.borrow_value(); + let data = args.data.borrow_buf(); let data = &*data; let mut d = self.decompress.lock();