Skip to content

Commit a77e78d

Browse files
committed
BorrowValue for bytes
1 parent 7a8045f commit a77e78d

14 files changed

Lines changed: 56 additions & 43 deletions

File tree

vm/src/bytesinner.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ impl TryFromObject for PyBytesInner {
3737
fn try_from_object(vm: &VirtualMachine, obj: PyObjectRef) -> PyResult<Self> {
3838
match_class!(match obj {
3939
i @ PyBytes => Ok(PyBytesInner {
40-
elements: i.get_value().to_vec()
40+
elements: i.borrow_value().to_vec()
4141
}),
4242
j @ PyByteArray => Ok(PyBytesInner {
4343
elements: j.borrow_value().elements.to_vec()
@@ -75,7 +75,7 @@ impl ByteInnerNewOptions {
7575
if let Ok(input) = eval.downcast::<PyString>() {
7676
let bytes = objstr::encode_string(input, Some(enc), None, vm)?;
7777
Ok(PyBytesInner {
78-
elements: bytes.get_value().to_vec(),
78+
elements: bytes.borrow_value().to_vec(),
7979
})
8080
} else {
8181
Err(vm.new_type_error("encoding without a string argument".to_owned()))
@@ -108,7 +108,7 @@ impl ByteInnerNewOptions {
108108
vm.new_type_error("string argument without an encoding".to_owned())
109109
);
110110
}
111-
i @ PyBytes => Ok(i.get_value().to_vec()),
111+
i @ PyBytes => Ok(i.borrow_value().to_vec()),
112112
j @ PyByteArray => Ok(j.borrow_value().elements.to_vec()),
113113
obj => {
114114
// TODO: only support this method in the bytes() constructor
@@ -1140,7 +1140,7 @@ where
11401140
F: Fn(&[u8]) -> R,
11411141
{
11421142
match_class!(match obj {
1143-
i @ PyBytes => Some(f(i.get_value())),
1143+
i @ PyBytes => Some(f(i.borrow_value())),
11441144
j @ PyByteArray => Some(f(&j.borrow_value().elements)),
11451145
_ => None,
11461146
})

vm/src/byteslike.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::obj::objbytearray::{PyByteArray, PyByteArrayRef};
22
use crate::obj::objbytes::{PyBytes, PyBytesRef};
33
use crate::pyobject::PyObjectRef;
4-
use crate::pyobject::{PyResult, TryFromObject, TypeProtocol};
4+
use crate::pyobject::{BorrowValue, PyResult, TryFromObject, TypeProtocol};
55
use crate::stdlib::array::{PyArray, PyArrayRef};
66
use crate::vm::VirtualMachine;
77

@@ -41,7 +41,7 @@ impl PyBytesLike {
4141

4242
pub fn to_cow(&self) -> std::borrow::Cow<[u8]> {
4343
match self {
44-
PyBytesLike::Bytes(b) => b.get_value().into(),
44+
PyBytesLike::Bytes(b) => b.borrow_value().into(),
4545
PyBytesLike::Bytearray(b) => b.borrow_value().elements.clone().into(),
4646
PyBytesLike::Array(array) => array.tobytes().into(),
4747
}
@@ -50,7 +50,7 @@ impl PyBytesLike {
5050
#[inline]
5151
pub fn with_ref<R>(&self, f: impl FnOnce(&[u8]) -> R) -> R {
5252
match self {
53-
PyBytesLike::Bytes(b) => f(b.get_value()),
53+
PyBytesLike::Bytes(b) => f(b.borrow_value()),
5454
PyBytesLike::Bytearray(b) => f(&b.borrow_value().elements),
5555
PyBytesLike::Array(array) => f(&*array.get_bytes()),
5656
}

vm/src/obj/objbytearray.rs

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,30 +41,40 @@ pub struct PyByteArray {
4141

4242
pub type PyByteArrayRef = PyRef<PyByteArray>;
4343

44+
impl<'a> BorrowValue<'a> for PyByteArray {
45+
type Borrowed = PyRwLockReadGuard<'a, PyBytesInner>;
46+
47+
fn borrow_value(&'a self) -> Self::Borrowed {
48+
self.inner.read()
49+
}
50+
}
51+
4452
impl PyByteArray {
4553
fn from_inner(inner: PyBytesInner) -> Self {
4654
PyByteArray {
4755
inner: PyRwLock::new(inner),
4856
}
4957
}
5058

51-
pub fn borrow_value(&self) -> PyRwLockReadGuard<'_, PyBytesInner> {
52-
self.inner.read()
53-
}
54-
5559
pub fn borrow_value_mut(&self) -> PyRwLockWriteGuard<'_, PyBytesInner> {
5660
self.inner.write()
5761
}
5862
}
5963

60-
impl From<Vec<u8>> for PyByteArray {
61-
fn from(elements: Vec<u8>) -> Self {
64+
impl From<PyBytesInner> for PyByteArray {
65+
fn from(inner: PyBytesInner) -> Self {
6266
Self {
63-
inner: PyRwLock::new(PyBytesInner { elements }),
67+
inner: PyRwLock::new(inner),
6468
}
6569
}
6670
}
6771

72+
impl From<Vec<u8>> for PyByteArray {
73+
fn from(elements: Vec<u8>) -> Self {
74+
Self::from(PyBytesInner { elements })
75+
}
76+
}
77+
6878
impl PyValue for PyByteArray {
6979
fn class(vm: &VirtualMachine) -> PyClassRef {
7080
vm.ctx.bytearray_type()

vm/src/obj/objbytes.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use crate::bytesinner::{
1414
};
1515
use crate::function::{OptionalArg, OptionalOption};
1616
use crate::pyobject::{
17-
Either, IntoPyObject,
17+
BorrowValue, Either, IntoPyObject,
1818
PyArithmaticValue::{self, *},
1919
PyClassImpl, PyComparisonValue, PyContext, PyIterable, PyObjectRef, PyRef, PyResult, PyValue,
2020
TryFromObject, TypeProtocol,
@@ -40,8 +40,10 @@ pub struct PyBytes {
4040

4141
pub type PyBytesRef = PyRef<PyBytes>;
4242

43-
impl PyBytes {
44-
pub fn get_value(&self) -> &[u8] {
43+
impl<'a> BorrowValue<'a> for PyBytes {
44+
type Borrowed = &'a [u8];
45+
46+
fn borrow_value(&'a self) -> Self::Borrowed {
4547
&self.inner.elements
4648
}
4749
}
@@ -504,7 +506,7 @@ impl PyBytesIterator {
504506
#[pymethod(name = "__next__")]
505507
fn next(&self, vm: &VirtualMachine) -> PyResult<u8> {
506508
let pos = self.position.fetch_add(1);
507-
if let Some(&ret) = self.bytes.get_value().get(pos) {
509+
if let Some(&ret) = self.bytes.borrow_value().get(pos) {
508510
Ok(ret)
509511
} else {
510512
Err(objiter::new_stop_iteration(vm))

vm/src/obj/objfloat.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -529,7 +529,7 @@ fn to_float(vm: &VirtualMachine, obj: &PyObjectRef) -> PyResult<f64> {
529529
vm.new_value_error(format!("could not convert string to float: '{}'", s))
530530
})?
531531
} else if let Some(bytes) = obj.payload_if_subclass::<PyBytes>(vm) {
532-
lexical_core::parse(bytes.get_value()).map_err(|_| {
532+
lexical_core::parse(bytes.borrow_value()).map_err(|_| {
533533
vm.new_value_error(format!(
534534
"could not convert string to float: '{}'",
535535
bytes.repr()

vm/src/obj/objint.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -761,7 +761,7 @@ pub(crate) fn to_int(vm: &VirtualMachine, obj: &PyObjectRef) -> PyResult<BigInt>
761761
bytes_to_int(s.as_bytes(), base)
762762
}
763763
bytes @ PyBytes => {
764-
let bytes = bytes.get_value();
764+
let bytes = bytes.borrow_value();
765765
bytes_to_int(bytes, base)
766766
}
767767
bytearray @ PyByteArray => {
@@ -814,7 +814,7 @@ fn to_int_radix(vm: &VirtualMachine, obj: &PyObjectRef, base: u32) -> PyResult<B
814814
bytes_to_int(s.as_bytes(), base)
815815
}
816816
bytes @ PyBytes => {
817-
let bytes = bytes.get_value();
817+
let bytes = bytes.borrow_value();
818818
bytes_to_int(bytes, base)
819819
}
820820
bytearray @ PyByteArray => {

vm/src/stdlib/array.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ use crate::obj::objstr::PyStringRef;
55
use crate::obj::objtype::PyClassRef;
66
use crate::obj::{objbool, objiter};
77
use crate::pyobject::{
8-
Either, IntoPyObject, PyClassImpl, PyIterable, PyObjectRef, PyRef, PyResult, PyValue,
9-
TryFromObject,
8+
BorrowValue, Either, IntoPyObject, PyClassImpl, PyIterable, PyObjectRef, PyRef, PyResult,
9+
PyValue, TryFromObject,
1010
};
1111
use crate::VirtualMachine;
1212

@@ -345,7 +345,7 @@ impl PyArray {
345345

346346
#[pymethod]
347347
fn frombytes(&self, b: PyBytesRef, vm: &VirtualMachine) -> PyResult<()> {
348-
let b = b.get_value();
348+
let b = b.borrow_value();
349349
let itemsize = self.borrow_value().itemsize();
350350
if b.len() % itemsize != 0 {
351351
return Err(vm.new_value_error("bytes length not a multiple of item size".to_owned()));

vm/src/stdlib/binascii.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ mod decl {
77
use crate::obj::objbytearray::{PyByteArray, PyByteArrayRef};
88
use crate::obj::objbytes::{PyBytes, PyBytesRef};
99
use crate::obj::objstr::{PyString, PyStringRef};
10-
use crate::pyobject::{PyObjectRef, PyResult, TryFromObject, TypeProtocol};
10+
use crate::pyobject::{BorrowValue, PyObjectRef, PyResult, TryFromObject, TypeProtocol};
1111
use crate::vm::VirtualMachine;
1212
use crc::{crc32, Hasher32};
1313
use itertools::Itertools;
@@ -44,7 +44,7 @@ mod decl {
4444
#[inline]
4545
pub fn with_ref<R>(&self, f: impl FnOnce(&[u8]) -> R) -> R {
4646
match self {
47-
SerializedData::Bytes(b) => f(b.get_value()),
47+
SerializedData::Bytes(b) => f(b.borrow_value()),
4848
SerializedData::Buffer(b) => f(&b.borrow_value().elements),
4949
SerializedData::Ascii(a) => f(a.as_str().as_bytes()),
5050
}

vm/src/stdlib/hashlib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::function::{OptionalArg, PyFuncArgs};
33
use crate::obj::objbytes::{PyBytes, PyBytesRef};
44
use crate::obj::objstr::PyStringRef;
55
use crate::obj::objtype::PyClassRef;
6-
use crate::pyobject::{PyClassImpl, PyObjectRef, PyResult, PyValue};
6+
use crate::pyobject::{BorrowValue, PyClassImpl, PyObjectRef, PyResult, PyValue};
77
use crate::vm::VirtualMachine;
88
use std::fmt;
99

@@ -68,7 +68,7 @@ impl PyHasher {
6868

6969
#[pymethod(name = "update")]
7070
fn update(&self, data: PyBytesRef, vm: &VirtualMachine) -> PyResult {
71-
self.borrow_value_mut().input(data.get_value());
71+
self.borrow_value_mut().input(data.borrow_value());
7272
Ok(vm.get_none())
7373
}
7474

vm/src/stdlib/io.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ use crate::obj::objiter;
2020
use crate::obj::objstr::{self, PyString, PyStringRef};
2121
use crate::obj::objtype::{self, PyClassRef};
2222
use crate::pyobject::{
23-
BufferProtocol, Either, IntoPyObject, PyObjectRef, PyRef, PyResult, PyValue, TryFromObject,
23+
BorrowValue, BufferProtocol, Either, IntoPyObject, PyObjectRef, PyRef, PyResult, PyValue,
24+
TryFromObject,
2425
};
2526
use crate::vm::VirtualMachine;
2627

@@ -393,7 +394,7 @@ fn bytes_io_new(
393394
) -> PyResult<PyBytesIORef> {
394395
let raw_bytes = object
395396
.flatten()
396-
.map_or_else(Vec::new, |input| input.get_value().to_vec());
397+
.map_or_else(Vec::new, |input| input.borrow_value().to_vec());
397398

398399
PyBytesIO {
399400
buffer: PyRwLock::new(BufferedIO::new(Cursor::new(raw_bytes))),

0 commit comments

Comments
 (0)