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