about summary refs log tree commit diff
path: root/src/libcore
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2017-09-18 17:30:29 +0000
committerbors <bors@rust-lang.org>2017-09-18 17:30:29 +0000
commit0701b37d97d08da7074ece7a7dcb4449498f4bfa (patch)
tree796f1ce756d1bce72a8462d6d74c28f11263895c /src/libcore
parent3a7b960731fd1cf86f1879b1a0d44196a0917c53 (diff)
parent929215db7c598722410709022e69f71702f02e0b (diff)
downloadrust-0701b37d97d08da7074ece7a7dcb4449498f4bfa.tar.gz
rust-0701b37d97d08da7074ece7a7dcb4449498f4bfa.zip
Auto merge of #44678 - alexcrichton:rollup, r=alexcrichton
Rollup of 11 pull requests

- Successful merges: #44364, #44466, #44537, #44548, #44640, #44651, #44657, #44661, #44668, #44671, #44675
- Failed merges:
Diffstat (limited to 'src/libcore')
-rw-r--r--src/libcore/intrinsics.rs33
-rw-r--r--src/libcore/iter/traits.rs17
-rw-r--r--src/libcore/ptr.rs75
-rw-r--r--src/libcore/str/mod.rs11
4 files changed, 97 insertions, 39 deletions
diff --git a/src/libcore/intrinsics.rs b/src/libcore/intrinsics.rs
index f7f1dd12d28..bc82f0230e5 100644
--- a/src/libcore/intrinsics.rs
+++ b/src/libcore/intrinsics.rs
@@ -1381,38 +1381,7 @@ extern "rust-intrinsic" {
 }
 
 #[cfg(stage0)]
