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