Skip to content

Commit 241a8ac

Browse files
committed
remove some redundant traits
1 parent e9bd41e commit 241a8ac

File tree

2 files changed

+65
-197
lines changed

2 files changed

+65
-197
lines changed

src/pycell.rs

+64-196
Original file line numberDiff line numberDiff line change
@@ -195,9 +195,59 @@ use std::ops::{Deref, DerefMut};
195195

196196
pub struct EmptySlot(());
197197
pub struct BorrowChecker(Cell<BorrowFlag>);
198-
pub struct FilledInAncestor(());
199198

200-
impl BorrowChecker {
199+
pub trait PyClassBorrowChecker {
200+
fn new() -> Self;
201+
202+
/// Increments immutable borrow count, if possible
203+
fn try_borrow(&self) -> Result<(), PyBorrowError>;
204+
205+
fn try_borrow_unguarded(&self) -> Result<(), PyBorrowError>;
206+
207+
/// Decrements immutable borrow count
208+
fn release_borrow(&self);
209+
/// Increments mutable borrow count, if possible
210+
fn try_borrow_mut(&self) -> Result<(), PyBorrowMutError>;
211+
/// Decremements mutable borrow count
212+
fn release_borrow_mut(&self);
213+
}
214+
215+
impl PyClassBorrowChecker for EmptySlot {
216+
#[inline]
217+
fn new() -> Self {
218+
Self(())
219+
}
220+
221+
#[inline]
222+
fn try_borrow(&self) -> Result<(), PyBorrowError> {
223+
Ok(())
224+
}
225+
226+
#[inline]
227+
fn try_borrow_unguarded(&self) -> Result<(), PyBorrowError> {
228+
Ok(())
229+
}
230+
231+
#[inline]
232+
fn release_borrow(&self) {}
233+
234+
#[inline]
235+
fn try_borrow_mut(&self) -> Result<(), PyBorrowMutError> {
236+
unreachable!()
237+
}
238+
239+
#[inline]
240+
fn release_borrow_mut(&self) {
241+
unreachable!()
242+
}
243+
}
244+
245+
impl PyClassBorrowChecker for BorrowChecker {
246+
#[inline]
247+
fn new() -> Self {
248+
Self(Cell::new(BorrowFlag::UNUSED))
249+
}
250+
201251
fn try_borrow(&self) -> Result<(), PyBorrowError> {
202252
let flag = self.0.get();
203253
if flag != BorrowFlag::HAS_MUTABLE_BORROW {
@@ -237,51 +287,16 @@ impl BorrowChecker {
237287
}
238288
}
239289

240-
pub trait PyClassMutabilityStorage {
241-
fn new() -> Self;
242-
}
243-
244-
impl PyClassMutabilityStorage for EmptySlot {
245-
fn new() -> Self {
246-
Self(())
247-
}
248-
}
249-
250-
impl PyClassMutabilityStorage for BorrowChecker {
251-
fn new() -> Self {
252-
Self(Cell::new(BorrowFlag::UNUSED))
253-
}
254-
}
255-
256-
// - Storage type, either empty, present, or in ancestor
257-
// - Mutability is either
258-
// - Immutable - i.e. EmptySlot
259-
// - Mutable - i.e. BorrowChecker
260-
// - ExtendsMutableAncestor<Mutability> - FilledInAncestor
261-
// - Mutability trait needs to encode the inheritance
262-
263290
pub trait PyClassMutability {
264291
// The storage for this inheritance layer. Only the first mutable class in
265292
// an inheritance hierarchy needs to store the borrow flag.
266-
type Storage: PyClassMutabilityStorage;
293+
type Storage: PyClassBorrowChecker;
267294
// The borrow flag needed to implement this class' mutability. Empty until
268295
// the first mutable class, at which point it is BorrowChecker and will be
269296
// for all subclasses.
270-
type Checker;
297+
type Checker: PyClassBorrowChecker;
271298
type ImmutableChild: PyClassMutability;
272299
type MutableChild: PyClassMutability;
273-
274-
/// Increments immutable borrow count, if possible
275-
fn try_borrow(checker: &Self::Checker) -> Result<(), PyBorrowError>;
276-
277-
fn try_borrow_unguarded(checker: &Self::Checker) -> Result<(), PyBorrowError>;
278-
279-
/// Decrements immutable borrow count
280-
fn release_borrow(checker: &Self::Checker);
281-
/// Increments mutable borrow count, if possible
282-
fn try_borrow_mut(checker: &Self::Checker) -> Result<(), PyBorrowMutError>;
283-
/// Decremements mutable borrow count
284-
fn release_borrow_mut(checker: &Self::Checker);
285300
}
286301

287302
pub trait GetBorrowChecker<T: PyClassImpl> {
@@ -321,175 +336,28 @@ impl PyClassMutability for ImmutableClass {
321336
type Checker = EmptySlot;
322337
type ImmutableChild = ImmutableClass;
323338
type MutableChild = MutableClass;
324-
325-
fn try_borrow(_: &EmptySlot) -> Result<(), PyBorrowError> {
326-
Ok(())
327-
}
328-
329-
fn try_borrow_unguarded(_: &EmptySlot) -> Result<(), PyBorrowError> {
330-
Ok(())
331-
}
332-
333-
fn release_borrow(_: &EmptySlot) {}
334-
335-
fn try_borrow_mut(_: &EmptySlot) -> Result<(), PyBorrowMutError> {
336-
unreachable!()
337-
}
338-
339-
fn release_borrow_mut(_: &EmptySlot) {
340-
unreachable!()
341-
}
342339
}
343340

344341
impl PyClassMutability for MutableClass {
345342
type Storage = BorrowChecker;
346343
type Checker = BorrowChecker;
347344
type ImmutableChild = ExtendsMutableAncestor<ImmutableClass>;
348345
type MutableChild = ExtendsMutableAncestor<MutableClass>;
349-
350-
// FIXME the below are all wrong
351-
352-
fn try_borrow(checker: &BorrowChecker) -> Result<(), PyBorrowError> {
353-
checker.try_borrow()
354-
}
355-
356-
fn try_borrow_unguarded(checker: &BorrowChecker) -> Result<(), PyBorrowError> {
357-
checker.try_borrow_unguarded()
358-
}
359-
360-
fn release_borrow(checker: &BorrowChecker) {
361-
checker.release_borrow()
362-
}
363-
364-
fn try_borrow_mut(checker: &BorrowChecker) -> Result<(), PyBorrowMutError> {
365-
checker.try_borrow_mut()
366-
}
367-
368-
fn release_borrow_mut(checker: &BorrowChecker) {
369-
checker.release_borrow_mut()
370-
}
371346
}
372347

373348
impl<M: PyClassMutability> PyClassMutability for ExtendsMutableAncestor<M> {
374349
type Storage = EmptySlot;
375350
type Checker = BorrowChecker;
376351
type ImmutableChild = ExtendsMutableAncestor<ImmutableClass>;
377352
type MutableChild = ExtendsMutableAncestor<MutableClass>;
378-
379-
// FIXME the below are all wrong
380-
381-
fn try_borrow(checker: &BorrowChecker) -> Result<(), PyBorrowError> {
382-
checker.try_borrow()
383-
}
384-
385-
fn try_borrow_unguarded(checker: &BorrowChecker) -> Result<(), PyBorrowError> {
386-
checker.try_borrow_unguarded()
387-
}
388-
389-
fn release_borrow(checker: &BorrowChecker) {
390-
checker.release_borrow()
391-
}
392-
393-
fn try_borrow_mut(checker: &BorrowChecker) -> Result<(), PyBorrowMutError> {
394-
checker.try_borrow_mut()
395-
}
396-
397-
fn release_borrow_mut(checker: &BorrowChecker) {
398-
checker.release_borrow_mut()
399-
}
400-
}
401-
402-
pub trait Mutability {
403-
/// Creates a new borrow checker
404-
fn new() -> Self;
405-
/// Increments immutable borrow count, if possible
406-
fn try_borrow(&self) -> Result<(), PyBorrowError>;
407-
408-
fn try_borrow_unguarded(&self) -> Result<(), PyBorrowError>;
409-
410-
/// Decrements immutable borrow count
411-
fn release_borrow(&self);
412-
/// Increments mutable borrow count, if possible
413-
fn try_borrow_mut(&self) -> Result<(), PyBorrowMutError>;
414-
/// Decremements mutable borrow count
415-
fn release_borrow_mut(&self);
416-
}
417-
418-
pub struct Mutable {
419-
flag: Cell<BorrowFlag>,
420-
}
421-
impl Mutability for Mutable {
422-
fn new() -> Self {
423-
Self {
424-
flag: Cell::new(BorrowFlag::UNUSED),
425-
}
426-
}
427-
428-
fn try_borrow(&self) -> Result<(), PyBorrowError> {
429-
let flag = self.flag.get();
430-
if flag != BorrowFlag::HAS_MUTABLE_BORROW {
431-
self.flag.set(flag.increment());
432-
Ok(())
433-
} else {
434-
Err(PyBorrowError { _private: () })
435-
}
436-
}
437-
438-
fn try_borrow_unguarded(&self) -> Result<(), PyBorrowError> {
439-
let flag = self.flag.get();
440-
if flag != BorrowFlag::HAS_MUTABLE_BORROW {
441-
Ok(())
442-
} else {
443-
Err(PyBorrowError { _private: () })
444-
}
445-
}
446-
447-
fn release_borrow(&self) {
448-
let flag = self.flag.get();
449-
self.flag.set(flag.decrement())
450-
}
451-
452-
fn try_borrow_mut(&self) -> Result<(), PyBorrowMutError> {
453-
let flag = self.flag.get();
454-
if flag == BorrowFlag::UNUSED {
455-
self.flag.set(BorrowFlag::HAS_MUTABLE_BORROW);
456-
Ok(())
457-
} else {
458-
Err(PyBorrowMutError { _private: () })
459-
}
460-
}
461-
462-
fn release_borrow_mut(&self) {
463-
self.flag.set(BorrowFlag::UNUSED)
464-
}
465-
}
466-
467-
pub struct Immutable {
468-
flag: PhantomData<Cell<BorrowFlag>>,
469353
}
470-
impl Mutability for Immutable {
471-
fn new() -> Self {
472-
Self { flag: PhantomData }
473-
}
474-
475-
fn try_borrow(&self) -> Result<(), PyBorrowError> {
476-
Ok(())
477-
}
478-
479-
fn try_borrow_unguarded(&self) -> Result<(), PyBorrowError> {
480-
Ok(())
481-
}
482354

483-
fn release_borrow(&self) {}
355+
pub trait Mutability { }
484356

485-
fn try_borrow_mut(&self) -> Result<(), PyBorrowMutError> {
486-
unreachable!()
487-
}
488-
489-
fn release_borrow_mut(&self) {
490-
unreachable!()
491-
}
492-
}
357+
pub struct Mutable;
358+
impl Mutability for Mutable {}
359+
pub struct Immutable;
360+
impl Mutability for Immutable {}
493361

494362
/// Base layout of PyCell.
495363
#[doc(hidden)]
@@ -615,7 +483,7 @@ impl<T: PyClass> PyCell<T> {
615483
/// ```
616484
pub fn try_borrow(&self) -> Result<PyRef<'_, T>, PyBorrowError> {
617485
self.ensure_threadsafe();
618-
T::PyClassMutability::try_borrow(self.borrow_checker()).map(|_| PyRef { inner: self })
486+
self.borrow_checker().try_borrow().map(|_| PyRef { inner: self })
619487
}
620488

621489
/// Mutably borrows the value `T`, returning an error if the value is currently borrowed.
@@ -644,7 +512,7 @@ impl<T: PyClass> PyCell<T> {
644512
T: MutablePyClass,
645513
{
646514
self.ensure_threadsafe();
647-
T::PyClassMutability::try_borrow_mut(self.borrow_checker())
515+
self.borrow_checker().try_borrow_mut()
648516
.map(|_| PyRefMut { inner: self })
649517
}
650518

@@ -679,7 +547,7 @@ impl<T: PyClass> PyCell<T> {
679547
/// ```
680548
pub unsafe fn try_borrow_unguarded(&self) -> Result<&T, PyBorrowError> {
681549
self.ensure_threadsafe();
682-
T::PyClassMutability::try_borrow_unguarded(self.borrow_checker())
550+
self.borrow_checker().try_borrow_unguarded()
683551
.map(|_: ()| &*self.contents.value.get())
684552
}
685553

@@ -983,7 +851,7 @@ impl<'p, T: PyClass> Deref for PyRef<'p, T> {
983851

984852
impl<'p, T: PyClass> Drop for PyRef<'p, T> {
985853
fn drop(&mut self) {
986-
T::PyClassMutability::release_borrow(self.inner.borrow_checker())
854+
self.inner.borrow_checker().release_borrow()
987855
}
988856
}
989857

@@ -1081,7 +949,7 @@ impl<'p, T: MutablePyClass> DerefMut for PyRefMut<'p, T> {
1081949

1082950
impl<'p, T: MutablePyClass> Drop for PyRefMut<'p, T> {
1083951
fn drop(&mut self) {
1084-
T::PyClassMutability::release_borrow_mut(self.inner.borrow_checker())
952+
self.inner.borrow_checker().release_borrow_mut()
1085953
}
1086954
}
1087955

src/pyclass_init.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::pyclass::MutablePyClass;
55
use crate::{ffi, PyCell, PyClass, PyErr, PyResult, Python};
66
use crate::{
77
ffi::PyTypeObject,
8-
pycell::{PyCellContents, PyClassMutability, PyClassMutabilityStorage},
8+
pycell::{PyCellContents, PyClassMutability, PyClassBorrowChecker},
99
type_object::{get_tp_alloc, PyTypeInfo},
1010
};
1111
use std::{

0 commit comments

Comments
 (0)