-/// Computes the byte offset that needs to be applied to `ptr` in order to
-/// make it aligned to `align`.
-/// If it is not possible to align `ptr`, the implementation returns
-/// `usize::max_value()`.
-///
-/// There are no guarantees whatsover that offsetting the pointer will not
-/// overflow or go beyond the allocation that `ptr` points into.
-/// It is up to the caller to ensure that the returned offset is correct
-/// in all terms other than alignment.
-///
-/// # Examples
-///
-/// Accessing adjacent `u8` as `u16`
-///
-/// ```
-/// # #![feature(core_intrinsics)]
-/// # fn foo(n: usize) {
-/// # use std::intrinsics::align_offset;
-/// # use std::mem::align_of;
-/// # unsafe {
-/// let x = [5u8, 6u8, 7u8, 8u8, 9u8];
-/// let ptr = &x[n] as *const u8;
-/// let offset = align_offset(ptr as *const (), align_of::<u16>());
-/// if offset < x.len() - n - 1 {
-///     let u16_ptr = ptr.offset(offset as isize) as *const u16;
-///     assert_ne!(*u16_ptr, 500);
-/// } else {
-///     // while the pointer can be aligned via `offset`, it would point
-///     // outside the allocation
-/// }
-/// # } }
-/// ```
+/// remove me after the next release
 pub unsafe fn align_offset(ptr: *const (), align: usize) -> usize {
     let offset = ptr as usize % align;
     if offset == 0 {
diff --git a/src/libcore/iter/traits.rs b/src/libcore/iter/traits.rs
index 2af129a67bd..7ef50396c82 100644
--- a/src/libcore/iter/traits.rs
+++ b/src/libcore/iter/traits.rs
@@ -196,6 +196,23 @@ pub trait FromIterator<A>: Sized {
 ///     assert_eq!(i as i32, n);
 /// }
 /// ```
+///
+/// It is common to use `IntoIterator` as a trait bound. This allows
+/// the input collection type to change, so long as it is still an
+/// iterator. Additional bounds can be specified by restricting on
+/// `Item`:
+///
+/// ```rust
+/// fn collect_as_strings<T>(collection: T) -> Vec<String>
+///     where T: IntoIterator,
+///           T::Item : std::fmt::Debug,
+/// {
+///     collection
+///         .into_iter()
+///         .map(|item| format!("{:?}", item))
+///         .collect()
+/// }
+/// ```
 #[stable(feature = "rust1", since = "1.0.0")]
 pub trait IntoIterator {
     /// The type of the elements being iterated over.
diff --git a/src/libcore/ptr.rs b/src/libcore/ptr.rs
index dd01534ec02..4041a3760e5 100644
--- a/src/libcore/ptr.rs
+++ b/src/libcore/ptr.rs
@@ -1064,7 +1064,43 @@ impl<T: ?Sized> *const T {
         copy_nonoverlapping(self, dest, count)
     }
 
-
+    /// Computes the byte offset that needs to be applied in order to
+    /// make the pointer aligned to `align`.
+    /// If it is not possible to align the pointer, the implementation returns
+    /// `usize::max_value()`.
+    ///
+    /// There are no guarantees whatsover that offsetting the pointer will not
+    /// overflow or go beyond the allocation that the pointer points into.
+    /// It is up to the caller to ensure that the returned offset is correct
+    /// in all terms other than alignment.
+    ///
+    /// # Examples
+    ///
+    /// Accessing adjacent `u8` as `u16`
+    ///
+    /// ```
+    /// # #![feature(align_offset)]
+    /// # fn foo(n: usize) {
+    /// # use std::mem::align_of;
+    /// # unsafe {
+    /// let x = [5u8, 6u8, 7u8, 8u8, 9u8];
+    /// let ptr = &x[n] as *const u8;
+    /// let offset = ptr.align_offset(align_of::<u16>());
+    /// if offset < x.len() - n - 1 {
+    ///     let u16_ptr = ptr.offset(offset as isize) as *const u16;
+    ///     assert_ne!(*u16_ptr, 500);
+    /// } else {
+    ///     // while the pointer can be aligned via `offset`, it would point
+    ///     // outside the allocation
+    /// }
+    /// # } }
+    /// ```
+    #[unstable(feature = "align_offset", issue = "44488")]
+    pub fn align_offset(self, align: usize) -> usize {
+        unsafe {
+            intrinsics::align_offset(self as *const _, align)
+        }
+    }
 }
 
 #[lang = "mut_ptr"]
@@ -1284,6 +1320,43 @@ impl<T: ?Sized> *mut T {
         }
     }
 
+    /// Computes the byte offset that needs to be applied in order to
+    /// make the pointer aligned to `align`.
+    /// If it is not possible to align the pointer, the implementation returns
+    /// `usize::max_value()`.
+    ///
+    /// There are no guarantees whatsover that offsetting the pointer will not
+    /// overflow or go beyond the allocation that the pointer points into.
+    /// It is up to the caller to ensure that the returned offset is correct
+    /// in all terms other than alignment.
+    ///
+    /// # Examples
+    ///
+    /// Accessing adjacent `u8` as `u16`
+    ///
+    /// ```
+    /// # #![feature(align_offset)]
+    /// # fn foo(n: usize) {
+    /// # use std::mem::align_of;
+    /// # unsafe {
+    /// let x = [5u8, 6u8, 7u8, 8u8, 9u8];
+    /// let ptr = &x[n] as *const u8;
+    /// let offset = ptr.align_offset(align_of::<u16>());
+    /// if offset < x.len() - n - 1 {
+    ///     let u16_ptr = ptr.offset(offset as isize) as *const u16;
+    ///     assert_ne!(*u16_ptr, 500);
+    /// } else {
+    ///     // while the pointer can be aligned via `offset`, it would point
+    ///     // outside the allocation
+    /// }
+    /// # } }
+    /// ```
+    #[unstable(feature = "align_offset", issue = "44488")]
+    pub fn align_offset(self, align: usize) -> usize {
+        unsafe {
+            intrinsics::align_offset(self as *const _, align)
+        }
+    }
 
     /// Calculates the offset from a pointer (convenience for `.offset(count as isize)`).
     ///
diff --git a/src/libcore/str/mod.rs b/src/libcore/str/mod.rs
index d4fef45ae4e..c24cdb30bad 100644
--- a/src/libcore/str/mod.rs
+++ b/src/libcore/str/mod.rs
@@ -23,7 +23,6 @@ use fmt;
 use iter::{Map, Cloned, FusedIterator};
 use slice::{self, SliceIndex};
 use mem;
-use intrinsics::align_offset;
 
 pub mod pattern;
 
@@ -404,7 +403,7 @@ unsafe fn from_raw_parts_mut<'a>(p: *mut u8, len: usize) -> &'a mut str {
 #[inline]
 #[stable(feature = "rust1", since = "1.0.0")]
 pub unsafe fn from_utf8_unchecked(v: &[u8]) -> &str {
-    mem::transmute(v)
+    &*(v as *const [u8] as *const str)
 }
 
 /// Converts a slice of bytes to a string slice without checking
@@ -429,7 +428,7 @@ pub unsafe fn from_utf8_unchecked(v: &[u8]) -> &str {
 #[inline]
 #[stable(feature = "str_mut_extras", since = "1.20.0")]
 pub unsafe fn from_utf8_unchecked_mut(v: &mut [u8]) -> &mut str {
-    mem::transmute(v)
+    &mut *(v as *mut [u8] as *mut str)
 }
 
 #[stable(feature = "rust1", since = "1.0.0")]
@@ -1515,7 +1514,7 @@ fn run_utf8_validation(v: &[u8]) -> Result<(), Utf8Error> {
             let ptr = v.as_ptr();
             let align = unsafe {
                 // the offset is safe, because `index` is guaranteed inbounds
-                align_offset(ptr.offset(index as isize) as *const (), usize_bytes)
+                ptr.offset(index as isize).align_offset(usize_bytes)
             };
             if align == 0 {
                 while index < blocks_end {
@@ -2447,12 +2446,12 @@ impl StrExt for str {
 
     #[inline]
     fn as_bytes(&self) -> &[u8] {
-        unsafe { mem::transmute(self) }
+        unsafe { &*(self as *const str as *const [u8]) }
     }
 
     #[inline]
     unsafe fn as_bytes_mut(&mut self) -> &mut [u8] {
-        mem::transmute(self)
+        &mut *(self as *mut str as *mut [u8])
     }
 
     fn find<'a, P: Pattern<'a>>(&'a self, pat: P) -> Option<usize> {