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

Allow creation of UniquePtrs to trivial aliased types. #361

Merged
merged 3 commits into from
Oct 16, 2020

Conversation

adetaylor
Copy link
Collaborator

Fixes #360.

@dtolnay the runtime check is out of character for cxx, but we can't do it at:

  • generation time, because we don't know if an ExternType has claimed a given type to be trivial;
  • build time, because what matters is whether the UniquePtr::new method is called rather than whether it exists.

I think it would be possible to make the check at build-time by splitting UniquePtrTarget into UniquePtrTarget and CreatableUniquePtrTarget (such that the new method simply doesn't exist for some subset of UniquePtrs) but that's significantly beyond my bravery level.

Copy link
Owner

@dtolnay dtolnay left a comment

Choose a reason for hiding this comment

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

it would be possible to make the check at build-time by splitting UniquePtrTarget into UniquePtrTarget and CreatableUniquePtrTarget (such that the new method simply doesn't exist for some subset of UniquePtrs)

For making the new method not exist for some subset of unique ptrs, a trait bound would be a better way I think.

pub fn new(value: T) -> Self {

pub fn new(value: T) -> Self
where
    T: ExternType<Kind = Trivial>,
{...}

@adetaylor
Copy link
Collaborator Author

Ah yes, I'll give that a go.

@adetaylor adetaylor force-pushed the trivial-alias-uniqueptr branch from bac6998 to 9f7ff2e Compare October 13, 2020 03:14
Copy link
Owner

@dtolnay dtolnay left a comment

Choose a reason for hiding this comment

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

Nice!

@dtolnay dtolnay merged commit 35b3cd3 into dtolnay:master Oct 16, 2020
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.

Allow UniquePtr::new on Trivial ExternTypes
2 participants