From 3d0ee2a28d7c471123218094645b3745a379edcd Mon Sep 17 00:00:00 2001 From: kngwyu Date: Fri, 21 Feb 2020 20:37:35 +0900 Subject: [PATCH] Use AsRef/AsMut instead of as_super/as_super_mut --- guide/src/class.md | 2 +- src/pycell.rs | 16 ++++++++-------- tests/test_gc.rs | 2 +- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/guide/src/class.md b/guide/src/class.md index 02a6abc3..92721a84 100644 --- a/guide/src/class.md +++ b/guide/src/class.md @@ -281,7 +281,7 @@ impl SubClass { } fn method2(self_: PyRef) -> PyResult { - let super_ = self_.as_super(); // Get &BaseClass + let super_ = self_.as_ref(); // Get &BaseClass super_.method().map(|x| x * self_.val2) } } diff --git a/src/pycell.rs b/src/pycell.rs index 40a07ec3..3d28a1c2 100644 --- a/src/pycell.rs +++ b/src/pycell.rs @@ -258,11 +258,8 @@ pub struct PyRef<'p, T: PyClass> { inner: &'p PyCellInner, } -impl<'p, T> PyRef<'p, T> -where - T: PyClass, -{ - pub fn as_super(&self) -> &T::BaseType { +impl<'p, T: PyClass> AsRef for PyRef<'p, T> { + fn as_ref(&self) -> &T::BaseType { unsafe { self.inner.ob_base.unchecked_ref() } } } @@ -326,11 +323,14 @@ pub struct PyRefMut<'p, T: PyClass> { inner: &'p PyCellInner, } -impl<'p, T: PyClass> PyRefMut<'p, T> { - pub fn as_super(&self) -> &T::BaseType { +impl<'p, T: PyClass> AsRef 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 for PyRefMut<'p, T> { + fn as_mut(&mut self) -> &mut T::BaseType { unsafe { self.inner.ob_base.unchecked_mut() } } } diff --git a/tests/test_gc.rs b/tests/test_gc.rs index 67a74215..04016a23 100644 --- a/tests/test_gc.rs +++ b/tests/test_gc.rs @@ -268,7 +268,7 @@ fn inheritance_with_new_methods_with_drop() { let obj: &PyCell = 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));