Skip to content

Commit 31f4999

Browse files
committed
Flatten PyObject/PyInner into one struct
1 parent 7a5ba0e commit 31f4999

File tree

2 files changed

+102
-170
lines changed

2 files changed

+102
-170
lines changed

vm/src/pyobject.rs

Lines changed: 4 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ use crate::dictdatatype::Dict;
3737
use crate::exceptions::{self, PyBaseExceptionRef};
3838
use crate::function::{IntoFuncArgs, IntoPyNativeFunc};
3939
use crate::iterator;
40-
pub use crate::pyobjectrc::{PyObjectRef, PyObjectWeak, PyRef, PyWeakRef};
40+
pub use crate::pyobjectrc::{PyObject, PyObjectRef, PyObjectWeak, PyRef, PyWeakRef};
4141
use crate::scope::Scope;
4242
use crate::slots::{PyTpFlags, PyTypeSlots};
4343
use crate::types::{create_type_with_slots, TypeZoo};
@@ -371,12 +371,7 @@ impl PyContext {
371371
}
372372

373373
pub fn new_base_object(&self, class: PyTypeRef, dict: Option<PyDictRef>) -> PyObjectRef {
374-
PyObject {
375-
typ: PyRwLock::new(class),
376-
dict: dict.map(PyRwLock::new),
377-
payload: object::PyBaseObject,
378-
}
379-
.into_ref()
374+
PyObject::new(object::PyBaseObject, class, dict)
380375
}
381376

382377
pub fn add_tp_new_wrapper(&self, ty: &PyTypeRef) {
@@ -399,19 +394,6 @@ impl Default for PyContext {
399394
}
400395
}
401396

402-
/// This is an actual python object. It consists of a `typ` which is the
403-
/// python class, and carries some rust payload optionally. This rust
404-
/// payload can be a rust float or rust int in case of float and int objects.
405-
#[repr(C)]
406-
pub struct PyObject<T>
407-
where
408-
T: ?Sized,
409-
{
410-
pub(crate) typ: PyRwLock<PyTypeRef>, // __class__ member
411-
pub(crate) dict: Option<PyRwLock<PyDictRef>>, // __dict__ member
412-
pub payload: T,
413-
}
414-
415397
impl<T> TryFromObject for PyRef<T>
416398
where
417399
T: PyValue,
@@ -628,10 +610,10 @@ pub trait TypeProtocol {
628610
}
629611
}
630612

631-
impl<T> TypeProtocol for PyObject<T> {
613+
impl TypeProtocol for PyObjectRef {
632614
fn class(&self) -> PyLease<'_, PyType> {
633615
PyLease {
634-
inner: self.typ.read(),
616+
inner: self.class_lock().read(),
635617
}
636618
}
637619
}
@@ -676,12 +658,6 @@ where
676658
}
677659
}
678660

679-
impl<T: fmt::Debug> fmt::Debug for PyObject<T> {
680-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
681-
write!(f, "[PyObj {:?}]", &self.payload)
682-
}
683-
}
684-
685661
/// An iterable Python object.
686662
///
687663
/// `PyIterable` implements `FromArgs` so that a built-in function can accept
@@ -882,43 +858,6 @@ where
882858
}
883859
}
884860

885-
impl<T> PyObject<T>
886-
where
887-
T: Sized + PyObjectPayload,
888-
{
889-
#[allow(clippy::new_ret_no_self)]
890-
pub fn new(payload: T, typ: PyTypeRef, dict: Option<PyDictRef>) -> PyObjectRef {
891-
PyObject {
892-
typ: PyRwLock::new(typ),
893-
dict: dict.map(PyRwLock::new),
894-
payload,
895-
}
896-
.into_ref()
897-
}
898-
899-
// Move this object into a reference object, transferring ownership.
900-
pub fn into_ref(self) -> PyObjectRef {
901-
PyObjectRef::new(self)
902-
}
903-
}
904-
905-
impl<T> PyObject<T> {
906-
pub fn dict(&self) -> Option<PyDictRef> {
907-
self.dict.as_ref().map(|mu| mu.read().clone())
908-
}
909-
/// Set the dict field. Returns `Err(dict)` if this object does not have a dict field
910-
/// in the first place.
911-
pub fn set_dict(&self, dict: PyDictRef) -> Result<(), PyDictRef> {
912-
match self.dict {
913-
Some(ref mu) => {
914-
*mu.write() = dict;
915-
Ok(())
916-
}
917-
None => Err(dict),
918-
}
919-
}
920-
}
921-
922861
cfg_if::cfg_if! {
923862
if #[cfg(feature = "threading")] {
924863
pub trait PyThreadingConstraint: Send + Sync {}

0 commit comments

Comments
 (0)