| 1 |
#![feature(negative_impls)] |
| 2 |
|
| 3 |
#[lang = "copy"] |
| 4 |
trait Copy {} |
| 5 |
|
| 6 |
mod copy_impls { |
| 7 |
use super::Copy; |
| 8 |
|
| 9 |
macro_rules! impl_copy { |
| 10 |
($($t:ty)*) => { |
| 11 |
$( |
| 12 |
impl Copy for $t {} |
| 13 |
)* |
| 14 |
} |
| 15 |
} |
| 16 |
|
| 17 |
impl_copy! { |
| 18 |
usize u8 u16 u32 u64 // u128 |
| 19 |
isize i8 i16 i32 i64 // i128 |
| 20 |
f32 f64 |
| 21 |
bool char |
| 22 |
} |
| 23 |
} |
| 24 |
|
| 25 |
#[stable(feature = "rust1", since = "1.0.0")] |
| 26 |
#[repr(transparent)] |
| 27 |
#[repr(no_niche)] // rust-lang/rust#68303. |
| 28 |
pub struct UnsafeCell<T: ?Sized> { |
| 29 |
value: T, |
| 30 |
// { dg-warning "field is never read" "" { target *-*-* } .-1 } |
| 31 |
} |
| 32 |
|
| 33 |
impl<T: ?Sized> UnsafeCell<T> { |
| 34 |
/// Gets a mutable pointer to the wrapped value. |
| 35 |
/// |
| 36 |
/// This can be cast to a pointer of any kind. |
| 37 |
/// Ensure that the access is unique (no active references, mutable or not) |
| 38 |
/// when casting to `&mut T`, and ensure that there are no mutations |
| 39 |
/// or mutable aliases going on when casting to `&T` |
| 40 |
/// |
| 41 |
/// # Examples |
| 42 |
/// |
| 43 |
/// |
| 44 |
/// use std::cell::UnsafeCell; |
| 45 |
/// |
| 46 |
/// let uc = UnsafeCell::new(5); |
| 47 |
/// |
| 48 |
/// let five = uc.get(); |
| 49 |
/// |
| 50 |
#[inline] |
| 51 |
#[stable(feature = "rust1", since = "1.0.0")] |
| 52 |
// #[rustc_const_stable(feature = "const_unsafecell_get", since = "1.32.0")] |
| 53 |
pub const fn get(&self) -> *mut T { |
| 54 |
// We can just cast the pointer from `UnsafeCell<T>` to `T` because of |
| 55 |
// #[repr(transparent)]. This exploits libstd's special status, there is |
| 56 |
// no guarantee for user code that this will work in future versions of the compiler! |
| 57 |
self as *const UnsafeCell<T> as *const T as *mut T |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
#[stable(feature = "rust1", since = "1.0.0")] |
| 62 |
#[repr(transparent)] |
| 63 |
pub struct Cell<T: ?Sized> { |
| 64 |
value: UnsafeCell<T>, |
| 65 |
// { dg-warning "field is never read" "" { target *-*-* } .-1 } |
| 66 |
} |
| 67 |
|
| 68 |
impl<T: Copy> Cell<T> { |
| 69 |
/// Returns a copy of the contained value. |
| 70 |
/// |
| 71 |
/// # Examples |
| 72 |
/// |
| 73 |
/// |
| 74 |
/// use std::cell::Cell; |
| 75 |
/// |
| 76 |
/// let c = Cell::new(5); |
| 77 |
/// |
| 78 |
/// let five = c.get(); |
| 79 |
/// |
| 80 |
#[inline] |
| 81 |
#[stable(feature = "rust1", since = "1.0.0")] |
| 82 |
pub fn get(&self) -> T { |
| 83 |
// SAFETY: This can cause data races if called from a separate thread, |
| 84 |
// but `Cell` is `!Sync` so this won't happen. |
| 85 |
unsafe { *self.value.get() } |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
#[lang = "sized"] |
| 90 |
trait Sized {} |
| 91 |
|
| 92 |
#[lang = "deref"] |
| 93 |
pub trait Deref { |
| 94 |
/// The resulting type after dereferencing. |
| 95 |
#[stable(feature = "rust1", since = "1.0.0")] |
| 96 |
// #[rustc_diagnostic_item = "deref_target"] |
| 97 |
type Target: ?Sized; |
| 98 |
|
| 99 |
/// Dereferences the value. |
| 100 |
#[must_use] |
| 101 |
#[stable(feature = "rust1", since = "1.0.0")] |
| 102 |
// #[rustc_diagnostic_item = "deref_method"] |
| 103 |
fn deref(&self) -> &Self::Target; |
| 104 |
} |
| 105 |
|
| 106 |
impl<T: ?Sized> Deref for &T { |
| 107 |
type Target = T; |
| 108 |
|
| 109 |
fn deref(&self) -> &T { |
| 110 |
*self |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
// this is added because of #3030 |
| 115 |
extern "C" { |
| 116 |
fn never() -> !; |
| 117 |
} |
| 118 |
|
| 119 |
impl<T: ?Sized> !DerefMut for &T { |
| 120 |
fn deref_mut(&mut self) -> &mut T { |
| 121 |
unsafe { never() } |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
impl<T: ?Sized> Deref for &mut T { |
| 126 |
type Target = T; |
| 127 |
|
| 128 |
fn deref(&self) -> &T { |
| 129 |
*self |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
#[lang = "deref_mut"] |
| 134 |
pub trait DerefMut: Deref { |
| 135 |
/// Mutably dereferences the value. |
| 136 |
#[stable(feature = "rust1", since = "1.0.0")] |
| 137 |
fn deref_mut(&mut self) -> &mut Self::Target; |
| 138 |
} |
| 139 |
|
| 140 |
#[inline] |
| 141 |
pub fn new<'b>(borrow: &'b Cell<i32>) { |
| 142 |
let b = borrow.get(); |
| 143 |
// { dg-warning "unused name" "" { target *-*-* } .-1 } |
| 144 |
} |