1#![stable(feature = "rust1", since = "1.0.0")]
38
39use crate::marker::{Destruct, PointeeSized};
40
41mod uninit;
42
43#[stable(feature = "rust1", since = "1.0.0")]
190#[lang = "clone"]
191#[rustc_diagnostic_item = "Clone"]
192#[rustc_trivial_field_reads]
193#[rustc_const_unstable(feature = "const_clone", issue = "142757")]
194#[const_trait]
195pub trait Clone: Sized {
196 #[stable(feature = "rust1", since = "1.0.0")]
233 #[must_use = "cloning is often expensive and is not expected to have side effects"]
234 #[lang = "clone_fn"]
237 fn clone(&self) -> Self;
238
239 #[inline]
245 #[stable(feature = "rust1", since = "1.0.0")]
246 fn clone_from(&mut self, source: &Self)
247 where
248 Self: [const] Destruct,
249 {
250 *self = source.clone()
251 }
252}
253
254#[rustc_builtin_macro]
256#[stable(feature = "builtin_macro_prelude", since = "1.38.0")]
257#[allow_internal_unstable(core_intrinsics, derive_clone_copy)]
258pub macro Clone($item:item) {
259 }
261
262#[unstable(feature = "ergonomic_clones", issue = "132290")]
294#[lang = "use_cloned"]
295pub trait UseCloned: Clone {
296 }
298
299macro_rules! impl_use_cloned {
300 ($($t:ty)*) => {
301 $(
302 #[unstable(feature = "ergonomic_clones", issue = "132290")]
303 impl UseCloned for $t {}
304 )*
305 }
306}
307
308impl_use_cloned! {
309 usize u8 u16 u32 u64 u128
310 isize i8 i16 i32 i64 i128
311 f16 f32 f64 f128
312 bool char
313}
314
315#[doc(hidden)]
320#[allow(missing_debug_implementations)]
321#[unstable(
322 feature = "derive_clone_copy",
323 reason = "deriving hack, should not be public",
324 issue = "none"
325)]
326pub struct AssertParamIsClone<T: Clone + PointeeSized> {
327 _field: crate::marker::PhantomData<T>,
328}
329#[doc(hidden)]
330#[allow(missing_debug_implementations)]
331#[unstable(
332 feature = "derive_clone_copy",
333 reason = "deriving hack, should not be public",
334 issue = "none"
335)]
336pub struct AssertParamIsCopy<T: Copy + PointeeSized> {
337 _field: crate::marker::PhantomData<T>,
338}
339
340#[unstable(feature = "clone_to_uninit", issue = "126799")]
476pub unsafe trait CloneToUninit {
477 unsafe fn clone_to_uninit(&self, dest: *mut u8);
513}
514
515#[unstable(feature = "clone_to_uninit", issue = "126799")]
516unsafe impl<T: Clone> CloneToUninit for T {
517 #[inline]
518 unsafe fn clone_to_uninit(&self, dest: *mut u8) {
519 unsafe { <T as self::uninit::CopySpec>::clone_one(self, dest.cast::<T>()) }
521 }
522}
523
524#[unstable(feature = "clone_to_uninit", issue = "126799")]
525unsafe impl<T: Clone> CloneToUninit for [T] {
526 #[inline]
527 #[cfg_attr(debug_assertions, track_caller)]
528 unsafe fn clone_to_uninit(&self, dest: *mut u8) {
529 let dest: *mut [T] = dest.with_metadata_of(self);
530 unsafe { <T as self::uninit::CopySpec>::clone_slice(self, dest) }
532 }
533}
534
535#[unstable(feature = "clone_to_uninit", issue = "126799")]
536unsafe impl CloneToUninit for str {
537 #[inline]
538 #[cfg_attr(debug_assertions, track_caller)]
539 unsafe fn clone_to_uninit(&self, dest: *mut u8) {
540 unsafe { self.as_bytes().clone_to_uninit(dest) }
542 }
543}
544
545#[unstable(feature = "clone_to_uninit", issue = "126799")]
546unsafe impl CloneToUninit for crate::ffi::CStr {
547 #[cfg_attr(debug_assertions, track_caller)]
548 unsafe fn clone_to_uninit(&self, dest: *mut u8) {
549 unsafe { self.to_bytes_with_nul().clone_to_uninit(dest) }
554 }
555}
556
557#[unstable(feature = "bstr", issue = "134915")]
558unsafe impl CloneToUninit for crate::bstr::ByteStr {
559 #[inline]
560 #[cfg_attr(debug_assertions, track_caller)]
561 unsafe fn clone_to_uninit(&self, dst: *mut u8) {
562 unsafe { self.as_bytes().clone_to_uninit(dst) }
564 }
565}
566
567mod impls {
573 use crate::marker::PointeeSized;
574
575 macro_rules! impl_clone {
576 ($($t:ty)*) => {
577 $(
578 #[stable(feature = "rust1", since = "1.0.0")]
579 impl Clone for $t {
580 #[inline(always)]
581 fn clone(&self) -> Self {
582 *self
583 }
584 }
585 )*
586 }
587 }
588
589 impl_clone! {
590 usize u8 u16 u32 u64 u128
591 isize i8 i16 i32 i64 i128
592 f16 f32 f64 f128
593 bool char
594 }
595
596 #[unstable(feature = "never_type", issue = "35121")]
597 impl Clone for ! {
598 #[inline]
599 fn clone(&self) -> Self {
600 *self
601 }
602 }
603
604 #[stable(feature = "rust1", since = "1.0.0")]
605 impl<T: PointeeSized> Clone for *const T {
606 #[inline(always)]
607 fn clone(&self) -> Self {
608 *self
609 }
610 }
611
612 #[stable(feature = "rust1", since = "1.0.0")]
613 impl<T: PointeeSized> Clone for *mut T {
614 #[inline(always)]
615 fn clone(&self) -> Self {
616 *self
617 }
618 }
619
620 #[stable(feature = "rust1", since = "1.0.0")]
622 impl<T: PointeeSized> Clone for &T {
623 #[inline(always)]
624 #[rustc_diagnostic_item = "noop_method_clone"]
625 fn clone(&self) -> Self {
626 self
627 }
628 }
629
630 #[stable(feature = "rust1", since = "1.0.0")]
632 impl<T: PointeeSized> !Clone for &mut T {}
633}
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4