Skip to content
Permalink
Browse files

Move constructors of boxed/rc’ed slices to matching `impl` blocks

  • Loading branch information...
SimonSapin committed Jul 6, 2019
1 parent bde1924 commit 170d933d2f8a7d4787355a86136093af86b03077
Showing with 106 additions and 100 deletions.
  1. +12 −10 src/liballoc/boxed.rs
  2. +47 −45 src/liballoc/rc.rs
  3. +47 −45 src/liballoc/sync.rs
@@ -149,14 +149,24 @@ impl<T> Box<T> {
Box(unique.cast())
}

/// Constructs a new `Pin<Box<T>>`. If `T` does not implement `Unpin`, then
/// `x` will be pinned in memory and unable to be moved.
#[stable(feature = "pin", since = "1.33.0")]
#[inline(always)]
pub fn pin(x: T) -> Pin<Box<T>> {
(box x).into()
}
}

impl<T> Box<[T]> {
/// Construct a new boxed slice with uninitialized contents.
///
/// # Examples
///
/// ```
/// #![feature(new_uninit)]
///
/// let mut values = Box::<u32>::new_uninit_slice(3);
/// let mut values = Box::<[u32]>::new_uninit_slice(3);
///
/// let values = unsafe {
/// // Deferred initialization:
@@ -177,14 +187,6 @@ impl<T> Box<T> {
let slice = unsafe { slice::from_raw_parts_mut(unique.cast().as_ptr(), len) };
Box(Unique::from(slice))
}

/// Constructs a new `Pin<Box<T>>`. If `T` does not implement `Unpin`, then
/// `x` will be pinned in memory and unable to be moved.
#[stable(feature = "pin", since = "1.33.0")]
#[inline(always)]
pub fn pin(x: T) -> Pin<Box<T>> {
(box x).into()
}
}

impl<T> Box<mem::MaybeUninit<T>> {
@@ -237,7 +239,7 @@ impl<T> Box<[mem::MaybeUninit<T>]> {
/// ```
/// #![feature(new_uninit)]
///
/// let mut values = Box::<u32>::new_uninit_slice(3);
/// let mut values = Box::<[u32]>::new_uninit_slice(3);
///
/// let values = unsafe {
/// // Deferred initialization:
@@ -364,50 +364,6 @@ impl<T> Rc<T> {
}
}

/// Construct a new reference-counted slice with uninitialized contents.
///
/// # Examples
///
/// ```
/// #![feature(new_uninit)]
/// #![feature(get_mut_unchecked)]
///
/// use std::rc::Rc;
///
/// let mut values = Rc::<u32>::new_uninit_slice(3);
///
/// let values = unsafe {
/// // Deferred initialization:
/// Rc::get_mut_unchecked(&mut values)[0].as_mut_ptr().write(1);
/// Rc::get_mut_unchecked(&mut values)[1].as_mut_ptr().write(2);
/// Rc::get_mut_unchecked(&mut values)[2].as_mut_ptr().write(3);
///
/// values.assume_init()
/// };
///
/// assert_eq!(*values, [1, 2, 3])
/// ```
#[unstable(feature = "new_uninit", issue = "0")]
pub fn new_uninit_slice(len: usize) -> Rc<[mem::MaybeUninit<T>]> {
let data_layout = Layout::array::<mem::MaybeUninit<T>>(len).unwrap();
let (layout, offset) = Layout::new::<RcBox<()>>().extend(data_layout).unwrap();
unsafe {
let allocated_ptr = Global.alloc(layout)
.unwrap_or_else(|_| handle_alloc_error(layout))
.as_ptr();
let data_ptr = allocated_ptr.add(offset) as *mut mem::MaybeUninit<T>;
let slice: *mut [mem::MaybeUninit<T>] = from_raw_parts_mut(data_ptr, len);
let wide_ptr = slice as *mut RcBox<[mem::MaybeUninit<T>]>;
let wide_ptr = set_data_ptr(wide_ptr, allocated_ptr);
ptr::write(&mut (*wide_ptr).strong, Cell::new(1));
ptr::write(&mut (*wide_ptr).weak, Cell::new(1));
Rc {
ptr: NonNull::new_unchecked(wide_ptr),
phantom: PhantomData,
}
}
}

/// Constructs a new `Pin<Rc<T>>`. If `T` does not implement `Unpin`, then
/// `value` will be pinned in memory and unable to be moved.
#[stable(feature = "pin", since = "1.33.0")]
@@ -458,6 +414,52 @@ impl<T> Rc<T> {
}
}

