Skip to content

First impl of fallible allocations#3494

Open
coolreader18 wants to merge 6 commits into
RustPython:mainfrom
coolreader18:fallible-allocations
Open

First impl of fallible allocations#3494
coolreader18 wants to merge 6 commits into
RustPython:mainfrom
coolreader18:fallible-allocations

Conversation

@coolreader18

Copy link
Copy Markdown
Member

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

Comment thread common/src/safe_alloc.rs
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;
}
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
    }
}

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh.. this is just copied from the standard library. I should add a comment.

Comment thread vm/src/function.rs
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))
}
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is different approach but solving a part of problems I tried to handle in #3326

@DimitrisJim

Copy link
Copy Markdown
Member

bumping here, any chance we could rebase this and get it in?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants