aboutsummaryrefslogtreecommitdiff
path: root/libcore/ptr.rs
diff options
context:
space:
mode:
authorpravic <[email protected]>2016-04-29 21:16:15 +0300
committerpravic <[email protected]>2016-04-29 21:16:15 +0300
commit77e9a3167b4aaadf3583a0c1d1ee0d9e63c9a000 (patch)
tree710e445d56a1a582b8eff19b7b4b180276eae122 /libcore/ptr.rs
parenttweak: /driver option specifies /fixed:no implicitly as well (diff)
downloadkmd-env-rs-77e9a3167b4aaadf3583a0c1d1ee0d9e63c9a000.tar.xz
kmd-env-rs-77e9a3167b4aaadf3583a0c1d1ee0d9e63c9a000.zip
update libcore to 2016-04-29 nightly
Diffstat (limited to 'libcore/ptr.rs')
-rw-r--r--libcore/ptr.rs119
1 files changed, 92 insertions, 27 deletions
diff --git a/libcore/ptr.rs b/libcore/ptr.rs
index 42aef3a..8b3a14b 100644
--- a/libcore/ptr.rs
+++ b/libcore/ptr.rs
@@ -119,6 +119,17 @@ pub unsafe fn replace<T>(dest: *mut T, mut src: T) -> T {
/// `src` is not used before the data is overwritten again (e.g. with `write`,
/// `zero_memory`, or `copy_memory`). Note that `*src = foo` counts as a use
/// because it will attempt to drop the value previously at `*src`.
+///
+/// # Examples
+///
+/// Basic usage:
+///
+/// ```
+/// let x = 12;
+/// let y = &x as *const i32;
+///
+/// unsafe { println!("{}", std::ptr::read(y)); }
+/// ```
#[inline(always)]
#[stable(feature = "rust1", since = "1.0.0")]
pub unsafe fn read<T>(src: *const T) -> T {
@@ -155,6 +166,21 @@ pub unsafe fn read_and_drop<T>(dest: *mut T) -> T {
///
/// This is appropriate for initializing uninitialized memory, or overwriting
/// memory that has previously been `read` from.
+///
+/// # Examples
+///
+/// Basic usage:
+///
+/// ```
+/// let mut x = 0;
+/// let y = &mut x as *mut i32;
+/// let z = 12;
+///
+/// unsafe {
+/// std::ptr::write(y, z);
+/// println!("{}", std::ptr::read(y));
+/// }
+/// ```
#[inline]
#[stable(feature = "rust1", since = "1.0.0")]
pub unsafe fn write<T>(dst: *mut T, src: T) {
@@ -166,9 +192,16 @@ pub unsafe fn write<T>(dst: *mut T, src: T) {
///
/// Volatile operations are intended to act on I/O memory, and are guaranteed
/// to not be elided or reordered by the compiler across other volatile
-/// operations. See the LLVM documentation on [[volatile]].
+/// operations.
+///
+/// # Notes
///
-/// [volatile]: http://llvm.org/docs/LangRef.html#volatile-memory-accesses
+/// Rust does not currently have a rigorously and formally defined memory model,
+/// so the precise semantics of what "volatile" means here is subject to change
+/// over time. That being said, the semantics will almost always end up pretty
+/// similar to [C11's definition of volatile][c11].
+///
+/// [c11]: http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf
///
/// # Safety
///
@@ -178,8 +211,19 @@ pub unsafe fn write<T>(dst: *mut T, src: T) {
/// `src` is not used before the data is overwritten again (e.g. with `write`,
/// `zero_memory`, or `copy_memory`). Note that `*src = foo` counts as a use
/// because it will attempt to drop the value previously at `*src`.
+///
+/// # Examples
+///
+/// Basic usage:
+///
+/// ```
+/// let x = 12;
+/// let y = &x as *const i32;
+///
+/// unsafe { println!("{}", std::ptr::read_volatile(y)); }
+/// ```
#[inline]
-#[unstable(feature = "volatile", reason = "recently added", issue = "31756")]
+#[stable(feature = "volatile", since = "1.9.0")]
pub unsafe fn read_volatile<T>(src: *const T) -> T {
intrinsics::volatile_load(src)
}
@@ -189,9 +233,16 @@ pub unsafe fn read_volatile<T>(src: *const T) -> T {
///
/// Volatile operations are intended to act on I/O memory, and are guaranteed
/// to not be elided or reordered by the compiler across other volatile
-/// operations. See the LLVM documentation on [[volatile]].
+/// operations.
+///
+/// # Notes
///
-/// [volatile]: http://llvm.org/docs/LangRef.html#volatile-memory-accesses
+/// Rust does not currently have a rigorously and formally defined memory model,
+/// so the precise semantics of what "volatile" means here is subject to change
+/// over time. That being said, the semantics will almost always end up pretty
+/// similar to [C11's definition of volatile][c11].
+///
+/// [c11]: http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf
///
/// # Safety
///
@@ -203,8 +254,23 @@ pub unsafe fn read_volatile<T>(src: *const T) -> T {
///
/// This is appropriate for initializing uninitialized memory, or overwriting
/// memory that has previously been `read` from.
+///
+/// # Examples
+///
+/// Basic usage:
+///
+/// ```
+/// let mut x = 0;
+/// let y = &mut x as *mut i32;
+/// let z = 12;
+///
+/// unsafe {
+/// std::ptr::write_volatile(y, z);
+/// println!("{}", std::ptr::read_volatile(y));
+/// }
+/// ```
#[inline]
-#[unstable(feature = "volatile", reason = "recently added", issue = "31756")]
+#[stable(feature = "volatile", since = "1.9.0")]
pub unsafe fn write_volatile<T>(dst: *mut T, src: T) {
intrinsics::volatile_store(dst, src);
}
@@ -238,6 +304,9 @@ impl<T: ?Sized> *const T {
/// operation because the returned value could be pointing to invalid
/// memory.
///
+ /// Additionally, the lifetime `'a` returned is arbitrarily chosen and does
+ /// not necessarily reflect the actual lifetime of the data.
+ ///
/// # Examples
///
/// Basic usage:
@@ -251,17 +320,13 @@ impl<T: ?Sized> *const T {
/// }
/// }
/// ```
- #[unstable(feature = "ptr_as_ref",
- reason = "Option is not clearly the right return type, and we \
- may want to tie the return lifetime to a borrow of \
- the raw pointer",
- issue = "27780")]
+ #[stable(feature = "ptr_as_ref", since = "1.9.0")]
#[inline]
- pub unsafe fn as_ref<'a>(&self) -> Option<&'a T> where T: Sized {
+ pub unsafe fn as_ref<'a>(self) -> Option<&'a T> where T: Sized {
if self.is_null() {
None
} else {
- Some(&**self)
+ Some(&*self)
}
}
@@ -324,6 +389,9 @@ impl<T: ?Sized> *mut T {
/// operation because the returned value could be pointing to invalid
/// memory.
///
+ /// Additionally, the lifetime `'a` returned is arbitrarily chosen and does
+ /// not necessarily reflect the actual lifetime of the data.
+ ///
/// # Examples
///
/// Basic usage:
@@ -337,17 +405,13 @@ impl<T: ?Sized> *mut T {
/// }
/// }
/// ```
- #[unstable(feature = "ptr_as_ref",
- reason = "Option is not clearly the right return type, and we \
- may want to tie the return lifetime to a borrow of \
- the raw pointer",
- issue = "27780")]
+ #[stable(feature = "ptr_as_ref", since = "1.9.0")]
#[inline]
- pub unsafe fn as_ref<'a>(&self) -> Option<&'a T> where T: Sized {
+ pub unsafe fn as_ref<'a>(self) -> Option<&'a T> where T: Sized {
if self.is_null() {
None
} else {
- Some(&**self)
+ Some(&*self)
}
}
@@ -385,7 +449,8 @@ impl<T: ?Sized> *mut T {
/// # Safety
///
/// As with `as_ref`, this is unsafe because it cannot verify the validity
- /// of the returned pointer.
+ /// of the returned pointer, nor can it ensure that the lifetime `'a`
+ /// returned is indeed a valid lifetime for the contained data.
///
/// # Examples
///
@@ -394,17 +459,17 @@ impl<T: ?Sized> *mut T {
/// ```
/// let mut s = [1, 2, 3];
/// let ptr: *mut u32 = s.as_mut_ptr();
+ /// let first_value = unsafe { ptr.as_mut().unwrap() };
+ /// *first_value = 4;
+ /// println!("{:?}", s); // It'll print: "[4, 2, 3]".
/// ```
- #[unstable(feature = "ptr_as_ref",
- reason = "return value does not necessarily convey all possible \
- information",
- issue = "27780")]
+ #[stable(feature = "ptr_as_ref", since = "1.9.0")]
#[inline]
- pub unsafe fn as_mut<'a>(&self) -> Option<&'a mut T> where T: Sized {
+ pub unsafe fn as_mut<'a>(self) -> Option<&'a mut T> where T: Sized {
if self.is_null() {
None
} else {
- Some(&mut **self)
+ Some(&mut *self)
}
}
}