slice.rs (2612e3bbc0386368a850140a6c9b990cd496a5ec) slice.rs (89eed1ab1161e7d60595917e3b982e03dfcc0f8d)
1// SPDX-License-Identifier: Apache-2.0 OR MIT
2
3//! Utilities for the slice primitive type.
4//!
5//! *[See also the slice primitive type](slice).*
6//!
7//! Most of the structs in this module are iterator types which can only be created
8//! using a certain function. For example, `slice.iter()` yields an [`Iter`].

--- 770 unchanged lines hidden (view full) ---

779
780#[stable(feature = "rust1", since = "1.0.0")]
781impl<T, A: Allocator> BorrowMut<[T]> for Vec<T, A> {
782 fn borrow_mut(&mut self) -> &mut [T] {
783 &mut self[..]
784 }
785}
786
1// SPDX-License-Identifier: Apache-2.0 OR MIT
2
3//! Utilities for the slice primitive type.
4//!
5//! *[See also the slice primitive type](slice).*
6//!
7//! Most of the structs in this module are iterator types which can only be created
8//! using a certain function. For example, `slice.iter()` yields an [`Iter`].

--- 770 unchanged lines hidden (view full) ---

779
780#[stable(feature = "rust1", since = "1.0.0")]
781impl<T, A: Allocator> BorrowMut<[T]> for Vec<T, A> {
782 fn borrow_mut(&mut self) -> &mut [T] {
783 &mut self[..]
784 }
785}
786
787// Specializable trait for implementing ToOwned::clone_into. This is
788// public in the crate and has the Allocator parameter so that
789// vec::clone_from use it too.
787#[cfg(not(no_global_oom_handling))]
790#[cfg(not(no_global_oom_handling))]
791pub(crate) trait SpecCloneIntoVec<T, A: Allocator> {
792 fn clone_into(&self, target: &mut Vec<T, A>);
793}
794
795#[cfg(not(no_global_oom_handling))]
796impl<T: Clone, A: Allocator> SpecCloneIntoVec<T, A> for [T] {
797 default fn clone_into(&self, target: &mut Vec<T, A>) {
798 // drop anything in target that will not be overwritten
799 target.truncate(self.len());
800
801 // target.len <= self.len due to the truncate above, so the
802 // slices here are always in-bounds.
803 let (init, tail) = self.split_at(target.len());
804
805 // reuse the contained values' allocations/resources.
806 target.clone_from_slice(init);
807 target.extend_from_slice(tail);
808 }
809}
810
811#[cfg(not(no_global_oom_handling))]
812impl<T: Copy, A: Allocator> SpecCloneIntoVec<T, A> for [T] {
813 fn clone_into(&self, target: &mut Vec<T, A>) {
814 target.clear();
815 target.extend_from_slice(self);
816 }
817}
818
819#[cfg(not(no_global_oom_handling))]
788#[stable(feature = "rust1", since = "1.0.0")]
789impl<T: Clone> ToOwned for [T] {
790 type Owned = Vec<T>;
791 #[cfg(not(test))]
792 fn to_owned(&self) -> Vec<T> {
793 self.to_vec()
794 }
795
796 #[cfg(test)]
797 fn to_owned(&self) -> Vec<T> {
798 hack::to_vec(self, Global)
799 }
800
801 fn clone_into(&self, target: &mut Vec<T>) {
820#[stable(feature = "rust1", since = "1.0.0")]
821impl<T: Clone> ToOwned for [T] {
822 type Owned = Vec<T>;
823 #[cfg(not(test))]
824 fn to_owned(&self) -> Vec<T> {
825 self.to_vec()
826 }
827
828 #[cfg(test)]
829 fn to_owned(&self) -> Vec<T> {
830 hack::to_vec(self, Global)
831 }
832
833 fn clone_into(&self, target: &mut Vec<T>) {
802 // drop anything in target that will not be overwritten
803 target.truncate(self.len());
804
805 // target.len <= self.len due to the truncate above, so the
806 // slices here are always in-bounds.
807 let (init, tail) = self.split_at(target.len());
808
809 // reuse the contained values' allocations/resources.
810 target.clone_from_slice(init);
811 target.extend_from_slice(tail);
834 SpecCloneIntoVec::clone_into(self, target);
812 }
813}
814
815////////////////////////////////////////////////////////////////////////////////
816// Sorting
817////////////////////////////////////////////////////////////////////////////////
818
819#[inline]

--- 48 unchanged lines hidden ---
835 }
836}
837
838////////////////////////////////////////////////////////////////////////////////
839// Sorting
840////////////////////////////////////////////////////////////////////////////////
841
842#[inline]

--- 48 unchanged lines hidden ---