Py::new_ref -> PyRef::new
This commit is contained in:
parent
f95f4e8c14
commit
4a552a88cf
|
@ -39,7 +39,7 @@ macro_rules! py_unary_pyref_func {
|
|||
let _pool = $crate::GILPool::new();
|
||||
let py = $crate::Python::assume_gil_acquired();
|
||||
let slf = py.mut_from_borrowed_ptr::<T>(slf);
|
||||
let res = $class::$f(PyRefMut::new(slf)).into();
|
||||
let res = $class::$f(PyRefMut::from_mut(slf)).into();
|
||||
$crate::callback::cb_convert($conv, py, res)
|
||||
}
|
||||
Some(wrap::<$class>)
|
||||
|
|
|
@ -52,7 +52,7 @@ pub trait PyNativeType: PyObjectWithGIL {}
|
|||
/// }
|
||||
/// let gil = Python::acquire_gil();
|
||||
/// let py = gil.python();
|
||||
/// let obj = py.init_ref(|| Point { x: 3, y: 4 }).unwrap();
|
||||
/// let obj = PyRef::new(gil.python(), || Point { x: 3, y: 4 }).unwrap();
|
||||
/// let d = vec![("p", obj)].into_py_dict(py);
|
||||
/// py.run("assert p.length() == 12", None, Some(d)).unwrap();
|
||||
/// ```
|
||||
|
@ -60,8 +60,23 @@ pub trait PyNativeType: PyObjectWithGIL {}
|
|||
pub struct PyRef<'a, T: PyTypeInfo>(&'a T, PhantomData<Rc<()>>);
|
||||
|
||||
impl<'a, T: PyTypeInfo> PyRef<'a, T> {
|
||||
pub(crate) fn new(t: &'a T) -> Self {
|
||||
PyRef(t, PhantomData)
|
||||
pub(crate) fn from_ref(r: &'a T) -> Self {
|
||||
PyRef(r, PhantomData)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, T> PyRef<'a, T>
|
||||
where
|
||||
T: PyTypeInfo + PyTypeObject + PyTypeCreate,
|
||||
{
|
||||
pub fn new<F>(py: Python, f: F) -> PyResult<PyRef<T>>
|
||||
where
|
||||
F: FnOnce() -> T,
|
||||
{
|
||||
let obj = T::create(py)?;
|
||||
obj.init(f)?;
|
||||
let ref_ = unsafe { py.from_owned_ptr(obj.into_ptr()) };
|
||||
Ok(PyRef::from_ref(ref_))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -85,15 +100,52 @@ impl<'a, T: PyTypeInfo> Deref for PyRef<'a, T> {
|
|||
}
|
||||
|
||||
/// Mutable version of [`PyRef`](struct.PyRef.html).
|
||||
/// # Example
|
||||
/// ```
|
||||
/// use pyo3::prelude::*;
|
||||
/// use pyo3::types::IntoPyDict;
|
||||
/// #[pyclass]
|
||||
/// struct Point {
|
||||
/// x: i32,
|
||||
/// y: i32,
|
||||
/// }
|
||||
/// #[pymethods]
|
||||
/// impl Point {
|
||||
/// fn length(&self) -> i32 {
|
||||
/// self.x * self.y
|
||||
/// }
|
||||
/// }
|
||||
/// let gil = Python::acquire_gil();
|
||||
/// let py = gil.python();
|
||||
/// let mut obj = PyRefMut::new(gil.python(), || Point { x: 3, y: 4 }).unwrap();
|
||||
/// let d = vec![("p", obj.to_object(py))].into_py_dict(py);
|
||||
/// obj.x = 5; obj.y = 20;
|
||||
/// py.run("assert p.length() == 100", None, Some(d)).unwrap();
|
||||
/// ```
|
||||
#[derive(Debug)]
|
||||
pub struct PyRefMut<'a, T: PyTypeInfo>(&'a mut T, PhantomData<Rc<()>>);
|
||||
|
||||
impl<'a, T: PyTypeInfo> PyRefMut<'a, T> {
|
||||
pub(crate) fn new(t: &'a mut T) -> Self {
|
||||
pub(crate) fn from_mut(t: &'a mut T) -> Self {
|
||||
PyRefMut(t, PhantomData)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, T> PyRefMut<'a, T>
|
||||
where
|
||||
T: PyTypeInfo + PyTypeObject + PyTypeCreate,
|
||||
{
|
||||
pub fn new<F>(py: Python, f: F) -> PyResult<PyRefMut<T>>
|
||||
where
|
||||
F: FnOnce() -> T,
|
||||
{
|
||||
let obj = T::create(py)?;
|
||||
obj.init(f)?;
|
||||
let ref_ = unsafe { py.mut_from_owned_ptr(obj.into_ptr()) };
|
||||
Ok(PyRefMut::from_mut(ref_))
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, T: PyTypeInfo> ToPyPointer for PyRefMut<'a, T> {
|
||||
fn as_ptr(&self) -> *mut ffi::PyObject {
|
||||
(self.0 as &T).as_ptr_dispatch()
|
||||
|
@ -309,32 +361,6 @@ where
|
|||
let ob = unsafe { Py::from_owned_ptr(ob.into_ptr()) };
|
||||
Ok(ob)
|
||||
}
|
||||
|
||||
/// Create new instance of `T` and move it under python management.
|
||||
/// Returns references to `T`
|
||||
pub fn new_ref<F>(py: Python, f: F) -> PyResult<PyRef<T>>
|
||||
where
|
||||
F: FnOnce() -> T,
|
||||
T: PyTypeObject + PyTypeInfo,
|
||||
{
|
||||
let ob = <T as PyTypeCreate>::create(py)?;
|
||||
ob.init(f)?;
|
||||
|
||||
unsafe { Ok(PyRef::new(py.from_owned_ptr(ob.into_ptr()))) }
|
||||
}
|
||||
|
||||
/// Create new instance of `T` and move it under python management.
|
||||
/// Returns mutable references to `T`
|
||||
pub fn new_mut<F>(py: Python, f: F) -> PyResult<PyRefMut<T>>
|
||||
where
|
||||
F: FnOnce() -> T,
|
||||
T: PyTypeObject + PyTypeInfo,
|
||||
{
|
||||
let ob = <T as PyTypeCreate>::create(py)?;
|
||||
ob.init(f)?;
|
||||
|
||||
unsafe { Ok(PyRefMut::new(py.mut_from_owned_ptr(ob.into_ptr()))) }
|
||||
}
|
||||
}
|
||||
|
||||
/// Specialization workaround
|
||||
|
@ -370,11 +396,11 @@ where
|
|||
{
|
||||
#[inline]
|
||||
fn as_ref(&self, py: Python) -> PyRef<T> {
|
||||
PyRef::new(self.as_ref_dispatch(py))
|
||||
PyRef::from_ref(self.as_ref_dispatch(py))
|
||||
}
|
||||
#[inline]
|
||||
fn as_mut(&mut self, py: Python) -> PyRefMut<T> {
|
||||
PyRefMut::new(self.as_mut_dispatch(py))
|
||||
PyRefMut::from_mut(self.as_mut_dispatch(py))
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -257,11 +257,11 @@ impl PyObject {
|
|||
impl AsPyRef<PyObjectRef> for PyObject {
|
||||
#[inline]
|
||||
fn as_ref(&self, _py: Python) -> PyRef<PyObjectRef> {
|
||||
unsafe { PyRef::new(&*(self as *const _ as *const PyObjectRef)) }
|
||||
unsafe { PyRef::from_ref(&*(self as *const _ as *const PyObjectRef)) }
|
||||
}
|
||||
#[inline]
|
||||
fn as_mut(&mut self, _py: Python) -> PyRefMut<PyObjectRef> {
|
||||
unsafe { PyRefMut::new(&mut *(self as *mut _ as *mut PyObjectRef)) }
|
||||
unsafe { PyRefMut::from_mut(&mut *(self as *mut _ as *mut PyObjectRef)) }
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -266,7 +266,7 @@ impl<'p> Python<'p> {
|
|||
F: FnOnce() -> T,
|
||||
T: PyTypeCreate,
|
||||
{
|
||||
Py::new_ref(self, f)
|
||||
PyRef::new(self, f)
|
||||
}
|
||||
|
||||
/// Create new instance of `T` and move it under python management.
|
||||
|
@ -277,7 +277,7 @@ impl<'p> Python<'p> {
|
|||
F: FnOnce() -> T,
|
||||
T: PyTypeCreate,
|
||||
{
|
||||
Py::new_mut(self, f)
|
||||
PyRefMut::new(self, f)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -439,7 +439,7 @@ struct DunderDictSupport {}
|
|||
fn dunder_dict_support() {
|
||||
let gil = Python::acquire_gil();
|
||||
let py = gil.python();
|
||||
let inst = Py::new_ref(py, || DunderDictSupport {}).unwrap();
|
||||
let inst = PyRef::new(py, || DunderDictSupport {}).unwrap();
|
||||
py_run!(
|
||||
py,
|
||||
inst,
|
||||
|
@ -457,7 +457,7 @@ struct WeakRefDunderDictSupport {}
|
|||
fn weakref_dunder_dict_support() {
|
||||
let gil = Python::acquire_gil();
|
||||
let py = gil.python();
|
||||
let inst = Py::new_ref(py, || WeakRefDunderDictSupport {}).unwrap();
|
||||
let inst = PyRef::new(py, || WeakRefDunderDictSupport {}).unwrap();
|
||||
py_run!(
|
||||
py,
|
||||
inst,
|
||||
|
|
|
@ -157,7 +157,7 @@ fn gc_integration() {
|
|||
{
|
||||
let gil = Python::acquire_gil();
|
||||
let py = gil.python();
|
||||
let inst = Py::new_ref(py, || GCIntegration {
|
||||
let inst = PyRef::new(py, || GCIntegration {
|
||||
self_ref: RefCell::new(py.None()),
|
||||
dropped: TestDropCall {
|
||||
drop_called: Arc::clone(&drop_called),
|
||||
|
@ -183,7 +183,7 @@ fn gc_integration2() {
|
|||
let py = gil.python();
|
||||
// Temporarily disable pythons garbage collector to avoid a race condition
|
||||
py.run("import gc; gc.disable()", None, None).unwrap();
|
||||
let inst = Py::new_ref(py, || GCIntegration2 {}).unwrap();
|
||||
let inst = PyRef::new(py, || GCIntegration2 {}).unwrap();
|
||||
py_run!(py, inst, "assert inst in gc.get_objects()");
|
||||
py.run("gc.enable()", None, None).unwrap();
|
||||
}
|
||||
|
@ -195,7 +195,7 @@ struct WeakRefSupport {}
|
|||
fn weakref_support() {
|
||||
let gil = Python::acquire_gil();
|
||||
let py = gil.python();
|
||||
let inst = Py::new_ref(py, || WeakRefSupport {}).unwrap();
|
||||
let inst = PyRef::new(py, || WeakRefSupport {}).unwrap();
|
||||
py_run!(
|
||||
py,
|
||||
inst,
|
||||
|
|
Loading…
Reference in New Issue