1 | //! Primitive traits and types representing basic properties of types. |
2 | //! |
3 | //! Rust types can be classified in various useful ways according to |
4 | //! their intrinsic properties. These classifications are represented |
5 | //! as traits. |
6 | |
7 | #![stable (feature = "rust1" , since = "1.0.0" )] |
8 | |
9 | use crate::cell::UnsafeCell; |
10 | use crate::cmp; |
11 | use crate::fmt::Debug; |
12 | use crate::hash::Hash; |
13 | use crate::hash::Hasher; |
14 | |
15 | /// Implements a given marker trait for multiple types at the same time. |
16 | /// |
17 | /// The basic syntax looks like this: |
18 | /// ```ignore private macro |
19 | /// marker_impls! { MarkerTrait for u8, i8 } |
20 | /// ``` |
21 | /// You can also implement `unsafe` traits |
22 | /// ```ignore private macro |
23 | /// marker_impls! { unsafe MarkerTrait for u8, i8 } |
24 | /// ``` |
25 | /// Add attributes to all impls: |
26 | /// ```ignore private macro |
27 | /// marker_impls! { |
28 | /// #[allow(lint)] |
29 | /// #[unstable(feature = "marker_trait" , issue = "none" )] |
30 | /// MarkerTrait for u8, i8 |
31 | /// } |
32 | /// ``` |
33 | /// And use generics: |
34 | /// ```ignore private macro |
35 | /// marker_impls! { |
36 | /// MarkerTrait for |
37 | /// u8, i8, |
38 | /// {T: ?Sized} *const T, |
39 | /// {T: ?Sized} *mut T, |
40 | /// {T: MarkerTrait} PhantomData<T>, |
41 | /// u32, |
42 | /// } |
43 | /// ``` |
44 | #[unstable (feature = "internal_impls_macro" , issue = "none" )] |
45 | macro marker_impls { |
46 | ( $(#[$($meta:tt)*])* $Trait:ident for $({$($bounds:tt)*})? $T:ty $(, $($rest:tt)*)? ) => { |
47 | $(#[$($meta)*])* impl< $($($bounds)*)? > $Trait for $T {} |
48 | marker_impls! { $(#[$($meta)*])* $Trait for $($($rest)*)? } |
49 | }, |
50 | ( $(#[$($meta:tt)*])* $Trait:ident for ) => {}, |
51 | |
52 | ( $(#[$($meta:tt)*])* unsafe $Trait:ident for $({$($bounds:tt)*})? $T:ty $(, $($rest:tt)*)? ) => { |
53 | $(#[$($meta)*])* unsafe impl< $($($bounds)*)? > $Trait for $T {} |
54 | marker_impls! { $(#[$($meta)*])* unsafe $Trait for $($($rest)*)? } |
55 | }, |
56 | ( $(#[$($meta:tt)*])* unsafe $Trait:ident for ) => {}, |
57 | } |
58 | |
59 | /// Types that can be transferred across thread boundaries. |
60 | /// |
61 | /// This trait is automatically implemented when the compiler determines it's |
62 | /// appropriate. |
63 | /// |
64 | /// An example of a non-`Send` type is the reference-counting pointer |
65 | /// [`rc::Rc`][`Rc`]. If two threads attempt to clone [`Rc`]s that point to the same |
66 | /// reference-counted value, they might try to update the reference count at the |
67 | /// same time, which is [undefined behavior][ub] because [`Rc`] doesn't use atomic |
68 | /// operations. Its cousin [`sync::Arc`][arc] does use atomic operations (incurring |
69 | /// some overhead) and thus is `Send`. |
70 | /// |
71 | /// See [the Nomicon](../../nomicon/send-and-sync.html) and the [`Sync`] trait for more details. |
72 | /// |
73 | /// [`Rc`]: ../../std/rc/struct.Rc.html |
74 | /// [arc]: ../../std/sync/struct.Arc.html |
75 | /// [ub]: ../../reference/behavior-considered-undefined.html |
76 | #[stable (feature = "rust1" , since = "1.0.0" )] |
77 | #[cfg_attr (not(test), rustc_diagnostic_item = "Send" )] |
78 | #[diagnostic::on_unimplemented( |
79 | message = "`{Self}` cannot be sent between threads safely" , |
80 | label = "`{Self}` cannot be sent between threads safely" |
81 | )] |
82 | pub unsafe auto trait Send { |
83 | // empty. |
84 | } |
85 | |
86 | #[stable (feature = "rust1" , since = "1.0.0" )] |
87 | impl<T: ?Sized> !Send for *const T {} |
88 | #[stable (feature = "rust1" , since = "1.0.0" )] |
89 | impl<T: ?Sized> !Send for *mut T {} |
90 | |
91 | // Most instances arise automatically, but this instance is needed to link up `T: Sync` with |
92 | // `&T: Send` (and it also removes the unsound default instance `T Send` -> `&T: Send` that would |
93 | // otherwise exist). |
94 | #[stable (feature = "rust1" , since = "1.0.0" )] |
95 | unsafe impl<T: Sync + ?Sized> Send for &T {} |
96 | |
97 | /// Types with a constant size known at compile time. |
98 | /// |
99 | /// All type parameters have an implicit bound of `Sized`. The special syntax |
100 | /// `?Sized` can be used to remove this bound if it's not appropriate. |
101 | /// |
102 | /// ``` |
103 | /// # #![allow (dead_code)] |
104 | /// struct Foo<T>(T); |
105 | /// struct Bar<T: ?Sized>(T); |
106 | /// |
107 | /// // struct FooUse(Foo<[i32]>); // error: Sized is not implemented for [i32] |
108 | /// struct BarUse(Bar<[i32]>); // OK |
109 | /// ``` |
110 | /// |
111 | /// The one exception is the implicit `Self` type of a trait. A trait does not |
112 | /// have an implicit `Sized` bound as this is incompatible with [trait object]s |
113 | /// where, by definition, the trait needs to work with all possible implementors, |
114 | /// and thus could be any size. |
115 | /// |
116 | /// Although Rust will let you bind `Sized` to a trait, you won't |
117 | /// be able to use it to form a trait object later: |
118 | /// |
119 | /// ``` |
120 | /// # #![allow(unused_variables)] |
121 | /// trait Foo { } |
122 | /// trait Bar: Sized { } |
123 | /// |
124 | /// struct Impl; |
125 | /// impl Foo for Impl { } |
126 | /// impl Bar for Impl { } |
127 | /// |
128 | /// let x: &dyn Foo = &Impl; // OK |
129 | /// // let y: &dyn Bar = &Impl; // error: the trait `Bar` cannot |
130 | /// // be made into an object |
131 | /// ``` |
132 | /// |
133 | /// [trait object]: ../../book/ch17-02-trait-objects.html |
134 | #[doc (alias = "?" , alias = "?Sized" )] |
135 | #[stable (feature = "rust1" , since = "1.0.0" )] |
136 | #[lang = "sized" ] |
137 | #[diagnostic::on_unimplemented( |
138 | message = "the size for values of type `{Self}` cannot be known at compilation time" , |
139 | label = "doesn't have a size known at compile-time" |
140 | )] |
141 | #[fundamental ] // for Default, for example, which requires that `[T]: !Default` be evaluatable |
142 | #[rustc_specialization_trait ] |
143 | #[rustc_deny_explicit_impl (implement_via_object = false)] |
144 | #[rustc_coinductive ] |
145 | pub trait Sized { |
146 | // Empty. |
147 | } |
148 | |
149 | /// Types that can be "unsized" to a dynamically-sized type. |
150 | /// |
151 | /// For example, the sized array type `[i8; 2]` implements `Unsize<[i8]>` and |
152 | /// `Unsize<dyn fmt::Debug>`. |
153 | /// |
154 | /// All implementations of `Unsize` are provided automatically by the compiler. |
155 | /// Those implementations are: |
156 | /// |
157 | /// - Arrays `[T; N]` implement `Unsize<[T]>`. |
158 | /// - A type implements `Unsize<dyn Trait + 'a>` if all of these conditions are met: |
159 | /// - The type implements `Trait`. |
160 | /// - `Trait` is object safe. |
161 | /// - The type is sized. |
162 | /// - The type outlives `'a`. |
163 | /// - Structs `Foo<..., T1, ..., Tn, ...>` implement `Unsize<Foo<..., U1, ..., Un, ...>>` |
164 | /// where any number of (type and const) parameters may be changed if all of these conditions |
165 | /// are met: |
166 | /// - Only the last field of `Foo` has a type involving the parameters `T1`, ..., `Tn`. |
167 | /// - All other parameters of the struct are equal. |
168 | /// - `Field<T1, ..., Tn>: Unsize<Field<U1, ..., Un>>`, where `Field<...>` stands for the actual |
169 | /// type of the struct's last field. |
170 | /// |
171 | /// `Unsize` is used along with [`ops::CoerceUnsized`] to allow |
172 | /// "user-defined" containers such as [`Rc`] to contain dynamically-sized |
173 | /// types. See the [DST coercion RFC][RFC982] and [the nomicon entry on coercion][nomicon-coerce] |
174 | /// for more details. |
175 | /// |
176 | /// [`ops::CoerceUnsized`]: crate::ops::CoerceUnsized |
177 | /// [`Rc`]: ../../std/rc/struct.Rc.html |
178 | /// [RFC982]: https://github.com/rust-lang/rfcs/blob/master/text/0982-dst-coercion.md |
179 | /// [nomicon-coerce]: ../../nomicon/coercions.html |
180 | #[unstable (feature = "unsize" , issue = "18598" )] |
181 | #[lang = "unsize" ] |
182 | #[rustc_deny_explicit_impl (implement_via_object = false)] |
183 | pub trait Unsize<T: ?Sized> { |
184 | // Empty. |
185 | } |
186 | |
187 | /// Required trait for constants used in pattern matches. |
188 | /// |
189 | /// Any type that derives `PartialEq` automatically implements this trait, |
190 | /// *regardless* of whether its type-parameters implement `PartialEq`. |
191 | /// |
192 | /// If a `const` item contains some type that does not implement this trait, |
193 | /// then that type either (1.) does not implement `PartialEq` (which means the |
194 | /// constant will not provide that comparison method, which code generation |
195 | /// assumes is available), or (2.) it implements *its own* version of |
196 | /// `PartialEq` (which we assume does not conform to a structural-equality |
197 | /// comparison). |
198 | /// |
199 | /// In either of the two scenarios above, we reject usage of such a constant in |
200 | /// a pattern match. |
201 | /// |
202 | /// See also the [structural match RFC][RFC1445], and [issue 63438] which |
203 | /// motivated migrating from an attribute-based design to this trait. |
204 | /// |
205 | /// [RFC1445]: https://github.com/rust-lang/rfcs/blob/master/text/1445-restrict-constants-in-patterns.md |
206 | /// [issue 63438]: https://github.com/rust-lang/rust/issues/63438 |
207 | #[unstable (feature = "structural_match" , issue = "31434" )] |
208 | #[diagnostic::on_unimplemented(message = "the type `{Self}` does not `#[derive(PartialEq)]`" )] |
209 | #[lang = "structural_peq" ] |
210 | pub trait StructuralPartialEq { |
211 | // Empty. |
212 | } |
213 | |
214 | marker_impls! { |
215 | #[unstable(feature = "structural_match" , issue = "31434" )] |
216 | StructuralPartialEq for |
217 | usize, u8, u16, u32, u64, u128, |
218 | isize, i8, i16, i32, i64, i128, |
219 | bool, |
220 | char, |
221 | str /* Technically requires `[u8]: StructuralPartialEq` */, |
222 | (), |
223 | {T, const N: usize} [T; N], |
224 | {T} [T], |
225 | {T: ?Sized} &T, |
226 | } |
227 | |
228 | /// Types whose values can be duplicated simply by copying bits. |
229 | /// |
230 | /// By default, variable bindings have 'move semantics.' In other |
231 | /// words: |
232 | /// |
233 | /// ``` |
234 | /// #[derive(Debug)] |
235 | /// struct Foo; |
236 | /// |
237 | /// let x = Foo; |
238 | /// |
239 | /// let y = x; |
240 | /// |
241 | /// // `x` has moved into `y`, and so cannot be used |
242 | /// |
243 | /// // println!("{x:?}"); // error: use of moved value |
244 | /// ``` |
245 | /// |
246 | /// However, if a type implements `Copy`, it instead has 'copy semantics': |
247 | /// |
248 | /// ``` |
249 | /// // We can derive a `Copy` implementation. `Clone` is also required, as it's |
250 | /// // a supertrait of `Copy`. |
251 | /// #[derive(Debug, Copy, Clone)] |
252 | /// struct Foo; |
253 | /// |
254 | /// let x = Foo; |
255 | /// |
256 | /// let y = x; |
257 | /// |
258 | /// // `y` is a copy of `x` |
259 | /// |
260 | /// println!("{x:?}" ); // A-OK! |
261 | /// ``` |
262 | /// |
263 | /// It's important to note that in these two examples, the only difference is whether you |
264 | /// are allowed to access `x` after the assignment. Under the hood, both a copy and a move |
265 | /// can result in bits being copied in memory, although this is sometimes optimized away. |
266 | /// |
267 | /// ## How can I implement `Copy`? |
268 | /// |
269 | /// There are two ways to implement `Copy` on your type. The simplest is to use `derive`: |
270 | /// |
271 | /// ``` |
272 | /// #[derive(Copy, Clone)] |
273 | /// struct MyStruct; |
274 | /// ``` |
275 | /// |
276 | /// You can also implement `Copy` and `Clone` manually: |
277 | /// |
278 | /// ``` |
279 | /// struct MyStruct; |
280 | /// |
281 | /// impl Copy for MyStruct { } |
282 | /// |
283 | /// impl Clone for MyStruct { |
284 | /// fn clone(&self) -> MyStruct { |
285 | /// *self |
286 | /// } |
287 | /// } |
288 | /// ``` |
289 | /// |
290 | /// There is a small difference between the two: the `derive` strategy will also place a `Copy` |
291 | /// bound on type parameters, which isn't always desired. |
292 | /// |
293 | /// ## What's the difference between `Copy` and `Clone`? |
294 | /// |
295 | /// Copies happen implicitly, for example as part of an assignment `y = x`. The behavior of |
296 | /// `Copy` is not overloadable; it is always a simple bit-wise copy. |
297 | /// |
298 | /// Cloning is an explicit action, `x.clone()`. The implementation of [`Clone`] can |
299 | /// provide any type-specific behavior necessary to duplicate values safely. For example, |
300 | /// the implementation of [`Clone`] for [`String`] needs to copy the pointed-to string |
301 | /// buffer in the heap. A simple bitwise copy of [`String`] values would merely copy the |
302 | /// pointer, leading to a double free down the line. For this reason, [`String`] is [`Clone`] |
303 | /// but not `Copy`. |
304 | /// |
305 | /// [`Clone`] is a supertrait of `Copy`, so everything which is `Copy` must also implement |
306 | /// [`Clone`]. If a type is `Copy` then its [`Clone`] implementation only needs to return `*self` |
307 | /// (see the example above). |
308 | /// |
309 | /// ## When can my type be `Copy`? |
310 | /// |
311 | /// A type can implement `Copy` if all of its components implement `Copy`. For example, this |
312 | /// struct can be `Copy`: |
313 | /// |
314 | /// ``` |
315 | /// # #[allow (dead_code)] |
316 | /// #[derive(Copy, Clone)] |
317 | /// struct Point { |
318 | /// x: i32, |
319 | /// y: i32, |
320 | /// } |
321 | /// ``` |
322 | /// |
323 | /// A struct can be `Copy`, and [`i32`] is `Copy`, therefore `Point` is eligible to be `Copy`. |
324 | /// By contrast, consider |
325 | /// |
326 | /// ``` |
327 | /// # #![allow(dead_code)] |
328 | /// # struct Point; |
329 | /// struct PointList { |
330 | /// points: Vec<Point>, |
331 | /// } |
332 | /// ``` |
333 | /// |
334 | /// The struct `PointList` cannot implement `Copy`, because [`Vec<T>`] is not `Copy`. If we |
335 | /// attempt to derive a `Copy` implementation, we'll get an error: |
336 | /// |
337 | /// ```text |
338 | /// the trait `Copy` cannot be implemented for this type; field `points` does not implement `Copy` |
339 | /// ``` |
340 | /// |
341 | /// Shared references (`&T`) are also `Copy`, so a type can be `Copy`, even when it holds |
342 | /// shared references of types `T` that are *not* `Copy`. Consider the following struct, |
343 | /// which can implement `Copy`, because it only holds a *shared reference* to our non-`Copy` |
344 | /// type `PointList` from above: |
345 | /// |
346 | /// ``` |
347 | /// # #![allow(dead_code)] |
348 | /// # struct PointList; |
349 | /// #[derive(Copy, Clone)] |
350 | /// struct PointListWrapper<'a> { |
351 | /// point_list_ref: &'a PointList, |
352 | /// } |
353 | /// ``` |
354 | /// |
355 | /// ## When *can't* my type be `Copy`? |
356 | /// |
357 | /// Some types can't be copied safely. For example, copying `&mut T` would create an aliased |
358 | /// mutable reference. Copying [`String`] would duplicate responsibility for managing the |
359 | /// [`String`]'s buffer, leading to a double free. |
360 | /// |
361 | /// Generalizing the latter case, any type implementing [`Drop`] can't be `Copy`, because it's |
362 | /// managing some resource besides its own [`size_of::<T>`] bytes. |
363 | /// |
364 | /// If you try to implement `Copy` on a struct or enum containing non-`Copy` data, you will get |
365 | /// the error [E0204]. |
366 | /// |
367 | /// [E0204]: ../../error_codes/E0204.html |
368 | /// |
369 | /// ## When *should* my type be `Copy`? |
370 | /// |
371 | /// Generally speaking, if your type _can_ implement `Copy`, it should. Keep in mind, though, |
372 | /// that implementing `Copy` is part of the public API of your type. If the type might become |
373 | /// non-`Copy` in the future, it could be prudent to omit the `Copy` implementation now, to |
374 | /// avoid a breaking API change. |
375 | /// |
376 | /// ## Additional implementors |
377 | /// |
378 | /// In addition to the [implementors listed below][impls], |
379 | /// the following types also implement `Copy`: |
380 | /// |
381 | /// * Function item types (i.e., the distinct types defined for each function) |
382 | /// * Function pointer types (e.g., `fn() -> i32`) |
383 | /// * Closure types, if they capture no value from the environment |
384 | /// or if all such captured values implement `Copy` themselves. |
385 | /// Note that variables captured by shared reference always implement `Copy` |
386 | /// (even if the referent doesn't), |
387 | /// while variables captured by mutable reference never implement `Copy`. |
388 | /// |
389 | /// [`Vec<T>`]: ../../std/vec/struct.Vec.html |
390 | /// [`String`]: ../../std/string/struct.String.html |
391 | /// [`size_of::<T>`]: crate::mem::size_of |
392 | /// [impls]: #implementors |
393 | #[stable (feature = "rust1" , since = "1.0.0" )] |
394 | #[lang = "copy" ] |
395 | // FIXME(matthewjasper) This allows copying a type that doesn't implement |
396 | // `Copy` because of unsatisfied lifetime bounds (copying `A<'_>` when only |
397 | // `A<'static>: Copy` and `A<'_>: Clone`). |
398 | // We have this attribute here for now only because there are quite a few |
399 | // existing specializations on `Copy` that already exist in the standard |
400 | // library, and there's no way to safely have this behavior right now. |
401 | #[rustc_unsafe_specialization_marker ] |
402 | #[rustc_diagnostic_item = "Copy" ] |
403 | pub trait Copy: Clone { |
404 | // Empty. |
405 | } |
406 | |
407 | /// Derive macro generating an impl of the trait `Copy`. |
408 | #[rustc_builtin_macro ] |
409 | #[stable (feature = "builtin_macro_prelude" , since = "1.38.0" )] |
410 | #[allow_internal_unstable (core_intrinsics, derive_clone_copy)] |
411 | pub macro Copy($item:item) { |
412 | /* compiler built-in */ |
413 | } |
414 | |
415 | // Implementations of `Copy` for primitive types. |
416 | // |
417 | // Implementations that cannot be described in Rust |
418 | // are implemented in `traits::SelectionContext::copy_clone_conditions()` |
419 | // in `rustc_trait_selection`. |
420 | marker_impls! { |
421 | #[stable(feature = "rust1" , since = "1.0.0" )] |
422 | Copy for |
423 | usize, u8, u16, u32, u64, u128, |
424 | isize, i8, i16, i32, i64, i128, |
425 | f16, f32, f64, f128, |
426 | bool, char, |
427 | {T: ?Sized} *const T, |
428 | {T: ?Sized} *mut T, |
429 | |
430 | } |
431 | |
432 | #[unstable (feature = "never_type" , issue = "35121" )] |
433 | impl Copy for ! {} |
434 | |
435 | /// Shared references can be copied, but mutable references *cannot*! |
436 | #[stable (feature = "rust1" , since = "1.0.0" )] |
437 | impl<T: ?Sized> Copy for &T {} |
438 | |
439 | /// Types for which it is safe to share references between threads. |
440 | /// |
441 | /// This trait is automatically implemented when the compiler determines |
442 | /// it's appropriate. |
443 | /// |
444 | /// The precise definition is: a type `T` is [`Sync`] if and only if `&T` is |
445 | /// [`Send`]. In other words, if there is no possibility of |
446 | /// [undefined behavior][ub] (including data races) when passing |
447 | /// `&T` references between threads. |
448 | /// |
449 | /// As one would expect, primitive types like [`u8`] and [`f64`] |
450 | /// are all [`Sync`], and so are simple aggregate types containing them, |
451 | /// like tuples, structs and enums. More examples of basic [`Sync`] |
452 | /// types include "immutable" types like `&T`, and those with simple |
453 | /// inherited mutability, such as [`Box<T>`][box], [`Vec<T>`][vec] and |
454 | /// most other collection types. (Generic parameters need to be [`Sync`] |
455 | /// for their container to be [`Sync`].) |
456 | /// |
457 | /// A somewhat surprising consequence of the definition is that `&mut T` |
458 | /// is `Sync` (if `T` is `Sync`) even though it seems like that might |
459 | /// provide unsynchronized mutation. The trick is that a mutable |
460 | /// reference behind a shared reference (that is, `& &mut T`) |
461 | /// becomes read-only, as if it were a `& &T`. Hence there is no risk |
462 | /// of a data race. |
463 | /// |
464 | /// A shorter overview of how [`Sync`] and [`Send`] relate to referencing: |
465 | /// * `&T` is [`Send`] if and only if `T` is [`Sync`] |
466 | /// * `&mut T` is [`Send`] if and only if `T` is [`Send`] |
467 | /// * `&T` and `&mut T` are [`Sync`] if and only if `T` is [`Sync`] |
468 | /// |
469 | /// Types that are not `Sync` are those that have "interior |
470 | /// mutability" in a non-thread-safe form, such as [`Cell`][cell] |
471 | /// and [`RefCell`][refcell]. These types allow for mutation of |
472 | /// their contents even through an immutable, shared reference. For |
473 | /// example the `set` method on [`Cell<T>`][cell] takes `&self`, so it requires |
474 | /// only a shared reference [`&Cell<T>`][cell]. The method performs no |
475 | /// synchronization, thus [`Cell`][cell] cannot be `Sync`. |
476 | /// |
477 | /// Another example of a non-`Sync` type is the reference-counting |
478 | /// pointer [`Rc`][rc]. Given any reference [`&Rc<T>`][rc], you can clone |
479 | /// a new [`Rc<T>`][rc], modifying the reference counts in a non-atomic way. |
480 | /// |
481 | /// For cases when one does need thread-safe interior mutability, |
482 | /// Rust provides [atomic data types], as well as explicit locking via |
483 | /// [`sync::Mutex`][mutex] and [`sync::RwLock`][rwlock]. These types |
484 | /// ensure that any mutation cannot cause data races, hence the types |
485 | /// are `Sync`. Likewise, [`sync::Arc`][arc] provides a thread-safe |
486 | /// analogue of [`Rc`][rc]. |
487 | /// |
488 | /// Any types with interior mutability must also use the |
489 | /// [`cell::UnsafeCell`][unsafecell] wrapper around the value(s) which |
490 | /// can be mutated through a shared reference. Failing to doing this is |
491 | /// [undefined behavior][ub]. For example, [`transmute`][transmute]-ing |
492 | /// from `&T` to `&mut T` is invalid. |
493 | /// |
494 | /// See [the Nomicon][nomicon-send-and-sync] for more details about `Sync`. |
495 | /// |
496 | /// [box]: ../../std/boxed/struct.Box.html |
497 | /// [vec]: ../../std/vec/struct.Vec.html |
498 | /// [cell]: crate::cell::Cell |
499 | /// [refcell]: crate::cell::RefCell |
500 | /// [rc]: ../../std/rc/struct.Rc.html |
501 | /// [arc]: ../../std/sync/struct.Arc.html |
502 | /// [atomic data types]: crate::sync::atomic |
503 | /// [mutex]: ../../std/sync/struct.Mutex.html |
504 | /// [rwlock]: ../../std/sync/struct.RwLock.html |
505 | /// [unsafecell]: crate::cell::UnsafeCell |
506 | /// [ub]: ../../reference/behavior-considered-undefined.html |
507 | /// [transmute]: crate::mem::transmute |
508 | /// [nomicon-send-and-sync]: ../../nomicon/send-and-sync.html |
509 | #[stable (feature = "rust1" , since = "1.0.0" )] |
510 | #[cfg_attr (not(test), rustc_diagnostic_item = "Sync" )] |
511 | #[lang = "sync" ] |
512 | #[rustc_on_unimplemented ( |
513 | on( |
514 | _Self = "core::cell::once::OnceCell<T>" , |
515 | note = "if you want to do aliasing and mutation between multiple threads, use `std::sync::OnceLock` instead" |
516 | ), |
517 | on( |
518 | _Self = "core::cell::Cell<u8>" , |
519 | note = "if you want to do aliasing and mutation between multiple threads, use `std::sync::RwLock` or `std::sync::atomic::AtomicU8` instead" , |
520 | ), |
521 | on( |
522 | _Self = "core::cell::Cell<u16>" , |
523 | note = "if you want to do aliasing and mutation between multiple threads, use `std::sync::RwLock` or `std::sync::atomic::AtomicU16` instead" , |
524 | ), |
525 | on( |
526 | _Self = "core::cell::Cell<u32>" , |
527 | note = "if you want to do aliasing and mutation between multiple threads, use `std::sync::RwLock` or `std::sync::atomic::AtomicU32` instead" , |
528 | ), |
529 | on( |
530 | _Self = "core::cell::Cell<u64>" , |
531 | note = "if you want to do aliasing and mutation between multiple threads, use `std::sync::RwLock` or `std::sync::atomic::AtomicU64` instead" , |
532 | ), |
533 | on( |
534 | _Self = "core::cell::Cell<usize>" , |
535 | note = "if you want to do aliasing and mutation between multiple threads, use `std::sync::RwLock` or `std::sync::atomic::AtomicUsize` instead" , |
536 | ), |
537 | on( |
538 | _Self = "core::cell::Cell<i8>" , |
539 | note = "if you want to do aliasing and mutation between multiple threads, use `std::sync::RwLock` or `std::sync::atomic::AtomicI8` instead" , |
540 | ), |
541 | on( |
542 | _Self = "core::cell::Cell<i16>" , |
543 | note = "if you want to do aliasing and mutation between multiple threads, use `std::sync::RwLock` or `std::sync::atomic::AtomicI16` instead" , |
544 | ), |
545 | on( |
546 | _Self = "core::cell::Cell<i32>" , |
547 | note = "if you want to do aliasing and mutation between multiple threads, use `std::sync::RwLock` or `std::sync::atomic::AtomicI32` instead" , |
548 | ), |
549 | on( |
550 | _Self = "core::cell::Cell<i64>" , |
551 | note = "if you want to do aliasing and mutation between multiple threads, use `std::sync::RwLock` or `std::sync::atomic::AtomicI64` instead" , |
552 | ), |
553 | on( |
554 | _Self = "core::cell::Cell<isize>" , |
555 | note = "if you want to do aliasing and mutation between multiple threads, use `std::sync::RwLock` or `std::sync::atomic::AtomicIsize` instead" , |
556 | ), |
557 | on( |
558 | _Self = "core::cell::Cell<bool>" , |
559 | note = "if you want to do aliasing and mutation between multiple threads, use `std::sync::RwLock` or `std::sync::atomic::AtomicBool` instead" , |
560 | ), |
561 | on( |
562 | all( |
563 | _Self = "core::cell::Cell<T>" , |
564 | not(_Self = "core::cell::Cell<u8>" ), |
565 | not(_Self = "core::cell::Cell<u16>" ), |
566 | not(_Self = "core::cell::Cell<u32>" ), |
567 | not(_Self = "core::cell::Cell<u64>" ), |
568 | not(_Self = "core::cell::Cell<usize>" ), |
569 | not(_Self = "core::cell::Cell<i8>" ), |
570 | not(_Self = "core::cell::Cell<i16>" ), |
571 | not(_Self = "core::cell::Cell<i32>" ), |
572 | not(_Self = "core::cell::Cell<i64>" ), |
573 | not(_Self = "core::cell::Cell<isize>" ), |
574 | not(_Self = "core::cell::Cell<bool>" ) |
575 | ), |
576 | note = "if you want to do aliasing and mutation between multiple threads, use `std::sync::RwLock`" , |
577 | ), |
578 | on( |
579 | _Self = "core::cell::RefCell<T>" , |
580 | note = "if you want to do aliasing and mutation between multiple threads, use `std::sync::RwLock` instead" , |
581 | ), |
582 | message = "`{Self}` cannot be shared between threads safely" , |
583 | label = "`{Self}` cannot be shared between threads safely" |
584 | )] |
585 | pub unsafe auto trait Sync { |
586 | // FIXME(estebank): once support to add notes in `rustc_on_unimplemented` |
587 | // lands in beta, and it has been extended to check whether a closure is |
588 | // anywhere in the requirement chain, extend it as such (#48534): |
589 | // ``` |
590 | // on( |
591 | // closure, |
592 | // note="`{Self}` cannot be shared safely, consider marking the closure `move`" |
593 | // ), |
594 | // ``` |
595 | |
596 | // Empty |
597 | } |
598 | |
599 | #[stable (feature = "rust1" , since = "1.0.0" )] |
600 | impl<T: ?Sized> !Sync for *const T {} |
601 | #[stable (feature = "rust1" , since = "1.0.0" )] |
602 | impl<T: ?Sized> !Sync for *mut T {} |
603 | |
604 | /// Zero-sized type used to mark things that "act like" they own a `T`. |
605 | /// |
606 | /// Adding a `PhantomData<T>` field to your type tells the compiler that your |
607 | /// type acts as though it stores a value of type `T`, even though it doesn't |
608 | /// really. This information is used when computing certain safety properties. |
609 | /// |
610 | /// For a more in-depth explanation of how to use `PhantomData<T>`, please see |
611 | /// [the Nomicon](../../nomicon/phantom-data.html). |
612 | /// |
613 | /// # A ghastly note 👻👻👻 |
614 | /// |
615 | /// Though they both have scary names, `PhantomData` and 'phantom types' are |
616 | /// related, but not identical. A phantom type parameter is simply a type |
617 | /// parameter which is never used. In Rust, this often causes the compiler to |
618 | /// complain, and the solution is to add a "dummy" use by way of `PhantomData`. |
619 | /// |
620 | /// # Examples |
621 | /// |
622 | /// ## Unused lifetime parameters |
623 | /// |
624 | /// Perhaps the most common use case for `PhantomData` is a struct that has an |
625 | /// unused lifetime parameter, typically as part of some unsafe code. For |
626 | /// example, here is a struct `Slice` that has two pointers of type `*const T`, |
627 | /// presumably pointing into an array somewhere: |
628 | /// |
629 | /// ```compile_fail,E0392 |
630 | /// struct Slice<'a, T> { |
631 | /// start: *const T, |
632 | /// end: *const T, |
633 | /// } |
634 | /// ``` |
635 | /// |
636 | /// The intention is that the underlying data is only valid for the |
637 | /// lifetime `'a`, so `Slice` should not outlive `'a`. However, this |
638 | /// intent is not expressed in the code, since there are no uses of |
639 | /// the lifetime `'a` and hence it is not clear what data it applies |
640 | /// to. We can correct this by telling the compiler to act *as if* the |
641 | /// `Slice` struct contained a reference `&'a T`: |
642 | /// |
643 | /// ``` |
644 | /// use std::marker::PhantomData; |
645 | /// |
646 | /// # #[allow (dead_code)] |
647 | /// struct Slice<'a, T> { |
648 | /// start: *const T, |
649 | /// end: *const T, |
650 | /// phantom: PhantomData<&'a T>, |
651 | /// } |
652 | /// ``` |
653 | /// |
654 | /// This also in turn infers the lifetime bound `T: 'a`, indicating |
655 | /// that any references in `T` are valid over the lifetime `'a`. |
656 | /// |
657 | /// When initializing a `Slice` you simply provide the value |
658 | /// `PhantomData` for the field `phantom`: |
659 | /// |
660 | /// ``` |
661 | /// # #![allow(dead_code)] |
662 | /// # use std::marker::PhantomData; |
663 | /// # struct Slice<'a, T> { |
664 | /// # start: *const T, |
665 | /// # end: *const T, |
666 | /// # phantom: PhantomData<&'a T>, |
667 | /// # } |
668 | /// fn borrow_vec<T>(vec: &Vec<T>) -> Slice<'_, T> { |
669 | /// let ptr = vec.as_ptr(); |
670 | /// Slice { |
671 | /// start: ptr, |
672 | /// end: unsafe { ptr.add(vec.len()) }, |
673 | /// phantom: PhantomData, |
674 | /// } |
675 | /// } |
676 | /// ``` |
677 | /// |
678 | /// ## Unused type parameters |
679 | /// |
680 | /// It sometimes happens that you have unused type parameters which |
681 | /// indicate what type of data a struct is "tied" to, even though that |
682 | /// data is not actually found in the struct itself. Here is an |
683 | /// example where this arises with [FFI]. The foreign interface uses |
684 | /// handles of type `*mut ()` to refer to Rust values of different |
685 | /// types. We track the Rust type using a phantom type parameter on |
686 | /// the struct `ExternalResource` which wraps a handle. |
687 | /// |
688 | /// [FFI]: ../../book/ch19-01-unsafe-rust.html#using-extern-functions-to-call-external-code |
689 | /// |
690 | /// ``` |
691 | /// # #![allow(dead_code)] |
692 | /// # trait ResType { } |
693 | /// # struct ParamType; |
694 | /// # mod foreign_lib { |
695 | /// # pub fn new(_: usize) -> *mut () { 42 as *mut () } |
696 | /// # pub fn do_stuff(_: *mut (), _: usize) {} |
697 | /// # } |
698 | /// # fn convert_params(_: ParamType) -> usize { 42 } |
699 | /// use std::marker::PhantomData; |
700 | /// use std::mem; |
701 | /// |
702 | /// struct ExternalResource<R> { |
703 | /// resource_handle: *mut (), |
704 | /// resource_type: PhantomData<R>, |
705 | /// } |
706 | /// |
707 | /// impl<R: ResType> ExternalResource<R> { |
708 | /// fn new() -> Self { |
709 | /// let size_of_res = mem::size_of::<R>(); |
710 | /// Self { |
711 | /// resource_handle: foreign_lib::new(size_of_res), |
712 | /// resource_type: PhantomData, |
713 | /// } |
714 | /// } |
715 | /// |
716 | /// fn do_stuff(&self, param: ParamType) { |
717 | /// let foreign_params = convert_params(param); |
718 | /// foreign_lib::do_stuff(self.resource_handle, foreign_params); |
719 | /// } |
720 | /// } |
721 | /// ``` |
722 | /// |
723 | /// ## Ownership and the drop check |
724 | /// |
725 | /// The exact interaction of `PhantomData` with drop check **may change in the future**. |
726 | /// |
727 | /// Currently, adding a field of type `PhantomData<T>` indicates that your type *owns* data of type |
728 | /// `T` in very rare circumstances. This in turn has effects on the Rust compiler's [drop check] |
729 | /// analysis. For the exact rules, see the [drop check] documentation. |
730 | /// |
731 | /// ## Layout |
732 | /// |
733 | /// For all `T`, the following are guaranteed: |
734 | /// * `size_of::<PhantomData<T>>() == 0` |
735 | /// * `align_of::<PhantomData<T>>() == 1` |
736 | /// |
737 | /// [drop check]: Drop#drop-check |
738 | #[lang = "phantom_data" ] |
739 | #[stable (feature = "rust1" , since = "1.0.0" )] |
740 | pub struct PhantomData<T: ?Sized>; |
741 | |
742 | #[stable (feature = "rust1" , since = "1.0.0" )] |
743 | impl<T: ?Sized> Hash for PhantomData<T> { |
744 | #[inline ] |
745 | fn hash<H: Hasher>(&self, _: &mut H) {} |
746 | } |
747 | |
748 | #[stable (feature = "rust1" , since = "1.0.0" )] |
749 | impl<T: ?Sized> cmp::PartialEq for PhantomData<T> { |
750 | fn eq(&self, _other: &PhantomData<T>) -> bool { |
751 | true |
752 | } |
753 | } |
754 | |
755 | #[stable (feature = "rust1" , since = "1.0.0" )] |
756 | impl<T: ?Sized> cmp::Eq for PhantomData<T> {} |
757 | |
758 | #[stable (feature = "rust1" , since = "1.0.0" )] |
759 | impl<T: ?Sized> cmp::PartialOrd for PhantomData<T> { |
760 | fn partial_cmp(&self, _other: &PhantomData<T>) -> Option<cmp::Ordering> { |
761 | Option::Some(cmp::Ordering::Equal) |
762 | } |
763 | } |
764 | |
765 | #[stable (feature = "rust1" , since = "1.0.0" )] |
766 | impl<T: ?Sized> cmp::Ord for PhantomData<T> { |
767 | fn cmp(&self, _other: &PhantomData<T>) -> cmp::Ordering { |
768 | cmp::Ordering::Equal |
769 | } |
770 | } |
771 | |
772 | #[stable (feature = "rust1" , since = "1.0.0" )] |
773 | impl<T: ?Sized> Copy for PhantomData<T> {} |
774 | |
775 | #[stable (feature = "rust1" , since = "1.0.0" )] |
776 | impl<T: ?Sized> Clone for PhantomData<T> { |
777 | fn clone(&self) -> Self { |
778 | Self |
779 | } |
780 | } |
781 | |
782 | #[stable (feature = "rust1" , since = "1.0.0" )] |
783 | impl<T: ?Sized> Default for PhantomData<T> { |
784 | fn default() -> Self { |
785 | Self |
786 | } |
787 | } |
788 | |
789 | #[unstable (feature = "structural_match" , issue = "31434" )] |
790 | impl<T: ?Sized> StructuralPartialEq for PhantomData<T> {} |
791 | |
792 | /// Compiler-internal trait used to indicate the type of enum discriminants. |
793 | /// |
794 | /// This trait is automatically implemented for every type and does not add any |
795 | /// guarantees to [`mem::Discriminant`]. It is **undefined behavior** to transmute |
796 | /// between `DiscriminantKind::Discriminant` and `mem::Discriminant`. |
797 | /// |
798 | /// [`mem::Discriminant`]: crate::mem::Discriminant |
799 | #[unstable ( |
800 | feature = "discriminant_kind" , |
801 | issue = "none" , |
802 | reason = "this trait is unlikely to ever be stabilized, use `mem::discriminant` instead" |
803 | )] |
804 | #[lang = "discriminant_kind" ] |
805 | #[rustc_deny_explicit_impl (implement_via_object = false)] |
806 | pub trait DiscriminantKind { |
807 | /// The type of the discriminant, which must satisfy the trait |
808 | /// bounds required by `mem::Discriminant`. |
809 | #[lang = "discriminant_type" ] |
810 | type Discriminant: Clone + Copy + Debug + Eq + PartialEq + Hash + Send + Sync + Unpin; |
811 | } |
812 | |
813 | /// Used to determine whether a type contains |
814 | /// any `UnsafeCell` internally, but not through an indirection. |
815 | /// This affects, for example, whether a `static` of that type is |
816 | /// placed in read-only static memory or writable static memory. |
817 | /// This can be used to declare that a constant with a generic type |
818 | /// will not contain interior mutability, and subsequently allow |
819 | /// placing the constant behind references. |
820 | /// |
821 | /// # Safety |
822 | /// |
823 | /// This trait is a core part of the language, it is just expressed as a trait in libcore for |
824 | /// convenience. Do *not* implement it for other types. |
825 | // FIXME: Eventually this trait should become `#[rustc_deny_explicit_impl]`. |
826 | // That requires porting the impls below to native internal impls. |
827 | #[lang = "freeze" ] |
828 | #[unstable (feature = "freeze" , issue = "121675" )] |
829 | pub unsafe auto trait Freeze {} |
830 | |
831 | #[unstable (feature = "freeze" , issue = "121675" )] |
832 | impl<T: ?Sized> !Freeze for UnsafeCell<T> {} |
833 | marker_impls! { |
834 | #[unstable(feature = "freeze" , issue = "121675" )] |
835 | unsafe Freeze for |
836 | {T: ?Sized} PhantomData<T>, |
837 | {T: ?Sized} *const T, |
838 | {T: ?Sized} *mut T, |
839 | {T: ?Sized} &T, |
840 | {T: ?Sized} &mut T, |
841 | } |
842 | |
843 | /// Types that do not require any pinning guarantees. |
844 | /// |
845 | /// For information on what "pinning" is, see the [`pin` module] documentation. |
846 | /// |
847 | /// Implementing the `Unpin` trait for `T` expresses the fact that `T` is pinning-agnostic: |
848 | /// it shall not expose nor rely on any pinning guarantees. This, in turn, means that a |
849 | /// `Pin`-wrapped pointer to such a type can feature a *fully unrestricted* API. |
850 | /// In other words, if `T: Unpin`, a value of type `T` will *not* be bound by the invariants |
851 | /// which pinning otherwise offers, even when "pinned" by a [`Pin<Ptr>`] pointing at it. |
852 | /// When a value of type `T` is pointed at by a [`Pin<Ptr>`], [`Pin`] will not restrict access |
853 | /// to the pointee value like it normally would, thus allowing the user to do anything that they |
854 | /// normally could with a non-[`Pin`]-wrapped `Ptr` to that value. |
855 | /// |
856 | /// The idea of this trait is to alleviate the reduced ergonomics of APIs that require the use |
857 | /// of [`Pin`] for soundness for some types, but which also want to be used by other types that |
858 | /// don't care about pinning. The prime example of such an API is [`Future::poll`]. There are many |
859 | /// [`Future`] types that don't care about pinning. These futures can implement `Unpin` and |
860 | /// therefore get around the pinning related restrictions in the API, while still allowing the |
861 | /// subset of [`Future`]s which *do* require pinning to be implemented soundly. |
862 | /// |
863 | /// For more discussion on the consequences of [`Unpin`] within the wider scope of the pinning |
864 | /// system, see the [section about `Unpin`] in the [`pin` module]. |
865 | /// |
866 | /// `Unpin` has no consequence at all for non-pinned data. In particular, [`mem::replace`] happily |
867 | /// moves `!Unpin` data, which would be immovable when pinned ([`mem::replace`] works for any |
868 | /// `&mut T`, not just when `T: Unpin`). |
869 | /// |
870 | /// *However*, you cannot use [`mem::replace`] on `!Unpin` data which is *pinned* by being wrapped |
871 | /// inside a [`Pin<Ptr>`] pointing at it. This is because you cannot (safely) use a |
872 | /// [`Pin<Ptr>`] to get an `&mut T` to its pointee value, which you would need to call |
873 | /// [`mem::replace`], and *that* is what makes this system work. |
874 | /// |
875 | /// So this, for example, can only be done on types implementing `Unpin`: |
876 | /// |
877 | /// ```rust |
878 | /// # #![allow (unused_must_use)] |
879 | /// use std::mem; |
880 | /// use std::pin::Pin; |
881 | /// |
882 | /// let mut string = "this" .to_string(); |
883 | /// let mut pinned_string = Pin::new(&mut string); |
884 | /// |
885 | /// // We need a mutable reference to call `mem::replace`. |
886 | /// // We can obtain such a reference by (implicitly) invoking `Pin::deref_mut`, |
887 | /// // but that is only possible because `String` implements `Unpin`. |
888 | /// mem::replace(&mut *pinned_string, "other" .to_string()); |
889 | /// ``` |
890 | /// |
891 | /// This trait is automatically implemented for almost every type. The compiler is free |
892 | /// to take the conservative stance of marking types as [`Unpin`] so long as all of the types that |
893 | /// compose its fields are also [`Unpin`]. This is because if a type implements [`Unpin`], then it |
894 | /// is unsound for that type's implementation to rely on pinning-related guarantees for soundness, |
895 | /// *even* when viewed through a "pinning" pointer! It is the responsibility of the implementor of |
896 | /// a type that relies upon pinning for soundness to ensure that type is *not* marked as [`Unpin`] |
897 | /// by adding [`PhantomPinned`] field. For more details, see the [`pin` module] docs. |
898 | /// |
899 | /// [`mem::replace`]: crate::mem::replace "mem replace" |
900 | /// [`Future`]: crate::future::Future "Future" |
901 | /// [`Future::poll`]: crate::future::Future::poll "Future poll" |
902 | /// [`Pin`]: crate::pin::Pin "Pin" |
903 | /// [`Pin<Ptr>`]: crate::pin::Pin "Pin" |
904 | /// [`pin` module]: crate::pin "pin module" |
905 | /// [section about `Unpin`]: crate::pin#unpin "pin module docs about unpin" |
906 | /// [`unsafe`]: ../../std/keyword.unsafe.html "keyword unsafe" |
907 | #[stable (feature = "pin" , since = "1.33.0" )] |
908 | #[diagnostic::on_unimplemented( |
909 | note = "consider using the `pin!` macro \nconsider using `Box::pin` if you need to access the pinned value outside of the current scope" , |
910 | message = "`{Self}` cannot be unpinned" |
911 | )] |
912 | #[lang = "unpin" ] |
913 | pub auto trait Unpin {} |
914 | |
915 | /// A marker type which does not implement `Unpin`. |
916 | /// |
917 | /// If a type contains a `PhantomPinned`, it will not implement `Unpin` by default. |
918 | #[stable (feature = "pin" , since = "1.33.0" )] |
919 | #[derive (Debug, Default, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)] |
920 | pub struct PhantomPinned; |
921 | |
922 | #[stable (feature = "pin" , since = "1.33.0" )] |
923 | impl !Unpin for PhantomPinned {} |
924 | |
925 | marker_impls! { |
926 | #[stable(feature = "pin" , since = "1.33.0" )] |
927 | Unpin for |
928 | {T: ?Sized} &T, |
929 | {T: ?Sized} &mut T, |
930 | } |
931 | |
932 | marker_impls! { |
933 | #[stable(feature = "pin_raw" , since = "1.38.0" )] |
934 | Unpin for |
935 | {T: ?Sized} *const T, |
936 | {T: ?Sized} *mut T, |
937 | } |
938 | |
939 | /// A marker for types that can be dropped. |
940 | /// |
941 | /// This should be used for `~const` bounds, |
942 | /// as non-const bounds will always hold for every type. |
943 | #[unstable (feature = "const_trait_impl" , issue = "67792" )] |
944 | #[lang = "destruct" ] |
945 | #[rustc_on_unimplemented (message = "can't drop `{Self}`" , append_const_msg)] |
946 | #[rustc_deny_explicit_impl (implement_via_object = false)] |
947 | #[const_trait ] |
948 | pub trait Destruct {} |
949 | |
950 | /// A marker for tuple types. |
951 | /// |
952 | /// The implementation of this trait is built-in and cannot be implemented |
953 | /// for any user type. |
954 | #[unstable (feature = "tuple_trait" , issue = "none" )] |
955 | #[lang = "tuple_trait" ] |
956 | #[diagnostic::on_unimplemented(message = "`{Self}` is not a tuple" )] |
957 | #[rustc_deny_explicit_impl (implement_via_object = false)] |
958 | pub trait Tuple {} |
959 | |
960 | /// A marker for pointer-like types. |
961 | /// |
962 | /// All types that have the same size and alignment as a `usize` or |
963 | /// `*const ()` automatically implement this trait. |
964 | #[unstable (feature = "pointer_like_trait" , issue = "none" )] |
965 | #[lang = "pointer_like" ] |
966 | #[diagnostic::on_unimplemented( |
967 | message = "`{Self}` needs to have the same ABI as a pointer" , |
968 | label = "`{Self}` needs to be a pointer-like type" |
969 | )] |
970 | pub trait PointerLike {} |
971 | |
972 | /// A marker for types which can be used as types of `const` generic parameters. |
973 | /// |
974 | /// These types must have a proper equivalence relation (`Eq`) and it must be automatically |
975 | /// derived (`StructuralPartialEq`). There's a hard-coded check in the compiler ensuring |
976 | /// that all fields are also `ConstParamTy`, which implies that recursively, all fields |
977 | /// are `StructuralPartialEq`. |
978 | #[lang = "const_param_ty" ] |
979 | #[unstable (feature = "adt_const_params" , issue = "95174" )] |
980 | #[diagnostic::on_unimplemented(message = "`{Self}` can't be used as a const parameter type" )] |
981 | #[allow (multiple_supertrait_upcastable)] |
982 | pub trait ConstParamTy: StructuralPartialEq + Eq {} |
983 | |
984 | /// Derive macro generating an impl of the trait `ConstParamTy`. |
985 | #[rustc_builtin_macro ] |
986 | #[unstable (feature = "adt_const_params" , issue = "95174" )] |
987 | pub macro ConstParamTy($item:item) { |
988 | /* compiler built-in */ |
989 | } |
990 | |
991 | // FIXME(adt_const_params): handle `ty::FnDef`/`ty::Closure` |
992 | marker_impls! { |
993 | #[unstable(feature = "adt_const_params" , issue = "95174" )] |
994 | ConstParamTy for |
995 | usize, u8, u16, u32, u64, u128, |
996 | isize, i8, i16, i32, i64, i128, |
997 | bool, |
998 | char, |
999 | str /* Technically requires `[u8]: ConstParamTy` */, |
1000 | {T: ConstParamTy, const N: usize} [T; N], |
1001 | {T: ConstParamTy} [T], |
1002 | {T: ?Sized + ConstParamTy} &T, |
1003 | } |
1004 | |
1005 | // FIXME(adt_const_params): Add to marker_impls call above once not in bootstrap |
1006 | #[unstable (feature = "adt_const_params" , issue = "95174" )] |
1007 | impl ConstParamTy for () {} |
1008 | |
1009 | /// A common trait implemented by all function pointers. |
1010 | #[unstable ( |
1011 | feature = "fn_ptr_trait" , |
1012 | issue = "none" , |
1013 | reason = "internal trait for implementing various traits for all function pointers" |
1014 | )] |
1015 | #[lang = "fn_ptr_trait" ] |
1016 | #[rustc_deny_explicit_impl (implement_via_object = false)] |
1017 | pub trait FnPtr: Copy + Clone { |
1018 | /// Returns the address of the function pointer. |
1019 | #[lang = "fn_ptr_addr" ] |
1020 | fn addr(self) -> *const (); |
1021 | } |
1022 | |