Skip to content

Conversation

@kjvalencik
Copy link
Member

The current lifetimes are overly restrictive. The reference is valid for as long as the handle is valid. Since the Context is always the most narrow scope, it will be valid for at least as long as Context, even if the Handle type is dropped. This allows downcasting and returning a borrow within the same function.

See this thread for more details. https://rust-bindings.slack.com/archives/C0HE1SA65/p1647378845225649

/// This may not be used if a mutable borrow is in scope. For the dynamically
/// checked variant see [`TypedArray::try_borrow`].
fn as_slice<'a: 'b, 'b, C>(&'b self, cx: &'b C) -> &'b [Self::Item]
fn as_slice<'cx, 'a, C>(&self, cx: &'a C) -> &'a [Self::Item]
Copy link
Member Author

Choose a reason for hiding this comment

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

'a is renamed to 'cx across the board for clarity. 'b becomes 'a since that's the only lifetime we need.

/// This may not be used if any other borrow is in scope. For the dynamically
/// checked variant see [`TypedArray::try_borrow_mut`].
fn as_mut_slice<'a: 'b, 'b, C>(&'b mut self, cx: &'b mut C) -> &'b mut [Self::Item]
fn as_mut_slice<'cx, 'a, C>(&mut self, cx: &'a mut C) -> &'a mut [Self::Item]
Copy link
Member Author

Choose a reason for hiding this comment

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

The 'a: 'b bound was unnecessarily complicated. It's not possible to have references that don't meet this requirement.

&self,
lock: &'b Lock<'b, C>,
) -> Result<Ref<'b, Self::Item>, BorrowError>
fn try_borrow<'cx, 'a, C>(&self, lock: &'a Lock<C>) -> Result<Ref<'a, Self::Item>, BorrowError>
Copy link
Member Author

Choose a reason for hiding this comment

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

Elided the inner lifetime of Lock to simplify.

}

if let Ok(v) = v.downcast::<JsBuffer, _>(cx) {
return Ok(Cow::Borrowed(v.as_slice(cx)));
Copy link
Member Author

Choose a reason for hiding this comment

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

This is the primary thing we are trying to allow; returning a reference to a handle that was downcast.

Copy link
Collaborator

Choose a reason for hiding this comment

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

This is really cool!

Copy link
Collaborator

@dherman dherman left a comment

Choose a reason for hiding this comment

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

I feel like I should have some suggestion, but borrowck did all the heavy lifting for me :P

LGTM!

}

if let Ok(v) = v.downcast::<JsBuffer, _>(cx) {
return Ok(Cow::Borrowed(v.as_slice(cx)));
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is really cool!

The current lifetimes are overly restrictive. The reference is valid for as long as the handle is valid. Since the `Context` is always the
most narrow scope, it will be valid for at *least* as long as `Context`, even if the `Handle` type is dropped. This allows downcasting and
returning a borrow within the same function.
@kjvalencik kjvalencik force-pushed the kv/relax-borrow-lifetimes branch from 3cb3b81 to 112e773 Compare March 23, 2022 20:08
@kjvalencik kjvalencik merged commit 4d63dc7 into main Mar 23, 2022
@kjvalencik kjvalencik deleted the kv/relax-borrow-lifetimes branch March 23, 2022 20:33
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