Skip to content
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

Copy safety #832

Merged
merged 1 commit into from
Jan 14, 2022
Merged

Copy safety #832

merged 1 commit into from
Jan 14, 2022

Conversation

kjvalencik
Copy link
Member

@kjvalencik kjvalencik commented Dec 2, 2021

Currently, it is possible to Copy and Clone Js* types which is unsafe. These types depend on being contained in a Handle to verify the lifetime. They should only exist as references outside of a Handle. Unfortunately, the ability to clone allows getting an owned copy from a reference.

This change attempts to minimize impact to users by making Handle<T> Copy, while making the inner T !Copy. This works by introducing a new marker trait TransparentWrapper which indicates a type is a transparent wrapper around a TransparentWrapper::Inner type--allowing transmutation between the two.

Using this trait, Handle actually stores a Copy inner type, while implementing Deref with a Target to the !Copy outer type.

Unfortunately, since Copy won't propagate upward from a Deref, all APIs that currently take self on a Value were changed to take &self to keep from requiring a manual clone(). This could still impact users in some places--for example when creating an Object with a circular reference.

@kjvalencik kjvalencik marked this pull request as ready for review December 2, 2021 19:24
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.

This looks like a great solution. I just found the explanation confusing which made it hard to review at first. Once we improve the presentation of the trait a little bit I think it’ll be manageable from a maintenance perspective.

src/context/mod.rs Show resolved Hide resolved
src/handle/internal.rs Outdated Show resolved Hide resolved
src/handle/internal.rs Show resolved Hide resolved
src/handle/mod.rs Outdated Show resolved Hide resolved
@kjvalencik kjvalencik force-pushed the kv/copy-safety branch 2 times, most recently from 927bbc8 to 84a8497 Compare January 14, 2022 15:53
@kjvalencik kjvalencik merged commit f7ae61c into next/0.10 Jan 14, 2022
@kjvalencik kjvalencik deleted the kv/copy-safety branch January 14, 2022 18:06
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.

2 participants