Skip to content

Commit d635a67

Browse files
committed
migrate TryIntoRef to IntoPyStrRef
1 parent 80b0c25 commit d635a67

File tree

4 files changed

+47
-50
lines changed

4 files changed

+47
-50
lines changed

vm/src/builtins/pystr.rs

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use crate::{
1616
stdlib::sys,
1717
utils::Either,
1818
IdProtocol, ItemProtocol, PyClassDef, PyClassImpl, PyComparisonValue, PyContext, PyObjectRef,
19-
PyRef, PyResult, PyValue, TryIntoRef, TypeProtocol, VirtualMachine,
19+
PyRef, PyResult, PyValue, TypeProtocol, VirtualMachine,
2020
};
2121
use ascii::{AsciiStr, AsciiString};
2222
use bstr::ByteSlice;
@@ -169,23 +169,41 @@ impl fmt::Display for PyStr {
169169
}
170170
}
171171

172-
impl TryIntoRef<PyStr> for AsciiString {
172+
pub trait IntoPyStrRef {
173+
fn into_pystr_ref(self, vm: &VirtualMachine) -> PyStrRef;
174+
}
175+
176+
impl IntoPyStrRef for PyStrRef {
177+
#[inline]
178+
fn into_pystr_ref(self, _vm: &VirtualMachine) -> PyRef<PyStr> {
179+
self
180+
}
181+
}
182+
183+
impl IntoPyStrRef for PyStr {
173184
#[inline]
174-
fn try_into_ref(self, vm: &VirtualMachine) -> PyRef<PyStr> {
175-
unsafe { PyStr::new_ascii_unchecked(self.into()) }.into_ref(vm)
185+
fn into_pystr_ref(self, vm: &VirtualMachine) -> PyRef<PyStr> {
186+
self.into_ref(vm)
187+
}
188+
}
189+
190+
impl IntoPyStrRef for AsciiString {
191+
#[inline]
192+
fn into_pystr_ref(self, vm: &VirtualMachine) -> PyRef<PyStr> {
193+
PyStr::from(self).into_ref(vm)
176194
}
177195
}
178196

179-
impl TryIntoRef<PyStr> for String {
197+
impl IntoPyStrRef for String {
180198
#[inline]
181-
fn try_into_ref(self, vm: &VirtualMachine) -> PyRef<PyStr> {
199+
fn into_pystr_ref(self, vm: &VirtualMachine) -> PyRef<PyStr> {
182200
PyStr::from(self).into_ref(vm)
183201
}
184202
}
185203

186-
impl TryIntoRef<PyStr> for &str {
204+
impl IntoPyStrRef for &str {
187205
#[inline]
188-
fn try_into_ref(self, vm: &VirtualMachine) -> PyRef<PyStr> {
206+
fn into_pystr_ref(self, vm: &VirtualMachine) -> PyRef<PyStr> {
189207
PyStr::from(self).into_ref(vm)
190208
}
191209
}

vm/src/pyobject.rs

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -407,11 +407,6 @@ impl<T: PyValue> IntoPyObject for PyRefExact<T> {
407407
self.obj.into()
408408
}
409409
}
410-
impl<T: PyValue> TryIntoRef<T> for PyRefExact<T> {
411-
fn try_into_ref(self, _vm: &VirtualMachine) -> PyRef<T> {
412-
self.obj
413-
}
414-
}
415410

416411
pub trait IdProtocol {
417412
fn get_id(&self) -> usize;
@@ -613,19 +608,6 @@ impl<T: TryFromObject> TryFromObject for Option<T> {
613608
}
614609
}
615610

616-
/// Allows coercion of a types into PyRefs, so that we can write functions that can take
617-
/// refs, pyobject refs or basic types.
618-
pub trait TryIntoRef<T: PyObjectPayload> {
619-
fn try_into_ref(self, vm: &VirtualMachine) -> PyRef<T>;
620-
}
621-
622-
impl<T: PyObjectPayload> TryIntoRef<T> for PyRef<T> {
623-
#[inline]
624-
fn try_into_ref(self, _vm: &VirtualMachine) -> PyRef<T> {
625-
self
626-
}
627-
}
628-
629611
/// Implemented by any type that can be created from a Python object.
630612
///
631613
/// Any type that implements `TryFromObject` is automatically `FromArgs`, and

vm/src/scope.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1+
use crate::{
2+
builtins::{pystr::IntoPyStrRef, PyDictRef, PyStrRef},
3+
function::IntoPyObject,
4+
ItemProtocol, VirtualMachine,
5+
};
16
use std::fmt;
27

3-
use crate::builtins::{PyDictRef, PyStr, PyStrRef};
4-
use crate::function::IntoPyObject;
5-
use crate::VirtualMachine;
6-
use crate::{ItemProtocol, TryIntoRef};
7-
88
#[derive(Clone)]
99
pub struct Scope {
1010
pub locals: PyDictRef,
@@ -145,7 +145,7 @@ mod sealed {
145145
impl Sealed for super::PyStrRef {}
146146
}
147147
pub trait PyName:
148-
sealed::Sealed + crate::dictdatatype::DictKey + Clone + IntoPyObject + TryIntoRef<PyStr>
148+
sealed::Sealed + crate::dictdatatype::DictKey + Clone + IntoPyObject + IntoPyStrRef
149149
{
150150
}
151151
impl PyName for &str {}

vm/src/vm.rs

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use crate::{
1010
builtins::{
1111
code::{self, PyCode},
1212
module, object,
13+
pystr::IntoPyStrRef,
1314
tuple::{IntoPyTuple, PyTuple, PyTupleRef, PyTupleTyped},
1415
PyBaseException, PyBaseExceptionRef, PyDictRef, PyInt, PyIntRef, PyList, PyModule, PyStr,
1516
PyStrRef, PyTypeRef,
@@ -29,7 +30,7 @@ use crate::{
2930
stdlib,
3031
utils::Either,
3132
IdProtocol, ItemProtocol, PyArithmeticValue, PyContext, PyLease, PyMethod, PyObject,
32-
PyObjectRef, PyRef, PyRefExact, PyResult, PyValue, TryFromObject, TryIntoRef, TypeProtocol,
33+
PyObjectRef, PyRef, PyRefExact, PyResult, PyValue, TryFromObject, TypeProtocol,
3334
};
3435
use crossbeam_utils::atomic::AtomicCell;
3536
use num_traits::{Signed, ToPrimitive};
@@ -781,14 +782,10 @@ impl VirtualMachine {
781782
syntax_error
782783
}
783784

784-
pub fn new_import_error(
785-
&self,
786-
msg: String,
787-
name: impl TryIntoRef<PyStr>,
788-
) -> PyBaseExceptionRef {
785+
pub fn new_import_error(&self, msg: String, name: impl IntoPyStrRef) -> PyBaseExceptionRef {
789786
let import_error = self.ctx.exceptions.import_error.clone();
790787
let exc = self.new_exception_msg(import_error, msg);
791-
self.set_attr(exc.as_object(), "name", name.try_into_ref(self))
788+
self.set_attr(exc.as_object(), "name", name.into_pystr_ref(self))
792789
.unwrap();
793790
exc
794791
}
@@ -917,11 +914,11 @@ impl VirtualMachine {
917914
#[inline]
918915
pub fn import(
919916
&self,
920-
module: impl TryIntoRef<PyStr>,
917+
module: impl IntoPyStrRef,
921918
from_list: Option<PyTupleTyped<PyStrRef>>,
922919
level: usize,
923920
) -> PyResult {
924-
self._import_inner(module.try_into_ref(self), from_list, level)
921+
self._import_inner(module.into_pystr_ref(self), from_list, level)
925922
}
926923

927924
fn _import_inner(
@@ -1364,9 +1361,9 @@ impl VirtualMachine {
13641361
#[cfg_attr(feature = "flame-it", flame("VirtualMachine"))]
13651362
pub fn get_attribute<T>(&self, obj: PyObjectRef, attr_name: T) -> PyResult
13661363
where
1367-
T: TryIntoRef<PyStr>,
1364+
T: IntoPyStrRef,
13681365
{
1369-
let attr_name = attr_name.try_into_ref(self);
1366+
let attr_name = attr_name.into_pystr_ref(self);
13701367
vm_trace!("vm.__getattribute__: {:?} {:?}", obj, attr_name);
13711368
let getattro = obj
13721369
.class()
@@ -1381,7 +1378,7 @@ impl VirtualMachine {
13811378
attr_name: T,
13821379
) -> PyResult<Option<PyObjectRef>>
13831380
where
1384-
T: TryIntoRef<PyStr>,
1381+
T: IntoPyStrRef,
13851382
{
13861383
match self.get_attribute(obj, attr_name) {
13871384
Ok(attr) => Ok(Some(attr)),
@@ -1416,15 +1413,15 @@ impl VirtualMachine {
14161413

14171414
pub fn set_attr<K, V>(&self, obj: &PyObjectRef, attr_name: K, attr_value: V) -> PyResult<()>
14181415
where
1419-
K: TryIntoRef<PyStr>,
1416+
K: IntoPyStrRef,
14201417
V: Into<PyObjectRef>,
14211418
{
1422-
let attr_name = attr_name.try_into_ref(self);
1419+
let attr_name = attr_name.into_pystr_ref(self);
14231420
self.call_set_attr(obj, attr_name, Some(attr_value.into()))
14241421
}
14251422

1426-
pub fn del_attr(&self, obj: &PyObjectRef, attr_name: impl TryIntoRef<PyStr>) -> PyResult<()> {
1427-
let attr_name = attr_name.try_into_ref(self);
1423+
pub fn del_attr(&self, obj: &PyObjectRef, attr_name: impl IntoPyStrRef) -> PyResult<()> {
1424+
let attr_name = attr_name.into_pystr_ref(self);
14281425
self.call_set_attr(obj, attr_name, None)
14291426
}
14301427

@@ -2134,11 +2131,11 @@ impl VirtualMachine {
21342131
pub fn __module_set_attr(
21352132
&self,
21362133
module: &PyObjectRef,
2137-
attr_name: impl TryIntoRef<PyStr>,
2134+
attr_name: impl IntoPyStrRef,
21382135
attr_value: impl Into<PyObjectRef>,
21392136
) -> PyResult<()> {
21402137
let val = attr_value.into();
2141-
object::setattr(module, attr_name.try_into_ref(self), Some(val), self)
2138+
object::setattr(module, attr_name.into_pystr_ref(self), Some(val), self)
21422139
}
21432140
}
21442141

0 commit comments

Comments
 (0)