]> git.lizzy.rs Git - rust.git/blob - library/core/src/intrinsics.rs
Rollup merge of #82372 - RalfJung:unsafe-cell, r=KodrAus
[rust.git] / library / core / src / intrinsics.rs
1 //! Compiler intrinsics.
2 //!
3 //! The corresponding definitions are in `compiler/rustc_codegen_llvm/src/intrinsic.rs`.
4 //! The corresponding const implementations are in `compiler/rustc_mir/src/interpret/intrinsics.rs`
5 //!
6 //! # Const intrinsics
7 //!
8 //! Note: any changes to the constness of intrinsics should be discussed with the language team.
9 //! This includes changes in the stability of the constness.
10 //!
11 //! In order to make an intrinsic usable at compile-time, one needs to copy the implementation
12 //! from <https://github.com/rust-lang/miri/blob/master/src/shims/intrinsics.rs> to
13 //! `compiler/rustc_mir/src/interpret/intrinsics.rs` and add a
14 //! `#[rustc_const_unstable(feature = "foo", issue = "01234")]` to the intrinsic.
15 //!
16 //! If an intrinsic is supposed to be used from a `const fn` with a `rustc_const_stable` attribute,
17 //! the intrinsic's attribute must be `rustc_const_stable`, too. Such a change should not be done
18 //! without T-lang consultation, because it bakes a feature into the language that cannot be
19 //! replicated in user code without compiler support.
20 //!
21 //! # Volatiles
22 //!
23 //! The volatile intrinsics provide operations intended to act on I/O
24 //! memory, which are guaranteed to not be reordered by the compiler
25 //! across other volatile intrinsics. See the LLVM documentation on
26 //! [[volatile]].
27 //!
28 //! [volatile]: http://llvm.org/docs/LangRef.html#volatile-memory-accesses
29 //!
30 //! # Atomics
31 //!
32 //! The atomic intrinsics provide common atomic operations on machine
33 //! words, with multiple possible memory orderings. They obey the same
34 //! semantics as C++11. See the LLVM documentation on [[atomics]].
35 //!
36 //! [atomics]: http://llvm.org/docs/Atomics.html
37 //!
38 //! A quick refresher on memory ordering:
39 //!
40 //! * Acquire - a barrier for acquiring a lock. Subsequent reads and writes
41 //!   take place after the barrier.
42 //! * Release - a barrier for releasing a lock. Preceding reads and writes
43 //!   take place before the barrier.
44 //! * Sequentially consistent - sequentially consistent operations are
45 //!   guaranteed to happen in order. This is the standard mode for working
46 //!   with atomic types and is equivalent to Java's `volatile`.
47
48 #![unstable(
49     feature = "core_intrinsics",
50     reason = "intrinsics are unlikely to ever be stabilized, instead \
51                       they should be used through stabilized interfaces \
52                       in the rest of the standard library",
53     issue = "none"
54 )]
55 #![allow(missing_docs)]
56
57 use crate::marker::DiscriminantKind;
58 use crate::mem;
59
60 // These imports are used for simplifying intra-doc links
61 #[allow(unused_imports)]
62 #[cfg(all(target_has_atomic = "8", target_has_atomic = "32", target_has_atomic = "ptr"))]
63 use crate::sync::atomic::{self, AtomicBool, AtomicI32, AtomicIsize, AtomicU32, Ordering};
64
65 #[stable(feature = "drop_in_place", since = "1.8.0")]
66 #[rustc_deprecated(
67     reason = "no longer an intrinsic - use `ptr::drop_in_place` directly",
68     since = "1.18.0"
69 )]
70 pub use crate::ptr::drop_in_place;
71
72 extern "rust-intrinsic" {
73     // N.B., these intrinsics take raw pointers because they mutate aliased
74     // memory, which is not valid for either `&` or `&mut`.
75
76     /// Stores a value if the current value is the same as the `old` value.
77     ///
78     /// The stabilized version of this intrinsic is available on the
79     /// [`atomic`] types via the `compare_exchange` method by passing
80     /// [`Ordering::SeqCst`] as both the `success` and `failure` parameters.
81     /// For example, [`AtomicBool::compare_exchange`].
82     pub fn atomic_cxchg<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
83     /// Stores a value if the current value is the same as the `old` value.
84     ///
85     /// The stabilized version of this intrinsic is available on the
86     /// [`atomic`] types via the `compare_exchange` method by passing
87     /// [`Ordering::Acquire`] as both the `success` and `failure` parameters.
88     /// For example, [`AtomicBool::compare_exchange`].
89     pub fn atomic_cxchg_acq<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
90     /// Stores a value if the current value is the same as the `old` value.
91     ///
92     /// The stabilized version of this intrinsic is available on the
93     /// [`atomic`] types via the `compare_exchange` method by passing
94     /// [`Ordering::Release`] as the `success` and [`Ordering::Relaxed`] as the
95     /// `failure` parameters. For example, [`AtomicBool::compare_exchange`].
96     pub fn atomic_cxchg_rel<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
97     /// Stores a value if the current value is the same as the `old` value.
98     ///
99     /// The stabilized version of this intrinsic is available on the
100     /// [`atomic`] types via the `compare_exchange` method by passing
101     /// [`Ordering::AcqRel`] as the `success` and [`Ordering::Acquire`] as the
102     /// `failure` parameters. For example, [`AtomicBool::compare_exchange`].
103     pub fn atomic_cxchg_acqrel<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
104     /// Stores a value if the current value is the same as the `old` value.
105     ///
106     /// The stabilized version of this intrinsic is available on the
107     /// [`atomic`] types via the `compare_exchange` method by passing
108     /// [`Ordering::Relaxed`] as both the `success` and `failure` parameters.
109     /// For example, [`AtomicBool::compare_exchange`].
110     pub fn atomic_cxchg_relaxed<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
111     /// Stores a value if the current value is the same as the `old` value.
112     ///
113     /// The stabilized version of this intrinsic is available on the
114     /// [`atomic`] types via the `compare_exchange` method by passing
115     /// [`Ordering::SeqCst`] as the `success` and [`Ordering::Relaxed`] as the
116     /// `failure` parameters. For example, [`AtomicBool::compare_exchange`].
117     pub fn atomic_cxchg_failrelaxed<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
118     /// Stores a value if the current value is the same as the `old` value.
119     ///
120     /// The stabilized version of this intrinsic is available on the
121     /// [`atomic`] types via the `compare_exchange` method by passing
122     /// [`Ordering::SeqCst`] as the `success` and [`Ordering::Acquire`] as the
123     /// `failure` parameters. For example, [`AtomicBool::compare_exchange`].
124     pub fn atomic_cxchg_failacq<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
125     /// Stores a value if the current value is the same as the `old` value.
126     ///
127     /// The stabilized version of this intrinsic is available on the
128     /// [`atomic`] types via the `compare_exchange` method by passing
129     /// [`Ordering::Acquire`] as the `success` and [`Ordering::Relaxed`] as the
130     /// `failure` parameters. For example, [`AtomicBool::compare_exchange`].
131     pub fn atomic_cxchg_acq_failrelaxed<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
132     /// Stores a value if the current value is the same as the `old` value.
133     ///
134     /// The stabilized version of this intrinsic is available on the
135     /// [`atomic`] types via the `compare_exchange` method by passing
136     /// [`Ordering::AcqRel`] as the `success` and [`Ordering::Relaxed`] as the
137     /// `failure` parameters. For example, [`AtomicBool::compare_exchange`].
138     pub fn atomic_cxchg_acqrel_failrelaxed<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
139
140     /// Stores a value if the current value is the same as the `old` value.
141     ///
142     /// The stabilized version of this intrinsic is available on the
143     /// [`atomic`] types via the `compare_exchange_weak` method by passing
144     /// [`Ordering::SeqCst`] as both the `success` and `failure` parameters.
145     /// For example, [`AtomicBool::compare_exchange_weak`].
146     pub fn atomic_cxchgweak<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
147     /// Stores a value if the current value is the same as the `old` value.
148     ///
149     /// The stabilized version of this intrinsic is available on the
150     /// [`atomic`] types via the `compare_exchange_weak` method by passing
151     /// [`Ordering::Acquire`] as both the `success` and `failure` parameters.
152     /// For example, [`AtomicBool::compare_exchange_weak`].
153     pub fn atomic_cxchgweak_acq<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
154     /// Stores a value if the current value is the same as the `old` value.
155     ///
156     /// The stabilized version of this intrinsic is available on the
157     /// [`atomic`] types via the `compare_exchange_weak` method by passing
158     /// [`Ordering::Release`] as the `success` and [`Ordering::Relaxed`] as the
159     /// `failure` parameters. For example, [`AtomicBool::compare_exchange_weak`].
160     pub fn atomic_cxchgweak_rel<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
161     /// Stores a value if the current value is the same as the `old` value.
162     ///
163     /// The stabilized version of this intrinsic is available on the
164     /// [`atomic`] types via the `compare_exchange_weak` method by passing
165     /// [`Ordering::AcqRel`] as the `success` and [`Ordering::Acquire`] as the
166     /// `failure` parameters. For example, [`AtomicBool::compare_exchange_weak`].
167     pub fn atomic_cxchgweak_acqrel<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
168     /// Stores a value if the current value is the same as the `old` value.
169     ///
170     /// The stabilized version of this intrinsic is available on the
171     /// [`atomic`] types via the `compare_exchange_weak` method by passing
172     /// [`Ordering::Relaxed`] as both the `success` and `failure` parameters.
173     /// For example, [`AtomicBool::compare_exchange_weak`].
174     pub fn atomic_cxchgweak_relaxed<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
175     /// Stores a value if the current value is the same as the `old` value.
176     ///
177     /// The stabilized version of this intrinsic is available on the
178     /// [`atomic`] types via the `compare_exchange_weak` method by passing
179     /// [`Ordering::SeqCst`] as the `success` and [`Ordering::Relaxed`] as the
180     /// `failure` parameters. For example, [`AtomicBool::compare_exchange_weak`].
181     pub fn atomic_cxchgweak_failrelaxed<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
182     /// Stores a value if the current value is the same as the `old` value.
183     ///
184     /// The stabilized version of this intrinsic is available on the
185     /// [`atomic`] types via the `compare_exchange_weak` method by passing
186     /// [`Ordering::SeqCst`] as the `success` and [`Ordering::Acquire`] as the
187     /// `failure` parameters. For example, [`AtomicBool::compare_exchange_weak`].
188     pub fn atomic_cxchgweak_failacq<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
189     /// Stores a value if the current value is the same as the `old` value.
190     ///
191     /// The stabilized version of this intrinsic is available on the
192     /// [`atomic`] types via the `compare_exchange_weak` method by passing
193     /// [`Ordering::Acquire`] as the `success` and [`Ordering::Relaxed`] as the
194     /// `failure` parameters. For example, [`AtomicBool::compare_exchange_weak`].
195     pub fn atomic_cxchgweak_acq_failrelaxed<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
196     /// Stores a value if the current value is the same as the `old` value.
197     ///
198     /// The stabilized version of this intrinsic is available on the
199     /// [`atomic`] types via the `compare_exchange_weak` method by passing
200     /// [`Ordering::AcqRel`] as the `success` and [`Ordering::Relaxed`] as the
201     /// `failure` parameters. For example, [`AtomicBool::compare_exchange_weak`].
202     pub fn atomic_cxchgweak_acqrel_failrelaxed<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
203
204     /// Loads the current value of the pointer.
205     ///
206     /// The stabilized version of this intrinsic is available on the
207     /// [`atomic`] types via the `load` method by passing
208     /// [`Ordering::SeqCst`] as the `order`. For example, [`AtomicBool::load`].
209     pub fn atomic_load<T: Copy>(src: *const T) -> T;
210     /// Loads the current value of the pointer.
211     ///
212     /// The stabilized version of this intrinsic is available on the
213     /// [`atomic`] types via the `load` method by passing
214     /// [`Ordering::Acquire`] as the `order`. For example, [`AtomicBool::load`].
215     pub fn atomic_load_acq<T: Copy>(src: *const T) -> T;
216     /// Loads the current value of the pointer.
217     ///
218     /// The stabilized version of this intrinsic is available on the
219     /// [`atomic`] types via the `load` method by passing
220     /// [`Ordering::Relaxed`] as the `order`. For example, [`AtomicBool::load`].
221     pub fn atomic_load_relaxed<T: Copy>(src: *const T) -> T;
222     pub fn atomic_load_unordered<T: Copy>(src: *const T) -> T;
223
224     /// Stores the value at the specified memory location.
225     ///
226     /// The stabilized version of this intrinsic is available on the
227     /// [`atomic`] types via the `store` method by passing
228     /// [`Ordering::SeqCst`] as the `order`. For example, [`AtomicBool::store`].
229     pub fn atomic_store<T: Copy>(dst: *mut T, val: T);
230     /// Stores the value at the specified memory location.
231     ///
232     /// The stabilized version of this intrinsic is available on the
233     /// [`atomic`] types via the `store` method by passing
234     /// [`Ordering::Release`] as the `order`. For example, [`AtomicBool::store`].
235     pub fn atomic_store_rel<T: Copy>(dst: *mut T, val: T);
236     /// Stores the value at the specified memory location.
237     ///
238     /// The stabilized version of this intrinsic is available on the
239     /// [`atomic`] types via the `store` method by passing
240     /// [`Ordering::Relaxed`] as the `order`. For example, [`AtomicBool::store`].
241     pub fn atomic_store_relaxed<T: Copy>(dst: *mut T, val: T);
242     pub fn atomic_store_unordered<T: Copy>(dst: *mut T, val: T);
243
244     /// Stores the value at the specified memory location, returning the old value.
245     ///
246     /// The stabilized version of this intrinsic is available on the
247     /// [`atomic`] types via the `swap` method by passing
248     /// [`Ordering::SeqCst`] as the `order`. For example, [`AtomicBool::swap`].
249     pub fn atomic_xchg<T: Copy>(dst: *mut T, src: T) -> T;
250     /// Stores the value at the specified memory location, returning the old value.
251     ///
252     /// The stabilized version of this intrinsic is available on the
253     /// [`atomic`] types via the `swap` method by passing
254     /// [`Ordering::Acquire`] as the `order`. For example, [`AtomicBool::swap`].
255     pub fn atomic_xchg_acq<T: Copy>(dst: *mut T, src: T) -> T;
256     /// Stores the value at the specified memory location, returning the old value.
257     ///
258     /// The stabilized version of this intrinsic is available on the
259     /// [`atomic`] types via the `swap` method by passing
260     /// [`Ordering::Release`] as the `order`. For example, [`AtomicBool::swap`].
261     pub fn atomic_xchg_rel<T: Copy>(dst: *mut T, src: T) -> T;
262     /// Stores the value at the specified memory location, returning the old value.
263     ///
264     /// The stabilized version of this intrinsic is available on the
265     /// [`atomic`] types via the `swap` method by passing
266     /// [`Ordering::AcqRel`] as the `order`. For example, [`AtomicBool::swap`].
267     pub fn atomic_xchg_acqrel<T: Copy>(dst: *mut T, src: T) -> T;
268     /// Stores the value at the specified memory location, returning the old value.
269     ///
270     /// The stabilized version of this intrinsic is available on the
271     /// [`atomic`] types via the `swap` method by passing
272     /// [`Ordering::Relaxed`] as the `order`. For example, [`AtomicBool::swap`].
273     pub fn atomic_xchg_relaxed<T: Copy>(dst: *mut T, src: T) -> T;
274
275     /// Adds to the current value, returning the previous value.
276     ///
277     /// The stabilized version of this intrinsic is available on the
278     /// [`atomic`] types via the `fetch_add` method by passing
279     /// [`Ordering::SeqCst`] as the `order`. For example, [`AtomicIsize::fetch_add`].
280     pub fn atomic_xadd<T: Copy>(dst: *mut T, src: T) -> T;
281     /// Adds to the current value, returning the previous value.
282     ///
283     /// The stabilized version of this intrinsic is available on the
284     /// [`atomic`] types via the `fetch_add` method by passing
285     /// [`Ordering::Acquire`] as the `order`. For example, [`AtomicIsize::fetch_add`].
286     pub fn atomic_xadd_acq<T: Copy>(dst: *mut T, src: T) -> T;
287     /// Adds to the current value, returning the previous value.
288     ///
289     /// The stabilized version of this intrinsic is available on the
290     /// [`atomic`] types via the `fetch_add` method by passing
291     /// [`Ordering::Release`] as the `order`. For example, [`AtomicIsize::fetch_add`].
292     pub fn atomic_xadd_rel<T: Copy>(dst: *mut T, src: T) -> T;
293     /// Adds to the current value, returning the previous value.
294     ///
295     /// The stabilized version of this intrinsic is available on the
296     /// [`atomic`] types via the `fetch_add` method by passing
297     /// [`Ordering::AcqRel`] as the `order`. For example, [`AtomicIsize::fetch_add`].
298     pub fn atomic_xadd_acqrel<T: Copy>(dst: *mut T, src: T) -> T;
299     /// Adds to the current value, returning the previous value.
300     ///
301     /// The stabilized version of this intrinsic is available on the
302     /// [`atomic`] types via the `fetch_add` method by passing
303     /// [`Ordering::Relaxed`] as the `order`. For example, [`AtomicIsize::fetch_add`].
304     pub fn atomic_xadd_relaxed<T: Copy>(dst: *mut T, src: T) -> T;
305
306     /// Subtract from the current value, returning the previous value.
307     ///
308     /// The stabilized version of this intrinsic is available on the
309     /// [`atomic`] types via the `fetch_sub` method by passing
310     /// [`Ordering::SeqCst`] as the `order`. For example, [`AtomicIsize::fetch_sub`].
311     pub fn atomic_xsub<T: Copy>(dst: *mut T, src: T) -> T;
312     /// Subtract from the current value, returning the previous value.
313     ///
314     /// The stabilized version of this intrinsic is available on the
315     /// [`atomic`] types via the `fetch_sub` method by passing
316     /// [`Ordering::Acquire`] as the `order`. For example, [`AtomicIsize::fetch_sub`].
317     pub fn atomic_xsub_acq<T: Copy>(dst: *mut T, src: T) -> T;
318     /// Subtract from the current value, returning the previous value.
319     ///
320     /// The stabilized version of this intrinsic is available on the
321     /// [`atomic`] types via the `fetch_sub` method by passing
322     /// [`Ordering::Release`] as the `order`. For example, [`AtomicIsize::fetch_sub`].
323     pub fn atomic_xsub_rel<T: Copy>(dst: *mut T, src: T) -> T;
324     /// Subtract from the current value, returning the previous value.
325     ///
326     /// The stabilized version of this intrinsic is available on the
327     /// [`atomic`] types via the `fetch_sub` method by passing
328     /// [`Ordering::AcqRel`] as the `order`. For example, [`AtomicIsize::fetch_sub`].
329     pub fn atomic_xsub_acqrel<T: Copy>(dst: *mut T, src: T) -> T;
330     /// Subtract from the current value, returning the previous value.
331     ///
332     /// The stabilized version of this intrinsic is available on the
333     /// [`atomic`] types via the `fetch_sub` method by passing
334     /// [`Ordering::Relaxed`] as the `order`. For example, [`AtomicIsize::fetch_sub`].
335     pub fn atomic_xsub_relaxed<T: Copy>(dst: *mut T, src: T) -> T;
336
337     /// Bitwise and with the current value, returning the previous value.
338     ///
339     /// The stabilized version of this intrinsic is available on the
340     /// [`atomic`] types via the `fetch_and` method by passing
341     /// [`Ordering::SeqCst`] as the `order`. For example, [`AtomicBool::fetch_and`].
342     pub fn atomic_and<T: Copy>(dst: *mut T, src: T) -> T;
343     /// Bitwise and with the current value, returning the previous value.
344     ///
345     /// The stabilized version of this intrinsic is available on the
346     /// [`atomic`] types via the `fetch_and` method by passing
347     /// [`Ordering::Acquire`] as the `order`. For example, [`AtomicBool::fetch_and`].
348     pub fn atomic_and_acq<T: Copy>(dst: *mut T, src: T) -> T;
349     /// Bitwise and with the current value, returning the previous value.
350     ///
351     /// The stabilized version of this intrinsic is available on the
352     /// [`atomic`] types via the `fetch_and` method by passing
353     /// [`Ordering::Release`] as the `order`. For example, [`AtomicBool::fetch_and`].
354     pub fn atomic_and_rel<T: Copy>(dst: *mut T, src: T) -> T;
355     /// Bitwise and with the current value, returning the previous value.
356     ///
357     /// The stabilized version of this intrinsic is available on the
358     /// [`atomic`] types via the `fetch_and` method by passing
359     /// [`Ordering::AcqRel`] as the `order`. For example, [`AtomicBool::fetch_and`].
360     pub fn atomic_and_acqrel<T: Copy>(dst: *mut T, src: T) -> T;
361     /// Bitwise and with the current value, returning the previous value.
362     ///
363     /// The stabilized version of this intrinsic is available on the
364     /// [`atomic`] types via the `fetch_and` method by passing
365     /// [`Ordering::Relaxed`] as the `order`. For example, [`AtomicBool::fetch_and`].
366     pub fn atomic_and_relaxed<T: Copy>(dst: *mut T, src: T) -> T;
367
368     /// Bitwise nand with the current value, returning the previous value.
369     ///
370     /// The stabilized version of this intrinsic is available on the
371     /// [`AtomicBool`] type via the `fetch_nand` method by passing
372     /// [`Ordering::SeqCst`] as the `order`. For example, [`AtomicBool::fetch_nand`].
373     pub fn atomic_nand<T: Copy>(dst: *mut T, src: T) -> T;
374     /// Bitwise nand with the current value, returning the previous value.
375     ///
376     /// The stabilized version of this intrinsic is available on the
377     /// [`AtomicBool`] type via the `fetch_nand` method by passing
378     /// [`Ordering::Acquire`] as the `order`. For example, [`AtomicBool::fetch_nand`].
379     pub fn atomic_nand_acq<T: Copy>(dst: *mut T, src: T) -> T;
380     /// Bitwise nand with the current value, returning the previous value.
381     ///
382     /// The stabilized version of this intrinsic is available on the
383     /// [`AtomicBool`] type via the `fetch_nand` method by passing
384     /// [`Ordering::Release`] as the `order`. For example, [`AtomicBool::fetch_nand`].
385     pub fn atomic_nand_rel<T: Copy>(dst: *mut T, src: T) -> T;
386     /// Bitwise nand with the current value, returning the previous value.
387     ///
388     /// The stabilized version of this intrinsic is available on the
389     /// [`AtomicBool`] type via the `fetch_nand` method by passing
390     /// [`Ordering::AcqRel`] as the `order`. For example, [`AtomicBool::fetch_nand`].
391     pub fn atomic_nand_acqrel<T: Copy>(dst: *mut T, src: T) -> T;
392     /// Bitwise nand with the current value, returning the previous value.
393     ///
394     /// The stabilized version of this intrinsic is available on the
395     /// [`AtomicBool`] type via the `fetch_nand` method by passing
396     /// [`Ordering::Relaxed`] as the `order`. For example, [`AtomicBool::fetch_nand`].
397     pub fn atomic_nand_relaxed<T: Copy>(dst: *mut T, src: T) -> T;
398
399     /// Bitwise or with the current value, returning the previous value.
400     ///
401     /// The stabilized version of this intrinsic is available on the
402     /// [`atomic`] types via the `fetch_or` method by passing
403     /// [`Ordering::SeqCst`] as the `order`. For example, [`AtomicBool::fetch_or`].
404     pub fn atomic_or<T: Copy>(dst: *mut T, src: T) -> T;
405     /// Bitwise or with the current value, returning the previous value.
406     ///
407     /// The stabilized version of this intrinsic is available on the
408     /// [`atomic`] types via the `fetch_or` method by passing
409     /// [`Ordering::Acquire`] as the `order`. For example, [`AtomicBool::fetch_or`].
410     pub fn atomic_or_acq<T: Copy>(dst: *mut T, src: T) -> T;
411     /// Bitwise or with the current value, returning the previous value.
412     ///
413     /// The stabilized version of this intrinsic is available on the
414     /// [`atomic`] types via the `fetch_or` method by passing
415     /// [`Ordering::Release`] as the `order`. For example, [`AtomicBool::fetch_or`].
416     pub fn atomic_or_rel<T: Copy>(dst: *mut T, src: T) -> T;
417     /// Bitwise or with the current value, returning the previous value.
418     ///
419     /// The stabilized version of this intrinsic is available on the
420     /// [`atomic`] types via the `fetch_or` method by passing
421     /// [`Ordering::AcqRel`] as the `order`. For example, [`AtomicBool::fetch_or`].
422     pub fn atomic_or_acqrel<T: Copy>(dst: *mut T, src: T) -> T;
423     /// Bitwise or with the current value, returning the previous value.
424     ///
425     /// The stabilized version of this intrinsic is available on the
426     /// [`atomic`] types via the `fetch_or` method by passing
427     /// [`Ordering::Relaxed`] as the `order`. For example, [`AtomicBool::fetch_or`].
428     pub fn atomic_or_relaxed<T: Copy>(dst: *mut T, src: T) -> T;
429
430     /// Bitwise xor with the current value, returning the previous value.
431     ///
432     /// The stabilized version of this intrinsic is available on the
433     /// [`atomic`] types via the `fetch_xor` method by passing
434     /// [`Ordering::SeqCst`] as the `order`. For example, [`AtomicBool::fetch_xor`].
435     pub fn atomic_xor<T: Copy>(dst: *mut T, src: T) -> T;
436     /// Bitwise xor with the current value, returning the previous value.
437     ///
438     /// The stabilized version of this intrinsic is available on the
439     /// [`atomic`] types via the `fetch_xor` method by passing
440     /// [`Ordering::Acquire`] as the `order`. For example, [`AtomicBool::fetch_xor`].
441     pub fn atomic_xor_acq<T: Copy>(dst: *mut T, src: T) -> T;
442     /// Bitwise xor with the current value, returning the previous value.
443     ///
444     /// The stabilized version of this intrinsic is available on the
445     /// [`atomic`] types via the `fetch_xor` method by passing
446     /// [`Ordering::Release`] as the `order`. For example, [`AtomicBool::fetch_xor`].
447     pub fn atomic_xor_rel<T: Copy>(dst: *mut T, src: T) -> T;
448     /// Bitwise xor with the current value, returning the previous value.
449     ///
450     /// The stabilized version of this intrinsic is available on the
451     /// [`atomic`] types via the `fetch_xor` method by passing
452     /// [`Ordering::AcqRel`] as the `order`. For example, [`AtomicBool::fetch_xor`].
453     pub fn atomic_xor_acqrel<T: Copy>(dst: *mut T, src: T) -> T;
454     /// Bitwise xor with the current value, returning the previous value.
455     ///
456     /// The stabilized version of this intrinsic is available on the
457     /// [`atomic`] types via the `fetch_xor` method by passing
458     /// [`Ordering::Relaxed`] as the `order`. For example, [`AtomicBool::fetch_xor`].
459     pub fn atomic_xor_relaxed<T: Copy>(dst: *mut T, src: T) -> T;
460
461     /// Maximum with the current value using a signed comparison.
462     ///
463     /// The stabilized version of this intrinsic is available on the
464     /// [`atomic`] signed integer types via the `fetch_max` method by passing
465     /// [`Ordering::SeqCst`] as the `order`. For example, [`AtomicI32::fetch_max`].
466     pub fn atomic_max<T: Copy>(dst: *mut T, src: T) -> T;
467     /// Maximum with the current value using a signed comparison.
468     ///
469     /// The stabilized version of this intrinsic is available on the
470     /// [`atomic`] signed integer types via the `fetch_max` method by passing
471     /// [`Ordering::Acquire`] as the `order`. For example, [`AtomicI32::fetch_max`].
472     pub fn atomic_max_acq<T: Copy>(dst: *mut T, src: T) -> T;
473     /// Maximum with the current value using a signed comparison.
474     ///
475     /// The stabilized version of this intrinsic is available on the
476     /// [`atomic`] signed integer types via the `fetch_max` method by passing
477     /// [`Ordering::Release`] as the `order`. For example, [`AtomicI32::fetch_max`].
478     pub fn atomic_max_rel<T: Copy>(dst: *mut T, src: T) -> T;
479     /// Maximum with the current value using a signed comparison.
480     ///
481     /// The stabilized version of this intrinsic is available on the
482     /// [`atomic`] signed integer types via the `fetch_max` method by passing
483     /// [`Ordering::AcqRel`] as the `order`. For example, [`AtomicI32::fetch_max`].
484     pub fn atomic_max_acqrel<T: Copy>(dst: *mut T, src: T) -> T;
485     /// Maximum with the current value.
486     ///
487     /// The stabilized version of this intrinsic is available on the
488     /// [`atomic`] signed integer types via the `fetch_max` method by passing
489     /// [`Ordering::Relaxed`] as the `order`. For example, [`AtomicI32::fetch_max`].
490     pub fn atomic_max_relaxed<T: Copy>(dst: *mut T, src: T) -> T;
491
492     /// Minimum with the current value using a signed comparison.
493     ///
494     /// The stabilized version of this intrinsic is available on the
495     /// [`atomic`] signed integer types via the `fetch_min` method by passing
496     /// [`Ordering::SeqCst`] as the `order`. For example, [`AtomicI32::fetch_min`].
497     pub fn atomic_min<T: Copy>(dst: *mut T, src: T) -> T;
498     /// Minimum with the current value using a signed comparison.
499     ///
500     /// The stabilized version of this intrinsic is available on the
501     /// [`atomic`] signed integer types via the `fetch_min` method by passing
502     /// [`Ordering::Acquire`] as the `order`. For example, [`AtomicI32::fetch_min`].
503     pub fn atomic_min_acq<T: Copy>(dst: *mut T, src: T) -> T;
504     /// Minimum with the current value using a signed comparison.
505     ///
506     /// The stabilized version of this intrinsic is available on the
507     /// [`atomic`] signed integer types via the `fetch_min` method by passing
508     /// [`Ordering::Release`] as the `order`. For example, [`AtomicI32::fetch_min`].
509     pub fn atomic_min_rel<T: Copy>(dst: *mut T, src: T) -> T;
510     /// Minimum with the current value using a signed comparison.
511     ///
512     /// The stabilized version of this intrinsic is available on the
513     /// [`atomic`] signed integer types via the `fetch_min` method by passing
514     /// [`Ordering::AcqRel`] as the `order`. For example, [`AtomicI32::fetch_min`].
515     pub fn atomic_min_acqrel<T: Copy>(dst: *mut T, src: T) -> T;
516     /// Minimum with the current value using a signed comparison.
517     ///
518     /// The stabilized version of this intrinsic is available on the
519     /// [`atomic`] signed integer types via the `fetch_min` method by passing
520     /// [`Ordering::Relaxed`] as the `order`. For example, [`AtomicI32::fetch_min`].
521     pub fn atomic_min_relaxed<T: Copy>(dst: *mut T, src: T) -> T;
522
523     /// Minimum with the current value using an unsigned comparison.
524     ///
525     /// The stabilized version of this intrinsic is available on the
526     /// [`atomic`] unsigned integer types via the `fetch_min` method by passing
527     /// [`Ordering::SeqCst`] as the `order`. For example, [`AtomicU32::fetch_min`].
528     pub fn atomic_umin<T: Copy>(dst: *mut T, src: T) -> T;
529     /// Minimum with the current value using an unsigned comparison.
530     ///
531     /// The stabilized version of this intrinsic is available on the
532     /// [`atomic`] unsigned integer types via the `fetch_min` method by passing
533     /// [`Ordering::Acquire`] as the `order`. For example, [`AtomicU32::fetch_min`].
534     pub fn atomic_umin_acq<T: Copy>(dst: *mut T, src: T) -> T;
535     /// Minimum with the current value using an unsigned comparison.
536     ///
537     /// The stabilized version of this intrinsic is available on the
538     /// [`atomic`] unsigned integer types via the `fetch_min` method by passing
539     /// [`Ordering::Release`] as the `order`. For example, [`AtomicU32::fetch_min`].
540     pub fn atomic_umin_rel<T: Copy>(dst: *mut T, src: T) -> T;
541     /// Minimum with the current value using an unsigned comparison.
542     ///
543     /// The stabilized version of this intrinsic is available on the
544     /// [`atomic`] unsigned integer types via the `fetch_min` method by passing
545     /// [`Ordering::AcqRel`] as the `order`. For example, [`AtomicU32::fetch_min`].
546     pub fn atomic_umin_acqrel<T: Copy>(dst: *mut T, src: T) -> T;
547     /// Minimum with the current value using an unsigned comparison.
548     ///
549     /// The stabilized version of this intrinsic is available on the
550     /// [`atomic`] unsigned integer types via the `fetch_min` method by passing
551     /// [`Ordering::Relaxed`] as the `order`. For example, [`AtomicU32::fetch_min`].
552     pub fn atomic_umin_relaxed<T: Copy>(dst: *mut T, src: T) -> T;
553
554     /// Maximum with the current value using an unsigned comparison.
555     ///
556     /// The stabilized version of this intrinsic is available on the
557     /// [`atomic`] unsigned integer types via the `fetch_max` method by passing
558     /// [`Ordering::SeqCst`] as the `order`. For example, [`AtomicU32::fetch_max`].
559     pub fn atomic_umax<T: Copy>(dst: *mut T, src: T) -> T;
560     /// Maximum with the current value using an unsigned comparison.
561     ///
562     /// The stabilized version of this intrinsic is available on the
563     /// [`atomic`] unsigned integer types via the `fetch_max` method by passing
564     /// [`Ordering::Acquire`] as the `order`. For example, [`AtomicU32::fetch_max`].
565     pub fn atomic_umax_acq<T: Copy>(dst: *mut T, src: T) -> T;
566     /// Maximum with the current value using an unsigned comparison.
567     ///
568     /// The stabilized version of this intrinsic is available on the
569     /// [`atomic`] unsigned integer types via the `fetch_max` method by passing
570     /// [`Ordering::Release`] as the `order`. For example, [`AtomicU32::fetch_max`].
571     pub fn atomic_umax_rel<T: Copy>(dst: *mut T, src: T) -> T;
572     /// Maximum with the current value using an unsigned comparison.
573     ///
574     /// The stabilized version of this intrinsic is available on the
575     /// [`atomic`] unsigned integer types via the `fetch_max` method by passing
576     /// [`Ordering::AcqRel`] as the `order`. For example, [`AtomicU32::fetch_max`].
577     pub fn atomic_umax_acqrel<T: Copy>(dst: *mut T, src: T) -> T;
578     /// Maximum with the current value using an unsigned comparison.
579     ///
580     /// The stabilized version of this intrinsic is available on the
581     /// [`atomic`] unsigned integer types via the `fetch_max` method by passing
582     /// [`Ordering::Relaxed`] as the `order`. For example, [`AtomicU32::fetch_max`].
583     pub fn atomic_umax_relaxed<T: Copy>(dst: *mut T, src: T) -> T;
584
585     /// The `prefetch` intrinsic is a hint to the code generator to insert a prefetch instruction
586     /// if supported; otherwise, it is a no-op.
587     /// Prefetches have no effect on the behavior of the program but can change its performance
588     /// characteristics.
589     ///
590     /// The `locality` argument must be a constant integer and is a temporal locality specifier
591     /// ranging from (0) - no locality, to (3) - extremely local keep in cache.
592     ///
593     /// This intrinsic does not have a stable counterpart.
594     pub fn prefetch_read_data<T>(data: *const T, locality: i32);
595     /// The `prefetch` intrinsic is a hint to the code generator to insert a prefetch instruction
596     /// if supported; otherwise, it is a no-op.
597     /// Prefetches have no effect on the behavior of the program but can change its performance
598     /// characteristics.
599     ///
600     /// The `locality` argument must be a constant integer and is a temporal locality specifier
601     /// ranging from (0) - no locality, to (3) - extremely local keep in cache.
602     ///
603     /// This intrinsic does not have a stable counterpart.
604     pub fn prefetch_write_data<T>(data: *const T, locality: i32);
605     /// The `prefetch` intrinsic is a hint to the code generator to insert a prefetch instruction
606     /// if supported; otherwise, it is a no-op.
607     /// Prefetches have no effect on the behavior of the program but can change its performance
608     /// characteristics.
609     ///
610     /// The `locality` argument must be a constant integer and is a temporal locality specifier
611     /// ranging from (0) - no locality, to (3) - extremely local keep in cache.
612     ///
613     /// This intrinsic does not have a stable counterpart.
614     pub fn prefetch_read_instruction<T>(data: *const T, locality: i32);
615     /// The `prefetch` intrinsic is a hint to the code generator to insert a prefetch instruction
616     /// if supported; otherwise, it is a no-op.
617     /// Prefetches have no effect on the behavior of the program but can change its performance
618     /// characteristics.
619     ///
620     /// The `locality` argument must be a constant integer and is a temporal locality specifier
621     /// ranging from (0) - no locality, to (3) - extremely local keep in cache.
622     ///
623     /// This intrinsic does not have a stable counterpart.
624     pub fn prefetch_write_instruction<T>(data: *const T, locality: i32);
625 }
626
627 extern "rust-intrinsic" {
628     /// An atomic fence.
629     ///
630     /// The stabilized version of this intrinsic is available in
631     /// [`atomic::fence`] by passing [`Ordering::SeqCst`]
632     /// as the `order`.
633     pub fn atomic_fence();
634     /// An atomic fence.
635     ///
636     /// The stabilized version of this intrinsic is available in
637     /// [`atomic::fence`] by passing [`Ordering::Acquire`]
638     /// as the `order`.
639     pub fn atomic_fence_acq();
640     /// An atomic fence.
641     ///
642     /// The stabilized version of this intrinsic is available in
643     /// [`atomic::fence`] by passing [`Ordering::Release`]
644     /// as the `order`.
645     pub fn atomic_fence_rel();
646     /// An atomic fence.
647     ///
648     /// The stabilized version of this intrinsic is available in
649     /// [`atomic::fence`] by passing [`Ordering::AcqRel`]
650     /// as the `order`.
651     pub fn atomic_fence_acqrel();
652
653     /// A compiler-only memory barrier.
654     ///
655     /// Memory accesses will never be reordered across this barrier by the
656     /// compiler, but no instructions will be emitted for it. This is
657     /// appropriate for operations on the same thread that may be preempted,
658     /// such as when interacting with signal handlers.
659     ///
660     /// The stabilized version of this intrinsic is available in
661     /// [`atomic::compiler_fence`] by passing [`Ordering::SeqCst`]
662     /// as the `order`.
663     pub fn atomic_singlethreadfence();
664     /// A compiler-only memory barrier.
665     ///
666     /// Memory accesses will never be reordered across this barrier by the
667     /// compiler, but no instructions will be emitted for it. This is
668     /// appropriate for operations on the same thread that may be preempted,
669     /// such as when interacting with signal handlers.
670     ///
671     /// The stabilized version of this intrinsic is available in
672     /// [`atomic::compiler_fence`] by passing [`Ordering::Acquire`]
673     /// as the `order`.
674     pub fn atomic_singlethreadfence_acq();
675     /// A compiler-only memory barrier.
676     ///
677     /// Memory accesses will never be reordered across this barrier by the
678     /// compiler, but no instructions will be emitted for it. This is
679     /// appropriate for operations on the same thread that may be preempted,
680     /// such as when interacting with signal handlers.
681     ///
682     /// The stabilized version of this intrinsic is available in
683     /// [`atomic::compiler_fence`] by passing [`Ordering::Release`]
684     /// as the `order`.
685     pub fn atomic_singlethreadfence_rel();
686     /// A compiler-only memory barrier.
687     ///
688     /// Memory accesses will never be reordered across this barrier by the
689     /// compiler, but no instructions will be emitted for it. This is
690     /// appropriate for operations on the same thread that may be preempted,
691     /// such as when interacting with signal handlers.
692     ///
693     /// The stabilized version of this intrinsic is available in
694     /// [`atomic::compiler_fence`] by passing [`Ordering::AcqRel`]
695     /// as the `order`.
696     pub fn atomic_singlethreadfence_acqrel();
697
698     /// Magic intrinsic that derives its meaning from attributes
699     /// attached to the function.
700     ///
701     /// For example, dataflow uses this to inject static assertions so
702     /// that `rustc_peek(potentially_uninitialized)` would actually
703     /// double-check that dataflow did indeed compute that it is
704     /// uninitialized at that point in the control flow.
705     ///
706     /// This intrinsic should not be used outside of the compiler.
707     pub fn rustc_peek<T>(_: T) -> T;
708
709     /// Aborts the execution of the process.
710     ///
711     /// A more user-friendly and stable version of this operation is
712     /// [`std::process::abort`](../../std/process/fn.abort.html).
713     pub fn abort() -> !;
714
715     /// Informs the optimizer that this point in the code is not reachable,
716     /// enabling further optimizations.
717     ///
718     /// N.B., this is very different from the `unreachable!()` macro: Unlike the
719     /// macro, which panics when it is executed, it is *undefined behavior* to
720     /// reach code marked with this function.
721     ///
722     /// The stabilized version of this intrinsic is [`core::hint::unreachable_unchecked`](crate::hint::unreachable_unchecked).
723     #[rustc_const_unstable(feature = "const_unreachable_unchecked", issue = "53188")]
724     pub fn unreachable() -> !;
725
726     /// Informs the optimizer that a condition is always true.
727     /// If the condition is false, the behavior is undefined.
728     ///
729     /// No code is generated for this intrinsic, but the optimizer will try
730     /// to preserve it (and its condition) between passes, which may interfere
731     /// with optimization of surrounding code and reduce performance. It should
732     /// not be used if the invariant can be discovered by the optimizer on its
733     /// own, or if it does not enable any significant optimizations.
734     ///
735     /// This intrinsic does not have a stable counterpart.
736     #[rustc_const_unstable(feature = "const_assume", issue = "76972")]
737     pub fn assume(b: bool);
738
739     /// Hints to the compiler that branch condition is likely to be true.
740     /// Returns the value passed to it.
741     ///
742     /// Any use other than with `if` statements will probably not have an effect.
743     ///
744     /// This intrinsic does not have a stable counterpart.
745     #[rustc_const_unstable(feature = "const_likely", issue = "none")]
746     pub fn likely(b: bool) -> bool;
747
748     /// Hints to the compiler that branch condition is likely to be false.
749     /// Returns the value passed to it.
750     ///
751     /// Any use other than with `if` statements will probably not have an effect.
752     ///
753     /// This intrinsic does not have a stable counterpart.
754     #[rustc_const_unstable(feature = "const_likely", issue = "none")]
755     pub fn unlikely(b: bool) -> bool;
756
757     /// Executes a breakpoint trap, for inspection by a debugger.
758     ///
759     /// This intrinsic does not have a stable counterpart.
760     pub fn breakpoint();
761
762     /// The size of a type in bytes.
763     ///
764     /// More specifically, this is the offset in bytes between successive
765     /// items of the same type, including alignment padding.
766     ///
767     /// The stabilized version of this intrinsic is [`core::mem::size_of`](crate::mem::size_of).
768     #[rustc_const_stable(feature = "const_size_of", since = "1.40.0")]
769     pub fn size_of<T>() -> usize;
770
771     /// The minimum alignment of a type.
772     ///
773     /// The stabilized version of this intrinsic is [`core::mem::align_of`](crate::mem::align_of).
774     #[rustc_const_stable(feature = "const_min_align_of", since = "1.40.0")]
775     pub fn min_align_of<T>() -> usize;
776     /// The preferred alignment of a type.
777     ///
778     /// This intrinsic does not have a stable counterpart.
779     #[rustc_const_unstable(feature = "const_pref_align_of", issue = "none")]
780     pub fn pref_align_of<T>() -> usize;
781
782     /// The size of the referenced value in bytes.
783     ///
784     /// The stabilized version of this intrinsic is [`mem::size_of_val`].
785     #[rustc_const_unstable(feature = "const_size_of_val", issue = "46571")]
786     pub fn size_of_val<T: ?Sized>(_: *const T) -> usize;
787     /// The required alignment of the referenced value.
788     ///
789     /// The stabilized version of this intrinsic is [`core::mem::align_of_val`](crate::mem::align_of_val).
790     #[rustc_const_unstable(feature = "const_align_of_val", issue = "46571")]
791     pub fn min_align_of_val<T: ?Sized>(_: *const T) -> usize;
792
793     /// Gets a static string slice containing the name of a type.
794     ///
795     /// The stabilized version of this intrinsic is [`core::any::type_name`](crate::any::type_name).
796     #[rustc_const_unstable(feature = "const_type_name", issue = "63084")]
797     pub fn type_name<T: ?Sized>() -> &'static str;
798
799     /// Gets an identifier which is globally unique to the specified type. This
800     /// function will return the same value for a type regardless of whichever
801     /// crate it is invoked in.
802     ///
803     /// The stabilized version of this intrinsic is [`core::any::TypeId::of`](crate::any::TypeId::of).
804     #[rustc_const_unstable(feature = "const_type_id", issue = "77125")]
805     pub fn type_id<T: ?Sized + 'static>() -> u64;
806
807     /// A guard for unsafe functions that cannot ever be executed if `T` is uninhabited:
808     /// This will statically either panic, or do nothing.
809     ///
810     /// This intrinsic does not have a stable counterpart.
811     #[rustc_const_unstable(feature = "const_assert_type", issue = "none")]
812     pub fn assert_inhabited<T>();
813
814     /// A guard for unsafe functions that cannot ever be executed if `T` does not permit
815     /// zero-initialization: This will statically either panic, or do nothing.
816     ///
817     /// This intrinsic does not have a stable counterpart.
818     pub fn assert_zero_valid<T>();
819
820     /// A guard for unsafe functions that cannot ever be executed if `T` has invalid
821     /// bit patterns: This will statically either panic, or do nothing.
822     ///
823     /// This intrinsic does not have a stable counterpart.
824     pub fn assert_uninit_valid<T>();
825
826     /// Gets a reference to a static `Location` indicating where it was called.
827     ///
828     /// Consider using [`core::panic::Location::caller`](crate::panic::Location::caller) instead.
829     #[rustc_const_unstable(feature = "const_caller_location", issue = "76156")]
830     pub fn caller_location() -> &'static crate::panic::Location<'static>;
831
832     /// Moves a value out of scope without running drop glue.
833     ///
834     /// This exists solely for [`mem::forget_unsized`]; normal `forget` uses
835     /// `ManuallyDrop` instead.
836     pub fn forget<T: ?Sized>(_: T);
837
838     /// Reinterprets the bits of a value of one type as another type.
839     ///
840     /// Both types must have the same size. Neither the original, nor the result,
841     /// may be an [invalid value](../../nomicon/what-unsafe-does.html).
842     ///
843     /// `transmute` is semantically equivalent to a bitwise move of one type
844     /// into another. It copies the bits from the source value into the
845     /// destination value, then forgets the original. It's equivalent to C's
846     /// `memcpy` under the hood, just like `transmute_copy`.
847     ///
848     /// `transmute` is **incredibly** unsafe. There are a vast number of ways to
849     /// cause [undefined behavior][ub] with this function. `transmute` should be
850     /// the absolute last resort.
851     ///
852     /// The [nomicon](../../nomicon/transmutes.html) has additional
853     /// documentation.
854     ///
855     /// [ub]: ../../reference/behavior-considered-undefined.html
856     ///
857     /// # Examples
858     ///
859     /// There are a few things that `transmute` is really useful for.
860     ///
861     /// Turning a pointer into a function pointer. This is *not* portable to
862     /// machines where function pointers and data pointers have different sizes.
863     ///
864     /// ```
865     /// fn foo() -> i32 {
866     ///     0
867     /// }
868     /// let pointer = foo as *const ();
869     /// let function = unsafe {
870     ///     std::mem::transmute::<*const (), fn() -> i32>(pointer)
871     /// };
872     /// assert_eq!(function(), 0);
873     /// ```
874     ///
875     /// Extending a lifetime, or shortening an invariant lifetime. This is
876     /// advanced, very unsafe Rust!
877     ///
878     /// ```
879     /// struct R<'a>(&'a i32);
880     /// unsafe fn extend_lifetime<'b>(r: R<'b>) -> R<'static> {
881     ///     std::mem::transmute::<R<'b>, R<'static>>(r)
882     /// }
883     ///
884     /// unsafe fn shorten_invariant_lifetime<'b, 'c>(r: &'b mut R<'static>)
885     ///                                              -> &'b mut R<'c> {
886     ///     std::mem::transmute::<&'b mut R<'static>, &'b mut R<'c>>(r)
887     /// }
888     /// ```
889     ///
890     /// # Alternatives
891     ///
892     /// Don't despair: many uses of `transmute` can be achieved through other means.
893     /// Below are common applications of `transmute` which can be replaced with safer
894     /// constructs.
895     ///
896     /// Turning raw bytes(`&[u8]`) to `u32`, `f64`, etc.:
897     ///
898     /// ```
899     /// let raw_bytes = [0x78, 0x56, 0x34, 0x12];
900     ///
901     /// let num = unsafe {
902     ///     std::mem::transmute::<[u8; 4], u32>(raw_bytes)
903     /// };
904     ///
905     /// // use `u32::from_ne_bytes` instead
906     /// let num = u32::from_ne_bytes(raw_bytes);
907     /// // or use `u32::from_le_bytes` or `u32::from_be_bytes` to specify the endianness
908     /// let num = u32::from_le_bytes(raw_bytes);
909     /// assert_eq!(num, 0x12345678);
910     /// let num = u32::from_be_bytes(raw_bytes);
911     /// assert_eq!(num, 0x78563412);
912     /// ```
913     ///
914     /// Turning a pointer into a `usize`:
915     ///
916     /// ```
917     /// let ptr = &0;
918     /// let ptr_num_transmute = unsafe {
919     ///     std::mem::transmute::<&i32, usize>(ptr)
920     /// };
921     ///
922     /// // Use an `as` cast instead
923     /// let ptr_num_cast = ptr as *const i32 as usize;
924     /// ```
925     ///
926     /// Turning a `*mut T` into an `&mut T`:
927     ///
928     /// ```
929     /// let ptr: *mut i32 = &mut 0;
930     /// let ref_transmuted = unsafe {
931     ///     std::mem::transmute::<*mut i32, &mut i32>(ptr)
932     /// };
933     ///
934     /// // Use a reborrow instead
935     /// let ref_casted = unsafe { &mut *ptr };
936     /// ```
937     ///
938     /// Turning an `&mut T` into an `&mut U`:
939     ///
940     /// ```
941     /// let ptr = &mut 0;
942     /// let val_transmuted = unsafe {
943     ///     std::mem::transmute::<&mut i32, &mut u32>(ptr)
944     /// };
945     ///
946     /// // Now, put together `as` and reborrowing - note the chaining of `as`
947     /// // `as` is not transitive
948     /// let val_casts = unsafe { &mut *(ptr as *mut i32 as *mut u32) };
949     /// ```
950     ///
951     /// Turning an `&str` into an `&[u8]`:
952     ///
953     /// ```
954     /// // this is not a good way to do this.
955     /// let slice = unsafe { std::mem::transmute::<&str, &[u8]>("Rust") };
956     /// assert_eq!(slice, &[82, 117, 115, 116]);
957     ///
958     /// // You could use `str::as_bytes`
959     /// let slice = "Rust".as_bytes();
960     /// assert_eq!(slice, &[82, 117, 115, 116]);
961     ///
962     /// // Or, just use a byte string, if you have control over the string
963     /// // literal
964     /// assert_eq!(b"Rust", &[82, 117, 115, 116]);
965     /// ```
966     ///
967     /// Turning a `Vec<&T>` into a `Vec<Option<&T>>`:
968     ///
969     /// ```
970     /// let store = [0, 1, 2, 3];
971     /// let v_orig = store.iter().collect::<Vec<&i32>>();
972     ///
973     /// // clone the vector as we will reuse them later
974     /// let v_clone = v_orig.clone();
975     ///
976     /// // Using transmute: this relies on the unspecified data layout of `Vec`, which is a
977     /// // bad idea and could cause Undefined Behavior.
978     /// // However, it is no-copy.
979     /// let v_transmuted = unsafe {
980     ///     std::mem::transmute::<Vec<&i32>, Vec<Option<&i32>>>(v_clone)
981     /// };
982     ///
983     /// let v_clone = v_orig.clone();
984     ///
985     /// // This is the suggested, safe way.
986     /// // It does copy the entire vector, though, into a new array.
987     /// let v_collected = v_clone.into_iter()
988     ///                          .map(Some)
989     ///                          .collect::<Vec<Option<&i32>>>();
990     ///
991     /// let v_clone = v_orig.clone();
992     ///
993     /// // The no-copy, unsafe way, still using transmute, but not relying on the data layout.
994     /// // Like the first approach, this reuses the `Vec` internals.
995     /// // Therefore, the new inner type must have the
996     /// // exact same size, *and the same alignment*, as the old type.
997     /// // The same caveats exist for this method as transmute, for
998     /// // the original inner type (`&i32`) to the converted inner type
999     /// // (`Option<&i32>`), so read the nomicon pages linked above and also
1000     /// // consult the [`from_raw_parts`] documentation.
1001     /// let v_from_raw = unsafe {
1002     // FIXME Update this when vec_into_raw_parts is stabilized
1003     ///     // Ensure the original vector is not dropped.
1004     ///     let mut v_clone = std::mem::ManuallyDrop::new(v_clone);
1005     ///     Vec::from_raw_parts(v_clone.as_mut_ptr() as *mut Option<&i32>,
1006     ///                         v_clone.len(),
1007     ///                         v_clone.capacity())
1008     /// };
1009     /// ```
1010     ///
1011     /// [`from_raw_parts`]: ../../std/vec/struct.Vec.html#method.from_raw_parts
1012     ///
1013     /// Implementing `split_at_mut`:
1014     ///
1015     /// ```
1016     /// use std::{slice, mem};
1017     ///
1018     /// // There are multiple ways to do this, and there are multiple problems
1019     /// // with the following (transmute) way.
1020     /// fn split_at_mut_transmute<T>(slice: &mut [T], mid: usize)
1021     ///                              -> (&mut [T], &mut [T]) {
1022     ///     let len = slice.len();
1023     ///     assert!(mid <= len);
1024     ///     unsafe {
1025     ///         let slice2 = mem::transmute::<&mut [T], &mut [T]>(slice);
1026     ///         // first: transmute is not type safe; all it checks is that T and
1027     ///         // U are of the same size. Second, right here, you have two
1028     ///         // mutable references pointing to the same memory.
1029     ///         (&mut slice[0..mid], &mut slice2[mid..len])
1030     ///     }
1031     /// }
1032     ///
1033     /// // This gets rid of the type safety problems; `&mut *` will *only* give
1034     /// // you an `&mut T` from an `&mut T` or `*mut T`.
1035     /// fn split_at_mut_casts<T>(slice: &mut [T], mid: usize)
1036     ///                          -> (&mut [T], &mut [T]) {
1037     ///     let len = slice.len();
1038     ///     assert!(mid <= len);
1039     ///     unsafe {
1040     ///         let slice2 = &mut *(slice as *mut [T]);
1041     ///         // however, you still have two mutable references pointing to
1042     ///         // the same memory.
1043     ///         (&mut slice[0..mid], &mut slice2[mid..len])
1044     ///     }
1045     /// }
1046     ///
1047     /// // This is how the standard library does it. This is the best method, if
1048     /// // you need to do something like this
1049     /// fn split_at_stdlib<T>(slice: &mut [T], mid: usize)
1050     ///                       -> (&mut [T], &mut [T]) {
1051     ///     let len = slice.len();
1052     ///     assert!(mid <= len);
1053     ///     unsafe {
1054     ///         let ptr = slice.as_mut_ptr();
1055     ///         // This now has three mutable references pointing at the same
1056     ///         // memory. `slice`, the rvalue ret.0, and the rvalue ret.1.
1057     ///         // `slice` is never used after `let ptr = ...`, and so one can
1058     ///         // treat it as "dead", and therefore, you only have two real
1059     ///         // mutable slices.
1060     ///         (slice::from_raw_parts_mut(ptr, mid),
1061     ///          slice::from_raw_parts_mut(ptr.add(mid), len - mid))
1062     ///     }
1063     /// }
1064     /// ```
1065     #[stable(feature = "rust1", since = "1.0.0")]
1066     // NOTE: While this makes the intrinsic const stable, we have some custom code in const fn
1067     // checks that prevent its use within `const fn`.
1068     #[rustc_const_stable(feature = "const_transmute", since = "1.46.0")]
1069     #[rustc_diagnostic_item = "transmute"]
1070     pub fn transmute<T, U>(e: T) -> U;
1071
1072     /// Returns `true` if the actual type given as `T` requires drop
1073     /// glue; returns `false` if the actual type provided for `T`
1074     /// implements `Copy`.
1075     ///
1076     /// If the actual type neither requires drop glue nor implements
1077     /// `Copy`, then the return value of this function is unspecified.
1078     ///
1079     /// The stabilized version of this intrinsic is [`mem::needs_drop`](crate::mem::needs_drop).
1080     #[rustc_const_stable(feature = "const_needs_drop", since = "1.40.0")]
1081     pub fn needs_drop<T>() -> bool;
1082
1083     /// Calculates the offset from a pointer.
1084     ///
1085     /// This is implemented as an intrinsic to avoid converting to and from an
1086     /// integer, since the conversion would throw away aliasing information.
1087     ///
1088     /// # Safety
1089     ///
1090     /// Both the starting and resulting pointer must be either in bounds or one
1091     /// byte past the end of an allocated object. If either pointer is out of
1092     /// bounds or arithmetic overflow occurs then any further use of the
1093     /// returned value will result in undefined behavior.
1094     ///
1095     /// The stabilized version of this intrinsic is
1096     /// [`std::pointer::offset`](../../std/primitive.pointer.html#method.offset).
1097     #[must_use = "returns a new pointer rather than modifying its argument"]
1098     #[rustc_const_unstable(feature = "const_ptr_offset", issue = "71499")]
1099     pub fn offset<T>(dst: *const T, offset: isize) -> *const T;
1100
1101     /// Calculates the offset from a pointer, potentially wrapping.
1102     ///
1103     /// This is implemented as an intrinsic to avoid converting to and from an
1104     /// integer, since the conversion inhibits certain optimizations.
1105     ///
1106     /// # Safety
1107     ///
1108     /// Unlike the `offset` intrinsic, this intrinsic does not restrict the
1109     /// resulting pointer to point into or one byte past the end of an allocated
1110     /// object, and it wraps with two's complement arithmetic. The resulting
1111     /// value is not necessarily valid to be used to actually access memory.
1112     ///
1113     /// The stabilized version of this intrinsic is
1114     /// [`std::pointer::wrapping_offset`](../../std/primitive.pointer.html#method.wrapping_offset).
1115     #[must_use = "returns a new pointer rather than modifying its argument"]
1116     #[rustc_const_unstable(feature = "const_ptr_offset", issue = "71499")]
1117     pub fn arith_offset<T>(dst: *const T, offset: isize) -> *const T;
1118
1119     /// Equivalent to the appropriate `llvm.memcpy.p0i8.0i8.*` intrinsic, with
1120     /// a size of `count` * `size_of::<T>()` and an alignment of
1121     /// `min_align_of::<T>()`
1122     ///
1123     /// The volatile parameter is set to `true`, so it will not be optimized out
1124     /// unless size is equal to zero.
1125     ///
1126     /// This intrinsic does not have a stable counterpart.
1127     pub fn volatile_copy_nonoverlapping_memory<T>(dst: *mut T, src: *const T, count: usize);
1128     /// Equivalent to the appropriate `llvm.memmove.p0i8.0i8.*` intrinsic, with
1129     /// a size of `count * size_of::<T>()` and an alignment of
1130     /// `min_align_of::<T>()`
1131     ///
1132     /// The volatile parameter is set to `true`, so it will not be optimized out
1133     /// unless size is equal to zero.
1134     ///
1135     /// This intrinsic does not have a stable counterpart.
1136     pub fn volatile_copy_memory<T>(dst: *mut T, src: *const T, count: usize);
1137     /// Equivalent to the appropriate `llvm.memset.p0i8.*` intrinsic, with a
1138     /// size of `count * size_of::<T>()` and an alignment of
1139     /// `min_align_of::<T>()`.
1140     ///
1141     /// The volatile parameter is set to `true`, so it will not be optimized out
1142     /// unless size is equal to zero.
1143     ///
1144     /// This intrinsic does not have a stable counterpart.
1145     pub fn volatile_set_memory<T>(dst: *mut T, val: u8, count: usize);
1146
1147     /// Performs a volatile load from the `src` pointer.
1148     ///
1149     /// The stabilized version of this intrinsic is [`core::ptr::read_volatile`](crate::ptr::read_volatile).
1150     pub fn volatile_load<T>(src: *const T) -> T;
1151     /// Performs a volatile store to the `dst` pointer.
1152     ///
1153     /// The stabilized version of this intrinsic is [`core::ptr::write_volatile`](crate::ptr::write_volatile).
1154     pub fn volatile_store<T>(dst: *mut T, val: T);
1155
1156     /// Performs a volatile load from the `src` pointer
1157     /// The pointer is not required to be aligned.
1158     ///
1159     /// This intrinsic does not have a stable counterpart.
1160     pub fn unaligned_volatile_load<T>(src: *const T) -> T;
1161     /// Performs a volatile store to the `dst` pointer.
1162     /// The pointer is not required to be aligned.
1163     ///
1164     /// This intrinsic does not have a stable counterpart.
1165     pub fn unaligned_volatile_store<T>(dst: *mut T, val: T);
1166
1167     /// Returns the square root of an `f32`
1168     ///
1169     /// The stabilized version of this intrinsic is
1170     /// [`f32::sqrt`](../../std/primitive.f32.html#method.sqrt)
1171     pub fn sqrtf32(x: f32) -> f32;
1172     /// Returns the square root of an `f64`
1173     ///
1174     /// The stabilized version of this intrinsic is
1175     /// [`f64::sqrt`](../../std/primitive.f64.html#method.sqrt)
1176     pub fn sqrtf64(x: f64) -> f64;
1177
1178     /// Raises an `f32` to an integer power.
1179     ///
1180     /// The stabilized version of this intrinsic is
1181     /// [`f32::powi`](../../std/primitive.f32.html#method.powi)
1182     pub fn powif32(a: f32, x: i32) -> f32;
1183     /// Raises an `f64` to an integer power.
1184     ///
1185     /// The stabilized version of this intrinsic is
1186     /// [`f64::powi`](../../std/primitive.f64.html#method.powi)
1187     pub fn powif64(a: f64, x: i32) -> f64;
1188
1189     /// Returns the sine of an `f32`.
1190     ///
1191     /// The stabilized version of this intrinsic is
1192     /// [`f32::sin`](../../std/primitive.f32.html#method.sin)
1193     pub fn sinf32(x: f32) -> f32;
1194     /// Returns the sine of an `f64`.
1195     ///
1196     /// The stabilized version of this intrinsic is
1197     /// [`f64::sin`](../../std/primitive.f64.html#method.sin)
1198     pub fn sinf64(x: f64) -> f64;
1199
1200     /// Returns the cosine of an `f32`.
1201     ///
1202     /// The stabilized version of this intrinsic is
1203     /// [`f32::cos`](../../std/primitive.f32.html#method.cos)
1204     pub fn cosf32(x: f32) -> f32;
1205     /// Returns the cosine of an `f64`.
1206     ///
1207     /// The stabilized version of this intrinsic is
1208     /// [`f64::cos`](../../std/primitive.f64.html#method.cos)
1209     pub fn cosf64(x: f64) -> f64;
1210
1211     /// Raises an `f32` to an `f32` power.
1212     ///
1213     /// The stabilized version of this intrinsic is
1214     /// [`f32::powf`](../../std/primitive.f32.html#method.powf)
1215     pub fn powf32(a: f32, x: f32) -> f32;
1216     /// Raises an `f64` to an `f64` power.
1217     ///
1218     /// The stabilized version of this intrinsic is
1219     /// [`f64::powf`](../../std/primitive.f64.html#method.powf)
1220     pub fn powf64(a: f64, x: f64) -> f64;
1221
1222     /// Returns the exponential of an `f32`.
1223     ///
1224     /// The stabilized version of this intrinsic is
1225     /// [`f32::exp`](../../std/primitive.f32.html#method.exp)
1226     pub fn expf32(x: f32) -> f32;
1227     /// Returns the exponential of an `f64`.
1228     ///
1229     /// The stabilized version of this intrinsic is
1230     /// [`f64::exp`](../../std/primitive.f64.html#method.exp)
1231     pub fn expf64(x: f64) -> f64;
1232
1233     /// Returns 2 raised to the power of an `f32`.
1234     ///
1235     /// The stabilized version of this intrinsic is
1236     /// [`f32::exp2`](../../std/primitive.f32.html#method.exp2)
1237     pub fn exp2f32(x: f32) -> f32;
1238     /// Returns 2 raised to the power of an `f64`.
1239     ///
1240     /// The stabilized version of this intrinsic is
1241     /// [`f64::exp2`](../../std/primitive.f64.html#method.exp2)
1242     pub fn exp2f64(x: f64) -> f64;
1243
1244     /// Returns the natural logarithm of an `f32`.
1245     ///
1246     /// The stabilized version of this intrinsic is
1247     /// [`f32::ln`](../../std/primitive.f32.html#method.ln)
1248     pub fn logf32(x: f32) -> f32;
1249     /// Returns the natural logarithm of an `f64`.
1250     ///
1251     /// The stabilized version of this intrinsic is
1252     /// [`f64::ln`](../../std/primitive.f64.html#method.ln)
1253     pub fn logf64(x: f64) -> f64;
1254
1255     /// Returns the base 10 logarithm of an `f32`.
1256     ///
1257     /// The stabilized version of this intrinsic is
1258     /// [`f32::log10`](../../std/primitive.f32.html#method.log10)
1259     pub fn log10f32(x: f32) -> f32;
1260     /// Returns the base 10 logarithm of an `f64`.
1261     ///
1262     /// The stabilized version of this intrinsic is
1263     /// [`f64::log10`](../../std/primitive.f64.html#method.log10)
1264     pub fn log10f64(x: f64) -> f64;
1265
1266     /// Returns the base 2 logarithm of an `f32`.
1267     ///
1268     /// The stabilized version of this intrinsic is
1269     /// [`f32::log2`](../../std/primitive.f32.html#method.log2)
1270     pub fn log2f32(x: f32) -> f32;
1271     /// Returns the base 2 logarithm of an `f64`.
1272     ///
1273     /// The stabilized version of this intrinsic is
1274     /// [`f64::log2`](../../std/primitive.f64.html#method.log2)
1275     pub fn log2f64(x: f64) -> f64;
1276
1277     /// Returns `a * b + c` for `f32` values.
1278     ///
1279     /// The stabilized version of this intrinsic is
1280     /// [`f32::mul_add`](../../std/primitive.f32.html#method.mul_add)
1281     pub fn fmaf32(a: f32, b: f32, c: f32) -> f32;
1282     /// Returns `a * b + c` for `f64` values.
1283     ///
1284     /// The stabilized version of this intrinsic is
1285     /// [`f64::mul_add`](../../std/primitive.f64.html#method.mul_add)
1286     pub fn fmaf64(a: f64, b: f64, c: f64) -> f64;
1287
1288     /// Returns the absolute value of an `f32`.
1289     ///
1290     /// The stabilized version of this intrinsic is
1291     /// [`f32::abs`](../../std/primitive.f32.html#method.abs)
1292     pub fn fabsf32(x: f32) -> f32;
1293     /// Returns the absolute value of an `f64`.
1294     ///
1295     /// The stabilized version of this intrinsic is
1296     /// [`f64::abs`](../../std/primitive.f64.html#method.abs)
1297     pub fn fabsf64(x: f64) -> f64;
1298
1299     /// Returns the minimum of two `f32` values.
1300     ///
1301     /// The stabilized version of this intrinsic is
1302     /// [`f32::min`]
1303     pub fn minnumf32(x: f32, y: f32) -> f32;
1304     /// Returns the minimum of two `f64` values.
1305     ///
1306     /// The stabilized version of this intrinsic is
1307     /// [`f64::min`]
1308     pub fn minnumf64(x: f64, y: f64) -> f64;
1309     /// Returns the maximum of two `f32` values.
1310     ///
1311     /// The stabilized version of this intrinsic is
1312     /// [`f32::max`]
1313     pub fn maxnumf32(x: f32, y: f32) -> f32;
1314     /// Returns the maximum of two `f64` values.
1315     ///
1316     /// The stabilized version of this intrinsic is
1317     /// [`f64::max`]
1318     pub fn maxnumf64(x: f64, y: f64) -> f64;
1319
1320     /// Copies the sign from `y` to `x` for `f32` values.
1321     ///
1322     /// The stabilized version of this intrinsic is
1323     /// [`f32::copysign`](../../std/primitive.f32.html#method.copysign)
1324     pub fn copysignf32(x: f32, y: f32) -> f32;
1325     /// Copies the sign from `y` to `x` for `f64` values.
1326     ///
1327     /// The stabilized version of this intrinsic is
1328     /// [`f64::copysign`](../../std/primitive.f64.html#method.copysign)
1329     pub fn copysignf64(x: f64, y: f64) -> f64;
1330
1331     /// Returns the largest integer less than or equal to an `f32`.
1332     ///
1333     /// The stabilized version of this intrinsic is
1334     /// [`f32::floor`](../../std/primitive.f32.html#method.floor)
1335     pub fn floorf32(x: f32) -> f32;
1336     /// Returns the largest integer less than or equal to an `f64`.
1337     ///
1338     /// The stabilized version of this intrinsic is
1339     /// [`f64::floor`](../../std/primitive.f64.html#method.floor)
1340     pub fn floorf64(x: f64) -> f64;
1341
1342     /// Returns the smallest integer greater than or equal to an `f32`.
1343     ///
1344     /// The stabilized version of this intrinsic is
1345     /// [`f32::ceil`](../../std/primitive.f32.html#method.ceil)
1346     pub fn ceilf32(x: f32) -> f32;
1347     /// Returns the smallest integer greater than or equal to an `f64`.
1348     ///
1349     /// The stabilized version of this intrinsic is
1350     /// [`f64::ceil`](../../std/primitive.f64.html#method.ceil)
1351     pub fn ceilf64(x: f64) -> f64;
1352
1353     /// Returns the integer part of an `f32`.
1354     ///
1355     /// The stabilized version of this intrinsic is
1356     /// [`f32::trunc`](../../std/primitive.f32.html#method.trunc)
1357     pub fn truncf32(x: f32) -> f32;
1358     /// Returns the integer part of an `f64`.
1359     ///
1360     /// The stabilized version of this intrinsic is
1361     /// [`f64::trunc`](../../std/primitive.f64.html#method.trunc)
1362     pub fn truncf64(x: f64) -> f64;
1363
1364     /// Returns the nearest integer to an `f32`. May raise an inexact floating-point exception
1365     /// if the argument is not an integer.
1366     pub fn rintf32(x: f32) -> f32;
1367     /// Returns the nearest integer to an `f64`. May raise an inexact floating-point exception
1368     /// if the argument is not an integer.
1369     pub fn rintf64(x: f64) -> f64;
1370
1371     /// Returns the nearest integer to an `f32`.
1372     ///
1373     /// This intrinsic does not have a stable counterpart.
1374     pub fn nearbyintf32(x: f32) -> f32;
1375     /// Returns the nearest integer to an `f64`.
1376     ///
1377     /// This intrinsic does not have a stable counterpart.
1378     pub fn nearbyintf64(x: f64) -> f64;
1379
1380     /// Returns the nearest integer to an `f32`. Rounds half-way cases away from zero.
1381     ///
1382     /// The stabilized version of this intrinsic is
1383     /// [`f32::round`](../../std/primitive.f32.html#method.round)
1384     pub fn roundf32(x: f32) -> f32;
1385     /// Returns the nearest integer to an `f64`. Rounds half-way cases away from zero.
1386     ///
1387     /// The stabilized version of this intrinsic is
1388     /// [`f64::round`](../../std/primitive.f64.html#method.round)
1389     pub fn roundf64(x: f64) -> f64;
1390
1391     /// Float addition that allows optimizations based on algebraic rules.
1392     /// May assume inputs are finite.
1393     ///
1394     /// This intrinsic does not have a stable counterpart.
1395     pub fn fadd_fast<T: Copy>(a: T, b: T) -> T;
1396
1397     /// Float subtraction that allows optimizations based on algebraic rules.
1398     /// May assume inputs are finite.
1399     ///
1400     /// This intrinsic does not have a stable counterpart.
1401     pub fn fsub_fast<T: Copy>(a: T, b: T) -> T;
1402
1403     /// Float multiplication that allows optimizations based on algebraic rules.
1404     /// May assume inputs are finite.
1405     ///
1406     /// This intrinsic does not have a stable counterpart.
1407     pub fn fmul_fast<T: Copy>(a: T, b: T) -> T;
1408
1409     /// Float division that allows optimizations based on algebraic rules.
1410     /// May assume inputs are finite.
1411     ///
1412     /// This intrinsic does not have a stable counterpart.
1413     pub fn fdiv_fast<T: Copy>(a: T, b: T) -> T;
1414
1415     /// Float remainder that allows optimizations based on algebraic rules.
1416     /// May assume inputs are finite.
1417     ///
1418     /// This intrinsic does not have a stable counterpart.
1419     pub fn frem_fast<T: Copy>(a: T, b: T) -> T;
1420
1421     /// Convert with LLVM’s fptoui/fptosi, which may return undef for values out of range
1422     /// (<https://github.com/rust-lang/rust/issues/10184>)
1423     ///
1424     /// Stabilized as [`f32::to_int_unchecked`] and [`f64::to_int_unchecked`].
1425     pub fn float_to_int_unchecked<Float: Copy, Int: Copy>(value: Float) -> Int;
1426
1427     /// Returns the number of bits set in an integer type `T`
1428     ///
1429     /// The stabilized versions of this intrinsic are available on the integer
1430     /// primitives via the `count_ones` method. For example,
1431     /// [`u32::count_ones`]
1432     #[rustc_const_stable(feature = "const_ctpop", since = "1.40.0")]
1433     pub fn ctpop<T: Copy>(x: T) -> T;
1434
1435     /// Returns the number of leading unset bits (zeroes) in an integer type `T`.
1436     ///
1437     /// The stabilized versions of this intrinsic are available on the integer
1438     /// primitives via the `leading_zeros` method. For example,
1439     /// [`u32::leading_zeros`]
1440     ///
1441     /// # Examples
1442     ///
1443     /// ```
1444     /// #![feature(core_intrinsics)]
1445     ///
1446     /// use std::intrinsics::ctlz;
1447     ///
1448     /// let x = 0b0001_1100_u8;
1449     /// let num_leading = ctlz(x);
1450     /// assert_eq!(num_leading, 3);
1451     /// ```
1452     ///
1453     /// An `x` with value `0` will return the bit width of `T`.
1454     ///
1455     /// ```
1456     /// #![feature(core_intrinsics)]
1457     ///
1458     /// use std::intrinsics::ctlz;
1459     ///
1460     /// let x = 0u16;
1461     /// let num_leading = ctlz(x);
1462     /// assert_eq!(num_leading, 16);
1463     /// ```
1464     #[rustc_const_stable(feature = "const_ctlz", since = "1.40.0")]
1465     pub fn ctlz<T: Copy>(x: T) -> T;
1466
1467     /// Like `ctlz`, but extra-unsafe as it returns `undef` when
1468     /// given an `x` with value `0`.
1469     ///
1470     /// This intrinsic does not have a stable counterpart.
1471     ///
1472     /// # Examples
1473     ///
1474     /// ```
1475     /// #![feature(core_intrinsics)]
1476     ///
1477     /// use std::intrinsics::ctlz_nonzero;
1478     ///
1479     /// let x = 0b0001_1100_u8;
1480     /// let num_leading = unsafe { ctlz_nonzero(x) };
1481     /// assert_eq!(num_leading, 3);
1482     /// ```
1483     #[rustc_const_stable(feature = "constctlz", since = "1.50.0")]
1484     pub fn ctlz_nonzero<T: Copy>(x: T) -> T;
1485
1486     /// Returns the number of trailing unset bits (zeroes) in an integer type `T`.
1487     ///
1488     /// The stabilized versions of this intrinsic are available on the integer
1489     /// primitives via the `trailing_zeros` method. For example,
1490     /// [`u32::trailing_zeros`]
1491     ///
1492     /// # Examples
1493     ///
1494     /// ```
1495     /// #![feature(core_intrinsics)]
1496     ///
1497     /// use std::intrinsics::cttz;
1498     ///
1499     /// let x = 0b0011_1000_u8;
1500     /// let num_trailing = cttz(x);
1501     /// assert_eq!(num_trailing, 3);
1502     /// ```
1503     ///
1504     /// An `x` with value `0` will return the bit width of `T`:
1505     ///
1506     /// ```
1507     /// #![feature(core_intrinsics)]
1508     ///
1509     /// use std::intrinsics::cttz;
1510     ///
1511     /// let x = 0u16;
1512     /// let num_trailing = cttz(x);
1513     /// assert_eq!(num_trailing, 16);
1514     /// ```
1515     #[rustc_const_stable(feature = "const_cttz", since = "1.40.0")]
1516     pub fn cttz<T: Copy>(x: T) -> T;
1517
1518     /// Like `cttz`, but extra-unsafe as it returns `undef` when
1519     /// given an `x` with value `0`.
1520     ///
1521     /// This intrinsic does not have a stable counterpart.
1522     ///
1523     /// # Examples
1524     ///
1525     /// ```
1526     /// #![feature(core_intrinsics)]
1527     ///
1528     /// use std::intrinsics::cttz_nonzero;
1529     ///
1530     /// let x = 0b0011_1000_u8;
1531     /// let num_trailing = unsafe { cttz_nonzero(x) };
1532     /// assert_eq!(num_trailing, 3);
1533     /// ```
1534     #[rustc_const_unstable(feature = "const_cttz", issue = "none")]
1535     pub fn cttz_nonzero<T: Copy>(x: T) -> T;
1536
1537     /// Reverses the bytes in an integer type `T`.
1538     ///
1539     /// The stabilized versions of this intrinsic are available on the integer
1540     /// primitives via the `swap_bytes` method. For example,
1541     /// [`u32::swap_bytes`]
1542     #[rustc_const_stable(feature = "const_bswap", since = "1.40.0")]
1543     pub fn bswap<T: Copy>(x: T) -> T;
1544
1545     /// Reverses the bits in an integer type `T`.
1546     ///
1547     /// The stabilized versions of this intrinsic are available on the integer
1548     /// primitives via the `reverse_bits` method. For example,
1549     /// [`u32::reverse_bits`]
1550     #[rustc_const_stable(feature = "const_bitreverse", since = "1.40.0")]
1551     pub fn bitreverse<T: Copy>(x: T) -> T;
1552
1553     /// Performs checked integer addition.
1554     ///
1555     /// The stabilized versions of this intrinsic are available on the integer
1556     /// primitives via the `overflowing_add` method. For example,
1557     /// [`u32::overflowing_add`]
1558     #[rustc_const_stable(feature = "const_int_overflow", since = "1.40.0")]
1559     pub fn add_with_overflow<T: Copy>(x: T, y: T) -> (T, bool);
1560
1561     /// Performs checked integer subtraction
1562     ///
1563     /// The stabilized versions of this intrinsic are available on the integer
1564     /// primitives via the `overflowing_sub` method. For example,
1565     /// [`u32::overflowing_sub`]
1566     #[rustc_const_stable(feature = "const_int_overflow", since = "1.40.0")]
1567     pub fn sub_with_overflow<T: Copy>(x: T, y: T) -> (T, bool);
1568
1569     /// Performs checked integer multiplication
1570     ///
1571     /// The stabilized versions of this intrinsic are available on the integer
1572     /// primitives via the `overflowing_mul` method. For example,
1573     /// [`u32::overflowing_mul`]
1574     #[rustc_const_stable(feature = "const_int_overflow", since = "1.40.0")]
1575     pub fn mul_with_overflow<T: Copy>(x: T, y: T) -> (T, bool);
1576
1577     /// Performs an exact division, resulting in undefined behavior where
1578     /// `x % y != 0` or `y == 0` or `x == T::MIN && y == -1`
1579     ///
1580     /// This intrinsic does not have a stable counterpart.
1581     pub fn exact_div<T: Copy>(x: T, y: T) -> T;
1582
1583     /// Performs an unchecked division, resulting in undefined behavior
1584     /// where `y == 0` or `x == T::MIN && y == -1`
1585     ///
1586     /// Safe wrappers for this intrinsic are available on the integer
1587     /// primitives via the `checked_div` method. For example,
1588     /// [`u32::checked_div`]
1589     #[rustc_const_stable(feature = "const_int_unchecked_arith", since = "1.52.0")]
1590     pub fn unchecked_div<T: Copy>(x: T, y: T) -> T;
1591     /// Returns the remainder of an unchecked division, resulting in
1592     /// undefined behavior when `y == 0` or `x == T::MIN && y == -1`
1593     ///
1594     /// Safe wrappers for this intrinsic are available on the integer
1595     /// primitives via the `checked_rem` method. For example,
1596     /// [`u32::checked_rem`]
1597     #[rustc_const_stable(feature = "const_int_unchecked_arith", since = "1.52.0")]
1598     pub fn unchecked_rem<T: Copy>(x: T, y: T) -> T;
1599
1600     /// Performs an unchecked left shift, resulting in undefined behavior when
1601     /// `y < 0` or `y >= N`, where N is the width of T in bits.
1602     ///
1603     /// Safe wrappers for this intrinsic are available on the integer
1604     /// primitives via the `checked_shl` method. For example,
1605     /// [`u32::checked_shl`]
1606     #[rustc_const_stable(feature = "const_int_unchecked", since = "1.40.0")]
1607     pub fn unchecked_shl<T: Copy>(x: T, y: T) -> T;
1608     /// Performs an unchecked right shift, resulting in undefined behavior when
1609     /// `y < 0` or `y >= N`, where N is the width of T in bits.
1610     ///
1611     /// Safe wrappers for this intrinsic are available on the integer
1612     /// primitives via the `checked_shr` method. For example,
1613     /// [`u32::checked_shr`]
1614     #[rustc_const_stable(feature = "const_int_unchecked", since = "1.40.0")]
1615     pub fn unchecked_shr<T: Copy>(x: T, y: T) -> T;
1616
1617     /// Returns the result of an unchecked addition, resulting in
1618     /// undefined behavior when `x + y > T::MAX` or `x + y < T::MIN`.
1619     ///
1620     /// This intrinsic does not have a stable counterpart.
1621     #[rustc_const_unstable(feature = "const_int_unchecked_arith", issue = "none")]
1622     pub fn unchecked_add<T: Copy>(x: T, y: T) -> T;
1623
1624     /// Returns the result of an unchecked subtraction, resulting in
1625     /// undefined behavior when `x - y > T::MAX` or `x - y < T::MIN`.
1626     ///
1627     /// This intrinsic does not have a stable counterpart.
1628     #[rustc_const_unstable(feature = "const_int_unchecked_arith", issue = "none")]
1629     pub fn unchecked_sub<T: Copy>(x: T, y: T) -> T;
1630
1631     /// Returns the result of an unchecked multiplication, resulting in
1632     /// undefined behavior when `x * y > T::MAX` or `x * y < T::MIN`.
1633     ///
1634     /// This intrinsic does not have a stable counterpart.
1635     #[rustc_const_unstable(feature = "const_int_unchecked_arith", issue = "none")]
1636     pub fn unchecked_mul<T: Copy>(x: T, y: T) -> T;
1637
1638     /// Performs rotate left.
1639     ///
1640     /// The stabilized versions of this intrinsic are available on the integer
1641     /// primitives via the `rotate_left` method. For example,
1642     /// [`u32::rotate_left`]
1643     #[rustc_const_stable(feature = "const_int_rotate", since = "1.40.0")]
1644     pub fn rotate_left<T: Copy>(x: T, y: T) -> T;
1645
1646     /// Performs rotate right.
1647     ///
1648     /// The stabilized versions of this intrinsic are available on the integer
1649     /// primitives via the `rotate_right` method. For example,
1650     /// [`u32::rotate_right`]
1651     #[rustc_const_stable(feature = "const_int_rotate", since = "1.40.0")]
1652     pub fn rotate_right<T: Copy>(x: T, y: T) -> T;
1653
1654     /// Returns (a + b) mod 2<sup>N</sup>, where N is the width of T in bits.
1655     ///
1656     /// The stabilized versions of this intrinsic are available on the integer
1657     /// primitives via the `wrapping_add` method. For example,
1658     /// [`u32::wrapping_add`]
1659     #[rustc_const_stable(feature = "const_int_wrapping", since = "1.40.0")]
1660     pub fn wrapping_add<T: Copy>(a: T, b: T) -> T;
1661     /// Returns (a - b) mod 2<sup>N</sup>, where N is the width of T in bits.
1662     ///
1663     /// The stabilized versions of this intrinsic are available on the integer
1664     /// primitives via the `wrapping_sub` method. For example,
1665     /// [`u32::wrapping_sub`]
1666     #[rustc_const_stable(feature = "const_int_wrapping", since = "1.40.0")]
1667     pub fn wrapping_sub<T: Copy>(a: T, b: T) -> T;
1668     /// Returns (a * b) mod 2<sup>N</sup>, where N is the width of T in bits.
1669     ///
1670     /// The stabilized versions of this intrinsic are available on the integer
1671     /// primitives via the `wrapping_mul` method. For example,
1672     /// [`u32::wrapping_mul`]
1673     #[rustc_const_stable(feature = "const_int_wrapping", since = "1.40.0")]
1674     pub fn wrapping_mul<T: Copy>(a: T, b: T) -> T;
1675
1676     /// Computes `a + b`, saturating at numeric bounds.
1677     ///
1678     /// The stabilized versions of this intrinsic are available on the integer
1679     /// primitives via the `saturating_add` method. For example,
1680     /// [`u32::saturating_add`]
1681     #[rustc_const_stable(feature = "const_int_saturating", since = "1.40.0")]
1682     pub fn saturating_add<T: Copy>(a: T, b: T) -> T;
1683     /// Computes `a - b`, saturating at numeric bounds.
1684     ///
1685     /// The stabilized versions of this intrinsic are available on the integer
1686     /// primitives via the `saturating_sub` method. For example,
1687     /// [`u32::saturating_sub`]
1688     #[rustc_const_stable(feature = "const_int_saturating", since = "1.40.0")]
1689     pub fn saturating_sub<T: Copy>(a: T, b: T) -> T;
1690
1691     /// Returns the value of the discriminant for the variant in 'v';
1692     /// if `T` has no discriminant, returns `0`.
1693     ///
1694     /// The stabilized version of this intrinsic is [`core::mem::discriminant`](crate::mem::discriminant).
1695     #[rustc_const_unstable(feature = "const_discriminant", issue = "69821")]
1696     pub fn discriminant_value<T>(v: &T) -> <T as DiscriminantKind>::Discriminant;
1697
1698     /// Returns the number of variants of the type `T` cast to a `usize`;
1699     /// if `T` has no variants, returns `0`. Uninhabited variants will be counted.
1700     ///
1701     /// The to-be-stabilized version of this intrinsic is [`mem::variant_count`].
1702     #[rustc_const_unstable(feature = "variant_count", issue = "73662")]
1703     pub fn variant_count<T>() -> usize;
1704
1705     /// Rust's "try catch" construct which invokes the function pointer `try_fn`
1706     /// with the data pointer `data`.
1707     ///
1708     /// The third argument is a function called if a panic occurs. This function
1709     /// takes the data pointer and a pointer to the target-specific exception
1710     /// object that was caught. For more information see the compiler's
1711     /// source as well as std's catch implementation.
1712     pub fn r#try(try_fn: fn(*mut u8), data: *mut u8, catch_fn: fn(*mut u8, *mut u8)) -> i32;
1713
1714     /// Emits a `!nontemporal` store according to LLVM (see their docs).
1715     /// Probably will never become stable.
1716     pub fn nontemporal_store<T>(ptr: *mut T, val: T);
1717
1718     /// See documentation of `<*const T>::offset_from` for details.
1719     #[rustc_const_unstable(feature = "const_ptr_offset_from", issue = "41079")]
1720     pub fn ptr_offset_from<T>(ptr: *const T, base: *const T) -> isize;
1721
1722     /// See documentation of `<*const T>::guaranteed_eq` for details.
1723     #[rustc_const_unstable(feature = "const_raw_ptr_comparison", issue = "53020")]
1724     pub fn ptr_guaranteed_eq<T>(ptr: *const T, other: *const T) -> bool;
1725
1726     /// See documentation of `<*const T>::guaranteed_ne` for details.
1727     #[rustc_const_unstable(feature = "const_raw_ptr_comparison", issue = "53020")]
1728     pub fn ptr_guaranteed_ne<T>(ptr: *const T, other: *const T) -> bool;
1729
1730     /// Allocate at compile time. Should not be called at runtime.
1731     #[rustc_const_unstable(feature = "const_heap", issue = "79597")]
1732     pub fn const_allocate(size: usize, align: usize) -> *mut u8;
1733
1734     /// Copies `count * size_of::<T>()` bytes from `src` to `dst`. The source
1735     /// and destination must *not* overlap.
1736     ///
1737     /// For regions of memory which might overlap, use [`copy`] instead.
1738     ///
1739     /// `copy_nonoverlapping` is semantically equivalent to C's [`memcpy`], but
1740     /// with the argument order swapped.
1741     ///
1742     /// [`memcpy`]: https://en.cppreference.com/w/c/string/byte/memcpy
1743     ///
1744     /// # Safety
1745     ///
1746     /// Behavior is undefined if any of the following conditions are violated:
1747     ///
1748     /// * `src` must be [valid] for reads of `count * size_of::<T>()` bytes.
1749     ///
1750     /// * `dst` must be [valid] for writes of `count * size_of::<T>()` bytes.
1751     ///
1752     /// * Both `src` and `dst` must be properly aligned.
1753     ///
1754     /// * The region of memory beginning at `src` with a size of `count *
1755     ///   size_of::<T>()` bytes must *not* overlap with the region of memory
1756     ///   beginning at `dst` with the same size.
1757     ///
1758     /// Like [`read`], `copy_nonoverlapping` creates a bitwise copy of `T`, regardless of
1759     /// whether `T` is [`Copy`]. If `T` is not [`Copy`], using *both* the values
1760     /// in the region beginning at `*src` and the region beginning at `*dst` can
1761     /// [violate memory safety][read-ownership].
1762     ///
1763     /// Note that even if the effectively copied size (`count * size_of::<T>()`) is
1764     /// `0`, the pointers must be non-NULL and properly aligned.
1765     ///
1766     /// [`read`]: crate::ptr::read
1767     /// [read-ownership]: crate::ptr::read#ownership-of-the-returned-value
1768     /// [valid]: crate::ptr#safety
1769     ///
1770     /// # Examples
1771     ///
1772     /// Manually implement [`Vec::append`]:
1773     ///
1774     /// ```
1775     /// use std::ptr;
1776     ///
1777     /// /// Moves all the elements of `src` into `dst`, leaving `src` empty.
1778     /// fn append<T>(dst: &mut Vec<T>, src: &mut Vec<T>) {
1779     ///     let src_len = src.len();
1780     ///     let dst_len = dst.len();
1781     ///
1782     ///     // Ensure that `dst` has enough capacity to hold all of `src`.
1783     ///     dst.reserve(src_len);
1784     ///
1785     ///     unsafe {
1786     ///         // The call to offset is always safe because `Vec` will never
1787     ///         // allocate more than `isize::MAX` bytes.
1788     ///         let dst_ptr = dst.as_mut_ptr().offset(dst_len as isize);
1789     ///         let src_ptr = src.as_ptr();
1790     ///
1791     ///         // Truncate `src` without dropping its contents. We do this first,
1792     ///         // to avoid problems in case something further down panics.
1793     ///         src.set_len(0);
1794     ///
1795     ///         // The two regions cannot overlap because mutable references do
1796     ///         // not alias, and two different vectors cannot own the same
1797     ///         // memory.
1798     ///         ptr::copy_nonoverlapping(src_ptr, dst_ptr, src_len);
1799     ///
1800     ///         // Notify `dst` that it now holds the contents of `src`.
1801     ///         dst.set_len(dst_len + src_len);
1802     ///     }
1803     /// }
1804     ///
1805     /// let mut a = vec!['r'];
1806     /// let mut b = vec!['u', 's', 't'];
1807     ///
1808     /// append(&mut a, &mut b);
1809     ///
1810     /// assert_eq!(a, &['r', 'u', 's', 't']);
1811     /// assert!(b.is_empty());
1812     /// ```
1813     ///
1814     /// [`Vec::append`]: ../../std/vec/struct.Vec.html#method.append
1815     #[doc(alias = "memcpy")]
1816     #[stable(feature = "rust1", since = "1.0.0")]
1817     #[rustc_const_unstable(feature = "const_intrinsic_copy", issue = "80697")]
1818     pub fn copy_nonoverlapping<T>(src: *const T, dst: *mut T, count: usize);
1819
1820     /// Copies `count * size_of::<T>()` bytes from `src` to `dst`. The source
1821     /// and destination may overlap.
1822     ///
1823     /// If the source and destination will *never* overlap,
1824     /// [`copy_nonoverlapping`] can be used instead.
1825     ///
1826     /// `copy` is semantically equivalent to C's [`memmove`], but with the argument
1827     /// order swapped. Copying takes place as if the bytes were copied from `src`
1828     /// to a temporary array and then copied from the array to `dst`.
1829     ///
1830     /// [`memmove`]: https://en.cppreference.com/w/c/string/byte/memmove
1831     ///
1832     /// # Safety
1833     ///
1834     /// Behavior is undefined if any of the following conditions are violated:
1835     ///
1836     /// * `src` must be [valid] for reads of `count * size_of::<T>()` bytes.
1837     ///
1838     /// * `dst` must be [valid] for writes of `count * size_of::<T>()` bytes.
1839     ///
1840     /// * Both `src` and `dst` must be properly aligned.
1841     ///
1842     /// Like [`read`], `copy` creates a bitwise copy of `T`, regardless of
1843     /// whether `T` is [`Copy`]. If `T` is not [`Copy`], using both the values
1844     /// in the region beginning at `*src` and the region beginning at `*dst` can
1845     /// [violate memory safety][read-ownership].
1846     ///
1847     /// Note that even if the effectively copied size (`count * size_of::<T>()`) is
1848     /// `0`, the pointers must be non-NULL and properly aligned.
1849     ///
1850     /// [`read`]: crate::ptr::read
1851     /// [read-ownership]: crate::ptr::read#ownership-of-the-returned-value
1852     /// [valid]: crate::ptr#safety
1853     ///
1854     /// # Examples
1855     ///
1856     /// Efficiently create a Rust vector from an unsafe buffer:
1857     ///
1858     /// ```
1859     /// use std::ptr;
1860     ///
1861     /// /// # Safety
1862     /// ///
1863     /// /// * `ptr` must be correctly aligned for its type and non-zero.
1864     /// /// * `ptr` must be valid for reads of `elts` contiguous elements of type `T`.
1865     /// /// * Those elements must not be used after calling this function unless `T: Copy`.
1866     /// # #[allow(dead_code)]
1867     /// unsafe fn from_buf_raw<T>(ptr: *const T, elts: usize) -> Vec<T> {
1868     ///     let mut dst = Vec::with_capacity(elts);
1869     ///
1870     ///     // SAFETY: Our precondition ensures the source is aligned and valid,
1871     ///     // and `Vec::with_capacity` ensures that we have usable space to write them.
1872     ///     ptr::copy(ptr, dst.as_mut_ptr(), elts);
1873     ///
1874     ///     // SAFETY: We created it with this much capacity earlier,
1875     ///     // and the previous `copy` has initialized these elements.
1876     ///     dst.set_len(elts);
1877     ///     dst
1878     /// }
1879     /// ```
1880     #[doc(alias = "memmove")]
1881     #[stable(feature = "rust1", since = "1.0.0")]
1882     #[rustc_const_unstable(feature = "const_intrinsic_copy", issue = "80697")]
1883     pub fn copy<T>(src: *const T, dst: *mut T, count: usize);
1884 }
1885
1886 // Some functions are defined here because they accidentally got made
1887 // available in this module on stable. See <https://github.com/rust-lang/rust/issues/15702>.
1888 // (`transmute` also falls into this category, but it cannot be wrapped due to the
1889 // check that `T` and `U` have the same size.)
1890
1891 /// Checks whether `ptr` is properly aligned with respect to
1892 /// `align_of::<T>()`.
1893 pub(crate) fn is_aligned_and_not_null<T>(ptr: *const T) -> bool {
1894     !ptr.is_null() && ptr as usize % mem::align_of::<T>() == 0
1895 }
1896
1897 /// Checks whether the regions of memory starting at `src` and `dst` of size
1898 /// `count * size_of::<T>()` do *not* overlap.
1899 pub(crate) fn is_nonoverlapping<T>(src: *const T, dst: *const T, count: usize) -> bool {
1900     let src_usize = src as usize;
1901     let dst_usize = dst as usize;
1902     let size = mem::size_of::<T>().checked_mul(count).unwrap();
1903     let diff = if src_usize > dst_usize { src_usize - dst_usize } else { dst_usize - src_usize };
1904     // If the absolute distance between the ptrs is at least as big as the size of the buffer,
1905     // they do not overlap.
1906     diff >= size
1907 }
1908
1909 /// Sets `count * size_of::<T>()` bytes of memory starting at `dst` to
1910 /// `val`.
1911 ///
1912 /// `write_bytes` is similar to C's [`memset`], but sets `count *
1913 /// size_of::<T>()` bytes to `val`.
1914 ///
1915 /// [`memset`]: https://en.cppreference.com/w/c/string/byte/memset
1916 ///
1917 /// # Safety
1918 ///
1919 /// Behavior is undefined if any of the following conditions are violated:
1920 ///
1921 /// * `dst` must be [valid] for writes of `count * size_of::<T>()` bytes.
1922 ///
1923 /// * `dst` must be properly aligned.
1924 ///
1925 /// Additionally, the caller must ensure that writing `count *
1926 /// size_of::<T>()` bytes to the given region of memory results in a valid
1927 /// value of `T`. Using a region of memory typed as a `T` that contains an
1928 /// invalid value of `T` is undefined behavior.
1929 ///
1930 /// Note that even if the effectively copied size (`count * size_of::<T>()`) is
1931 /// `0`, the pointer must be non-NULL and properly aligned.
1932 ///
1933 /// [valid]: crate::ptr#safety
1934 ///
1935 /// # Examples
1936 ///
1937 /// Basic usage:
1938 ///
1939 /// ```
1940 /// use std::ptr;
1941 ///
1942 /// let mut vec = vec![0u32; 4];
1943 /// unsafe {
1944 ///     let vec_ptr = vec.as_mut_ptr();
1945 ///     ptr::write_bytes(vec_ptr, 0xfe, 2);
1946 /// }
1947 /// assert_eq!(vec, [0xfefefefe, 0xfefefefe, 0, 0]);
1948 /// ```
1949 ///
1950 /// Creating an invalid value:
1951 ///
1952 /// ```
1953 /// use std::ptr;
1954 ///
1955 /// let mut v = Box::new(0i32);
1956 ///
1957 /// unsafe {
1958 ///     // Leaks the previously held value by overwriting the `Box<T>` with
1959 ///     // a null pointer.
1960 ///     ptr::write_bytes(&mut v as *mut Box<i32>, 0, 1);
1961 /// }
1962 ///
1963 /// // At this point, using or dropping `v` results in undefined behavior.
1964 /// // drop(v); // ERROR
1965 ///
1966 /// // Even leaking `v` "uses" it, and hence is undefined behavior.
1967 /// // mem::forget(v); // ERROR
1968 ///
1969 /// // In fact, `v` is invalid according to basic type layout invariants, so *any*
1970 /// // operation touching it is undefined behavior.
1971 /// // let v2 = v; // ERROR
1972 ///
1973 /// unsafe {
1974 ///     // Let us instead put in a valid value
1975 ///     ptr::write(&mut v as *mut Box<i32>, Box::new(42i32));
1976 /// }
1977 ///
1978 /// // Now the box is fine
1979 /// assert_eq!(*v, 42);
1980 /// ```
1981 #[stable(feature = "rust1", since = "1.0.0")]
1982 #[inline]
1983 pub unsafe fn write_bytes<T>(dst: *mut T, val: u8, count: usize) {
1984     extern "rust-intrinsic" {
1985         fn write_bytes<T>(dst: *mut T, val: u8, count: usize);
1986     }
1987
1988     debug_assert!(is_aligned_and_not_null(dst), "attempt to write to unaligned or null pointer");
1989
1990     // SAFETY: the safety contract for `write_bytes` must be upheld by the caller.
1991     unsafe { write_bytes(dst, val, count) }
1992 }