impl<T> Rc<[T]> {
/// Construct a new reference-counted slice with uninitialized contents.
///
/// # Examples
///
/// ```
/// #![feature(new_uninit)]
/// #![feature(get_mut_unchecked)]
///
/// use std::rc::Rc;
///
/// let mut values = Rc::<[u32]>::new_uninit_slice(3);
///
/// let values = unsafe {
/// // Deferred initialization:
/// Rc::get_mut_unchecked(&mut values)[0].as_mut_ptr().write(1);
/// Rc::get_mut_unchecked(&mut values)[1].as_mut_ptr().write(2);
/// Rc::get_mut_unchecked(&mut values)[2].as_mut_ptr().write(3);
///
/// values.assume_init()
/// };
///
/// assert_eq!(*values, [1, 2, 3])
/// ```
#[unstable(feature = "new_uninit", issue = "0")]
pub fn new_uninit_slice(len: usize) -> Rc<[mem::MaybeUninit<T>]> {
let data_layout = Layout::array::<mem::MaybeUninit<T>>(len).unwrap();
let (layout, offset) = Layout::new::<RcBox<()>>().extend(data_layout).unwrap();
unsafe {
let allocated_ptr = Global.alloc(layout)
.unwrap_or_else(|_| handle_alloc_error(layout))
.as_ptr();
let data_ptr = allocated_ptr.add(offset) as *mut mem::MaybeUninit<T>;
let slice: *mut [mem::MaybeUninit<T>] = from_raw_parts_mut(data_ptr, len);
let wide_ptr = slice as *mut RcBox<[mem::MaybeUninit<T>]>;
let wide_ptr = set_data_ptr(wide_ptr, allocated_ptr);
ptr::write(&mut (*wide_ptr).strong, Cell::new(1));
ptr::write(&mut (*wide_ptr).weak, Cell::new(1));
Rc {
ptr: NonNull::new_unchecked(wide_ptr),
phantom: PhantomData,
}
}
}
}

impl<T> Rc<mem::MaybeUninit<T>> {
/// Convert to `Rc<T>`.
///
@@ -519,7 +521,7 @@ impl<T> Rc<[mem::MaybeUninit<T>]> {
///
/// use std::rc::Rc;
///
/// let mut values = Rc::<u32>::new_uninit_slice(3);
/// let mut values = Rc::<[u32]>::new_uninit_slice(3);
///
/// let values = unsafe {
/// // Deferred initialization:
@@ -348,50 +348,6 @@ impl<T> Arc<T> {
}
}

/// Construct a new reference-counted slice with uninitialized contents.
///
/// # Examples
///
/// ```
/// #![feature(new_uninit)]
/// #![feature(get_mut_unchecked)]
///
/// use std::sync::Arc;
///
/// let mut values = Arc::<u32>::new_uninit_slice(3);
///
/// let values = unsafe {
/// // Deferred initialization:
/// Arc::get_mut_unchecked(&mut values)[0].as_mut_ptr().write(1);
/// Arc::get_mut_unchecked(&mut values)[1].as_mut_ptr().write(2);
/// Arc::get_mut_unchecked(&mut values)[2].as_mut_ptr().write(3);
///
/// values.assume_init()
/// };
///
/// assert_eq!(*values, [1, 2, 3])
/// ```
#[unstable(feature = "new_uninit", issue = "0")]
pub fn new_uninit_slice(len: usize) -> Arc<[mem::MaybeUninit<T>]> {
let data_layout = Layout::array::<mem::MaybeUninit<T>>(len).unwrap();
let (layout, offset) = Layout::new::<ArcInner<()>>().extend(data_layout).unwrap();
unsafe {
let allocated_ptr = Global.alloc(layout)
.unwrap_or_else(|_| handle_alloc_error(layout))
.as_ptr();
let data_ptr = allocated_ptr.add(offset) as *mut mem::MaybeUninit<T>;
let slice: *mut [mem::MaybeUninit<T>] = from_raw_parts_mut(data_ptr, len);
let wide_ptr = slice as *mut ArcInner<[mem::MaybeUninit<T>]>;
let wide_ptr = set_data_ptr(wide_ptr, allocated_ptr);
ptr::write(&mut (*wide_ptr).strong, atomic::AtomicUsize::new(1));
ptr::write(&mut (*wide_ptr).weak, atomic::AtomicUsize::new(1));
Arc {
ptr: NonNull::new_unchecked(wide_ptr),
phantom: PhantomData,
}
}
}

/// Constructs a new `Pin<Arc<T>>`. If `T` does not implement `Unpin`, then
/// `data` will be pinned in memory and unable to be moved.
#[stable(feature = "pin", since = "1.33.0")]
@@ -442,6 +398,52 @@ impl<T> Arc<T> {
}
}

