diff options
| author | Esteban Küber <esteban@kuber.com.ar> | 2016-12-27 17:02:52 -0800 |
|---|---|---|
| committer | Esteban Küber <esteban@kuber.com.ar> | 2016-12-27 17:02:52 -0800 |
| commit | e766c465d2e4c4e3c106bfa8343cbe6f9192d445 (patch) | |
| tree | 821a7cf1e0b04ac9c0cddede6eb760bbf2d0ce62 /src/libcore | |
| parent | 96c52d4fd86aed6320732a511c04bcbfff7d117f (diff) | |
| parent | 314c28b729ae359b99586cc62c486c28e0d44424 (diff) | |
| download | rust-e766c465d2e4c4e3c106bfa8343cbe6f9192d445.tar.gz rust-e766c465d2e4c4e3c106bfa8343cbe6f9192d445.zip | |
Merge branch 'master' into escape-reason-docs
Diffstat (limited to 'src/libcore')
| -rw-r--r-- | src/libcore/cell.rs | 8 | ||||
| -rw-r--r-- | src/libcore/char.rs | 4 | ||||
| -rw-r--r-- | src/libcore/fmt/mod.rs | 10 | ||||
| -rw-r--r-- | src/libcore/hash/mod.rs | 40 | ||||
| -rw-r--r-- | src/libcore/iter/iterator.rs | 6 | ||||
| -rw-r--r-- | src/libcore/ptr.rs | 83 | ||||
| -rw-r--r-- | src/libcore/result.rs | 29 | ||||
| -rw-r--r-- | src/libcore/slice.rs | 22 | ||||
| -rw-r--r-- | src/libcore/sync/atomic.rs | 18 |
9 files changed, 179 insertions, 41 deletions
diff --git a/src/libcore/cell.rs b/src/libcore/cell.rs index 64a7a8c5ef7..c3f862e7c54 100644 --- a/src/libcore/cell.rs +++ b/src/libcore/cell.rs @@ -393,6 +393,8 @@ pub struct RefCell<T: ?Sized> { /// An enumeration of values returned from the `state` method on a `RefCell<T>`. #[derive(Copy, Clone, PartialEq, Eq, Debug)] #[unstable(feature = "borrow_state", issue = "27733")] +#[rustc_deprecated(since = "1.15.0", reason = "use `try_borrow` instead")] +#[allow(deprecated)] pub enum BorrowState { /// The cell is currently being read, there is at least one active `borrow`. Reading, @@ -511,6 +513,8 @@ impl<T: ?Sized> RefCell<T> { /// } /// ``` #[unstable(feature = "borrow_state", issue = "27733")] + #[rustc_deprecated(since = "1.15.0", reason = "use `try_borrow` instead")] + #[allow(deprecated)] #[inline] pub fn borrow_state(&self) -> BorrowState { match self.borrow.get() { @@ -888,9 +892,7 @@ impl<'b, T: ?Sized> Ref<'b, T> { /// `Ref::clone(...)`. A `Clone` implementation or a method would interfere /// with the widespread use of `r.borrow().clone()` to clone the contents of /// a `RefCell`. - #[unstable(feature = "cell_extras", - reason = "likely to be moved to a method, pending language changes", - issue = "27746")] + #[stable(feature = "cell_extras", since = "1.15.0")] #[inline] pub fn clone(orig: &Ref<'b, T>) -> Ref<'b, T> { Ref { diff --git a/src/libcore/char.rs b/src/libcore/char.rs index 7f3ac13bac1..c14ae6e0898 100644 --- a/src/libcore/char.rs +++ b/src/libcore/char.rs @@ -327,9 +327,9 @@ pub trait CharExt { fn len_utf8(self) -> usize; #[stable(feature = "core", since = "1.6.0")] fn len_utf16(self) -> usize; - #[unstable(feature = "unicode", issue = "27784")] + #[stable(feature = "unicode_encode_char", since = "1.15.0")] fn encode_utf8(self, dst: &mut [u8]) -> &mut str; - #[unstable(feature = "unicode", issue = "27784")] + #[stable(feature = "unicode_encode_char", since = "1.15.0")] fn encode_utf16(self, dst: &mut [u16]) -> &mut [u16]; } diff --git a/src/libcore/fmt/mod.rs b/src/libcore/fmt/mod.rs index 9167264ba9d..2ba7d6e8bd1 100644 --- a/src/libcore/fmt/mod.rs +++ b/src/libcore/fmt/mod.rs @@ -12,7 +12,7 @@ #![stable(feature = "rust1", since = "1.0.0")] -use cell::{UnsafeCell, Cell, RefCell, Ref, RefMut, BorrowState}; +use cell::{UnsafeCell, Cell, RefCell, Ref, RefMut}; use marker::PhantomData; use mem; use num::flt2dec; @@ -1634,13 +1634,13 @@ impl<T: Copy + Debug> Debug for Cell<T> { #[stable(feature = "rust1", since = "1.0.0")] impl<T: ?Sized + Debug> Debug for RefCell<T> { fn fmt(&self, f: &mut Formatter) -> Result { - match self.borrow_state() { - BorrowState::Unused | BorrowState::Reading => { + match self.try_borrow() { + Ok(borrow) => { f.debug_struct("RefCell") - .field("value", &self.borrow()) + .field("value", &borrow) .finish() } - BorrowState::Writing => { + Err(_) => { f.debug_struct("RefCell") .field("value", &"<borrowed>") .finish() diff --git a/src/libcore/hash/mod.rs b/src/libcore/hash/mod.rs index ac36cbaace7..18b465d85a1 100644 --- a/src/libcore/hash/mod.rs +++ b/src/libcore/hash/mod.rs @@ -255,10 +255,44 @@ pub trait BuildHasher { fn build_hasher(&self) -> Self::Hasher; } -/// A structure which implements `BuildHasher` for all `Hasher` types which also -/// implement `Default`. +/// The `BuildHasherDefault` structure is used in scenarios where one has a +/// type that implements [`Hasher`] and [`Default`], but needs that type to +/// implement [`BuildHasher`]. /// -/// This struct is 0-sized and does not need construction. +/// This structure is zero-sized and does not need construction. +/// +/// # Examples +/// +/// Using `BuildHasherDefault` to specify a custom [`BuildHasher`] for +/// [`HashMap`]: +/// +/// ``` +/// use std::collections::HashMap; +/// use std::hash::{BuildHasherDefault, Hasher}; +/// +/// #[derive(Default)] +/// struct MyHasher; +/// +/// impl Hasher for MyHasher { +/// fn write(&mut self, bytes: &[u8]) { +/// // Your hashing algorithm goes here! +/// unimplemented!() +/// } +/// +/// fn finish(&self) -> u64 { +/// // Your hashing algorithm goes here! +/// unimplemented!() +/// } +/// } +/// +/// type MyBuildHasher = BuildHasherDefault<MyHasher>; +/// +/// let hash_map = HashMap::<u32, u32, MyBuildHasher>::default(); +/// ``` +/// +/// [`BuildHasher`]: trait.BuildHasher.html +/// [`Default`]: ../default/trait.Default.html +/// [`Hasher`]: trait.Hasher.html #[stable(since = "1.7.0", feature = "build_hasher")] pub struct BuildHasherDefault<H>(marker::PhantomData<H>); diff --git a/src/libcore/iter/iterator.rs b/src/libcore/iter/iterator.rs index 48808b601c1..ec590d2bd06 100644 --- a/src/libcore/iter/iterator.rs +++ b/src/libcore/iter/iterator.rs @@ -1696,12 +1696,11 @@ pub trait Iterator { /// # Examples /// /// ``` - /// #![feature(iter_max_by)] /// let a = [-3_i32, 0, 1, 5, -10]; /// assert_eq!(*a.iter().max_by(|x, y| x.cmp(y)).unwrap(), 5); /// ``` #[inline] - #[unstable(feature = "iter_max_by", issue="36105")] + #[stable(feature = "iter_max_by", since = "1.15.0")] fn max_by<F>(self, mut compare: F) -> Option<Self::Item> where Self: Sized, F: FnMut(&Self::Item, &Self::Item) -> Ordering, { @@ -1746,12 +1745,11 @@ pub trait Iterator { /// # Examples /// /// ``` - /// #![feature(iter_min_by)] /// let a = [-3_i32, 0, 1, 5, -10]; /// assert_eq!(*a.iter().min_by(|x, y| x.cmp(y)).unwrap(), -10); /// ``` #[inline] - #[unstable(feature = "iter_min_by", issue="36105")] + #[stable(feature = "iter_min_by", since = "1.15.0")] fn min_by<F>(self, mut compare: F) -> Option<Self::Item> where Self: Sized, F: FnMut(&Self::Item, &Self::Item) -> Ordering, { diff --git a/src/libcore/ptr.rs b/src/libcore/ptr.rs index 2ad38de72b1..e3ca8eca76c 100644 --- a/src/libcore/ptr.rs +++ b/src/libcore/ptr.rs @@ -117,6 +117,8 @@ pub unsafe fn replace<T>(dest: *mut T, mut src: T) -> T { /// `zero_memory`, or `copy_memory`). Note that `*src = foo` counts as a use /// because it will attempt to drop the value previously at `*src`. /// +/// The pointer must be aligned; use `read_unaligned` if that is not the case. +/// /// # Examples /// /// Basic usage: @@ -137,6 +139,44 @@ pub unsafe fn read<T>(src: *const T) -> T { tmp } +/// Reads the value from `src` without moving it. This leaves the +/// memory in `src` unchanged. +/// +/// Unlike `read`, the pointer may be unaligned. +/// +/// # Safety +/// +/// Beyond accepting a raw pointer, this is unsafe because it semantically +/// moves the value out of `src` without preventing further usage of `src`. +/// If `T` is not `Copy`, then care must be taken to ensure that the value at +/// `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: +/// +/// ``` +/// #![feature(ptr_unaligned)] +/// +/// let x = 12; +/// let y = &x as *const i32; +/// +/// unsafe { +/// assert_eq!(std::ptr::read_unaligned(y), 12); +/// } +/// ``` +#[inline(always)] +#[unstable(feature = "ptr_unaligned", issue = "37955")] +pub unsafe fn read_unaligned<T>(src: *const T) -> T { + let mut tmp: T = mem::uninitialized(); + copy_nonoverlapping(src as *const u8, + &mut tmp as *mut T as *mut u8, + mem::size_of::<T>()); + tmp +} + /// Overwrites a memory location with the given value without reading or /// dropping the old value. /// @@ -151,6 +191,8 @@ pub unsafe fn read<T>(src: *const T) -> T { /// This is appropriate for initializing uninitialized memory, or overwriting /// memory that has previously been `read` from. /// +/// The pointer must be aligned; use `write_unaligned` if that is not the case. +/// /// # Examples /// /// Basic usage: @@ -171,6 +213,47 @@ pub unsafe fn write<T>(dst: *mut T, src: T) { intrinsics::move_val_init(&mut *dst, src) } +/// Overwrites a memory location with the given value without reading or +/// dropping the old value. +/// +/// Unlike `write`, the pointer may be unaligned. +/// +/// # Safety +/// +/// This operation is marked unsafe because it accepts a raw pointer. +/// +/// It does not drop the contents of `dst`. This is safe, but it could leak +/// allocations or resources, so care must be taken not to overwrite an object +/// that should be dropped. +/// +/// This is appropriate for initializing uninitialized memory, or overwriting +/// memory that has previously been `read` from. +/// +/// # Examples +/// +/// Basic usage: +/// +/// ``` +/// #![feature(ptr_unaligned)] +/// +/// let mut x = 0; +/// let y = &mut x as *mut i32; +/// let z = 12; +/// +/// unsafe { +/// std::ptr::write_unaligned(y, z); +/// assert_eq!(std::ptr::read_unaligned(y), 12); +/// } +/// ``` +#[inline] +#[unstable(feature = "ptr_unaligned", issue = "37955")] +pub unsafe fn write_unaligned<T>(dst: *mut T, src: T) { + copy_nonoverlapping(&src as *const T as *const u8, + dst as *mut u8, + mem::size_of::<T>()); + mem::forget(src); +} + /// Performs a volatile read of the value from `src` without moving it. This /// leaves the memory in `src` unchanged. /// diff --git a/src/libcore/result.rs b/src/libcore/result.rs index afed99d265f..99c407e5273 100644 --- a/src/libcore/result.rs +++ b/src/libcore/result.rs @@ -501,6 +501,8 @@ impl<T, E> Result<T, E> { /// Returns an iterator over the possibly contained value. /// + /// The iterator yields one value if the result is [`Ok`], otherwise none. + /// /// # Examples /// /// Basic usage: @@ -512,6 +514,8 @@ impl<T, E> Result<T, E> { /// let x: Result<u32, &str> = Err("nothing!"); /// assert_eq!(x.iter().next(), None); /// ``` + /// + /// [`Ok`]: enum.Result.html#variant.Ok #[inline] #[stable(feature = "rust1", since = "1.0.0")] pub fn iter(&self) -> Iter<T> { @@ -520,6 +524,8 @@ impl<T, E> Result<T, E> { /// Returns a mutable iterator over the possibly contained value. /// + /// The iterator yields one value if the result is [`Ok`], otherwise none. + /// /// # Examples /// /// Basic usage: @@ -535,6 +541,8 @@ impl<T, E> Result<T, E> { /// let mut x: Result<u32, &str> = Err("nothing!"); /// assert_eq!(x.iter_mut().next(), None); /// ``` + /// + /// [`Ok`]: enum.Result.html#variant.Ok #[inline] #[stable(feature = "rust1", since = "1.0.0")] pub fn iter_mut(&mut self) -> IterMut<T> { @@ -848,6 +856,8 @@ impl<T, E> IntoIterator for Result<T, E> { /// Returns a consuming iterator over the possibly contained value. /// + /// The iterator yields one value if the result is [`Ok`], otherwise none. + /// /// # Examples /// /// Basic usage: @@ -861,6 +871,8 @@ impl<T, E> IntoIterator for Result<T, E> { /// let v: Vec<u32> = x.into_iter().collect(); /// assert_eq!(v, []); /// ``` + /// + /// [`Ok`]: enum.Result.html#variant.Ok #[inline] fn into_iter(self) -> IntoIter<T> { IntoIter { inner: self.ok() } @@ -893,8 +905,13 @@ impl<'a, T, E> IntoIterator for &'a mut Result<T, E> { /// An iterator over a reference to the [`Ok`] variant of a [`Result`]. /// +/// The iterator yields one value if the result is [`Ok`], otherwise none. +/// +/// Created by [`Result::iter`]. +/// /// [`Ok`]: enum.Result.html#variant.Ok /// [`Result`]: enum.Result.html +/// [`Result::iter`]: enum.Result.html#method.iter #[derive(Debug)] #[stable(feature = "rust1", since = "1.0.0")] pub struct Iter<'a, T: 'a> { inner: Option<&'a T> } @@ -934,8 +951,11 @@ impl<'a, T> Clone for Iter<'a, T> { /// An iterator over a mutable reference to the [`Ok`] variant of a [`Result`]. /// +/// Created by [`Result::iter_mut`]. +/// /// [`Ok`]: enum.Result.html#variant.Ok /// [`Result`]: enum.Result.html +/// [`Result::iter_mut`]: enum.Result.html#method.iter_mut #[derive(Debug)] #[stable(feature = "rust1", since = "1.0.0")] pub struct IterMut<'a, T: 'a> { inner: Option<&'a mut T> } @@ -968,9 +988,12 @@ impl<'a, T> FusedIterator for IterMut<'a, T> {} #[unstable(feature = "trusted_len", issue = "37572")] unsafe impl<'a, A> TrustedLen for IterMut<'a, A> {} -/// An iterator over the value in a [`Ok`] variant of a [`Result`]. This struct is -/// created by the [`into_iter`] method on [`Result`][`Result`] (provided by -/// the [`IntoIterator`] trait). +/// An iterator over the value in a [`Ok`] variant of a [`Result`]. +/// +/// The iterator yields one value if the result is [`Ok`], otherwise none. +/// +/// This struct is created by the [`into_iter`] method on +/// [`Result`][`Result`] (provided by the [`IntoIterator`] trait). /// /// [`Ok`]: enum.Result.html#variant.Ok /// [`Result`]: enum.Result.html diff --git a/src/libcore/slice.rs b/src/libcore/slice.rs index a4a90e7a9da..e0a49e2ae45 100644 --- a/src/libcore/slice.rs +++ b/src/libcore/slice.rs @@ -33,6 +33,7 @@ // * The `raw` and `bytes` submodules. // * Boilerplate trait implementations. +use borrow::Borrow; use cmp::Ordering::{self, Less, Equal, Greater}; use cmp; use fmt; @@ -100,15 +101,17 @@ pub trait SliceExt { #[stable(feature = "core", since = "1.6.0")] fn as_ptr(&self) -> *const Self::Item; #[stable(feature = "core", since = "1.6.0")] - fn binary_search(&self, x: &Self::Item) -> Result<usize, usize> - where Self::Item: Ord; + fn binary_search<Q: ?Sized>(&self, x: &Q) -> Result<usize, usize> + where Self::Item: Borrow<Q>, + Q: Ord; #[stable(feature = "core", since = "1.6.0")] fn binary_search_by<'a, F>(&'a self, f: F) -> Result<usize, usize> where F: FnMut(&'a Self::Item) -> Ordering; #[stable(feature = "slice_binary_search_by_key", since = "1.10.0")] - fn binary_search_by_key<'a, B, F>(&'a self, b: &B, f: F) -> Result<usize, usize> + fn binary_search_by_key<'a, B, F, Q: ?Sized>(&'a self, b: &Q, f: F) -> Result<usize, usize> where F: FnMut(&'a Self::Item) -> B, - B: Ord; + B: Borrow<Q>, + Q: Ord; #[stable(feature = "core", since = "1.6.0")] fn len(&self) -> usize; #[stable(feature = "core", since = "1.6.0")] @@ -493,8 +496,8 @@ impl<T> SliceExt for [T] { m >= n && needle == &self[m-n..] } - fn binary_search(&self, x: &T) -> Result<usize, usize> where T: Ord { - self.binary_search_by(|p| p.cmp(x)) + fn binary_search<Q: ?Sized>(&self, x: &Q) -> Result<usize, usize> where T: Borrow<Q>, Q: Ord { + self.binary_search_by(|p| p.borrow().cmp(x)) } #[inline] @@ -522,11 +525,12 @@ impl<T> SliceExt for [T] { } #[inline] - fn binary_search_by_key<'a, B, F>(&'a self, b: &B, mut f: F) -> Result<usize, usize> + fn binary_search_by_key<'a, B, F, Q: ?Sized>(&'a self, b: &Q, mut f: F) -> Result<usize, usize> where F: FnMut(&'a Self::Item) -> B, - B: Ord + B: Borrow<Q>, + Q: Ord { - self.binary_search_by(|k| f(k).cmp(b)) + self.binary_search_by(|k| f(k).borrow().cmp(b)) } } diff --git a/src/libcore/sync/atomic.rs b/src/libcore/sync/atomic.rs index c10f7e39fc3..198db0e7c0a 100644 --- a/src/libcore/sync/atomic.rs +++ b/src/libcore/sync/atomic.rs @@ -203,7 +203,6 @@ impl AtomicBool { /// # Examples /// /// ``` - /// #![feature(atomic_access)] /// use std::sync::atomic::{AtomicBool, Ordering}; /// /// let mut some_bool = AtomicBool::new(true); @@ -212,7 +211,7 @@ impl AtomicBool { /// assert_eq!(some_bool.load(Ordering::SeqCst), false); /// ``` #[inline] - #[unstable(feature = "atomic_access", issue = "35603")] + #[stable(feature = "atomic_access", since = "1.15.0")] pub fn get_mut(&mut self) -> &mut bool { unsafe { &mut *(self.v.get() as *mut bool) } } @@ -225,14 +224,13 @@ impl AtomicBool { /// # Examples /// /// ``` - /// #![feature(atomic_access)] /// use std::sync::atomic::AtomicBool; /// /// let some_bool = AtomicBool::new(true); /// assert_eq!(some_bool.into_inner(), true); /// ``` #[inline] - #[unstable(feature = "atomic_access", issue = "35603")] + #[stable(feature = "atomic_access", since = "1.15.0")] pub fn into_inner(self) -> bool { unsafe { self.v.into_inner() != 0 } } @@ -588,7 +586,6 @@ impl<T> AtomicPtr<T> { /// # Examples /// /// ``` - /// #![feature(atomic_access)] /// use std::sync::atomic::{AtomicPtr, Ordering}; /// /// let mut atomic_ptr = AtomicPtr::new(&mut 10); @@ -596,7 +593,7 @@ impl<T> AtomicPtr<T> { /// assert_eq!(unsafe { *atomic_ptr.load(Ordering::SeqCst) }, 5); /// ``` #[inline] - #[unstable(feature = "atomic_access", issue = "35603")] + #[stable(feature = "atomic_access", since = "1.15.0")] pub fn get_mut(&mut self) -> &mut *mut T { unsafe { &mut *self.p.get() } } @@ -609,14 +606,13 @@ impl<T> AtomicPtr<T> { /// # Examples /// /// ``` - /// #![feature(atomic_access)] /// use std::sync::atomic::AtomicPtr; /// /// let atomic_ptr = AtomicPtr::new(&mut 5); /// assert_eq!(unsafe { *atomic_ptr.into_inner() }, 5); /// ``` #[inline] - #[unstable(feature = "atomic_access", issue = "35603")] + #[stable(feature = "atomic_access", since = "1.15.0")] pub fn into_inner(self) -> *mut T { unsafe { self.p.into_inner() } } @@ -883,7 +879,6 @@ macro_rules! atomic_int { /// # Examples /// /// ``` - /// #![feature(atomic_access)] /// use std::sync::atomic::{AtomicIsize, Ordering}; /// /// let mut some_isize = AtomicIsize::new(10); @@ -905,7 +900,6 @@ macro_rules! atomic_int { /// # Examples /// /// ``` - /// #![feature(atomic_access)] /// use std::sync::atomic::AtomicIsize; /// /// let some_isize = AtomicIsize::new(5); @@ -1261,7 +1255,7 @@ atomic_int!{ stable(feature = "rust1", since = "1.0.0"), stable(feature = "extended_compare_and_swap", since = "1.10.0"), stable(feature = "atomic_debug", since = "1.3.0"), - unstable(feature = "atomic_access", issue = "35603"), + stable(feature = "atomic_access", since = "1.15.0"), isize AtomicIsize ATOMIC_ISIZE_INIT } #[cfg(target_has_atomic = "ptr")] @@ -1269,7 +1263,7 @@ atomic_int!{ stable(feature = "rust1", since = "1.0.0"), stable(feature = "extended_compare_and_swap", since = "1.10.0"), stable(feature = "atomic_debug", since = "1.3.0"), - unstable(feature = "atomic_access", issue = "35603"), + stable(feature = "atomic_access", since = "1.15.0"), usize AtomicUsize ATOMIC_USIZE_INIT } |
