diff --git a/vm/src/builtins/memory.rs b/vm/src/builtins/memory.rs index cf2d85cbc..6da565255 100644 --- a/vm/src/builtins/memory.rs +++ b/vm/src/builtins/memory.rs @@ -19,7 +19,7 @@ use crate::{ IdProtocol, IntoPyObject, PyClassImpl, PyComparisonValue, PyContext, PyObjectRef, PyRef, PyResult, PyThreadingConstraint, PyValue, TypeProtocol, }; -use crate::{TryFromObject, VirtualMachine}; +use crate::{TryFromBorrowedObject, TryFromObject, VirtualMachine}; use crossbeam_utils::atomic::AtomicCell; use itertools::Itertools; use num_bigint::BigInt; @@ -28,9 +28,8 @@ use num_traits::{One, Signed, ToPrimitive, Zero}; #[derive(Debug)] pub struct PyBufferRef(Box); -impl TryFromObject for PyBufferRef { - // FIXME: `obj: &PyObjectRef` is enough - fn try_from_object(vm: &VirtualMachine, obj: PyObjectRef) -> PyResult { +impl TryFromBorrowedObject for PyBufferRef { + fn try_from_borrowed_object(vm: &VirtualMachine, obj: &PyObjectRef) -> PyResult { let obj_cls = obj.class(); for cls in obj_cls.iter_mro() { if let Some(f) = cls.slots.as_buffer.as_ref() { @@ -277,7 +276,7 @@ impl PyMemoryView { args: PyMemoryViewNewArgs, vm: &VirtualMachine, ) -> PyResult> { - let buffer = PyBufferRef::try_from_object(vm, args.object.clone())?; + let buffer = PyBufferRef::try_from_borrowed_object(vm, &args.object)?; let zelf = PyMemoryView::from_buffer(args.object, buffer, vm)?; zelf.into_ref_with_type(vm, cls) } @@ -782,7 +781,7 @@ impl PyMemoryView { return Ok(false); } - let other = match PyBufferRef::try_from_object(vm, other.clone()) { + let other = match PyBufferRef::try_from_borrowed_object(vm, other) { Ok(buf) => buf, Err(_) => return Ok(false), }; diff --git a/vm/src/byteslike.rs b/vm/src/byteslike.rs index f769f1847..751f58e2f 100644 --- a/vm/src/byteslike.rs +++ b/vm/src/byteslike.rs @@ -2,7 +2,7 @@ use crate::builtins::memory::PyBufferRef; use crate::builtins::PyStrRef; use crate::common::borrow::{BorrowedValue, BorrowedValueMut}; use crate::vm::VirtualMachine; -use crate::{PyObjectRef, PyResult, TryFromObject}; +use crate::{PyObjectRef, PyResult, TryFromBorrowedObject, TryFromObject}; #[derive(Debug)] pub struct PyBytesLike(PyBufferRef); @@ -50,7 +50,7 @@ impl PyRwBytesLike { impl PyBytesLike { pub fn new(vm: &VirtualMachine, obj: &PyObjectRef) -> PyResult { - let buffer = PyBufferRef::try_from_object(vm, obj.clone())?; + let buffer = PyBufferRef::try_from_borrowed_object(vm, obj)?; if buffer.get_options().contiguous { Ok(Self(buffer)) } else { @@ -78,7 +78,7 @@ pub fn try_bytes_like( obj: &PyObjectRef, f: impl FnOnce(&[u8]) -> R, ) -> PyResult { - let buffer = PyBufferRef::try_from_object(vm, obj.clone())?; + let buffer = PyBufferRef::try_from_borrowed_object(vm, obj)?; buffer.as_contiguous().map(|x| f(&*x)).ok_or_else(|| { vm.new_type_error("non-contiguous buffer is not a bytes-like object".to_owned()) }) @@ -89,7 +89,7 @@ pub fn try_rw_bytes_like( obj: &PyObjectRef, f: impl FnOnce(&mut [u8]) -> R, ) -> PyResult { - let buffer = PyBufferRef::try_from_object(vm, obj.clone())?; + let buffer = PyBufferRef::try_from_borrowed_object(vm, obj)?; buffer .as_contiguous_mut() .map(|mut x| f(&mut *x)) @@ -98,7 +98,7 @@ pub fn try_rw_bytes_like( impl PyRwBytesLike { pub fn new(vm: &VirtualMachine, obj: &PyObjectRef) -> PyResult { - let buffer = PyBufferRef::try_from_object(vm, obj.clone())?; + let buffer = PyBufferRef::try_from_borrowed_object(vm, obj)?; let options = buffer.get_options(); if !options.contiguous { Err(vm.new_type_error("non-contiguous buffer is not a bytes-like object".to_owned())) diff --git a/vm/src/cformat.rs b/vm/src/cformat.rs index 13cb6e907..0b4d0ae3b 100644 --- a/vm/src/cformat.rs +++ b/vm/src/cformat.rs @@ -7,7 +7,9 @@ use crate::builtins::pystr::PyStr; use crate::builtins::{tuple, PyBytes}; use crate::common::float_ops; use crate::vm::VirtualMachine; -use crate::{ItemProtocol, PyObjectRef, PyResult, TryFromObject, TypeProtocol}; +use crate::{ + ItemProtocol, PyObjectRef, PyResult, TryFromBorrowedObject, TryFromObject, TypeProtocol, +}; use itertools::Itertools; use num_bigint::{BigInt, Sign}; use num_traits::cast::ToPrimitive; @@ -364,7 +366,7 @@ impl CFormatSpec { Ok(s.into_bytes()) } CFormatPreconversor::Str | CFormatPreconversor::Bytes => { - if let Ok(buffer) = PyBufferRef::try_from_object(vm, obj.clone()) { + if let Ok(buffer) = PyBufferRef::try_from_borrowed_object(vm, &obj) { let guard; let vec; let bytes = match buffer.as_contiguous() { diff --git a/vm/src/stdlib/sre.rs b/vm/src/stdlib/sre.rs index bdc25f48f..df62d90e7 100644 --- a/vm/src/stdlib/sre.rs +++ b/vm/src/stdlib/sre.rs @@ -18,7 +18,7 @@ mod _sre { use crate::VirtualMachine; use crate::{ IntoPyObject, ItemProtocol, PyCallable, PyComparisonValue, PyObjectRef, PyRef, PyResult, - PyValue, StaticType, TryFromObject, + PyValue, StaticType, TryFromBorrowedObject, TryFromObject, }; use core::str; use sre_engine::constants::SreFlag; @@ -153,7 +153,7 @@ mod _sre { let vec; let s; let str_drive = if self.isbytes { - buffer = PyBufferRef::try_from_object(vm, string.clone())?; + buffer = PyBufferRef::try_from_borrowed_object(vm, &string)?; let bytes = match buffer.as_contiguous() { Some(bytes) => { guard = bytes;