Add UseCloned trait related code

This commit is contained in:
Santiago Pastorino 2024-12-11 18:00:56 -03:00
parent 57cb498989
commit dcdfd551f0
No known key found for this signature in database
GPG key ID: 8131A24E0C79EFAF
16 changed files with 198 additions and 44 deletions

View file

@ -184,6 +184,40 @@ pub macro Clone($item:item) {
/* compiler built-in */
}
/// Trait for objects whose [`Clone`] impl is lightweight (e.g. reference-counted)
///
/// Cloning an object implementing this trait should in general:
/// - be O(1) (constant) time regardless of the amount of data managed by the object,
/// - not require a memory allocation,
/// - not require copying more than roughly 64 bytes (a typical cache line size),
/// - not block the current thread,
/// - not have any semantic side effects (e.g. allocating a file descriptor), and
/// - not have overhead larger than a couple of atomic operations.
///
/// The `UseCloned` trait does not provide a method; instead, it indicates that
/// `Clone::clone` is lightweight, and allows the use of the `.use` syntax.
#[unstable(feature = "ergonomic_clones", issue = "132290")]
#[cfg_attr(not(bootstrap), lang = "use_cloned")]
pub trait UseCloned: Clone {
// Empty.
}
macro_rules! impl_use_cloned {
($($t:ty)*) => {
$(
#[unstable(feature = "ergonomic_clones", issue = "132290")]
impl UseCloned for $t {}
)*
}
}
impl_use_cloned! {
usize u8 u16 u32 u64 u128
isize i8 i16 i32 i64 i128
f16 f32 f64 f128
bool char
}
// FIXME(aburka): these structs are used solely by #[derive] to
// assert that every component of a type implements Clone or Copy.
//

View file

@ -405,6 +405,8 @@ macro_rules! define_bignum {
}
}
impl crate::clone::UseCloned for $name {}
impl crate::fmt::Debug for $name {
fn fmt(&self, f: &mut crate::fmt::Formatter<'_>) -> crate::fmt::Result {
let sz = if self.size < 1 { 1 } else { self.size };

View file

@ -1,6 +1,7 @@
//! Definitions of integer that is known not to equal zero.
use super::{IntErrorKind, ParseIntError};
use crate::clone::UseCloned;
use crate::cmp::Ordering;
use crate::hash::{Hash, Hasher};
use crate::marker::{Freeze, StructuralPartialEq};
@ -183,6 +184,9 @@ where
}
}
#[unstable(feature = "ergonomic_clones", issue = "132290")]
impl<T> UseCloned for NonZero<T> where T: ZeroablePrimitive {}
#[stable(feature = "nonzero", since = "1.28.0")]
impl<T> Copy for NonZero<T> where T: ZeroablePrimitive {}

View file

@ -2050,6 +2050,9 @@ where
}
}
#[unstable(feature = "ergonomic_clones", issue = "132290")]
impl<T> crate::clone::UseCloned for Option<T> where T: crate::clone::UseCloned {}
#[stable(feature = "rust1", since = "1.0.0")]
impl<T> Default for Option<T> {
/// Returns [`None`][Option::None].

View file

@ -1744,6 +1744,14 @@ where
}
}
#[unstable(feature = "ergonomic_clones", issue = "132290")]
impl<T, E> crate::clone::UseCloned for Result<T, E>
where
T: crate::clone::UseCloned,
E: crate::clone::UseCloned,
{
}
#[stable(feature = "rust1", since = "1.0.0")]
impl<T, E> IntoIterator for Result<T, E> {
type Item = T;