diff options
Diffstat (limited to 'src/liballoc')
| -rw-r--r-- | src/liballoc/boxed.rs | 44 | ||||
| -rw-r--r-- | src/liballoc/lib.rs | 1 | ||||
| -rw-r--r-- | src/liballoc/vec.rs | 25 |
3 files changed, 65 insertions, 5 deletions
diff --git a/src/liballoc/boxed.rs b/src/liballoc/boxed.rs index 35c8530b4dd..79292d390e5 100644 --- a/src/liballoc/boxed.rs +++ b/src/liballoc/boxed.rs @@ -269,7 +269,38 @@ impl<T: ?Sized> Box<T> { #[stable(feature = "box_raw", since = "1.4.0")] #[inline] pub unsafe fn from_raw(raw: *mut T) -> Self { - mem::transmute(raw) + Box::from_unique(Unique::new_unchecked(raw)) + } + + /// Constructs a `Box` from a `Unique<T>` pointer. + /// + /// After calling this function, the memory is owned by a `Box` and `T` can + /// then be destroyed and released upon drop. + /// + /// # Safety + /// + /// A `Unique<T>` can be safely created via [`Unique::new`] and thus doesn't + /// necessarily own the data pointed to nor is the data guaranteed to live + /// as long as the pointer. + /// + /// [`Unique::new`]: ../../core/ptr/struct.Unique.html#method.new + /// + /// # Examples + /// + /// ``` + /// #![feature(unique)] + /// + /// fn main() { + /// let x = Box::new(5); + /// let ptr = Box::into_unique(x); + /// let x = unsafe { Box::from_unique(ptr) }; + /// } + /// ``` + #[unstable(feature = "unique", reason = "needs an RFC to flesh out design", + issue = "27730")] + #[inline] + pub unsafe fn from_unique(u: Unique<T>) -> Self { + mem::transmute(u) } /// Consumes the `Box`, returning the wrapped raw pointer. @@ -295,7 +326,7 @@ impl<T: ?Sized> Box<T> { #[stable(feature = "box_raw", since = "1.4.0")] #[inline] pub fn into_raw(b: Box<T>) -> *mut T { - unsafe { mem::transmute(b) } + Box::into_unique(b).as_ptr() } /// Consumes the `Box`, returning the wrapped pointer as `Unique<T>`. @@ -303,13 +334,18 @@ impl<T: ?Sized> Box<T> { /// After calling this function, the caller is responsible for the /// memory previously managed by the `Box`. In particular, the /// caller should properly destroy `T` and release the memory. The - /// proper way to do so is to convert the raw pointer back into a - /// `Box` with the [`Box::from_raw`] function. + /// proper way to do so is to either convert the `Unique<T>` pointer: + /// + /// - Into a `Box` with the [`Box::from_unique`] function. + /// + /// - Into a raw pointer and back into a `Box` with the [`Box::from_raw`] + /// function. /// /// Note: this is an associated function, which means that you have /// to call it as `Box::into_unique(b)` instead of `b.into_unique()`. This /// is so that there is no conflict with a method on the inner type. /// + /// [`Box::from_unique`]: struct.Box.html#method.from_unique /// [`Box::from_raw`]: struct.Box.html#method.from_raw /// /// # Examples diff --git a/src/liballoc/lib.rs b/src/liballoc/lib.rs index d51aaa23c6a..0cbfc9e9dac 100644 --- a/src/liballoc/lib.rs +++ b/src/liballoc/lib.rs @@ -121,6 +121,7 @@ #![feature(unique)] #![feature(unsize)] #![feature(allocator_internals)] +#![feature(on_unimplemented)] #![cfg_attr(not(test), feature(fused, fn_traits, placement_new_protocol, swap_with_slice, i128))] #![cfg_attr(test, feature(test, box_heap))] diff --git a/src/liballoc/vec.rs b/src/liballoc/vec.rs index 861291194f3..cf34e195dea 100644 --- a/src/liballoc/vec.rs +++ b/src/liballoc/vec.rs @@ -1543,6 +1543,7 @@ impl<T: Hash> Hash for Vec<T> { } #[stable(feature = "rust1", since = "1.0.0")] +#[rustc_on_unimplemented = "vector indices are of type `usize` or ranges of `usize`"] impl<T> Index<usize> for Vec<T> { type Output = T; @@ -1554,6 +1555,7 @@ impl<T> Index<usize> for Vec<T> { } #[stable(feature = "rust1", since = "1.0.0")] +#[rustc_on_unimplemented = "vector indices are of type `usize` or ranges of `usize`"] impl<T> IndexMut<usize> for Vec<T> { #[inline] fn index_mut(&mut self, index: usize) -> &mut T { @@ -1562,8 +1564,8 @@ impl<T> IndexMut<usize> for Vec<T> { } } - #[stable(feature = "rust1", since = "1.0.0")] +#[rustc_on_unimplemented = "vector indices are of type `usize` or ranges of `usize`"] impl<T> ops::Index<ops::Range<usize>> for Vec<T> { type Output = [T]; @@ -1572,7 +1574,9 @@ impl<T> ops::Index<ops::Range<usize>> for Vec<T> { Index::index(&**self, index) } } + #[stable(feature = "rust1", since = "1.0.0")] +#[rustc_on_unimplemented = "vector indices are of type `usize` or ranges of `usize`"] impl<T> ops::Index<ops::RangeTo<usize>> for Vec<T> { type Output = [T]; @@ -1581,7 +1585,9 @@ impl<T> ops::Index<ops::RangeTo<usize>> for Vec<T> { Index::index(&**self, index) } } + #[stable(feature = "rust1", since = "1.0.0")] +#[rustc_on_unimplemented = "vector indices are of type `usize` or ranges of `usize`"] impl<T> ops::Index<ops::RangeFrom<usize>> for Vec<T> { type Output = [T]; @@ -1590,7 +1596,9 @@ impl<T> ops::Index<ops::RangeFrom<usize>> for Vec<T> { Index::index(&**self, index) } } + #[stable(feature = "rust1", since = "1.0.0")] +#[rustc_on_unimplemented = "vector indices are of type `usize` or ranges of `usize`"] impl<T> ops::Index<ops::RangeFull> for Vec<T> { type Output = [T]; @@ -1599,7 +1607,9 @@ impl<T> ops::Index<ops::RangeFull> for Vec<T> { self } } + #[unstable(feature = "inclusive_range", reason = "recently added, follows RFC", issue = "28237")] +#[rustc_on_unimplemented = "vector indices are of type `usize` or ranges of `usize`"] impl<T> ops::Index<ops::RangeInclusive<usize>> for Vec<T> { type Output = [T]; @@ -1608,7 +1618,9 @@ impl<T> ops::Index<ops::RangeInclusive<usize>> for Vec<T> { Index::index(&**self, index) } } + #[unstable(feature = "inclusive_range", reason = "recently added, follows RFC", issue = "28237")] +#[rustc_on_unimplemented = "vector indices are of type `usize` or ranges of `usize`"] impl<T> ops::Index<ops::RangeToInclusive<usize>> for Vec<T> { type Output = [T]; @@ -1619,41 +1631,52 @@ impl<T> ops::Index<ops::RangeToInclusive<usize>> for Vec<T> { } #[stable(feature = "rust1", since = "1.0.0")] +#[rustc_on_unimplemented = "vector indices are of type `usize` or ranges of `usize`"] impl<T> ops::IndexMut<ops::Range<usize>> for Vec<T> { #[inline] fn index_mut(&mut self, index: ops::Range<usize>) -> &mut [T] { IndexMut::index_mut(&mut **self, index) } } + #[stable(feature = "rust1", since = "1.0.0")] +#[rustc_on_unimplemented = "vector indices are of type `usize` or ranges of `usize`"] impl<T> ops::IndexMut<ops::RangeTo<usize>> for Vec<T> { #[inline] fn index_mut(&mut self, index: ops::RangeTo<usize>) -> &mut [T] { IndexMut::index_mut(&mut **self, index) } } + #[stable(feature = "rust1", since = "1.0.0")] +#[rustc_on_unimplemented = "vector indices are of type `usize` or ranges of `usize`"] impl<T> ops::IndexMut<ops::RangeFrom<usize>> for Vec<T> { #[inline] fn index_mut(&mut self, index: ops::RangeFrom<usize>) -> &mut [T] { IndexMut::index_mut(&mut **self, index) } } + #[stable(feature = "rust1", since = "1.0.0")] +#[rustc_on_unimplemented = "vector indices are of type `usize` or ranges of `usize`"] impl<T> ops::IndexMut<ops::RangeFull> for Vec<T> { #[inline] fn index_mut(&mut self, _index: ops::RangeFull) -> &mut [T] { self } } + #[unstable(feature = "inclusive_range", reason = "recently added, follows RFC", issue = "28237")] +#[rustc_on_unimplemented = "vector indices are of type `usize` or ranges of `usize`"] impl<T> ops::IndexMut<ops::RangeInclusive<usize>> for Vec<T> { #[inline] fn index_mut(&mut self, index: ops::RangeInclusive<usize>) -> &mut [T] { IndexMut::index_mut(&mut **self, index) } } + #[unstable(feature = "inclusive_range", reason = "recently added, follows RFC", issue = "28237")] +#[rustc_on_unimplemented = "vector indices are of type `usize` or ranges of `usize`"] impl<T> ops::IndexMut<ops::RangeToInclusive<usize>> for Vec<T> { #[inline] fn index_mut(&mut self, index: ops::RangeToInclusive<usize>) -> &mut [T] { |
