-
Notifications
You must be signed in to change notification settings - Fork 13.4k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -256,27 +256,46 @@ pub unsafe fn position<T>(buf: *const T, f: |&T| -> bool) -> uint { | |
pub trait RawPtr<T> { | ||
/// Returns the null pointer. | ||
fn null() -> Self; | ||
|
||
/// Returns true if the pointer is equal to the null pointer. | ||
fn is_null(&self) -> bool; | ||
|
||
/// Returns true if the pointer is not equal to the null pointer. | ||
fn is_not_null(&self) -> bool { !self.is_null() } | ||
|
||
/// Returns the value of this pointer (ie, the address it points to) | ||
fn to_uint(&self) -> uint; | ||
/// Returns `None` if the pointer is null, or else returns the value wrapped | ||
/// in `Some`. | ||
|
||
/// Returns `None` if the pointer is null, or else returns a reference to the | ||
/// value wrapped in `Some`. | ||
/// | ||
/// # Safety Notes | ||
/// | ||
/// While this method is 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 to_option(&self) -> Option<&T>; | ||
/// 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>; | ||
|
||
/// A synonym for `as_ref`, except with incorrect lifetime semantics | ||
#[deprecated="Use `as_ref` instead"] | ||
unsafe fn to_option<'a>(&'a self) -> Option<&'a T> { | ||
mem::transmute(self.as_ref()) | ||
} | ||
|
||
/// Calculates the offset from a pointer. The offset *must* be in-bounds of | ||
/// the object, or one-byte-past-the-end. `count` is in units of T; e.g. a | ||
/// `count` of 3 represents a pointer offset of `3 * sizeof::<T>()` bytes. | ||
unsafe fn offset(self, count: int) -> Self; | ||
} | ||
|
||
/// Methods on mutable raw pointers | ||
pub trait RawMutPtr<T>{ | ||
/// Returns `None` if the pointer is null, or else returns a mutable reference | ||
/// to the value wrapped in `Some`. As with `as_ref`, this is unsafe because | ||
/// it cannot verify the validity of the returned pointer. | ||
unsafe fn as_mut<'a>(&self) -> Option<&'a mut T>; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same comment about |
||
} | ||
|
||
impl<T> RawPtr<T> for *const T { | ||
#[inline] | ||
fn null() -> *const T { null() } | ||
|
@@ -293,7 +312,7 @@ impl<T> RawPtr<T> for *const T { | |
} | ||
|
||
#[inline] | ||
unsafe fn to_option(&self) -> Option<&T> { | ||
unsafe fn as_ref<'a>(&self) -> Option<&'a T> { | ||
if self.is_null() { | ||
None | ||
} else { | ||
|
@@ -318,7 +337,7 @@ impl<T> RawPtr<T> for *mut T { | |
} | ||
|
||
#[inline] | ||
unsafe fn to_option(&self) -> Option<&T> { | ||
unsafe fn as_ref<'a>(&self) -> Option<&'a T> { | ||
if self.is_null() { | ||
None | ||
} else { | ||
|
@@ -327,6 +346,17 @@ impl<T> RawPtr<T> for *mut T { | |
} | ||
} | ||
|
||
impl<T> RawMutPtr<T> for *mut T { | ||
#[inline] | ||
unsafe fn as_mut<'a>(&self) -> Option<&'a mut T> { | ||
if self.is_null() { | ||
None | ||
} else { | ||
Some(&mut **self) | ||
} | ||
} | ||
} | ||
|
||
// Equality for pointers | ||
impl<T> PartialEq for *const T { | ||
#[inline] | ||
|
There was a problem hiding this comment.
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: