Skip to content

Commit

Permalink
Rename unchecked_refmut -> unchecked_mut
Browse files Browse the repository at this point in the history
  • Loading branch information
kngwyu committed Feb 18, 2020
1 parent 98d810e commit 4b746af
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 24 deletions.
38 changes: 19 additions & 19 deletions src/pycell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ where
unsafe fn unchecked_ref(&self) -> &T {
&*((&self) as *const &Self as *const _)
}
unsafe fn unchecked_refmut(&self) -> &mut T {
unsafe fn unchecked_mut(&self) -> &mut T {
&mut *((&self) as *const &Self as *const _ as *mut _)
}
}
Expand Down Expand Up @@ -64,7 +64,7 @@ unsafe impl<T: PyClass> PyObjectLayout<T> for PyCellInner<T> {
unsafe fn unchecked_ref(&self) -> &T {
&*self.value.get()
}
unsafe fn unchecked_refmut(&self) -> &mut T {
unsafe fn unchecked_mut(&self) -> &mut T {
&mut *self.value.get()
}
unsafe fn py_init(&mut self, value: T) {
Expand Down Expand Up @@ -202,8 +202,8 @@ unsafe impl<T: PyClass> PyObjectLayout<T> for PyCell<T> {
unsafe fn unchecked_ref(&self) -> &T {
self.inner.unchecked_ref()
}
unsafe fn unchecked_refmut(&self) -> &mut T {
self.inner.unchecked_refmut()
unsafe fn unchecked_mut(&self) -> &mut T {
self.inner.unchecked_mut()
}
unsafe fn py_init(&mut self, value: T) {
self.inner.value = ManuallyDrop::new(UnsafeCell::new(value));
Expand Down Expand Up @@ -262,7 +262,7 @@ impl<'p, T> PyRef<'p, T>
where
T: PyClass,
{
pub fn as_super(&'p self) -> &'p T::BaseType {
pub fn as_super(&self) -> &T::BaseType {
unsafe { self.inner.ob_base.unchecked_ref() }
}
}
Expand All @@ -273,11 +273,11 @@ where
U: PyClass,
{
pub fn into_super(self) -> PyRef<'p, U> {
let res = PyRef {
inner: &self.inner.ob_base,
};
std::mem::forget(self); // Avoid drop
res
let PyRef { inner } = self;
std::mem::forget(self);
PyRef {
inner: &inner.ob_base,
}
}
}

Expand Down Expand Up @@ -327,11 +327,11 @@ pub struct PyRefMut<'p, T: PyClass> {
}

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

Expand All @@ -341,11 +341,11 @@ where
U: PyClass,
{
pub fn into_super(self) -> PyRefMut<'p, U> {
let res = PyRefMut {
inner: &self.inner.ob_base,
};
std::mem::forget(self); // Avoid drop
res
let PyRefMut { inner } = self;
std::mem::forget(self);
PyRefMut {
inner: &inner.ob_base,
}
}
}

Expand All @@ -361,7 +361,7 @@ impl<'p, T: PyClass> Deref for PyRefMut<'p, T> {
impl<'p, T: PyClass> DerefMut for PyRefMut<'p, T> {
#[inline]
fn deref_mut(&mut self) -> &mut T {
unsafe { self.inner.unchecked_refmut() }
unsafe { self.inner.unchecked_mut() }
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/type_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub unsafe trait PyObjectLayout<T: PyTypeInfo> {
unsafe fn py_init(&mut self, _value: T) {}
unsafe fn py_drop(&mut self, _py: Python) {}
unsafe fn unchecked_ref(&self) -> &T;
unsafe fn unchecked_refmut(&self) -> &mut T;
unsafe fn unchecked_mut(&self) -> &mut T;
}

/// `T: PyObjectSizedLayout<U>` represents `T` is not a instance of
Expand Down
2 changes: 1 addition & 1 deletion src/types/any.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ unsafe impl crate::type_object::PyObjectLayout<PyAny> for ffi::PyObject {
unsafe fn unchecked_ref(&self) -> &PyAny {
&*((&self) as *const &Self as *const _)
}
unsafe fn unchecked_refmut(&self) -> &mut PyAny {
unsafe fn unchecked_mut(&self) -> &mut PyAny {
&mut *((&self) as *const &Self as *const _ as *mut _)
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ macro_rules! impl_layout {
unsafe fn unchecked_ref(&self) -> &$name {
&*((&self) as *const &Self as *const _)
}
unsafe fn unchecked_refmut(&self) -> &mut $name {
unsafe fn unchecked_mut(&self) -> &mut $name {
&mut *((&self) as *const &Self as *const _ as *mut _)
}
}
Expand Down
3 changes: 1 addition & 2 deletions tests/test_gc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,8 +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));
let super_ = obj_ref_mut.get_super_mut();
super_.data = Some(Arc::clone(&drop_called2));
obj_ref_mut.as_super_mut().data = Some(Arc::clone(&drop_called2));
}

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

0 comments on commit 4b746af

Please sign in to comment.