Skip to content

Commit

Permalink
Use AsRef/AsMut instead of as_super/as_super_mut
Browse files Browse the repository at this point in the history
  • Loading branch information
kngwyu committed Feb 21, 2020
1 parent 1f5cb83 commit 3d0ee2a
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 10 deletions.
2 changes: 1 addition & 1 deletion guide/src/class.md
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ impl SubClass {
}

fn method2(self_: PyRef<Self>) -> PyResult<usize> {
let super_ = self_.as_super(); // Get &BaseClass
let super_ = self_.as_ref(); // Get &BaseClass
super_.method().map(|x| x * self_.val2)
}
}
Expand Down
16 changes: 8 additions & 8 deletions src/pycell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,11 +258,8 @@ pub struct PyRef<'p, T: PyClass> {
inner: &'p PyCellInner<T>,
}

impl<'p, T> PyRef<'p, T>
where
T: PyClass,
{
pub fn as_super(&self) -> &T::BaseType {
impl<'p, T: PyClass> AsRef<T::BaseType> for PyRef<'p, T> {
fn as_ref(&self) -> &T::BaseType {
unsafe { self.inner.ob_base.unchecked_ref() }
}
}
Expand Down Expand Up @@ -326,11 +323,14 @@ pub struct PyRefMut<'p, T: PyClass> {
inner: &'p PyCellInner<T>,
}

impl<'p, T: PyClass> PyRefMut<'p, T> {
pub fn as_super(&self) -> &T::BaseType {
impl<'p, T: PyClass> AsRef<T::BaseType> for PyRefMut<'p, T> {
fn as_ref(&self) -> &T::BaseType {
unsafe { self.inner.ob_base.unchecked_ref() }
}
pub fn as_super_mut(&mut self) -> &mut T::BaseType {
}

impl<'p, T: PyClass> AsMut<T::BaseType> for PyRefMut<'p, T> {
fn as_mut(&mut self) -> &mut T::BaseType {
unsafe { self.inner.ob_base.unchecked_mut() }
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/test_gc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ fn inheritance_with_new_methods_with_drop() {
let obj: &PyCell<SubClassWithDrop> = inst.try_into().unwrap();
let mut obj_ref_mut = obj.borrow_mut();
obj_ref_mut.data = Some(Arc::clone(&drop_called1));
obj_ref_mut.as_super_mut().data = Some(Arc::clone(&drop_called2));
obj_ref_mut.as_mut().data = Some(Arc::clone(&drop_called2));
}

assert!(drop_called1.load(Ordering::Relaxed));
Expand Down

0 comments on commit 3d0ee2a

Please sign in to comment.