-
Notifications
You must be signed in to change notification settings - Fork 13.4k
std: clean up ptr a bit #12282
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
Closed
Closed
std: clean up ptr a bit #12282
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -102,10 +102,10 @@ pub unsafe fn zero_memory<T>(dst: *mut T, count: uint) { | |
|
||
/** | ||
* Swap the values at two mutable locations of the same type, without | ||
* deinitialising or copying either one. | ||
* deinitialising either. They may overlap. | ||
*/ | ||
#[inline] | ||
pub unsafe fn swap_ptr<T>(x: *mut T, y: *mut T) { | ||
pub unsafe fn swap<T>(x: *mut T, y: *mut T) { | ||
// Give ourselves some scratch space to work with | ||
let mut tmp: T = mem::uninit(); | ||
let t: *mut T = &mut tmp; | ||
|
@@ -122,19 +122,19 @@ pub unsafe fn swap_ptr<T>(x: *mut T, y: *mut T) { | |
|
||
/** | ||
* Replace the value at a mutable location with a new one, returning the old | ||
* value, without deinitialising or copying either one. | ||
* value, without deinitialising either. | ||
*/ | ||
#[inline] | ||
pub unsafe fn replace_ptr<T>(dest: *mut T, mut src: T) -> T { | ||
pub unsafe fn replace<T>(dest: *mut T, mut src: T) -> T { | ||
mem::swap(cast::transmute(dest), &mut src); // cannot overlap | ||
src | ||
} | ||
|
||
/** | ||
* Reads the value from `*src` and returns it. Does not copy `*src`. | ||
* Reads the value from `*src` and returns it. | ||
*/ | ||
#[inline(always)] | ||
pub unsafe fn read_ptr<T>(src: *T) -> T { | ||
pub unsafe fn read<T>(src: *T) -> T { | ||
let mut tmp: T = mem::uninit(); | ||
copy_nonoverlapping_memory(&mut tmp, src, 1); | ||
tmp | ||
|
@@ -145,28 +145,16 @@ pub unsafe fn read_ptr<T>(src: *T) -> T { | |
* This currently prevents destructors from executing. | ||
*/ | ||
#[inline(always)] | ||
pub unsafe fn read_and_zero_ptr<T>(dest: *mut T) -> T { | ||
pub unsafe fn read_and_zero<T>(dest: *mut T) -> T { | ||
// Copy the data out from `dest`: | ||
let tmp = read_ptr(&*dest); | ||
let tmp = read(&*dest); | ||
|
||
// Now zero out `dest`: | ||
zero_memory(dest, 1); | ||
|
||
tmp | ||
} | ||
|
||
/// Transform a region pointer - &T - to an unsafe pointer - *T. | ||
#[inline] | ||
pub fn to_unsafe_ptr<T>(thing: &T) -> *T { | ||
thing as *T | ||
} | ||
|
||
/// Transform a mutable region pointer - &mut T - to a mutable unsafe pointer - *mut T. | ||
#[inline] | ||
pub fn to_mut_unsafe_ptr<T>(thing: &mut T) -> *mut T { | ||
thing as *mut T | ||
} | ||
|
||
/** | ||
Given a **T (pointer to an array of pointers), | ||
iterate through each *T, up to the provided `len`, | ||
|
@@ -176,7 +164,7 @@ pub fn to_mut_unsafe_ptr<T>(thing: &mut T) -> *mut T { | |
*/ | ||
pub unsafe fn array_each_with_len<T>(arr: **T, len: uint, cb: |*T|) { | ||
debug!("array_each_with_len: before iterate"); | ||
if arr as uint == 0 { | ||
if arr.is_null() { | ||
fail!("ptr::array_each_with_len failure: arr input is null pointer"); | ||
} | ||
//let start_ptr = *arr; | ||
|
@@ -197,108 +185,82 @@ pub unsafe fn array_each_with_len<T>(arr: **T, len: uint, cb: |*T|) { | |
Dragons be here. | ||
*/ | ||
pub unsafe fn array_each<T>(arr: **T, cb: |*T|) { | ||
if arr as uint == 0 { | ||
if arr.is_null() { | ||
fail!("ptr::array_each_with_len failure: arr input is null pointer"); | ||
} | ||
let len = buf_len(arr); | ||
debug!("array_each inferred len: {}", len); | ||
array_each_with_len(arr, len, cb); | ||
} | ||
|
||
#[allow(missing_doc)] | ||
/// Extension methods for raw pointers. | ||
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; | ||
fn is_not_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`. | ||
/// | ||
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. thanks! |
||
/// # 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>; | ||
/// Calculates the offset from a pointer. The offset *must* be in-bounds of | ||
/// the object, or one-byte-past-the-end. | ||
unsafe fn offset(self, count: int) -> Self; | ||
} | ||
|
||
/// Extension methods for immutable pointers | ||
impl<T> RawPtr<T> for *T { | ||
/// Returns the null pointer. | ||
#[inline] | ||
fn null() -> *T { null() } | ||
|
||
/// Returns true if the pointer is equal to the null pointer. | ||
#[inline] | ||
fn is_null(&self) -> bool { *self == RawPtr::null() } | ||
|
||
/// Returns true if the pointer is not equal to the null pointer. | ||
#[inline] | ||
fn is_not_null(&self) -> bool { *self != RawPtr::null() } | ||
fn to_uint(&self) -> uint { *self as uint } | ||
|
||
/// Returns the address of this pointer. | ||
#[inline] | ||
fn to_uint(&self) -> uint { *self as uint } | ||
unsafe fn offset(self, count: int) -> *T { intrinsics::offset(self, count) } | ||
|
||
/// | ||
/// Returns `None` if the pointer is null, or else returns 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. | ||
/// | ||
#[inline] | ||
unsafe fn to_option(&self) -> Option<&T> { | ||
if self.is_null() { None } else { | ||
if self.is_null() { | ||
None | ||
} else { | ||
Some(cast::transmute(*self)) | ||
} | ||
} | ||
|
||
/// Calculates the offset from a pointer. The offset *must* be in-bounds of | ||
/// the object, or one-byte-past-the-end. | ||
#[inline] | ||
unsafe fn offset(self, count: int) -> *T { intrinsics::offset(self, count) } | ||
} | ||
|
||
/// Extension methods for mutable pointers | ||
impl<T> RawPtr<T> for *mut T { | ||
/// Returns the null pointer. | ||
#[inline] | ||
fn null() -> *mut T { mut_null() } | ||
|
||
/// Returns true if the pointer is equal to the null pointer. | ||
#[inline] | ||
fn is_null(&self) -> bool { *self == RawPtr::null() } | ||
|
||
/// Returns true if the pointer is not equal to the null pointer. | ||
#[inline] | ||
fn is_not_null(&self) -> bool { *self != RawPtr::null() } | ||
fn to_uint(&self) -> uint { *self as uint } | ||
|
||
/// Returns the address of this pointer. | ||
#[inline] | ||
fn to_uint(&self) -> uint { *self as uint } | ||
unsafe fn offset(self, count: int) -> *mut T { intrinsics::offset(self as *T, count) as *mut T } | ||
|
||
/// | ||
/// Returns `None` if the pointer is null, or else returns 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. | ||
/// | ||
#[inline] | ||
unsafe fn to_option(&self) -> Option<&T> { | ||
if self.is_null() { None } else { | ||
if self.is_null() { | ||
None | ||
} else { | ||
Some(cast::transmute(*self)) | ||
} | ||
} | ||
|
||
/// Calculates the offset from a pointer. The offset *must* be in-bounds of | ||
/// the object, or one-byte-past-the-end. An arithmetic overflow is also | ||
/// undefined behaviour. | ||
/// | ||
/// This method should be preferred over `offset` when the guarantee can be | ||
/// satisfied, to enable better optimization. | ||
#[inline] | ||
unsafe fn offset(self, count: int) -> *mut T { intrinsics::offset(self as *T, count) as *mut T } | ||
} | ||
|
||
// Equality for pointers | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Is there a reason these can't be methods on
RawPtr
?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.
+1 for RawPtr::read()
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.
(Reproduced here)
Changing ptr::read to RawPtr::read is extremely inconvenient because, with ptr::read,
&T
will be coerced, whereas it will not be when it is a method. This introduces quite unnecessary casts. I do not wish to change it.