impl<T> Arc<[T]> {
/// Construct a new reference-counted slice with uninitialized contents.
///
/// # Examples
///
/// ```
/// #![feature(new_uninit)]
/// #![feature(get_mut_unchecked)]
///
/// use std::sync::Arc;
///
/// let mut values = Arc::<[u32]>::new_uninit_slice(3);
///
/// let values = unsafe {
/// // Deferred initialization:
/// Arc::get_mut_unchecked(&mut values)[0].as_mut_ptr().write(1);
/// Arc::get_mut_unchecked(&mut values)[1].as_mut_ptr().write(2);
/// Arc::get_mut_unchecked(&mut values)[2].as_mut_ptr().write(3);
///
/// values.assume_init()
/// };
///
/// assert_eq!(*values, [1, 2, 3])
/// ```
#[unstable(feature = "new_uninit", issue = "0")]
pub fn new_uninit_slice(len: usize) -> Arc<[mem::MaybeUninit<T>]> {
let data_layout = Layout::array::<mem::MaybeUninit<T>>(len).unwrap();
let (layout, offset) = Layout::new::<ArcInner<()>>().extend(data_layout).unwrap();
unsafe {
let allocated_ptr = Global.alloc(layout)
.unwrap_or_else(|_| handle_alloc_error(layout))
.as_ptr();
let data_ptr = allocated_ptr.add(offset) as *mut mem::MaybeUninit<T>;
let slice: *mut [mem::MaybeUninit<T>] = from_raw_parts_mut(data_ptr, len);
let wide_ptr = slice as *mut ArcInner<[mem::MaybeUninit<T>]>;
let wide_ptr = set_data_ptr(wide_ptr, allocated_ptr);
ptr::write(&mut (*wide_ptr).strong, atomic::AtomicUsize::new(1));
ptr::write(&mut (*wide_ptr).weak, atomic::AtomicUsize::new(1));
Arc {
ptr: NonNull::new_unchecked(wide_ptr),
phantom: PhantomData,
}
}
}
}

impl<T> Arc<mem::MaybeUninit<T>> {
/// Convert to `Arc<T>`.
///
@@ -503,7 +505,7 @@ impl<T> Arc<[mem::MaybeUninit<T>]> {
///
/// use std::sync::Arc;
///
/// let mut values = Arc::<u32>::new_uninit_slice(3);
/// let mut values = Arc::<[u32]>::new_uninit_slice(3);
///
/// let values = unsafe {
/// // Deferred initialization:

0 comments on commit 170d933

Please sign in to comment.
You can’t perform that action at this time.