X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=src%2Fliballoc%2Fboxed.rs;h=87a4924f9becc4edb6afb14019668ebc2228a52d;hb=5c384ab00c7b4b39e457b75d385e9cbe12e699f5;hp=1c39a3721f465cd1e758360f06387a755cdacd44;hpb=59eed49115c98da4dca8387a89035e16a9d395c4;p=rust.git diff --git a/src/liballoc/boxed.rs b/src/liballoc/boxed.rs index 1c39a3721f4..87a4924f9be 100644 --- a/src/liballoc/boxed.rs +++ b/src/liballoc/boxed.rs @@ -134,21 +134,21 @@ use core::fmt; use core::future::Future; use core::hash::{Hash, Hasher}; -use core::iter::{Iterator, FromIterator, FusedIterator}; +use core::iter::{FromIterator, FusedIterator, Iterator}; use core::marker::{Unpin, Unsize}; use core::mem; -use core::pin::Pin; use core::ops::{ - CoerceUnsized, DispatchFromDyn, Deref, DerefMut, Receiver, Generator, GeneratorState + CoerceUnsized, Deref, DerefMut, DispatchFromDyn, Generator, GeneratorState, Receiver, }; +use core::pin::Pin; use core::ptr::{self, NonNull, Unique}; use core::slice; use core::task::{Context, Poll}; -use crate::alloc::{self, Global, Alloc}; -use crate::vec::Vec; +use crate::alloc::{self, Alloc, Global}; use crate::raw_vec::RawVec; use crate::str::from_boxed_utf8_unchecked; +use crate::vec::Vec; /// A pointer type for heap allocation. /// @@ -196,12 +196,10 @@ pub fn new(x: T) -> Box { pub fn new_uninit() -> Box> { let layout = alloc::Layout::new::>(); if layout.size() == 0 { - return Box(NonNull::dangling().into()) + return Box(NonNull::dangling().into()); } - let ptr = unsafe { - Global.alloc(layout) - .unwrap_or_else(|_| alloc::handle_alloc_error(layout)) - }; + let ptr = + unsafe { Global.alloc(layout).unwrap_or_else(|_| alloc::handle_alloc_error(layout)) }; Box(ptr.cast().into()) } @@ -269,9 +267,7 @@ pub fn new_uninit_slice(len: usize) -> Box<[mem::MaybeUninit]> { NonNull::dangling() } else { unsafe { - Global.alloc(layout) - .unwrap_or_else(|_| alloc::handle_alloc_error(layout)) - .cast() + Global.alloc(layout).unwrap_or_else(|_| alloc::handle_alloc_error(layout)).cast() } }; let slice = unsafe { slice::from_raw_parts_mut(ptr.as_ptr(), len) }; @@ -476,7 +472,7 @@ pub fn into_raw_non_null(b: Box) -> NonNull { Box::into_unique(b).into() } - #[unstable(feature = "ptr_internals", issue = "0", reason = "use into_raw_non_null instead")] + #[unstable(feature = "ptr_internals", issue = "none", reason = "use into_raw_non_null instead")] #[inline] #[doc(hidden)] pub fn into_unique(b: Box) -> Unique { @@ -532,7 +528,7 @@ pub fn into_unique(b: Box) -> Unique { #[inline] pub fn leak<'a>(b: Box) -> &'a mut T where - T: 'a // Technically not needed, but kept to be explicit. + T: 'a, // Technically not needed, but kept to be explicit. { unsafe { &mut *Box::into_raw(b) } } @@ -625,15 +621,12 @@ fn clone_from(&mut self, source: &Box) { } } - #[stable(feature = "box_slice_clone", since = "1.3.0")] impl Clone for Box { fn clone(&self) -> Self { // this makes a copy of the data let buf: Box<[u8]> = self.as_bytes().into(); - unsafe { - from_boxed_utf8_unchecked(buf) - } + unsafe { from_boxed_utf8_unchecked(buf) } } } @@ -830,7 +823,7 @@ fn from(s: Box) -> Self { } } -#[unstable(feature = "boxed_slice_try_from", issue = "0")] +#[unstable(feature = "boxed_slice_try_from", issue = "none")] impl TryFrom> for Box<[T; N]> where [T; N]: LengthAtMost32, @@ -946,7 +939,7 @@ fn deref_mut(&mut self) -> &mut T { } } -#[unstable(feature = "receiver_trait", issue = "0")] +#[unstable(feature = "receiver_trait", issue = "none")] impl Receiver for Box {} #[stable(feature = "rust1", since = "1.0.0")] @@ -1040,7 +1033,7 @@ extern "rust-call" fn call(&self, args: A) -> Self::Output { #[unstable(feature = "coerce_unsized", issue = "27732")] impl, U: ?Sized> CoerceUnsized> for Box {} -#[unstable(feature = "dispatch_from_dyn", issue = "0")] +#[unstable(feature = "dispatch_from_dyn", issue = "none")] impl, U: ?Sized> DispatchFromDyn> for Box {} #[stable(feature = "boxed_slice_from_iter", since = "1.32.0")] @@ -1053,51 +1046,7 @@ fn from_iter>(iter: T) -> Self { #[stable(feature = "box_slice_clone", since = "1.3.0")] impl Clone for Box<[T]> { fn clone(&self) -> Self { - let mut new = BoxBuilder { - data: RawVec::with_capacity(self.len()), - len: 0, - }; - - let mut target = new.data.ptr(); - - for item in self.iter() { - unsafe { - ptr::write(target, item.clone()); - target = target.offset(1); - }; - - new.len += 1; - } - - return unsafe { new.into_box() }; - - // Helper type for responding to panics correctly. - struct BoxBuilder { - data: RawVec, - len: usize, - } - - impl BoxBuilder { - unsafe fn into_box(self) -> Box<[T]> { - let raw = ptr::read(&self.data); - mem::forget(self); - raw.into_box() - } - } - - impl Drop for BoxBuilder { - fn drop(&mut self) { - let mut data = self.data.ptr(); - let max = unsafe { data.add(self.len) }; - - while data != max { - unsafe { - ptr::read(data); - data = data.offset(1); - } - } - } - } + self.to_vec().into_boxed_slice() } } @@ -1152,7 +1101,7 @@ fn as_mut(&mut self) -> &mut T { * could have a method to project a Pin from it. */ #[stable(feature = "pin", since = "1.33.0")] -impl Unpin for Box { } +impl Unpin for Box {} #[unstable(feature = "generator_trait", issue = "43122")] impl Generator for Box {