-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Move BorrowValue to rustpython-common, add BorrowedValue enum #2228
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+166
−7
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,3 +18,4 @@ cfg-if = "0.1" | |
| once_cell = "1.4.1" | ||
| siphasher = "0.3" | ||
| rand = "0.7.3" | ||
| derive_more = "0.99.9" | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| use crate::cell::{ | ||
| PyMappedMutexGuard, PyMappedRwLockReadGuard, PyMappedRwLockWriteGuard, PyMutexGuard, | ||
| PyRwLockReadGuard, PyRwLockWriteGuard, | ||
| }; | ||
| use std::ops::{Deref, DerefMut}; | ||
|
|
||
| pub trait BorrowValue<'a> { | ||
| type Borrowed: 'a + Deref; | ||
| fn borrow_value(&'a self) -> Self::Borrowed; | ||
| } | ||
|
|
||
| #[derive(Debug, derive_more::From)] | ||
| pub enum BorrowedValue<'a, T: ?Sized> { | ||
| Ref(&'a T), | ||
| MuLock(PyMutexGuard<'a, T>), | ||
| MappedMuLock(PyMappedMutexGuard<'a, T>), | ||
| ReadLock(PyRwLockReadGuard<'a, T>), | ||
| MappedReadLock(PyMappedRwLockReadGuard<'a, T>), | ||
| } | ||
|
|
||
| impl<T: ?Sized> Deref for BorrowedValue<'_, T> { | ||
| type Target = T; | ||
| fn deref(&self) -> &T { | ||
| match self { | ||
| Self::Ref(r) => r, | ||
| Self::MuLock(m) => &m, | ||
| Self::MappedMuLock(m) => &m, | ||
| Self::ReadLock(r) => &r, | ||
| Self::MappedReadLock(m) => &m, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[derive(Debug, derive_more::From)] | ||
| pub enum BorrowedValueMut<'a, T: ?Sized> { | ||
| RefMut(&'a mut T), | ||
| MuLock(PyMutexGuard<'a, T>), | ||
| MappedMuLock(PyMappedMutexGuard<'a, T>), | ||
| WriteLock(PyRwLockWriteGuard<'a, T>), | ||
| MappedWriteLock(PyMappedRwLockWriteGuard<'a, T>), | ||
| } | ||
|
|
||
| impl<T: ?Sized> Deref for BorrowedValueMut<'_, T> { | ||
| type Target = T; | ||
| fn deref(&self) -> &T { | ||
| match self { | ||
| Self::RefMut(r) => r, | ||
| Self::MuLock(m) => &m, | ||
| Self::MappedMuLock(m) => &m, | ||
| Self::WriteLock(w) => &w, | ||
| Self::MappedWriteLock(w) => &w, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl<T: ?Sized> DerefMut for BorrowedValueMut<'_, T> { | ||
| fn deref_mut(&mut self) -> &mut T { | ||
| match self { | ||
| Self::RefMut(r) => r, | ||
| Self::MuLock(m) => &mut *m, | ||
| Self::MappedMuLock(m) => &mut *m, | ||
| Self::WriteLock(w) => &mut *w, | ||
| Self::MappedWriteLock(w) => &mut *w, | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is this better to be an enum rather than concrete type or boxed dyn?
I thought buffer protocol help to avoid this situation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I could probably add a Dyn variant that's just
Box<dyn Deref>if eventually needed, but as is these are the most common forms of borrowing, and they all just have the underlying borrow stored as a reference/pointer, so no function calls are necessary to Deref it (unlike a dyn Deref)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe I don't know rust pratice well, so this is trying discussion.
When I am seeing BorrowValue for specific type like
PyBytesLike, it doesn't use all the enum fields but its borrowed type is BorrowedValue. I think this is giving unnessassary cost.using enum for each case is ok - because it is what it need to be. But BorrowedValue itself as enum is hiding somewhat not expected to be hided.
my suggestion is, defining specific type for each borrowed types.
BorrowedBytesLikeorBorrowedRwBytes. And BorrowedValue itself as a trait (if it is needed). We can find better abstraction or we can usedyn BorrowedValuein future complicated scenarios.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is good because with the buffer protocol
PyBytesLikeandPyRwBytesLikewill become extensible and having a flexible return type fromborrow_valuewill make it easier for implementer's of the buffer protocol. I don't this will actually have much cost as all variants probably will optimize down to a simply deref anyways.