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