Skip to content

Rename RawPtr::to_option() to RawPtr::as_ref() #16634

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

Merged
merged 1 commit into from
Sep 3, 2014

Conversation

apoelstra
Copy link
Contributor

As outlined in

https://aturon.github.io/style/naming/conversions.html

to_ functions names should only be used for expensive operations.
Thus to_option is better named as_option. Also, putting type
names into method names is considered bad style; what the user is
really trying to get is a reference. This as_ref is even better.

Also, we are missing a mutable version of this method.

Finally, there is a bug in the signature of to_option which has
been around since lifetime elision: originally the returned reference
had 'static lifetime, but since the elision changes this become
the lifetime of the raw pointer (which does not make sense, since
the pointer lifetime and referent lifetime are unrelated). We fix
the bug to return a reference with a fresh lifetime which will be
inferred from the calling context.

[breaking-change]

@apoelstra apoelstra changed the title Rename RawPtr::to_option() to RawPtr::as_ref(), add corresponding `a... Rename RawPtr::to_option() to RawPtr::as_ref() Aug 20, 2014
/// that this is still an unsafe operation because the returned value could
/// be pointing to invalid memory.
unsafe fn to_option(&self) -> Option<&T>;
/// While this method and its mutable counterpart ary useful for null-safety,
Copy link
Contributor

Choose a reason for hiding this comment

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

"are useful"

@apoelstra apoelstra force-pushed the to-option-fix branch 2 times, most recently from c781bae to 2aeb3df Compare August 20, 2014 20:24
@@ -293,13 +303,22 @@ impl<T> RawPtr<T> for *const T {
}

#[inline]
unsafe fn to_option(&self) -> Option<&T> {
Copy link
Contributor

Choose a reason for hiding this comment

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

Why aren't you preserving and deprecating this with a migration path message?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Because I wasn't thinking :) will fix.

Edit: This is fixed above. I'm not sure why the line note hasn't disappeared.

@Gankra
Copy link
Contributor

Gankra commented Aug 20, 2014

@apoelstra I'm not an expert at this stuff, but I believe for various "magic" built-in types, these traits are necessary to even define methods on them. Thus you may have to introduce a RawMutPtr trait or something in order to get the extra method on *mut T.

@Gankra
Copy link
Contributor

Gankra commented Aug 25, 2014

@apoelstra Ugh sorry I let this slip through the cracks, rebasing makes PRs confusing as hell, and I thought you hadn't updated this. Other than the typo, this all basically seems sane, and is something I've been craving for a while, so a big +1 from me. You're gonna need to convince one of the big kids to check this out, though.

@brson
Copy link
Contributor

brson commented Aug 26, 2014

@aturon Does this work for you?

@apoelstra
Copy link
Contributor Author

(just fix the validite->validity typo @gankro mentioned)

/// While this method and its mutable counterpart are useful for null-safety,
/// it is important to note that this is still an unsafe operation because
/// the returned value could be pointing to invalid memory.
unsafe fn as_ref<'a>(&self) -> Option<&'a T>;
Copy link
Member

Choose a reason for hiding this comment

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

It's a bit more clear to write this as:

unsafe fn as_ref(&self) -> Option<&'static T>;

@aturon
Copy link
Member

aturon commented Aug 26, 2014

Looks good to me, modulo some minor nits about the signature; I'll merge once it's updated.

Thanks @apoelstra!

@apoelstra
Copy link
Contributor Author

@aturon Using 'static will forces you to transmute in the DList case, presumably because in that context 'a is an impl parameter and the compiler therefore thinks it knows nothing about it....apparantly not even that it is a subset of 'static!

Or maybe this is a bug in lifetime ordering?

Edit: Oh, the actual error is mismatched types: expected core::option::Option<&'a T>but foundcore::option::Option<&'static T> (lifetime mismatch). I think this is a known bug, but nonetheless it changes the semantics of using 'static rather than a fresh lifetime as I did.

As outlined in

  https://aturon.github.io/style/naming/conversions.html

`to_` functions names should only be used for expensive operations.
Thus `to_option` is better named `as_option`. Also, putting type
names into method names is considered bad style; what the user is
really trying to get is a reference. This `as_ref` is even better.

Also, we are missing a mutable version of this method. So add a
new trait `RawMutPtr` with a corresponding `as_mut` methode.

Finally, there is a bug in the signature of `to_option` which has
been around since lifetime elision: originally the returned reference
had 'static lifetime, but since the elision changes this become
the lifetime of the raw pointer (which does not make sense, since
the pointer lifetime and referent lifetime are unrelated). Fix
the bug to return a reference with a fresh lifetime (which will
be inferred from the calling context).

[breaking-change]
@apoelstra
Copy link
Contributor Author

Rebase so that the new liblog uses as_ref.

@aturon Do you stil want me to add 'static? With the current borrowck this forces users to use transmute in some unnecessary places.

@aturon
Copy link
Member

aturon commented Sep 2, 2014

@apoelstra Sorry, I missed your earlier message. It's fine with me to not use 'static for now.

bors added a commit that referenced this pull request Sep 3, 2014
As outlined in

  https://aturon.github.io/style/naming/conversions.html

`to_` functions names should only be used for expensive operations.
Thus `to_option` is better named `as_option`. Also, putting type
names into method names is considered bad style; what the user is
really trying to get is a reference. This `as_ref` is even better.

Also, we are missing a mutable version of this method.

Finally, there is a bug in the signature of `to_option` which has
been around since lifetime elision: originally the returned reference
had 'static lifetime, but since the elision changes this become
the lifetime of the raw pointer (which does not make sense, since
the pointer lifetime and referent lifetime are unrelated). We fix
the bug to return a reference with a fresh lifetime which will be
inferred from the calling context.

[breaking-change]
@bors bors closed this Sep 3, 2014
@bors bors merged commit 00ff5aa into rust-lang:master Sep 3, 2014
@apoelstra apoelstra deleted the to-option-fix branch September 3, 2014 23:22
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.

5 participants