]> git.lizzy.rs Git - rust.git/blob - src/libcore/intrinsics.rs
Rollup merge of #67354 - VirrageS:blame-wrong-line, r=estebank
[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     pub fn size_of<T>() -> usize;
671
672     /// Moves a value to an uninitialized memory location.
673     ///
674     /// Drop glue is not run on the destination.
675     pub fn move_val_init<T>(dst: *mut T, src: T);
676
677     pub fn min_align_of<T>() -> usize;
678     pub fn pref_align_of<T>() -> usize;
679
680     /// The size of the referenced value in bytes.
681     ///
682     /// The stabilized version of this intrinsic is
683     /// [`std::mem::size_of_val`](../../std/mem/fn.size_of_val.html).
684     pub fn size_of_val<T: ?Sized>(_: &T) -> usize;
685     pub fn min_align_of_val<T: ?Sized>(_: &T) -> usize;
686
687     /// Gets a static string slice containing the name of a type.
688     pub fn type_name<T: ?Sized>() -> &'static str;
689
690     /// Gets an identifier which is globally unique to the specified type. This
691     /// function will return the same value for a type regardless of whichever
692     /// crate it is invoked in.
693     pub fn type_id<T: ?Sized + 'static>() -> u64;
694
695     /// A guard for unsafe functions that cannot ever be executed if `T` is uninhabited:
696     /// This will statically either panic, or do nothing.
697     pub fn panic_if_uninhabited<T>();
698
699     /// Gets a reference to a static `Location` indicating where it was called.
700     pub fn caller_location() -> &'static crate::panic::Location<'static>;
701
702     /// Creates a value initialized to zero.
703     ///
704     /// `init` is unsafe because it returns a zeroed-out datum,
705     /// which is unsafe unless `T` is `Copy`. Also, even if T is
706     /// `Copy`, an all-zero value may not correspond to any legitimate
707     /// state for the type in question.
708     #[unstable(feature = "core_intrinsics",
709                reason = "intrinsics are unlikely to ever be stabilized, instead \
710                          they should be used through stabilized interfaces \
711                          in the rest of the standard library",
712                issue = "0")]
713     #[rustc_deprecated(reason = "superseded by MaybeUninit, removal planned",
714                        since = "1.38.0")]
715     pub fn init<T>() -> T;
716
717     /// Creates an uninitialized value.
718     ///
719     /// `uninit` is unsafe because there is no guarantee of what its
720     /// contents are. In particular its drop-flag may be set to any
721     /// state, which means it may claim either dropped or
722     /// undropped. In the general case one must use `ptr::write` to
723     /// initialize memory previous set to the result of `uninit`.
724     #[unstable(feature = "core_intrinsics",
725                reason = "intrinsics are unlikely to ever be stabilized, instead \
726                          they should be used through stabilized interfaces \
727                          in the rest of the standard library",
728                issue = "0")]
729     #[rustc_deprecated(reason = "superseded by MaybeUninit, removal planned",
730                        since = "1.38.0")]
731     pub fn uninit<T>() -> T;
732
733     /// Moves a value out of scope without running drop glue.
734     pub fn forget<T: ?Sized>(_: T);
735
736     /// Reinterprets the bits of a value of one type as another type.
737     ///
738     /// Both types must have the same size. Neither the original, nor the result,
739     /// may be an [invalid value](../../nomicon/what-unsafe-does.html).
740     ///
741     /// `transmute` is semantically equivalent to a bitwise move of one type
742     /// into another. It copies the bits from the source value into the
743     /// destination value, then forgets the original. It's equivalent to C's
744     /// `memcpy` under the hood, just like `transmute_copy`.
745     ///
746     /// `transmute` is **incredibly** unsafe. There are a vast number of ways to
747     /// cause [undefined behavior][ub] with this function. `transmute` should be
748     /// the absolute last resort.
749     ///
750     /// The [nomicon](../../nomicon/transmutes.html) has additional
751     /// documentation.
752     ///
753     /// [ub]: ../../reference/behavior-considered-undefined.html
754     ///
755     /// # Examples
756     ///
757     /// There are a few things that `transmute` is really useful for.
758     ///
759     /// Turning a pointer into a function pointer. This is *not* portable to
760     /// machines where function pointers and data pointers have different sizes.
761     ///
762     /// ```
763     /// fn foo() -> i32 {
764     ///     0
765     /// }
766     /// let pointer = foo as *const ();
767     /// let function = unsafe {
768     ///     std::mem::transmute::<*const (), fn() -> i32>(pointer)
769     /// };
770     /// assert_eq!(function(), 0);
771     /// ```
772     ///
773     /// Extending a lifetime, or shortening an invariant lifetime. This is
774     /// advanced, very unsafe Rust!
775     ///
776     /// ```
777     /// struct R<'a>(&'a i32);
778     /// unsafe fn extend_lifetime<'b>(r: R<'b>) -> R<'static> {
779     ///     std::mem::transmute::<R<'b>, R<'static>>(r)
780     /// }
781     ///
782     /// unsafe fn shorten_invariant_lifetime<'b, 'c>(r: &'b mut R<'static>)
783     ///                                              -> &'b mut R<'c> {
784     ///     std::mem::transmute::<&'b mut R<'static>, &'b mut R<'c>>(r)
785     /// }
786     /// ```
787     ///
788     /// # Alternatives
789     ///
790     /// Don't despair: many uses of `transmute` can be achieved through other means.
791     /// Below are common applications of `transmute` which can be replaced with safer
792     /// constructs.
793     ///
794     /// Turning a pointer into a `usize`:
795     ///
796     /// ```
797     /// let ptr = &0;
798     /// let ptr_num_transmute = unsafe {
799     ///     std::mem::transmute::<&i32, usize>(ptr)
800     /// };
801     ///
802     /// // Use an `as` cast instead
803     /// let ptr_num_cast = ptr as *const i32 as usize;
804     /// ```
805     ///
806     /// Turning a `*mut T` into an `&mut T`:
807     ///
808     /// ```
809     /// let ptr: *mut i32 = &mut 0;
810     /// let ref_transmuted = unsafe {
811     ///     std::mem::transmute::<*mut i32, &mut i32>(ptr)
812     /// };
813     ///
814     /// // Use a reborrow instead
815     /// let ref_casted = unsafe { &mut *ptr };
816     /// ```
817     ///
818     /// Turning an `&mut T` into an `&mut U`:
819     ///
820     /// ```
821     /// let ptr = &mut 0;
822     /// let val_transmuted = unsafe {
823     ///     std::mem::transmute::<&mut i32, &mut u32>(ptr)
824     /// };
825     ///
826     /// // Now, put together `as` and reborrowing - note the chaining of `as`
827     /// // `as` is not transitive
828     /// let val_casts = unsafe { &mut *(ptr as *mut i32 as *mut u32) };
829     /// ```
830     ///
831     /// Turning an `&str` into an `&[u8]`:
832     ///
833     /// ```
834     /// // this is not a good way to do this.
835     /// let slice = unsafe { std::mem::transmute::<&str, &[u8]>("Rust") };
836     /// assert_eq!(slice, &[82, 117, 115, 116]);
837     ///
838     /// // You could use `str::as_bytes`
839     /// let slice = "Rust".as_bytes();
840     /// assert_eq!(slice, &[82, 117, 115, 116]);
841     ///
842     /// // Or, just use a byte string, if you have control over the string
843     /// // literal
844     /// assert_eq!(b"Rust", &[82, 117, 115, 116]);
845     /// ```
846     ///
847     /// Turning a `Vec<&T>` into a `Vec<Option<&T>>`:
848     ///
849     /// ```
850     /// let store = [0, 1, 2, 3];
851     /// let v_orig = store.iter().collect::<Vec<&i32>>();
852     ///
853     /// // clone the vector as we will reuse them later
854     /// let v_clone = v_orig.clone();
855     ///
856     /// // Using transmute: this is Undefined Behavior, and a bad idea.
857     /// // However, it is no-copy.
858     /// let v_transmuted = unsafe {
859     ///     std::mem::transmute::<Vec<&i32>, Vec<Option<&i32>>>(v_clone)
860     /// };
861     ///
862     /// let v_clone = v_orig.clone();
863     ///
864     /// // This is the suggested, safe way.
865     /// // It does copy the entire vector, though, into a new array.
866     /// let v_collected = v_clone.into_iter()
867     ///                          .map(Some)
868     ///                          .collect::<Vec<Option<&i32>>>();
869     ///
870     /// let v_clone = v_orig.clone();
871     ///
872     /// // The no-copy, unsafe way, still using transmute, but not UB.
873     /// // This is equivalent to the original, but safer, and reuses the
874     /// // same `Vec` internals. Therefore, the new inner type must have the
875     /// // exact same size, and the same alignment, as the old type.
876     /// // The same caveats exist for this method as transmute, for
877     /// // the original inner type (`&i32`) to the converted inner type
878     /// // (`Option<&i32>`), so read the nomicon pages linked above.
879     /// let v_from_raw = unsafe {
880     // FIXME Update this when vec_into_raw_parts is stabilized
881     ///     // Ensure the original vector is not dropped.
882     ///     let mut v_clone = std::mem::ManuallyDrop::new(v_clone);
883     ///     Vec::from_raw_parts(v_clone.as_mut_ptr() as *mut Option<&i32>,
884     ///                         v_clone.len(),
885     ///                         v_clone.capacity())
886     /// };
887     /// ```
888     ///
889     /// Implementing `split_at_mut`:
890     ///
891     /// ```
892     /// use std::{slice, mem};
893     ///
894     /// // There are multiple ways to do this, and there are multiple problems
895     /// // with the following (transmute) way.
896     /// fn split_at_mut_transmute<T>(slice: &mut [T], mid: usize)
897     ///                              -> (&mut [T], &mut [T]) {
898     ///     let len = slice.len();
899     ///     assert!(mid <= len);
900     ///     unsafe {
901     ///         let slice2 = mem::transmute::<&mut [T], &mut [T]>(slice);
902     ///         // first: transmute is not typesafe; all it checks is that T and
903     ///         // U are of the same size. Second, right here, you have two
904     ///         // mutable references pointing to the same memory.
905     ///         (&mut slice[0..mid], &mut slice2[mid..len])
906     ///     }
907     /// }
908     ///
909     /// // This gets rid of the typesafety problems; `&mut *` will *only* give
910     /// // you an `&mut T` from an `&mut T` or `*mut T`.
911     /// fn split_at_mut_casts<T>(slice: &mut [T], mid: usize)
912     ///                          -> (&mut [T], &mut [T]) {
913     ///     let len = slice.len();
914     ///     assert!(mid <= len);
915     ///     unsafe {
916     ///         let slice2 = &mut *(slice as *mut [T]);
917     ///         // however, you still have two mutable references pointing to
918     ///         // the same memory.
919     ///         (&mut slice[0..mid], &mut slice2[mid..len])
920     ///     }
921     /// }
922     ///
923     /// // This is how the standard library does it. This is the best method, if
924     /// // you need to do something like this
925     /// fn split_at_stdlib<T>(slice: &mut [T], mid: usize)
926     ///                       -> (&mut [T], &mut [T]) {
927     ///     let len = slice.len();
928     ///     assert!(mid <= len);
929     ///     unsafe {
930     ///         let ptr = slice.as_mut_ptr();
931     ///         // This now has three mutable references pointing at the same
932     ///         // memory. `slice`, the rvalue ret.0, and the rvalue ret.1.
933     ///         // `slice` is never used after `let ptr = ...`, and so one can
934     ///         // treat it as "dead", and therefore, you only have two real
935     ///         // mutable slices.
936     ///         (slice::from_raw_parts_mut(ptr, mid),
937     ///          slice::from_raw_parts_mut(ptr.add(mid), len - mid))
938     ///     }
939     /// }
940     /// ```
941     #[stable(feature = "rust1", since = "1.0.0")]
942     #[rustc_const_unstable(feature = "const_transmute", issue = "53605")]
943     pub fn transmute<T, U>(e: T) -> U;
944
945     /// Returns `true` if the actual type given as `T` requires drop
946     /// glue; returns `false` if the actual type provided for `T`
947     /// implements `Copy`.
948     ///
949     /// If the actual type neither requires drop glue nor implements
950     /// `Copy`, then may return `true` or `false`.
951     ///
952     /// The stabilized version of this intrinsic is
953     /// [`std::mem::needs_drop`](../../std/mem/fn.needs_drop.html).
954     pub fn needs_drop<T>() -> bool;
955
956     /// Calculates the offset from a pointer.
957     ///
958     /// This is implemented as an intrinsic to avoid converting to and from an
959     /// integer, since the conversion would throw away aliasing information.
960     ///
961     /// # Safety
962     ///
963     /// Both the starting and resulting pointer must be either in bounds or one
964     /// byte past the end of an allocated object. If either pointer is out of
965     /// bounds or arithmetic overflow occurs then any further use of the
966     /// returned value will result in undefined behavior.
967     pub fn offset<T>(dst: *const T, offset: isize) -> *const T;
968
969     /// Calculates the offset from a pointer, potentially wrapping.
970     ///
971     /// This is implemented as an intrinsic to avoid converting to and from an
972     /// integer, since the conversion inhibits certain optimizations.
973     ///
974     /// # Safety
975     ///
976     /// Unlike the `offset` intrinsic, this intrinsic does not restrict the
977     /// resulting pointer to point into or one byte past the end of an allocated
978     /// object, and it wraps with two's complement arithmetic. The resulting
979     /// value is not necessarily valid to be used to actually access memory.
980     pub fn arith_offset<T>(dst: *const T, offset: isize) -> *const T;
981
982     /// Equivalent to the appropriate `llvm.memcpy.p0i8.0i8.*` intrinsic, with
983     /// a size of `count` * `size_of::<T>()` and an alignment of
984     /// `min_align_of::<T>()`
985     ///
986     /// The volatile parameter is set to `true`, so it will not be optimized out
987     /// unless size is equal to zero.
988     pub fn volatile_copy_nonoverlapping_memory<T>(dst: *mut T, src: *const T,
989                                                   count: usize);
990     /// Equivalent to the appropriate `llvm.memmove.p0i8.0i8.*` intrinsic, with
991     /// a size of `count` * `size_of::<T>()` and an alignment of
992     /// `min_align_of::<T>()`
993     ///
994     /// The volatile parameter is set to `true`, so it will not be optimized out
995     /// unless size is equal to zero.
996     pub fn volatile_copy_memory<T>(dst: *mut T, src: *const T, count: usize);
997     /// Equivalent to the appropriate `llvm.memset.p0i8.*` intrinsic, with a
998     /// 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_set_memory<T>(dst: *mut T, val: u8, count: usize);
1004
1005     /// Performs a volatile load from the `src` pointer.
1006     /// The stabilized version of this intrinsic is
1007     /// [`std::ptr::read_volatile`](../../std/ptr/fn.read_volatile.html).
1008     pub fn volatile_load<T>(src: *const T) -> T;
1009     /// Performs a volatile store to the `dst` pointer.
1010     /// The stabilized version of this intrinsic is
1011     /// [`std::ptr::write_volatile`](../../std/ptr/fn.write_volatile.html).
1012     pub fn volatile_store<T>(dst: *mut T, val: T);
1013
1014     /// Performs a volatile load from the `src` pointer
1015     /// The pointer is not required to be aligned.
1016     pub fn unaligned_volatile_load<T>(src: *const T) -> T;
1017     /// Performs a volatile store to the `dst` pointer.
1018     /// The pointer is not required to be aligned.
1019     pub fn unaligned_volatile_store<T>(dst: *mut T, val: T);
1020
1021     /// Returns the square root of an `f32`
1022     pub fn sqrtf32(x: f32) -> f32;
1023     /// Returns the square root of an `f64`
1024     pub fn sqrtf64(x: f64) -> f64;
1025
1026     /// Raises an `f32` to an integer power.
1027     pub fn powif32(a: f32, x: i32) -> f32;
1028     /// Raises an `f64` to an integer power.
1029     pub fn powif64(a: f64, x: i32) -> f64;
1030
1031     /// Returns the sine of an `f32`.
1032     pub fn sinf32(x: f32) -> f32;
1033     /// Returns the sine of an `f64`.
1034     pub fn sinf64(x: f64) -> f64;
1035
1036     /// Returns the cosine of an `f32`.
1037     pub fn cosf32(x: f32) -> f32;
1038     /// Returns the cosine of an `f64`.
1039     pub fn cosf64(x: f64) -> f64;
1040
1041     /// Raises an `f32` to an `f32` power.
1042     pub fn powf32(a: f32, x: f32) -> f32;
1043     /// Raises an `f64` to an `f64` power.
1044     pub fn powf64(a: f64, x: f64) -> f64;
1045
1046     /// Returns the exponential of an `f32`.
1047     pub fn expf32(x: f32) -> f32;
1048     /// Returns the exponential of an `f64`.
1049     pub fn expf64(x: f64) -> f64;
1050
1051     /// Returns 2 raised to the power of an `f32`.
1052     pub fn exp2f32(x: f32) -> f32;
1053     /// Returns 2 raised to the power of an `f64`.
1054     pub fn exp2f64(x: f64) -> f64;
1055
1056     /// Returns the natural logarithm of an `f32`.
1057     pub fn logf32(x: f32) -> f32;
1058     /// Returns the natural logarithm of an `f64`.
1059     pub fn logf64(x: f64) -> f64;
1060
1061     /// Returns the base 10 logarithm of an `f32`.
1062     pub fn log10f32(x: f32) -> f32;
1063     /// Returns the base 10 logarithm of an `f64`.
1064     pub fn log10f64(x: f64) -> f64;
1065
1066     /// Returns the base 2 logarithm of an `f32`.
1067     pub fn log2f32(x: f32) -> f32;
1068     /// Returns the base 2 logarithm of an `f64`.
1069     pub fn log2f64(x: f64) -> f64;
1070
1071     /// Returns `a * b + c` for `f32` values.
1072     pub fn fmaf32(a: f32, b: f32, c: f32) -> f32;
1073     /// Returns `a * b + c` for `f64` values.
1074     pub fn fmaf64(a: f64, b: f64, c: f64) -> f64;
1075
1076     /// Returns the absolute value of an `f32`.
1077     pub fn fabsf32(x: f32) -> f32;
1078     /// Returns the absolute value of an `f64`.
1079     pub fn fabsf64(x: f64) -> f64;
1080
1081     /// Returns the minimum of two `f32` values.
1082     pub fn minnumf32(x: f32, y: f32) -> f32;
1083     /// Returns the minimum of two `f64` values.
1084     pub fn minnumf64(x: f64, y: f64) -> f64;
1085     /// Returns the maximum of two `f32` values.
1086     pub fn maxnumf32(x: f32, y: f32) -> f32;
1087     /// Returns the maximum of two `f64` values.
1088     pub fn maxnumf64(x: f64, y: f64) -> f64;
1089
1090     /// Copies the sign from `y` to `x` for `f32` values.
1091     pub fn copysignf32(x: f32, y: f32) -> f32;
1092     /// Copies the sign from `y` to `x` for `f64` values.
1093     pub fn copysignf64(x: f64, y: f64) -> f64;
1094
1095     /// Returns the largest integer less than or equal to an `f32`.
1096     pub fn floorf32(x: f32) -> f32;
1097     /// Returns the largest integer less than or equal to an `f64`.
1098     pub fn floorf64(x: f64) -> f64;
1099
1100     /// Returns the smallest integer greater than or equal to an `f32`.
1101     pub fn ceilf32(x: f32) -> f32;
1102     /// Returns the smallest integer greater than or equal to an `f64`.
1103     pub fn ceilf64(x: f64) -> f64;
1104
1105     /// Returns the integer part of an `f32`.
1106     pub fn truncf32(x: f32) -> f32;
1107     /// Returns the integer part of an `f64`.
1108     pub fn truncf64(x: f64) -> f64;
1109
1110     /// Returns the nearest integer to an `f32`. May raise an inexact floating-point exception
1111     /// if the argument is not an integer.
1112     pub fn rintf32(x: f32) -> f32;
1113     /// Returns the nearest integer to an `f64`. May raise an inexact floating-point exception
1114     /// if the argument is not an integer.
1115     pub fn rintf64(x: f64) -> f64;
1116
1117     /// Returns the nearest integer to an `f32`.
1118     pub fn nearbyintf32(x: f32) -> f32;
1119     /// Returns the nearest integer to an `f64`.
1120     pub fn nearbyintf64(x: f64) -> f64;
1121
1122     /// Returns the nearest integer to an `f32`. Rounds half-way cases away from zero.
1123     pub fn roundf32(x: f32) -> f32;
1124     /// Returns the nearest integer to an `f64`. Rounds half-way cases away from zero.
1125     pub fn roundf64(x: f64) -> f64;
1126
1127     /// Float addition that allows optimizations based on algebraic rules.
1128     /// May assume inputs are finite.
1129     pub fn fadd_fast<T>(a: T, b: T) -> T;
1130
1131     /// Float subtraction that allows optimizations based on algebraic rules.
1132     /// May assume inputs are finite.
1133     pub fn fsub_fast<T>(a: T, b: T) -> T;
1134
1135     /// Float multiplication that allows optimizations based on algebraic rules.
1136     /// May assume inputs are finite.
1137     pub fn fmul_fast<T>(a: T, b: T) -> T;
1138
1139     /// Float division that allows optimizations based on algebraic rules.
1140     /// May assume inputs are finite.
1141     pub fn fdiv_fast<T>(a: T, b: T) -> T;
1142
1143     /// Float remainder that allows optimizations based on algebraic rules.
1144     /// May assume inputs are finite.
1145     pub fn frem_fast<T>(a: T, b: T) -> T;
1146
1147     /// Convert with LLVM’s fptoui/fptosi, which may return undef for values out of range
1148     /// https://github.com/rust-lang/rust/issues/10184
1149     pub fn float_to_int_approx_unchecked<Float, Int>(value: Float) -> Int;
1150
1151
1152     /// Returns the number of bits set in an integer type `T`
1153     pub fn ctpop<T>(x: T) -> T;
1154
1155     /// Returns the number of leading unset bits (zeroes) in an integer type `T`.
1156     ///
1157     /// # Examples
1158     ///
1159     /// ```
1160     /// #![feature(core_intrinsics)]
1161     ///
1162     /// use std::intrinsics::ctlz;
1163     ///
1164     /// let x = 0b0001_1100_u8;
1165     /// let num_leading = ctlz(x);
1166     /// assert_eq!(num_leading, 3);
1167     /// ```
1168     ///
1169     /// An `x` with value `0` will return the bit width of `T`.
1170     ///
1171     /// ```
1172     /// #![feature(core_intrinsics)]
1173     ///
1174     /// use std::intrinsics::ctlz;
1175     ///
1176     /// let x = 0u16;
1177     /// let num_leading = ctlz(x);
1178     /// assert_eq!(num_leading, 16);
1179     /// ```
1180     pub fn ctlz<T>(x: T) -> T;
1181
1182     /// Like `ctlz`, but extra-unsafe as it returns `undef` when
1183     /// given an `x` with value `0`.
1184     ///
1185     /// # Examples
1186     ///
1187     /// ```
1188     /// #![feature(core_intrinsics)]
1189     ///
1190     /// use std::intrinsics::ctlz_nonzero;
1191     ///
1192     /// let x = 0b0001_1100_u8;
1193     /// let num_leading = unsafe { ctlz_nonzero(x) };
1194     /// assert_eq!(num_leading, 3);
1195     /// ```
1196     pub fn ctlz_nonzero<T>(x: T) -> T;
1197
1198     /// Returns the number of trailing unset bits (zeroes) in an integer type `T`.
1199     ///
1200     /// # Examples
1201     ///
1202     /// ```
1203     /// #![feature(core_intrinsics)]
1204     ///
1205     /// use std::intrinsics::cttz;
1206     ///
1207     /// let x = 0b0011_1000_u8;
1208     /// let num_trailing = cttz(x);
1209     /// assert_eq!(num_trailing, 3);
1210     /// ```
1211     ///
1212     /// An `x` with value `0` will return the bit width of `T`:
1213     ///
1214     /// ```
1215     /// #![feature(core_intrinsics)]
1216     ///
1217     /// use std::intrinsics::cttz;
1218     ///
1219     /// let x = 0u16;
1220     /// let num_trailing = cttz(x);
1221     /// assert_eq!(num_trailing, 16);
1222     /// ```
1223     pub fn cttz<T>(x: T) -> T;
1224
1225     /// Like `cttz`, but extra-unsafe as it returns `undef` when
1226     /// given an `x` with value `0`.
1227     ///
1228     /// # Examples
1229     ///
1230     /// ```
1231     /// #![feature(core_intrinsics)]
1232     ///
1233     /// use std::intrinsics::cttz_nonzero;
1234     ///
1235     /// let x = 0b0011_1000_u8;
1236     /// let num_trailing = unsafe { cttz_nonzero(x) };
1237     /// assert_eq!(num_trailing, 3);
1238     /// ```
1239     pub fn cttz_nonzero<T>(x: T) -> T;
1240
1241     /// Reverses the bytes in an integer type `T`.
1242     pub fn bswap<T>(x: T) -> T;
1243
1244     /// Reverses the bits in an integer type `T`.
1245     pub fn bitreverse<T>(x: T) -> T;
1246
1247     /// Performs checked integer addition.
1248     /// The stabilized versions of this intrinsic are available on the integer
1249     /// primitives via the `overflowing_add` method. For example,
1250     /// [`std::u32::overflowing_add`](../../std/primitive.u32.html#method.overflowing_add)
1251     pub fn add_with_overflow<T>(x: T, y: T) -> (T, bool);
1252
1253     /// Performs checked integer subtraction
1254     /// The stabilized versions of this intrinsic are available on the integer
1255     /// primitives via the `overflowing_sub` method. For example,
1256     /// [`std::u32::overflowing_sub`](../../std/primitive.u32.html#method.overflowing_sub)
1257     pub fn sub_with_overflow<T>(x: T, y: T) -> (T, bool);
1258
1259     /// Performs checked integer multiplication
1260     /// The stabilized versions of this intrinsic are available on the integer
1261     /// primitives via the `overflowing_mul` method. For example,
1262     /// [`std::u32::overflowing_mul`](../../std/primitive.u32.html#method.overflowing_mul)
1263     pub fn mul_with_overflow<T>(x: T, y: T) -> (T, bool);
1264
1265     /// Performs an exact division, resulting in undefined behavior where
1266     /// `x % y != 0` or `y == 0` or `x == T::min_value() && y == -1`
1267     pub fn exact_div<T>(x: T, y: T) -> T;
1268
1269     /// Performs an unchecked division, resulting in undefined behavior
1270     /// where y = 0 or x = `T::min_value()` and y = -1
1271     pub fn unchecked_div<T>(x: T, y: T) -> T;
1272     /// Returns the remainder of an unchecked division, resulting in
1273     /// undefined behavior where y = 0 or x = `T::min_value()` and y = -1
1274     pub fn unchecked_rem<T>(x: T, y: T) -> T;
1275
1276     /// Performs an unchecked left shift, resulting in undefined behavior when
1277     /// y < 0 or y >= N, where N is the width of T in bits.
1278     pub fn unchecked_shl<T>(x: T, y: T) -> T;
1279     /// Performs an unchecked right shift, resulting in undefined behavior when
1280     /// y < 0 or y >= N, where N is the width of T in bits.
1281     pub fn unchecked_shr<T>(x: T, y: T) -> T;
1282
1283     /// Returns the result of an unchecked addition, resulting in
1284     /// undefined behavior when `x + y > T::max_value()` or `x + y < T::min_value()`.
1285     pub fn unchecked_add<T>(x: T, y: T) -> T;
1286
1287     /// Returns the result of an unchecked subtraction, resulting in
1288     /// undefined behavior when `x - y > T::max_value()` or `x - y < T::min_value()`.
1289     pub fn unchecked_sub<T>(x: T, y: T) -> T;
1290
1291     /// Returns the result of an unchecked multiplication, resulting in
1292     /// undefined behavior when `x * y > T::max_value()` or `x * y < T::min_value()`.
1293     pub fn unchecked_mul<T>(x: T, y: T) -> T;
1294
1295     /// Performs rotate left.
1296     /// The stabilized versions of this intrinsic are available on the integer
1297     /// primitives via the `rotate_left` method. For example,
1298     /// [`std::u32::rotate_left`](../../std/primitive.u32.html#method.rotate_left)
1299     pub fn rotate_left<T>(x: T, y: T) -> T;
1300
1301     /// Performs rotate right.
1302     /// The stabilized versions of this intrinsic are available on the integer
1303     /// primitives via the `rotate_right` method. For example,
1304     /// [`std::u32::rotate_right`](../../std/primitive.u32.html#method.rotate_right)
1305     pub fn rotate_right<T>(x: T, y: T) -> T;
1306
1307     /// Returns (a + b) mod 2<sup>N</sup>, where N is the width of T in bits.
1308     /// The stabilized versions of this intrinsic are available on the integer
1309     /// primitives via the `wrapping_add` method. For example,
1310     /// [`std::u32::wrapping_add`](../../std/primitive.u32.html#method.wrapping_add)
1311     pub fn wrapping_add<T>(a: T, b: T) -> T;
1312     /// Returns (a - b) mod 2<sup>N</sup>, where N is the width of T in bits.
1313     /// The stabilized versions of this intrinsic are available on the integer
1314     /// primitives via the `wrapping_sub` method. For example,
1315     /// [`std::u32::wrapping_sub`](../../std/primitive.u32.html#method.wrapping_sub)
1316     pub fn wrapping_sub<T>(a: T, b: T) -> T;
1317     /// Returns (a * b) mod 2<sup>N</sup>, where N is the width of T in bits.
1318     /// The stabilized versions of this intrinsic are available on the integer
1319     /// primitives via the `wrapping_mul` method. For example,
1320     /// [`std::u32::wrapping_mul`](../../std/primitive.u32.html#method.wrapping_mul)
1321     pub fn wrapping_mul<T>(a: T, b: T) -> T;
1322
1323     /// Computes `a + b`, while saturating at numeric bounds.
1324     /// The stabilized versions of this intrinsic are available on the integer
1325     /// primitives via the `saturating_add` method. For example,
1326     /// [`std::u32::saturating_add`](../../std/primitive.u32.html#method.saturating_add)
1327     pub fn saturating_add<T>(a: T, b: T) -> T;
1328     /// Computes `a - b`, while saturating at numeric bounds.
1329     /// The stabilized versions of this intrinsic are available on the integer
1330     /// primitives via the `saturating_sub` method. For example,
1331     /// [`std::u32::saturating_sub`](../../std/primitive.u32.html#method.saturating_sub)
1332     pub fn saturating_sub<T>(a: T, b: T) -> T;
1333
1334     /// Returns the value of the discriminant for the variant in 'v',
1335     /// cast to a `u64`; if `T` has no discriminant, returns 0.
1336     pub fn discriminant_value<T>(v: &T) -> u64;
1337
1338     /// Rust's "try catch" construct which invokes the function pointer `f` with
1339     /// the data pointer `data`.
1340     ///
1341     /// The third pointer is a target-specific data pointer which is filled in
1342     /// with the specifics of the exception that occurred. For examples on Unix
1343     /// platforms this is a `*mut *mut T` which is filled in by the compiler and
1344     /// on MSVC it's `*mut [usize; 2]`. For more information see the compiler's
1345     /// source as well as std's catch implementation.
1346     pub fn r#try(f: fn(*mut u8), data: *mut u8, local_ptr: *mut u8) -> i32;
1347
1348     /// Emits a `!nontemporal` store according to LLVM (see their docs).
1349     /// Probably will never become stable.
1350     pub fn nontemporal_store<T>(ptr: *mut T, val: T);
1351
1352     /// See documentation of `<*const T>::offset_from` for details.
1353     pub fn ptr_offset_from<T>(ptr: *const T, base: *const T) -> isize;
1354
1355     /// Internal hook used by Miri to implement unwinding.
1356     /// Compiles to a NOP during non-Miri codegen.
1357     ///
1358     /// Perma-unstable: do not use
1359     pub fn miri_start_panic(data: *mut (dyn crate::any::Any + crate::marker::Send)) -> ();
1360 }
1361
1362 // Some functions are defined here because they accidentally got made
1363 // available in this module on stable. See <https://github.com/rust-lang/rust/issues/15702>.
1364 // (`transmute` also falls into this category, but it cannot be wrapped due to the
1365 // check that `T` and `U` have the same size.)
1366
1367 /// Checks whether `ptr` is properly aligned with respect to
1368 /// `align_of::<T>()`.
1369 pub(crate) fn is_aligned_and_not_null<T>(ptr: *const T) -> bool {
1370     !ptr.is_null() && ptr as usize % mem::align_of::<T>() == 0
1371 }
1372
1373 /// Checks whether the regions of memory starting at `src` and `dst` of size
1374 /// `count * size_of::<T>()` overlap.
1375 fn overlaps<T>(src: *const T, dst: *const T, count: usize) -> bool {
1376     let src_usize = src as usize;
1377     let dst_usize = dst as usize;
1378     let size = mem::size_of::<T>().checked_mul(count).unwrap();
1379     let diff = if src_usize > dst_usize {
1380         src_usize - dst_usize
1381     } else {
1382         dst_usize - src_usize
1383     };
1384     size > diff
1385 }
1386
1387 /// Copies `count * size_of::<T>()` bytes from `src` to `dst`. The source
1388 /// and destination must *not* overlap.
1389 ///
1390 /// For regions of memory which might overlap, use [`copy`] instead.
1391 ///
1392 /// `copy_nonoverlapping` is semantically equivalent to C's [`memcpy`], but
1393 /// with the argument order swapped.
1394 ///
1395 /// [`copy`]: ./fn.copy.html
1396 /// [`memcpy`]: https://en.cppreference.com/w/c/string/byte/memcpy
1397 ///
1398 /// # Safety
1399 ///
1400 /// Behavior is undefined if any of the following conditions are violated:
1401 ///
1402 /// * `src` must be [valid] for reads of `count * size_of::<T>()` bytes.
1403 ///
1404 /// * `dst` must be [valid] for writes of `count * size_of::<T>()` bytes.
1405 ///
1406 /// * Both `src` and `dst` must be properly aligned.
1407 ///
1408 /// * The region of memory beginning at `src` with a size of `count *
1409 ///   size_of::<T>()` bytes must *not* overlap with the region of memory
1410 ///   beginning at `dst` with the same size.
1411 ///
1412 /// Like [`read`], `copy_nonoverlapping` creates a bitwise copy of `T`, regardless of
1413 /// whether `T` is [`Copy`]. If `T` is not [`Copy`], using *both* the values
1414 /// in the region beginning at `*src` and the region beginning at `*dst` can
1415 /// [violate memory safety][read-ownership].
1416 ///
1417 /// Note that even if the effectively copied size (`count * size_of::<T>()`) is
1418 /// `0`, the pointers must be non-NULL and properly aligned.
1419 ///
1420 /// [`Copy`]: ../marker/trait.Copy.html
1421 /// [`read`]: ../ptr/fn.read.html
1422 /// [read-ownership]: ../ptr/fn.read.html#ownership-of-the-returned-value
1423 /// [valid]: ../ptr/index.html#safety
1424 ///
1425 /// # Examples
1426 ///
1427 /// Manually implement [`Vec::append`]:
1428 ///
1429 /// ```
1430 /// use std::ptr;
1431 ///
1432 /// /// Moves all the elements of `src` into `dst`, leaving `src` empty.
1433 /// fn append<T>(dst: &mut Vec<T>, src: &mut Vec<T>) {
1434 ///     let src_len = src.len();
1435 ///     let dst_len = dst.len();
1436 ///
1437 ///     // Ensure that `dst` has enough capacity to hold all of `src`.
1438 ///     dst.reserve(src_len);
1439 ///
1440 ///     unsafe {
1441 ///         // The call to offset is always safe because `Vec` will never
1442 ///         // allocate more than `isize::MAX` bytes.
1443 ///         let dst_ptr = dst.as_mut_ptr().offset(dst_len as isize);
1444 ///         let src_ptr = src.as_ptr();
1445 ///
1446 ///         // Truncate `src` without dropping its contents. We do this first,
1447 ///         // to avoid problems in case something further down panics.
1448 ///         src.set_len(0);
1449 ///
1450 ///         // The two regions cannot overlap because mutable references do
1451 ///         // not alias, and two different vectors cannot own the same
1452 ///         // memory.
1453 ///         ptr::copy_nonoverlapping(src_ptr, dst_ptr, src_len);
1454 ///
1455 ///         // Notify `dst` that it now holds the contents of `src`.
1456 ///         dst.set_len(dst_len + src_len);
1457 ///     }
1458 /// }
1459 ///
1460 /// let mut a = vec!['r'];
1461 /// let mut b = vec!['u', 's', 't'];
1462 ///
1463 /// append(&mut a, &mut b);
1464 ///
1465 /// assert_eq!(a, &['r', 'u', 's', 't']);
1466 /// assert!(b.is_empty());
1467 /// ```
1468 ///
1469 /// [`Vec::append`]: ../../std/vec/struct.Vec.html#method.append
1470 #[stable(feature = "rust1", since = "1.0.0")]
1471 #[inline]
1472 pub unsafe fn copy_nonoverlapping<T>(src: *const T, dst: *mut T, count: usize) {
1473     extern "rust-intrinsic" {
1474         fn copy_nonoverlapping<T>(src: *const T, dst: *mut T, count: usize);
1475     }
1476
1477     debug_assert!(is_aligned_and_not_null(src), "attempt to copy from unaligned or null pointer");
1478     debug_assert!(is_aligned_and_not_null(dst), "attempt to copy to unaligned or null pointer");
1479     debug_assert!(!overlaps(src, dst, count), "attempt to copy to overlapping memory");
1480     copy_nonoverlapping(src, dst, count)
1481 }
1482
1483 /// Copies `count * size_of::<T>()` bytes from `src` to `dst`. The source
1484 /// and destination may overlap.
1485 ///
1486 /// If the source and destination will *never* overlap,
1487 /// [`copy_nonoverlapping`] can be used instead.
1488 ///
1489 /// `copy` is semantically equivalent to C's [`memmove`], but with the argument
1490 /// order swapped. Copying takes place as if the bytes were copied from `src`
1491 /// to a temporary array and then copied from the array to `dst`.
1492 ///
1493 /// [`copy_nonoverlapping`]: ./fn.copy_nonoverlapping.html
1494 /// [`memmove`]: https://en.cppreference.com/w/c/string/byte/memmove
1495 ///
1496 /// # Safety
1497 ///
1498 /// Behavior is undefined if any of the following conditions are violated:
1499 ///
1500 /// * `src` must be [valid] for reads of `count * size_of::<T>()` bytes.
1501 ///
1502 /// * `dst` must be [valid] for writes of `count * size_of::<T>()` bytes.
1503 ///
1504 /// * Both `src` and `dst` must be properly aligned.
1505 ///
1506 /// Like [`read`], `copy` creates a bitwise copy of `T`, regardless of
1507 /// whether `T` is [`Copy`]. If `T` is not [`Copy`], using both the values
1508 /// in the region beginning at `*src` and the region beginning at `*dst` can
1509 /// [violate memory safety][read-ownership].
1510 ///
1511 /// Note that even if the effectively copied size (`count * size_of::<T>()`) is
1512 /// `0`, the pointers must be non-NULL and properly aligned.
1513 ///
1514 /// [`Copy`]: ../marker/trait.Copy.html
1515 /// [`read`]: ../ptr/fn.read.html
1516 /// [read-ownership]: ../ptr/fn.read.html#ownership-of-the-returned-value
1517 /// [valid]: ../ptr/index.html#safety
1518 ///
1519 /// # Examples
1520 ///
1521 /// Efficiently create a Rust vector from an unsafe buffer:
1522 ///
1523 /// ```
1524 /// use std::ptr;
1525 ///
1526 /// # #[allow(dead_code)]
1527 /// unsafe fn from_buf_raw<T>(ptr: *const T, elts: usize) -> Vec<T> {
1528 ///     let mut dst = Vec::with_capacity(elts);
1529 ///     dst.set_len(elts);
1530 ///     ptr::copy(ptr, dst.as_mut_ptr(), elts);
1531 ///     dst
1532 /// }
1533 /// ```
1534 #[stable(feature = "rust1", since = "1.0.0")]
1535 #[inline]
1536 pub unsafe fn copy<T>(src: *const T, dst: *mut T, count: usize) {
1537     extern "rust-intrinsic" {
1538         fn copy<T>(src: *const T, dst: *mut T, count: usize);
1539     }
1540
1541     debug_assert!(is_aligned_and_not_null(src), "attempt to copy from unaligned or null pointer");
1542     debug_assert!(is_aligned_and_not_null(dst), "attempt to copy to unaligned or null pointer");
1543     copy(src, dst, count)
1544 }
1545
1546 /// Sets `count * size_of::<T>()` bytes of memory starting at `dst` to
1547 /// `val`.
1548 ///
1549 /// `write_bytes` is similar to C's [`memset`], but sets `count *
1550 /// size_of::<T>()` bytes to `val`.
1551 ///
1552 /// [`memset`]: https://en.cppreference.com/w/c/string/byte/memset
1553 ///
1554 /// # Safety
1555 ///
1556 /// Behavior is undefined if any of the following conditions are violated:
1557 ///
1558 /// * `dst` must be [valid] for writes of `count * size_of::<T>()` bytes.
1559 ///
1560 /// * `dst` must be properly aligned.
1561 ///
1562 /// Additionally, the caller must ensure that writing `count *
1563 /// size_of::<T>()` bytes to the given region of memory results in a valid
1564 /// value of `T`. Using a region of memory typed as a `T` that contains an
1565 /// invalid value of `T` is undefined behavior.
1566 ///
1567 /// Note that even if the effectively copied size (`count * size_of::<T>()`) is
1568 /// `0`, the pointer must be non-NULL and properly aligned.
1569 ///
1570 /// [valid]: ../ptr/index.html#safety
1571 ///
1572 /// # Examples
1573 ///
1574 /// Basic usage:
1575 ///
1576 /// ```
1577 /// use std::ptr;
1578 ///
1579 /// let mut vec = vec![0u32; 4];
1580 /// unsafe {
1581 ///     let vec_ptr = vec.as_mut_ptr();
1582 ///     ptr::write_bytes(vec_ptr, 0xfe, 2);
1583 /// }
1584 /// assert_eq!(vec, [0xfefefefe, 0xfefefefe, 0, 0]);
1585 /// ```
1586 ///
1587 /// Creating an invalid value:
1588 ///
1589 /// ```
1590 /// use std::ptr;
1591 ///
1592 /// let mut v = Box::new(0i32);
1593 ///
1594 /// unsafe {
1595 ///     // Leaks the previously held value by overwriting the `Box<T>` with
1596 ///     // a null pointer.
1597 ///     ptr::write_bytes(&mut v as *mut Box<i32>, 0, 1);
1598 /// }
1599 ///
1600 /// // At this point, using or dropping `v` results in undefined behavior.
1601 /// // drop(v); // ERROR
1602 ///
1603 /// // Even leaking `v` "uses" it, and hence is undefined behavior.
1604 /// // mem::forget(v); // ERROR
1605 ///
1606 /// // In fact, `v` is invalid according to basic type layout invariants, so *any*
1607 /// // operation touching it is undefined behavior.
1608 /// // let v2 = v; // ERROR
1609 ///
1610 /// unsafe {
1611 ///     // Let us instead put in a valid value
1612 ///     ptr::write(&mut v as *mut Box<i32>, Box::new(42i32));
1613 /// }
1614 ///
1615 /// // Now the box is fine
1616 /// assert_eq!(*v, 42);
1617 /// ```
1618 #[stable(feature = "rust1", since = "1.0.0")]
1619 #[inline]
1620 pub unsafe fn write_bytes<T>(dst: *mut T, val: u8, count: usize) {
1621     extern "rust-intrinsic" {
1622         fn write_bytes<T>(dst: *mut T, val: u8, count: usize);
1623     }
1624
1625     debug_assert!(is_aligned_and_not_null(dst), "attempt to write to unaligned or null pointer");
1626     write_bytes(dst, val, count)
1627 }