-
Notifications
You must be signed in to change notification settings - Fork 13.3k
std: Align raw
modules with unsafe conventions
#19152
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
Changes from all commits
Commits
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1547,15 +1547,55 @@ pub fn mut_ref_slice<'a, A>(s: &'a mut A) -> &'a mut [A] { | |
} | ||
} | ||
|
||
/// Forms a slice from a pointer and a length. | ||
/// | ||
/// The pointer given is actually a reference to the base of the slice. This | ||
/// reference is used to give a concrete lifetime to tie the returned slice to. | ||
/// Typically this should indicate that the slice is valid for as long as the | ||
/// pointer itself is valid. | ||
/// | ||
/// The `len` argument is the number of **elements**, not the number of bytes. | ||
/// | ||
/// This function is unsafe as there is no guarantee that the given pointer is | ||
/// valid for `len` elements, nor whether the lifetime provided is a suitable | ||
/// lifetime for the returned slice. | ||
/// | ||
/// # Example | ||
/// | ||
/// ```rust | ||
/// use std::slice; | ||
/// | ||
/// // manifest a slice out of thin air! | ||
/// let ptr = 0x1234 as *const uint; | ||
/// let amt = 10; | ||
/// unsafe { | ||
/// let slice = slice::from_raw_buf(&ptr, amt); | ||
/// } | ||
/// ``` | ||
#[inline] | ||
#[unstable = "just renamed from `mod raw`"] | ||
pub unsafe fn from_raw_buf<'a, T>(p: &'a *const T, len: uint) -> &'a [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. I'm reasonably happy with this design; it's pragmatic. |
||
transmute(RawSlice { data: *p, len: len }) | ||
} | ||
|
||
|
||
/// Performs the same functionality as `from_raw_buf`, except that a mutable | ||
/// slice is returned. | ||
/// | ||
/// This function is unsafe for the same reasons as `from_raw_buf`, as well as | ||
/// not being able to provide a non-aliasing guarantee of the returned mutable | ||
/// slice. | ||
#[inline] | ||
#[unstable = "just renamed from `mod raw`"] | ||
pub unsafe fn from_raw_mut_buf<'a, T>(p: &'a *mut T, len: uint) -> &'a mut [T] { | ||
transmute(RawSlice { data: *p as *const T, len: len }) | ||
} | ||
|
||
// | ||
// Submodules | ||
// | ||
|
||
/// Unsafe operations | ||
#[experimental = "needs review"] | ||
#[deprecated] | ||
pub mod raw { | ||
use mem::transmute; | ||
use ptr::RawPtr; | ||
|
@@ -1567,6 +1607,7 @@ pub mod raw { | |
* not bytes). | ||
*/ | ||
#[inline] | ||
#[deprecated = "renamed to slice::from_raw_buf"] | ||
pub unsafe fn buf_as_slice<T,U>(p: *const T, len: uint, f: |v: &[T]| -> U) | ||
-> U { | ||
f(transmute(Slice { | ||
|
@@ -1580,6 +1621,7 @@ pub mod raw { | |
* not bytes). | ||
*/ | ||
#[inline] | ||
#[deprecated = "renamed to slice::from_raw_mut_buf"] | ||
pub unsafe fn mut_buf_as_slice<T, | ||
U>( | ||
p: *mut T, | ||
|
@@ -1598,6 +1640,7 @@ pub mod raw { | |
* if the slice is empty. O(1). | ||
*/ | ||
#[inline] | ||
#[deprecated = "inspect `Slice::{data, len}` manually (increment data by 1)"] | ||
pub unsafe fn shift_ptr<T>(slice: &mut Slice<T>) -> Option<*const T> { | ||
if slice.len == 0 { return None; } | ||
let head: *const T = slice.data; | ||
|
@@ -1611,7 +1654,8 @@ pub mod raw { | |
* slice so it no longer contains that element. Returns None | ||
* if the slice is empty. O(1). | ||
*/ | ||
#[inline] | ||
#[inline] | ||
#[deprecated = "inspect `Slice::{data, len}` manually (decrement len by 1)"] | ||
pub unsafe fn pop_ptr<T>(slice: &mut Slice<T>) -> Option<*const T> { | ||
if slice.len == 0 { return None; } | ||
let tail: *const T = slice.data.offset((slice.len - 1) as int); | ||
|
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.
Hm, I would've thought we'd deprecate the free
replace
fn in favor of this method.