Skip to content

Commit d05912e

Browse files
committed
Add UseCloned trait related code
1 parent 0f84f75 commit d05912e

File tree

8 files changed

+66
-0
lines changed

8 files changed

+66
-0
lines changed

alloc/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@
113113
#![feature(deprecated_suggestion)]
114114
#![feature(deref_pure_trait)]
115115
#![feature(dispatch_from_dyn)]
116+
#![feature(ergonomic_clones)]
116117
#![feature(error_generic_member_access)]
117118
#![feature(exact_size_is_empty)]
118119
#![feature(extend_one)]

alloc/src/rc.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,7 @@ use core::any::Any;
245245
use core::cell::Cell;
246246
#[cfg(not(no_global_oom_handling))]
247247
use core::clone::CloneToUninit;
248+
use core::clone::UseCloned;
248249
use core::cmp::Ordering;
249250
use core::hash::{Hash, Hasher};
250251
use core::intrinsics::abort;
@@ -2333,6 +2334,9 @@ impl<T: ?Sized, A: Allocator + Clone> Clone for Rc<T, A> {
23332334
}
23342335
}
23352336

2337+
#[unstable(feature = "ergonomic_clones", issue = "132290")]
2338+
impl<T: ?Sized, A: Allocator + Clone> UseCloned for Rc<T, A> {}
2339+
23362340
#[cfg(not(no_global_oom_handling))]
23372341
#[stable(feature = "rust1", since = "1.0.0")]
23382342
impl<T: Default> Default for Rc<T> {
@@ -3496,6 +3500,9 @@ impl<T: ?Sized, A: Allocator + Clone> Clone for Weak<T, A> {
34963500
}
34973501
}
34983502

3503+
#[unstable(feature = "ergonomic_clones", issue = "132290")]
3504+
impl<T: ?Sized, A: Allocator + Clone> UseCloned for Weak<T, A> {}
3505+
34993506
#[stable(feature = "rc_weak", since = "1.4.0")]
35003507
impl<T: ?Sized, A: Allocator> fmt::Debug for Weak<T, A> {
35013508
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {

alloc/src/sync.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use core::any::Any;
1212
#[cfg(not(no_global_oom_handling))]
1313
use core::clone::CloneToUninit;
14+
use core::clone::UseCloned;
1415
use core::cmp::Ordering;
1516
use core::hash::{Hash, Hasher};
1617
use core::intrinsics::abort;
@@ -2197,6 +2198,9 @@ impl<T: ?Sized, A: Allocator + Clone> Clone for Arc<T, A> {
21972198
}
21982199
}
21992200

2201+
#[unstable(feature = "ergonomic_clones", issue = "132290")]
2202+
impl<T: ?Sized, A: Allocator + Clone> UseCloned for Arc<T, A> {}
2203+
22002204
#[stable(feature = "rust1", since = "1.0.0")]
22012205
impl<T: ?Sized, A: Allocator> Deref for Arc<T, A> {
22022206
type Target = T;
@@ -3158,6 +3162,9 @@ impl<T: ?Sized, A: Allocator + Clone> Clone for Weak<T, A> {
31583162
}
31593163
}
31603164

3165+
#[unstable(feature = "ergonomic_clones", issue = "132290")]
3166+
impl<T: ?Sized, A: Allocator + Clone> UseCloned for Weak<T, A> {}
3167+
31613168
#[stable(feature = "downgraded_weak", since = "1.10.0")]
31623169
impl<T> Default for Weak<T> {
31633170
/// Constructs a new `Weak<T>`, without allocating memory.

core/src/clone.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,40 @@ pub macro Clone($item:item) {
184184
/* compiler built-in */
185185
}
186186

187+
/// Trait for objects whose [`Clone`] impl is lightweight (e.g. reference-counted)
188+
///
189+
/// Cloning an object implementing this trait should in general:
190+
/// - be O(1) (constant) time regardless of the amount of data managed by the object,
191+
/// - not require a memory allocation,
192+
/// - not require copying more than roughly 64 bytes (a typical cache line size),
193+
/// - not block the current thread,
194+
/// - not have any semantic side effects (e.g. allocating a file descriptor), and
195+
/// - not have overhead larger than a couple of atomic operations.
196+
///
197+
/// The `UseCloned` trait does not provide a method; instead, it indicates that
198+
/// `Clone::clone` is lightweight, and allows the use of the `.use` syntax.
199+
#[unstable(feature = "ergonomic_clones", issue = "132290")]
200+
#[cfg_attr(not(bootstrap), lang = "use_cloned")]
201+
pub trait UseCloned: Clone {
202+
// Empty.
203+
}
204+
205+
macro_rules! impl_use_cloned {
206+
($($t:ty)*) => {
207+
$(
208+
#[unstable(feature = "ergonomic_clones", issue = "132290")]
209+
impl UseCloned for $t {}
210+
)*
211+
}
212+
}
213+
214+
impl_use_cloned! {
215+
usize u8 u16 u32 u64 u128
216+
isize i8 i16 i32 i64 i128
217+
f16 f32 f64 f128
218+
bool char
219+
}
220+
187221
// FIXME(aburka): these structs are used solely by #[derive] to
188222
// assert that every component of a type implements Clone or Copy.
189223
//

core/src/num/bignum.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,8 @@ macro_rules! define_bignum {
405405
}
406406
}
407407

408+
impl crate::clone::UseCloned for $name {}
409+
408410
impl crate::fmt::Debug for $name {
409411
fn fmt(&self, f: &mut crate::fmt::Formatter<'_>) -> crate::fmt::Result {
410412
let sz = if self.size < 1 { 1 } else { self.size };

core/src/num/nonzero.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! Definitions of integer that is known not to equal zero.
22
33
use super::{IntErrorKind, ParseIntError};
4+
use crate::clone::UseCloned;
45
use crate::cmp::Ordering;
56
use crate::hash::{Hash, Hasher};
67
use crate::marker::{Freeze, StructuralPartialEq};
@@ -183,6 +184,9 @@ where
183184
}
184185
}
185186

187+
#[unstable(feature = "ergonomic_clones", issue = "132290")]
188+
impl<T> UseCloned for NonZero<T> where T: ZeroablePrimitive {}
189+
186190
#[stable(feature = "nonzero", since = "1.28.0")]
187191
impl<T> Copy for NonZero<T> where T: ZeroablePrimitive {}
188192

core/src/option.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2050,6 +2050,9 @@ where
20502050
}
20512051
}
20522052

2053+
#[unstable(feature = "ergonomic_clones", issue = "132290")]
2054+
impl<T> crate::clone::UseCloned for Option<T> where T: crate::clone::UseCloned {}
2055+
20532056
#[stable(feature = "rust1", since = "1.0.0")]
20542057
impl<T> Default for Option<T> {
20552058
/// Returns [`None`][Option::None].

core/src/result.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1744,6 +1744,14 @@ where
17441744
}
17451745
}
17461746

1747+
#[unstable(feature = "ergonomic_clones", issue = "132290")]
1748+
impl<T, E> crate::clone::UseCloned for Result<T, E>
1749+
where
1750+
T: crate::clone::UseCloned,
1751+
E: crate::clone::UseCloned,
1752+
{
1753+
}
1754+
17471755
#[stable(feature = "rust1", since = "1.0.0")]
17481756
impl<T, E> IntoIterator for Result<T, E> {
17491757
type Item = T;

0 commit comments

Comments
 (0)