pub struct PyRef<'p, T: PyClass> { /* private fields */ }
Expand description
A wrapper type for an immutably borrowed value from a [Bound<'py, T>
].
See the Bound
documentation for more information.
§Examples
You can use PyRef
as an alternative to a &self
receiver when
- you need to access the pointer of the
Bound
, or - you want to get a super class.
#[pyclass(subclass)]
struct Parent {
basename: &'static str,
}
#[pyclass(extends=Parent)]
struct Child {
name: &'static str,
}
#[pymethods]
impl Child {
#[new]
fn new() -> (Self, Parent) {
(Child { name: "Caterpillar" }, Parent { basename: "Butterfly" })
}
fn format(slf: PyRef<'_, Self>) -> String {
// We can get *mut ffi::PyObject from PyRef
let refcnt = unsafe { pyo3::ffi::Py_REFCNT(slf.as_ptr()) };
// We can get &Self::BaseType by as_ref
let basename = slf.as_ref().basename;
format!("{}(base: {}, cnt: {})", slf.name, basename, refcnt)
}
}
See the module-level documentation for more information.
Implementations§
Source§impl<'py, T: PyClass> PyRef<'py, T>
impl<'py, T: PyClass> PyRef<'py, T>
Source§impl<'p, T, U> PyRef<'p, T>
impl<'p, T, U> PyRef<'p, T>
Sourcepub fn into_super(self) -> PyRef<'p, U>
pub fn into_super(self) -> PyRef<'p, U>
Gets a PyRef<T::BaseType>
.
While as_ref()
returns a reference of type &T::BaseType
, this cannot be
used to get the base of T::BaseType
.
But with the help of this method, you can get hold of instances of the super-superclass when needed.
§Examples
#[pyclass(subclass)]
struct Base1 {
name1: &'static str,
}
#[pyclass(extends=Base1, subclass)]
struct Base2 {
name2: &'static str,
}
#[pyclass(extends=Base2)]
struct Sub {
name3: &'static str,
}
#[pymethods]
impl Sub {
#[new]
fn new() -> PyClassInitializer<Self> {
PyClassInitializer::from(Base1 { name1: "base1" })
.add_subclass(Base2 { name2: "base2" })
.add_subclass(Self { name3: "sub" })
}
fn name(slf: PyRef<'_, Self>) -> String {
let subname = slf.name3;
let super_ = slf.into_super();
format!("{} {} {}", super_.as_ref().name1, super_.name2, subname)
}
}
Sourcepub fn as_super(&self) -> &PyRef<'p, U>
pub fn as_super(&self) -> &PyRef<'p, U>
Borrows a shared reference to PyRef<T::BaseType>
.
With the help of this method, you can access attributes and call methods
on the superclass without consuming the PyRef<T>
. This method can also
be chained to access the super-superclass (and so on).
§Examples
#[pyclass(subclass)]
struct Base {
base_name: &'static str,
}
#[pymethods]
impl Base {
fn base_name_len(&self) -> usize {
self.base_name.len()
}
}
#[pyclass(extends=Base)]
struct Sub {
sub_name: &'static str,
}
#[pymethods]
impl Sub {
#[new]
fn new() -> (Self, Base) {
(Self { sub_name: "sub_name" }, Base { base_name: "base_name" })
}
fn sub_name_len(&self) -> usize {
self.sub_name.len()
}
fn format_name_lengths(slf: PyRef<'_, Self>) -> String {
format!("{} {}", slf.as_super().base_name_len(), slf.sub_name_len())
}
}
Trait Implementations§
Source§impl<'a, T: PyClass> AsPyPointer for PyRef<'a, T>
impl<'a, T: PyClass> AsPyPointer for PyRef<'a, T>
Source§impl<'py, T> FromPyObject<'py> for PyRef<'py, T>where
T: PyClass,
impl<'py, T> FromPyObject<'py> for PyRef<'py, T>where
T: PyClass,
Auto Trait Implementations§
impl<'p, T> Freeze for PyRef<'p, T>
impl<'p, T> RefUnwindSafe for PyRef<'p, T>where
T: RefUnwindSafe,
impl<'p, T> !Send for PyRef<'p, T>
impl<'p, T> !Sync for PyRef<'p, T>
impl<'p, T> Unpin for PyRef<'p, T>where
T: Unpin,
impl<'p, T> UnwindSafe for PyRef<'p, T>where
T: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more