mirror of
https://github.com/rust-lang/rust.git
synced 2026-05-07 01:05:39 +03:00
9316d4508c
They cause significant binary size overhead while contributing little value. Also removes them from the wrapping String methods that do not panic.
65 lines
1.9 KiB
Rust
65 lines
1.9 KiB
Rust
use super::Vec;
|
|
use crate::borrow::Cow;
|
|
|
|
#[stable(feature = "cow_from_vec", since = "1.8.0")]
|
|
impl<'a, T: Clone> From<&'a [T]> for Cow<'a, [T]> {
|
|
/// Creates a [`Borrowed`] variant of [`Cow`]
|
|
/// from a slice.
|
|
///
|
|
/// This conversion does not allocate or clone the data.
|
|
///
|
|
/// [`Borrowed`]: crate::borrow::Cow::Borrowed
|
|
fn from(s: &'a [T]) -> Cow<'a, [T]> {
|
|
Cow::Borrowed(s)
|
|
}
|
|
}
|
|
|
|
#[stable(feature = "cow_from_array_ref", since = "1.77.0")]
|
|
impl<'a, T: Clone, const N: usize> From<&'a [T; N]> for Cow<'a, [T]> {
|
|
/// Creates a [`Borrowed`] variant of [`Cow`]
|
|
/// from a reference to an array.
|
|
///
|
|
/// This conversion does not allocate or clone the data.
|
|
///
|
|
/// [`Borrowed`]: crate::borrow::Cow::Borrowed
|
|
fn from(s: &'a [T; N]) -> Cow<'a, [T]> {
|
|
Cow::Borrowed(s as &[_])
|
|
}
|
|
}
|
|
|
|
#[stable(feature = "cow_from_vec", since = "1.8.0")]
|
|
impl<'a, T: Clone> From<Vec<T>> for Cow<'a, [T]> {
|
|
/// Creates an [`Owned`] variant of [`Cow`]
|
|
/// from an owned instance of [`Vec`].
|
|
///
|
|
/// This conversion does not allocate or clone the data.
|
|
///
|
|
/// [`Owned`]: crate::borrow::Cow::Owned
|
|
fn from(v: Vec<T>) -> Cow<'a, [T]> {
|
|
Cow::Owned(v)
|
|
}
|
|
}
|
|
|
|
#[stable(feature = "cow_from_vec_ref", since = "1.28.0")]
|
|
impl<'a, T: Clone> From<&'a Vec<T>> for Cow<'a, [T]> {
|
|
/// Creates a [`Borrowed`] variant of [`Cow`]
|
|
/// from a reference to [`Vec`].
|
|
///
|
|
/// This conversion does not allocate or clone the data.
|
|
///
|
|
/// [`Borrowed`]: crate::borrow::Cow::Borrowed
|
|
fn from(v: &'a Vec<T>) -> Cow<'a, [T]> {
|
|
Cow::Borrowed(v.as_slice())
|
|
}
|
|
}
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
|
impl<'a, T> FromIterator<T> for Cow<'a, [T]>
|
|
where
|
|
T: Clone,
|
|
{
|
|
fn from_iter<I: IntoIterator<Item = T>>(it: I) -> Cow<'a, [T]> {
|
|
Cow::Owned(FromIterator::from_iter(it))
|
|
}
|
|
}
|