First impl of fallible allocations#3494
Open
coolreader18 wants to merge 6 commits into
Open
Conversation
youknowone
approved these changes
Dec 9, 2021
Comment on lines
+74
to
+92
| struct SetLenOnDrop<'a, T> { | ||
| vec: &'a mut Vec<T>, | ||
| local_len: usize, | ||
| } | ||
|
|
||
| impl<'a, T> SetLenOnDrop<'a, T> { | ||
| #[inline] | ||
| fn new(vec: &'a mut Vec<T>) -> Self { | ||
| SetLenOnDrop { | ||
| local_len: vec.len(), | ||
| vec, | ||
| } | ||
| } | ||
|
|
||
| #[inline] | ||
| fn increment_len(&mut self, increment: usize) { | ||
| self.local_len += increment; | ||
| } | ||
| } |
Member
There was a problem hiding this comment.
This idea looks good. I think it can have similar interface like MaybeUninit because they have similar concept.
struct VecBuilder {
vec: Vec<T>,
len: usize,
}
impl VecUninit {
fn try_with_len(len: usize) -> TryReserveResult {
Self::try_with_capacity(len, len)
}
/// SAFETY: len <= capacity
unsafe fn try_with_capacity(capacity: usize, len: usize) -> TryReserveResult {
debug_assert!(len <= capacity);
Self {
vec: Vec::try_with_capacity(...)
len,
}
}
unsafe fn assume_init(self) -> Vec {
self.vec.set_len(self.len);
self.vec
}
}
Member
Author
There was a problem hiding this comment.
Oh.. this is just copied from the standard library. I should add a comment.
Comment on lines
+70
to
+81
| pub trait PyErrResultExt { | ||
| type Ok; | ||
| fn map_pyerr(self, vm: &VirtualMachine) -> PyResult<Self::Ok>; | ||
| } | ||
|
|
||
| impl<T, E: IntoPyException> PyErrResultExt for Result<T, E> { | ||
| type Ok = T; | ||
| #[inline] | ||
| fn map_pyerr(self, vm: &VirtualMachine) -> PyResult<T> { | ||
| self.map_err(|e| e.into_pyexception(vm)) | ||
| } | ||
| } |
Member
Author
There was a problem hiding this comment.
Glad you like it! I've been thinking about something like this for a while, to get rid of ".map_err(|e| e.into_pyexception(vm))` everywhere
Member
There was a problem hiding this comment.
I think this is different approach but solving a part of problems I tried to handle in #3326
Member
|
bumping here, any chance we could rebase this and get it in? |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
This PR doesn't change a ton of things to use fallible allocations, but it does set up a framework for switching over to fallible allocations inside RustPython.
Related to #3493