1 //! Compiler intrinsics.
3 //! The corresponding definitions are in `compiler/rustc_codegen_llvm/src/intrinsic.rs`.
4 //! The corresponding const implementations are in `compiler/rustc_mir/src/interpret/intrinsics.rs`
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 //! `compiler/rustc_mir/src/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 consultation, 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`.
49 feature = "core_intrinsics",
50 reason = "intrinsics are unlikely to ever be stabilized, instead \
51 they should be used through stabilized interfaces \
52 in the rest of the standard library",
55 #![allow(missing_docs)]
57 use crate::marker::DiscriminantKind;
60 // These imports are used for simplifying intra-doc links
61 #[allow(unused_imports)]
62 #[cfg(all(target_has_atomic = "8", target_has_atomic = "32", target_has_atomic = "ptr"))]
63 use crate::sync::atomic::{self, AtomicBool, AtomicI32, AtomicIsize, AtomicU32, Ordering};
65 #[stable(feature = "drop_in_place", since = "1.8.0")]
67 reason = "no longer an intrinsic - use `ptr::drop_in_place` directly",
70 pub use crate::ptr::drop_in_place;
72 extern "rust-intrinsic" {
73 // N.B., these intrinsics take raw pointers because they mutate aliased
74 // memory, which is not valid for either `&` or `&mut`.
76 /// 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 /// [`atomic`] types via the `compare_exchange` method by passing
80 /// [`Ordering::SeqCst`] as both the `success` and `failure` parameters.
81 /// For example, [`AtomicBool::compare_exchange`].
82 pub fn atomic_cxchg<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
83 /// Stores a value if the current value is the same as the `old` value.
85 /// The stabilized version of this intrinsic is available on the
86 /// [`atomic`] types via the `compare_exchange` method by passing
87 /// [`Ordering::Acquire`] as both the `success` and `failure` parameters.
88 /// For example, [`AtomicBool::compare_exchange`].
89 pub fn atomic_cxchg_acq<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
90 /// Stores a value if the current value is the same as the `old` value.
92 /// The stabilized version of this intrinsic is available on the
93 /// [`atomic`] types via the `compare_exchange` method by passing
94 /// [`Ordering::Release`] as the `success` and [`Ordering::Relaxed`] as the
95 /// `failure` parameters. For example, [`AtomicBool::compare_exchange`].
96 pub fn atomic_cxchg_rel<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
97 /// Stores a value if the current value is the same as the `old` value.
99 /// The stabilized version of this intrinsic is available on the
100 /// [`atomic`] types via the `compare_exchange` method by passing
101 /// [`Ordering::AcqRel`] as the `success` and [`Ordering::Acquire`] as the
102 /// `failure` parameters. For example, [`AtomicBool::compare_exchange`].
103 pub fn atomic_cxchg_acqrel<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
104 /// Stores a value if the current value is the same as the `old` value.
106 /// The stabilized version of this intrinsic is available on the
107 /// [`atomic`] types via the `compare_exchange` method by passing
108 /// [`Ordering::Relaxed`] as both the `success` and `failure` parameters.
109 /// For example, [`AtomicBool::compare_exchange`].
110 pub fn atomic_cxchg_relaxed<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
111 /// Stores a value if the current value is the same as the `old` value.
113 /// The stabilized version of this intrinsic is available on the
114 /// [`atomic`] types via the `compare_exchange` method by passing
115 /// [`Ordering::SeqCst`] as the `success` and [`Ordering::Relaxed`] as the
116 /// `failure` parameters. For example, [`AtomicBool::compare_exchange`].
117 pub fn atomic_cxchg_failrelaxed<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
118 /// Stores a value if the current value is the same as the `old` value.
120 /// The stabilized version of this intrinsic is available on the
121 /// [`atomic`] types via the `compare_exchange` method by passing
122 /// [`Ordering::SeqCst`] as the `success` and [`Ordering::Acquire`] as the
123 /// `failure` parameters. For example, [`AtomicBool::compare_exchange`].
124 pub fn atomic_cxchg_failacq<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
125 /// Stores a value if the current value is the same as the `old` value.
127 /// The stabilized version of this intrinsic is available on the
128 /// [`atomic`] types via the `compare_exchange` method by passing
129 /// [`Ordering::Acquire`] as the `success` and [`Ordering::Relaxed`] as the
130 /// `failure` parameters. For example, [`AtomicBool::compare_exchange`].
131 pub fn atomic_cxchg_acq_failrelaxed<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
132 /// Stores a value if the current value is the same as the `old` value.
134 /// The stabilized version of this intrinsic is available on the
135 /// [`atomic`] types via the `compare_exchange` method by passing
136 /// [`Ordering::AcqRel`] as the `success` and [`Ordering::Relaxed`] as the
137 /// `failure` parameters. For example, [`AtomicBool::compare_exchange`].
138 pub fn atomic_cxchg_acqrel_failrelaxed<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
140 /// Stores a value if the current value is the same as the `old` value.
142 /// The stabilized version of this intrinsic is available on the
143 /// [`atomic`] types via the `compare_exchange_weak` method by passing
144 /// [`Ordering::SeqCst`] as both the `success` and `failure` parameters.
145 /// For example, [`AtomicBool::compare_exchange_weak`].
146 pub fn atomic_cxchgweak<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
147 /// Stores a value if the current value is the same as the `old` value.
149 /// The stabilized version of this intrinsic is available on the
150 /// [`atomic`] types via the `compare_exchange_weak` method by passing
151 /// [`Ordering::Acquire`] as both the `success` and `failure` parameters.
152 /// For example, [`AtomicBool::compare_exchange_weak`].
153 pub fn atomic_cxchgweak_acq<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
154 /// Stores a value if the current value is the same as the `old` value.
156 /// The stabilized version of this intrinsic is available on the
157 /// [`atomic`] types via the `compare_exchange_weak` method by passing
158 /// [`Ordering::Release`] as the `success` and [`Ordering::Relaxed`] as the
159 /// `failure` parameters. For example, [`AtomicBool::compare_exchange_weak`].
160 pub fn atomic_cxchgweak_rel<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
161 /// Stores a value if the current value is the same as the `old` value.
163 /// The stabilized version of this intrinsic is available on the
164 /// [`atomic`] types via the `compare_exchange_weak` method by passing
165 /// [`Ordering::AcqRel`] as the `success` and [`Ordering::Acquire`] as the
166 /// `failure` parameters. For example, [`AtomicBool::compare_exchange_weak`].
167 pub fn atomic_cxchgweak_acqrel<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
168 /// Stores a value if the current value is the same as the `old` value.
170 /// The stabilized version of this intrinsic is available on the
171 /// [`atomic`] types via the `compare_exchange_weak` method by passing
172 /// [`Ordering::Relaxed`] as both the `success` and `failure` parameters.
173 /// For example, [`AtomicBool::compare_exchange_weak`].
174 pub fn atomic_cxchgweak_relaxed<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
175 /// Stores a value if the current value is the same as the `old` value.
177 /// The stabilized version of this intrinsic is available on the
178 /// [`atomic`] types via the `compare_exchange_weak` method by passing
179 /// [`Ordering::SeqCst`] as the `success` and [`Ordering::Relaxed`] as the
180 /// `failure` parameters. For example, [`AtomicBool::compare_exchange_weak`].
181 pub fn atomic_cxchgweak_failrelaxed<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
182 /// Stores a value if the current value is the same as the `old` value.
184 /// The stabilized version of this intrinsic is available on the
185 /// [`atomic`] types via the `compare_exchange_weak` method by passing
186 /// [`Ordering::SeqCst`] as the `success` and [`Ordering::Acquire`] as the
187 /// `failure` parameters. For example, [`AtomicBool::compare_exchange_weak`].
188 pub fn atomic_cxchgweak_failacq<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
189 /// Stores a value if the current value is the same as the `old` value.
191 /// The stabilized version of this intrinsic is available on the
192 /// [`atomic`] types via the `compare_exchange_weak` method by passing
193 /// [`Ordering::Acquire`] as the `success` and [`Ordering::Relaxed`] as the
194 /// `failure` parameters. For example, [`AtomicBool::compare_exchange_weak`].
195 pub fn atomic_cxchgweak_acq_failrelaxed<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
196 /// Stores a value if the current value is the same as the `old` value.
198 /// The stabilized version of this intrinsic is available on the
199 /// [`atomic`] types via the `compare_exchange_weak` method by passing
200 /// [`Ordering::AcqRel`] as the `success` and [`Ordering::Relaxed`] as the
201 /// `failure` parameters. For example, [`AtomicBool::compare_exchange_weak`].
202 pub fn atomic_cxchgweak_acqrel_failrelaxed<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
204 /// Loads the current value of the pointer.
206 /// The stabilized version of this intrinsic is available on the
207 /// [`atomic`] types via the `load` method by passing
208 /// [`Ordering::SeqCst`] as the `order`. For example, [`AtomicBool::load`].
209 pub fn atomic_load<T: Copy>(src: *const T) -> T;
210 /// Loads the current value of the pointer.
212 /// The stabilized version of this intrinsic is available on the
213 /// [`atomic`] types via the `load` method by passing
214 /// [`Ordering::Acquire`] as the `order`. For example, [`AtomicBool::load`].
215 pub fn atomic_load_acq<T: Copy>(src: *const T) -> T;
216 /// Loads the current value of the pointer.
218 /// The stabilized version of this intrinsic is available on the
219 /// [`atomic`] types via the `load` method by passing
220 /// [`Ordering::Relaxed`] as the `order`. For example, [`AtomicBool::load`].
221 pub fn atomic_load_relaxed<T: Copy>(src: *const T) -> T;
222 pub fn atomic_load_unordered<T: Copy>(src: *const T) -> T;
224 /// Stores the value at the specified memory location.
226 /// The stabilized version of this intrinsic is available on the
227 /// [`atomic`] types via the `store` method by passing
228 /// [`Ordering::SeqCst`] as the `order`. For example, [`AtomicBool::store`].
229 pub fn atomic_store<T: Copy>(dst: *mut T, val: T);
230 /// Stores the value at the specified memory location.
232 /// The stabilized version of this intrinsic is available on the
233 /// [`atomic`] types via the `store` method by passing
234 /// [`Ordering::Release`] as the `order`. For example, [`AtomicBool::store`].
235 pub fn atomic_store_rel<T: Copy>(dst: *mut T, val: T);
236 /// Stores the value at the specified memory location.
238 /// The stabilized version of this intrinsic is available on the
239 /// [`atomic`] types via the `store` method by passing
240 /// [`Ordering::Relaxed`] as the `order`. For example, [`AtomicBool::store`].
241 pub fn atomic_store_relaxed<T: Copy>(dst: *mut T, val: T);
242 pub fn atomic_store_unordered<T: Copy>(dst: *mut T, val: T);
244 /// Stores the value at the specified memory location, returning the old value.
246 /// The stabilized version of this intrinsic is available on the
247 /// [`atomic`] types via the `swap` method by passing
248 /// [`Ordering::SeqCst`] as the `order`. For example, [`AtomicBool::swap`].
249 pub fn atomic_xchg<T: Copy>(dst: *mut T, src: T) -> T;
250 /// Stores the value at the specified memory location, returning the old value.
252 /// The stabilized version of this intrinsic is available on the
253 /// [`atomic`] types via the `swap` method by passing
254 /// [`Ordering::Acquire`] as the `order`. For example, [`AtomicBool::swap`].
255 pub fn atomic_xchg_acq<T: Copy>(dst: *mut T, src: T) -> T;
256 /// Stores the value at the specified memory location, returning the old value.
258 /// The stabilized version of this intrinsic is available on the
259 /// [`atomic`] types via the `swap` method by passing
260 /// [`Ordering::Release`] as the `order`. For example, [`AtomicBool::swap`].
261 pub fn atomic_xchg_rel<T: Copy>(dst: *mut T, src: T) -> T;
262 /// Stores the value at the specified memory location, returning the old value.
264 /// The stabilized version of this intrinsic is available on the
265 /// [`atomic`] types via the `swap` method by passing
266 /// [`Ordering::AcqRel`] as the `order`. For example, [`AtomicBool::swap`].
267 pub fn atomic_xchg_acqrel<T: Copy>(dst: *mut T, src: T) -> T;
268 /// Stores the value at the specified memory location, returning the old value.
270 /// The stabilized version of this intrinsic is available on the
271 /// [`atomic`] types via the `swap` method by passing
272 /// [`Ordering::Relaxed`] as the `order`. For example, [`AtomicBool::swap`].
273 pub fn atomic_xchg_relaxed<T: Copy>(dst: *mut T, src: T) -> T;
275 /// Adds to the current value, returning the previous value.
277 /// The stabilized version of this intrinsic is available on the
278 /// [`atomic`] types via the `fetch_add` method by passing
279 /// [`Ordering::SeqCst`] as the `order`. For example, [`AtomicIsize::fetch_add`].
280 pub fn atomic_xadd<T: Copy>(dst: *mut T, src: T) -> T;
281 /// Adds to the current value, returning the previous value.
283 /// The stabilized version of this intrinsic is available on the
284 /// [`atomic`] types via the `fetch_add` method by passing
285 /// [`Ordering::Acquire`] as the `order`. For example, [`AtomicIsize::fetch_add`].
286 pub fn atomic_xadd_acq<T: Copy>(dst: *mut T, src: T) -> T;
287 /// Adds to the current value, returning the previous value.
289 /// The stabilized version of this intrinsic is available on the
290 /// [`atomic`] types via the `fetch_add` method by passing
291 /// [`Ordering::Release`] as the `order`. For example, [`AtomicIsize::fetch_add`].
292 pub fn atomic_xadd_rel<T: Copy>(dst: *mut T, src: T) -> T;
293 /// Adds to the current value, returning the previous value.
295 /// The stabilized version of this intrinsic is available on the
296 /// [`atomic`] types via the `fetch_add` method by passing
297 /// [`Ordering::AcqRel`] as the `order`. For example, [`AtomicIsize::fetch_add`].
298 pub fn atomic_xadd_acqrel<T: Copy>(dst: *mut T, src: T) -> T;
299 /// Adds to the current value, returning the previous value.
301 /// The stabilized version of this intrinsic is available on the
302 /// [`atomic`] types via the `fetch_add` method by passing
303 /// [`Ordering::Relaxed`] as the `order`. For example, [`AtomicIsize::fetch_add`].
304 pub fn atomic_xadd_relaxed<T: Copy>(dst: *mut T, src: T) -> T;
306 /// Subtract from the current value, returning the previous value.
308 /// The stabilized version of this intrinsic is available on the
309 /// [`atomic`] types via the `fetch_sub` method by passing
310 /// [`Ordering::SeqCst`] as the `order`. For example, [`AtomicIsize::fetch_sub`].
311 pub fn atomic_xsub<T: Copy>(dst: *mut T, src: T) -> T;
312 /// Subtract from the current value, returning the previous value.
314 /// The stabilized version of this intrinsic is available on the
315 /// [`atomic`] types via the `fetch_sub` method by passing
316 /// [`Ordering::Acquire`] as the `order`. For example, [`AtomicIsize::fetch_sub`].
317 pub fn atomic_xsub_acq<T: Copy>(dst: *mut T, src: T) -> T;
318 /// Subtract from the current value, returning the previous value.
320 /// The stabilized version of this intrinsic is available on the
321 /// [`atomic`] types via the `fetch_sub` method by passing
322 /// [`Ordering::Release`] as the `order`. For example, [`AtomicIsize::fetch_sub`].
323 pub fn atomic_xsub_rel<T: Copy>(dst: *mut T, src: T) -> T;
324 /// Subtract from the current value, returning the previous value.
326 /// The stabilized version of this intrinsic is available on the
327 /// [`atomic`] types via the `fetch_sub` method by passing
328 /// [`Ordering::AcqRel`] as the `order`. For example, [`AtomicIsize::fetch_sub`].
329 pub fn atomic_xsub_acqrel<T: Copy>(dst: *mut T, src: T) -> T;
330 /// Subtract from the current value, returning the previous value.
332 /// The stabilized version of this intrinsic is available on the
333 /// [`atomic`] types via the `fetch_sub` method by passing
334 /// [`Ordering::Relaxed`] as the `order`. For example, [`AtomicIsize::fetch_sub`].
335 pub fn atomic_xsub_relaxed<T: Copy>(dst: *mut T, src: T) -> T;
337 /// Bitwise and with the current value, returning the previous value.
339 /// The stabilized version of this intrinsic is available on the
340 /// [`atomic`] types via the `fetch_and` method by passing
341 /// [`Ordering::SeqCst`] as the `order`. For example, [`AtomicBool::fetch_and`].
342 pub fn atomic_and<T: Copy>(dst: *mut T, src: T) -> T;
343 /// Bitwise and with the current value, returning the previous value.
345 /// The stabilized version of this intrinsic is available on the
346 /// [`atomic`] types via the `fetch_and` method by passing
347 /// [`Ordering::Acquire`] as the `order`. For example, [`AtomicBool::fetch_and`].
348 pub fn atomic_and_acq<T: Copy>(dst: *mut T, src: T) -> T;
349 /// Bitwise and with the current value, returning the previous value.
351 /// The stabilized version of this intrinsic is available on the
352 /// [`atomic`] types via the `fetch_and` method by passing
353 /// [`Ordering::Release`] as the `order`. For example, [`AtomicBool::fetch_and`].
354 pub fn atomic_and_rel<T: Copy>(dst: *mut T, src: T) -> T;
355 /// Bitwise and with the current value, returning the previous value.
357 /// The stabilized version of this intrinsic is available on the
358 /// [`atomic`] types via the `fetch_and` method by passing
359 /// [`Ordering::AcqRel`] as the `order`. For example, [`AtomicBool::fetch_and`].
360 pub fn atomic_and_acqrel<T: Copy>(dst: *mut T, src: T) -> T;
361 /// Bitwise and with the current value, returning the previous value.
363 /// The stabilized version of this intrinsic is available on the
364 /// [`atomic`] types via the `fetch_and` method by passing
365 /// [`Ordering::Relaxed`] as the `order`. For example, [`AtomicBool::fetch_and`].
366 pub fn atomic_and_relaxed<T: Copy>(dst: *mut T, src: T) -> T;
368 /// Bitwise nand with the current value, returning the previous value.
370 /// The stabilized version of this intrinsic is available on the
371 /// [`AtomicBool`] type via the `fetch_nand` method by passing
372 /// [`Ordering::SeqCst`] as the `order`. For example, [`AtomicBool::fetch_nand`].
373 pub fn atomic_nand<T: Copy>(dst: *mut T, src: T) -> T;
374 /// Bitwise nand with the current value, returning the previous value.
376 /// The stabilized version of this intrinsic is available on the
377 /// [`AtomicBool`] type via the `fetch_nand` method by passing
378 /// [`Ordering::Acquire`] as the `order`. For example, [`AtomicBool::fetch_nand`].
379 pub fn atomic_nand_acq<T: Copy>(dst: *mut T, src: T) -> T;
380 /// Bitwise nand with the current value, returning the previous value.
382 /// The stabilized version of this intrinsic is available on the
383 /// [`AtomicBool`] type via the `fetch_nand` method by passing
384 /// [`Ordering::Release`] as the `order`. For example, [`AtomicBool::fetch_nand`].
385 pub fn atomic_nand_rel<T: Copy>(dst: *mut T, src: T) -> T;
386 /// Bitwise nand with the current value, returning the previous value.
388 /// The stabilized version of this intrinsic is available on the
389 /// [`AtomicBool`] type via the `fetch_nand` method by passing
390 /// [`Ordering::AcqRel`] as the `order`. For example, [`AtomicBool::fetch_nand`].
391 pub fn atomic_nand_acqrel<T: Copy>(dst: *mut T, src: T) -> T;
392 /// Bitwise nand with the current value, returning the previous value.
394 /// The stabilized version of this intrinsic is available on the
395 /// [`AtomicBool`] type via the `fetch_nand` method by passing
396 /// [`Ordering::Relaxed`] as the `order`. For example, [`AtomicBool::fetch_nand`].
397 pub fn atomic_nand_relaxed<T: Copy>(dst: *mut T, src: T) -> T;
399 /// Bitwise or with the current value, returning the previous value.
401 /// The stabilized version of this intrinsic is available on the
402 /// [`atomic`] types via the `fetch_or` method by passing
403 /// [`Ordering::SeqCst`] as the `order`. For example, [`AtomicBool::fetch_or`].
404 pub fn atomic_or<T: Copy>(dst: *mut T, src: T) -> T;
405 /// Bitwise or with the current value, returning the previous value.
407 /// The stabilized version of this intrinsic is available on the
408 /// [`atomic`] types via the `fetch_or` method by passing
409 /// [`Ordering::Acquire`] as the `order`. For example, [`AtomicBool::fetch_or`].
410 pub fn atomic_or_acq<T: Copy>(dst: *mut T, src: T) -> T;
411 /// Bitwise or with the current value, returning the previous value.
413 /// The stabilized version of this intrinsic is available on the
414 /// [`atomic`] types via the `fetch_or` method by passing
415 /// [`Ordering::Release`] as the `order`. For example, [`AtomicBool::fetch_or`].
416 pub fn atomic_or_rel<T: Copy>(dst: *mut T, src: T) -> T;
417 /// Bitwise or with the current value, returning the previous value.
419 /// The stabilized version of this intrinsic is available on the
420 /// [`atomic`] types via the `fetch_or` method by passing
421 /// [`Ordering::AcqRel`] as the `order`. For example, [`AtomicBool::fetch_or`].
422 pub fn atomic_or_acqrel<T: Copy>(dst: *mut T, src: T) -> T;
423 /// Bitwise or with the current value, returning the previous value.
425 /// The stabilized version of this intrinsic is available on the
426 /// [`atomic`] types via the `fetch_or` method by passing
427 /// [`Ordering::Relaxed`] as the `order`. For example, [`AtomicBool::fetch_or`].
428 pub fn atomic_or_relaxed<T: Copy>(dst: *mut T, src: T) -> T;
430 /// Bitwise xor with the current value, returning the previous value.
432 /// The stabilized version of this intrinsic is available on the
433 /// [`atomic`] types via the `fetch_xor` method by passing
434 /// [`Ordering::SeqCst`] as the `order`. For example, [`AtomicBool::fetch_xor`].
435 pub fn atomic_xor<T: Copy>(dst: *mut T, src: T) -> T;
436 /// Bitwise xor with the current value, returning the previous value.
438 /// The stabilized version of this intrinsic is available on the
439 /// [`atomic`] types via the `fetch_xor` method by passing
440 /// [`Ordering::Acquire`] as the `order`. For example, [`AtomicBool::fetch_xor`].
441 pub fn atomic_xor_acq<T: Copy>(dst: *mut T, src: T) -> T;
442 /// Bitwise xor with the current value, returning the previous value.
444 /// The stabilized version of this intrinsic is available on the
445 /// [`atomic`] types via the `fetch_xor` method by passing
446 /// [`Ordering::Release`] as the `order`. For example, [`AtomicBool::fetch_xor`].
447 pub fn atomic_xor_rel<T: Copy>(dst: *mut T, src: T) -> T;
448 /// Bitwise xor with the current value, returning the previous value.
450 /// The stabilized version of this intrinsic is available on the
451 /// [`atomic`] types via the `fetch_xor` method by passing
452 /// [`Ordering::AcqRel`] as the `order`. For example, [`AtomicBool::fetch_xor`].
453 pub fn atomic_xor_acqrel<T: Copy>(dst: *mut T, src: T) -> T;
454 /// Bitwise xor with the current value, returning the previous value.
456 /// The stabilized version of this intrinsic is available on the
457 /// [`atomic`] types via the `fetch_xor` method by passing
458 /// [`Ordering::Relaxed`] as the `order`. For example, [`AtomicBool::fetch_xor`].
459 pub fn atomic_xor_relaxed<T: Copy>(dst: *mut T, src: T) -> T;
461 /// Maximum with the current value using a signed comparison.
463 /// The stabilized version of this intrinsic is available on the
464 /// [`atomic`] signed integer types via the `fetch_max` method by passing
465 /// [`Ordering::SeqCst`] as the `order`. For example, [`AtomicI32::fetch_max`].
466 pub fn atomic_max<T: Copy>(dst: *mut T, src: T) -> T;
467 /// Maximum with the current value using a signed comparison.
469 /// The stabilized version of this intrinsic is available on the
470 /// [`atomic`] signed integer types via the `fetch_max` method by passing
471 /// [`Ordering::Acquire`] as the `order`. For example, [`AtomicI32::fetch_max`].
472 pub fn atomic_max_acq<T: Copy>(dst: *mut T, src: T) -> T;
473 /// Maximum with the current value using a signed comparison.
475 /// The stabilized version of this intrinsic is available on the
476 /// [`atomic`] signed integer types via the `fetch_max` method by passing
477 /// [`Ordering::Release`] as the `order`. For example, [`AtomicI32::fetch_max`].
478 pub fn atomic_max_rel<T: Copy>(dst: *mut T, src: T) -> T;
479 /// Maximum with the current value using a signed comparison.
481 /// The stabilized version of this intrinsic is available on the
482 /// [`atomic`] signed integer types via the `fetch_max` method by passing
483 /// [`Ordering::AcqRel`] as the `order`. For example, [`AtomicI32::fetch_max`].
484 pub fn atomic_max_acqrel<T: Copy>(dst: *mut T, src: T) -> T;
485 /// Maximum with the current value.
487 /// The stabilized version of this intrinsic is available on the
488 /// [`atomic`] signed integer types via the `fetch_max` method by passing
489 /// [`Ordering::Relaxed`] as the `order`. For example, [`AtomicI32::fetch_max`].
490 pub fn atomic_max_relaxed<T: Copy>(dst: *mut T, src: T) -> T;
492 /// Minimum with the current value using a signed comparison.
494 /// The stabilized version of this intrinsic is available on the
495 /// [`atomic`] signed integer types via the `fetch_min` method by passing
496 /// [`Ordering::SeqCst`] as the `order`. For example, [`AtomicI32::fetch_min`].
497 pub fn atomic_min<T: Copy>(dst: *mut T, src: T) -> T;
498 /// Minimum with the current value using a signed comparison.
500 /// The stabilized version of this intrinsic is available on the
501 /// [`atomic`] signed integer types via the `fetch_min` method by passing
502 /// [`Ordering::Acquire`] as the `order`. For example, [`AtomicI32::fetch_min`].
503 pub fn atomic_min_acq<T: Copy>(dst: *mut T, src: T) -> T;
504 /// Minimum with the current value using a signed comparison.
506 /// The stabilized version of this intrinsic is available on the
507 /// [`atomic`] signed integer types via the `fetch_min` method by passing
508 /// [`Ordering::Release`] as the `order`. For example, [`AtomicI32::fetch_min`].
509 pub fn atomic_min_rel<T: Copy>(dst: *mut T, src: T) -> T;
510 /// Minimum with the current value using a signed comparison.
512 /// The stabilized version of this intrinsic is available on the
513 /// [`atomic`] signed integer types via the `fetch_min` method by passing
514 /// [`Ordering::AcqRel`] as the `order`. For example, [`AtomicI32::fetch_min`].
515 pub fn atomic_min_acqrel<T: Copy>(dst: *mut T, src: T) -> T;
516 /// Minimum with the current value using a signed comparison.
518 /// The stabilized version of this intrinsic is available on the
519 /// [`atomic`] signed integer types via the `fetch_min` method by passing
520 /// [`Ordering::Relaxed`] as the `order`. For example, [`AtomicI32::fetch_min`].
521 pub fn atomic_min_relaxed<T: Copy>(dst: *mut T, src: T) -> T;
523 /// Minimum with the current value using an unsigned comparison.
525 /// The stabilized version of this intrinsic is available on the
526 /// [`atomic`] unsigned integer types via the `fetch_min` method by passing
527 /// [`Ordering::SeqCst`] as the `order`. For example, [`AtomicU32::fetch_min`].
528 pub fn atomic_umin<T: Copy>(dst: *mut T, src: T) -> T;
529 /// Minimum with the current value using an unsigned comparison.
531 /// The stabilized version of this intrinsic is available on the
532 /// [`atomic`] unsigned integer types via the `fetch_min` method by passing
533 /// [`Ordering::Acquire`] as the `order`. For example, [`AtomicU32::fetch_min`].
534 pub fn atomic_umin_acq<T: Copy>(dst: *mut T, src: T) -> T;
535 /// Minimum with the current value using an unsigned comparison.
537 /// The stabilized version of this intrinsic is available on the
538 /// [`atomic`] unsigned integer types via the `fetch_min` method by passing
539 /// [`Ordering::Release`] as the `order`. For example, [`AtomicU32::fetch_min`].
540 pub fn atomic_umin_rel<T: Copy>(dst: *mut T, src: T) -> T;
541 /// Minimum with the current value using an unsigned comparison.
543 /// The stabilized version of this intrinsic is available on the
544 /// [`atomic`] unsigned integer types via the `fetch_min` method by passing
545 /// [`Ordering::AcqRel`] as the `order`. For example, [`AtomicU32::fetch_min`].
546 pub fn atomic_umin_acqrel<T: Copy>(dst: *mut T, src: T) -> T;
547 /// Minimum with the current value using an unsigned comparison.
549 /// The stabilized version of this intrinsic is available on the
550 /// [`atomic`] unsigned integer types via the `fetch_min` method by passing
551 /// [`Ordering::Relaxed`] as the `order`. For example, [`AtomicU32::fetch_min`].
552 pub fn atomic_umin_relaxed<T: Copy>(dst: *mut T, src: T) -> T;
554 /// Maximum with the current value using an unsigned comparison.
556 /// The stabilized version of this intrinsic is available on the
557 /// [`atomic`] unsigned integer types via the `fetch_max` method by passing
558 /// [`Ordering::SeqCst`] as the `order`. For example, [`AtomicU32::fetch_max`].
559 pub fn atomic_umax<T: Copy>(dst: *mut T, src: T) -> T;
560 /// Maximum with the current value using an unsigned comparison.
562 /// The stabilized version of this intrinsic is available on the
563 /// [`atomic`] unsigned integer types via the `fetch_max` method by passing
564 /// [`Ordering::Acquire`] as the `order`. For example, [`AtomicU32::fetch_max`].
565 pub fn atomic_umax_acq<T: Copy>(dst: *mut T, src: T) -> T;
566 /// Maximum with the current value using an unsigned comparison.
568 /// The stabilized version of this intrinsic is available on the
569 /// [`atomic`] unsigned integer types via the `fetch_max` method by passing
570 /// [`Ordering::Release`] as the `order`. For example, [`AtomicU32::fetch_max`].
571 pub fn atomic_umax_rel<T: Copy>(dst: *mut T, src: T) -> T;
572 /// Maximum with the current value using an unsigned comparison.
574 /// The stabilized version of this intrinsic is available on the
575 /// [`atomic`] unsigned integer types via the `fetch_max` method by passing
576 /// [`Ordering::AcqRel`] as the `order`. For example, [`AtomicU32::fetch_max`].
577 pub fn atomic_umax_acqrel<T: Copy>(dst: *mut T, src: T) -> T;
578 /// Maximum with the current value using an unsigned comparison.
580 /// The stabilized version of this intrinsic is available on the
581 /// [`atomic`] unsigned integer types via the `fetch_max` method by passing
582 /// [`Ordering::Relaxed`] as the `order`. For example, [`AtomicU32::fetch_max`].
583 pub fn atomic_umax_relaxed<T: Copy>(dst: *mut T, src: T) -> T;
585 /// The `prefetch` intrinsic is a hint to the code generator to insert a prefetch instruction
586 /// if supported; otherwise, it is a no-op.
587 /// Prefetches have no effect on the behavior of the program but can change its performance
590 /// The `locality` argument must be a constant integer and is a temporal locality specifier
591 /// ranging from (0) - no locality, to (3) - extremely local keep in cache.
593 /// This intrinsic does not have a stable counterpart.
594 pub fn prefetch_read_data<T>(data: *const T, locality: i32);
595 /// The `prefetch` intrinsic is a hint to the code generator to insert a prefetch instruction
596 /// if supported; otherwise, it is a no-op.
597 /// Prefetches have no effect on the behavior of the program but can change its performance
600 /// The `locality` argument must be a constant integer and is a temporal locality specifier
601 /// ranging from (0) - no locality, to (3) - extremely local keep in cache.
603 /// This intrinsic does not have a stable counterpart.
604 pub fn prefetch_write_data<T>(data: *const T, locality: i32);
605 /// The `prefetch` intrinsic is a hint to the code generator to insert a prefetch instruction
606 /// if supported; otherwise, it is a no-op.
607 /// Prefetches have no effect on the behavior of the program but can change its performance
610 /// The `locality` argument must be a constant integer and is a temporal locality specifier
611 /// ranging from (0) - no locality, to (3) - extremely local keep in cache.
613 /// This intrinsic does not have a stable counterpart.
614 pub fn prefetch_read_instruction<T>(data: *const T, locality: i32);
615 /// The `prefetch` intrinsic is a hint to the code generator to insert a prefetch instruction
616 /// if supported; otherwise, it is a no-op.
617 /// Prefetches have no effect on the behavior of the program but can change its performance
620 /// The `locality` argument must be a constant integer and is a temporal locality specifier
621 /// ranging from (0) - no locality, to (3) - extremely local keep in cache.
623 /// This intrinsic does not have a stable counterpart.
624 pub fn prefetch_write_instruction<T>(data: *const T, locality: i32);
627 extern "rust-intrinsic" {
630 /// The stabilized version of this intrinsic is available in
631 /// [`atomic::fence`] by passing [`Ordering::SeqCst`]
633 pub fn atomic_fence();
636 /// The stabilized version of this intrinsic is available in
637 /// [`atomic::fence`] by passing [`Ordering::Acquire`]
639 pub fn atomic_fence_acq();
642 /// The stabilized version of this intrinsic is available in
643 /// [`atomic::fence`] by passing [`Ordering::Release`]
645 pub fn atomic_fence_rel();
648 /// The stabilized version of this intrinsic is available in
649 /// [`atomic::fence`] by passing [`Ordering::AcqRel`]
651 pub fn atomic_fence_acqrel();
653 /// A compiler-only memory barrier.
655 /// Memory accesses will never be reordered across this barrier by the
656 /// compiler, but no instructions will be emitted for it. This is
657 /// appropriate for operations on the same thread that may be preempted,
658 /// such as when interacting with signal handlers.
660 /// The stabilized version of this intrinsic is available in
661 /// [`atomic::compiler_fence`] by passing [`Ordering::SeqCst`]
663 pub fn atomic_singlethreadfence();
664 /// A compiler-only memory barrier.
666 /// Memory accesses will never be reordered across this barrier by the
667 /// compiler, but no instructions will be emitted for it. This is
668 /// appropriate for operations on the same thread that may be preempted,
669 /// such as when interacting with signal handlers.
671 /// The stabilized version of this intrinsic is available in
672 /// [`atomic::compiler_fence`] by passing [`Ordering::Acquire`]
674 pub fn atomic_singlethreadfence_acq();
675 /// A compiler-only memory barrier.
677 /// Memory accesses will never be reordered across this barrier by the
678 /// compiler, but no instructions will be emitted for it. This is
679 /// appropriate for operations on the same thread that may be preempted,
680 /// such as when interacting with signal handlers.
682 /// The stabilized version of this intrinsic is available in
683 /// [`atomic::compiler_fence`] by passing [`Ordering::Release`]
685 pub fn atomic_singlethreadfence_rel();
686 /// A compiler-only memory barrier.
688 /// Memory accesses will never be reordered across this barrier by the
689 /// compiler, but no instructions will be emitted for it. This is
690 /// appropriate for operations on the same thread that may be preempted,
691 /// such as when interacting with signal handlers.
693 /// The stabilized version of this intrinsic is available in
694 /// [`atomic::compiler_fence`] by passing [`Ordering::AcqRel`]
696 pub fn atomic_singlethreadfence_acqrel();
698 /// Magic intrinsic that derives its meaning from attributes
699 /// attached to the function.
701 /// For example, dataflow uses this to inject static assertions so
702 /// that `rustc_peek(potentially_uninitialized)` would actually
703 /// double-check that dataflow did indeed compute that it is
704 /// uninitialized at that point in the control flow.
706 /// This intrinsic should not be used outside of the compiler.
707 pub fn rustc_peek<T>(_: T) -> T;
709 /// Aborts the execution of the process.
711 /// A more user-friendly and stable version of this operation is
712 /// [`std::process::abort`](../../std/process/fn.abort.html).
715 /// Tells LLVM that this point in the code is not reachable, enabling
716 /// further optimizations.
718 /// N.B., this is very different from the `unreachable!()` macro: Unlike the
719 /// macro, which panics when it is executed, it is *undefined behavior* to
720 /// reach code marked with this function.
722 /// The stabilized version of this intrinsic is [`core::hint::unreachable_unchecked`](crate::hint::unreachable_unchecked).
723 #[rustc_const_unstable(feature = "const_unreachable_unchecked", issue = "53188")]
724 pub fn unreachable() -> !;
726 /// Informs the optimizer that a condition is always true.
727 /// If the condition is false, the behavior is undefined.
729 /// No code is generated for this intrinsic, but the optimizer will try
730 /// to preserve it (and its condition) between passes, which may interfere
731 /// with optimization of surrounding code and reduce performance. It should
732 /// not be used if the invariant can be discovered by the optimizer on its
733 /// own, or if it does not enable any significant optimizations.
735 /// This intrinsic does not have a stable counterpart.
736 #[rustc_const_unstable(feature = "const_assume", issue = "76972")]
737 pub fn assume(b: bool);
739 /// Hints to the compiler that branch condition is likely to be true.
740 /// Returns the value passed to it.
742 /// Any use other than with `if` statements will probably not have an effect.
744 /// This intrinsic does not have a stable counterpart.
745 #[rustc_const_unstable(feature = "const_likely", issue = "none")]
746 pub fn likely(b: bool) -> bool;
748 /// Hints to the compiler that branch condition is likely to be false.
749 /// Returns the value passed to it.
751 /// Any use other than with `if` statements will probably not have an effect.
753 /// This intrinsic does not have a stable counterpart.
754 #[rustc_const_unstable(feature = "const_likely", issue = "none")]
755 pub fn unlikely(b: bool) -> bool;
757 /// Executes a breakpoint trap, for inspection by a debugger.
759 /// This intrinsic does not have a stable counterpart.
762 /// The size of a type in bytes.
764 /// More specifically, this is the offset in bytes between successive
765 /// items of the same type, including alignment padding.
767 /// The stabilized version of this intrinsic is [`core::mem::size_of`](crate::mem::size_of).
768 #[rustc_const_stable(feature = "const_size_of", since = "1.40.0")]
769 pub fn size_of<T>() -> usize;
771 /// Moves a value to an uninitialized memory location.
773 /// Drop glue is not run on the destination.
775 /// The stabilized version of this intrinsic is [`core::ptr::write`](crate::ptr::write).
776 pub fn move_val_init<T>(dst: *mut T, src: T);
778 /// The minimum alignment of a type.
780 /// The stabilized version of this intrinsic is [`core::mem::align_of`](crate::mem::align_of).
781 #[rustc_const_stable(feature = "const_min_align_of", since = "1.40.0")]
782 pub fn min_align_of<T>() -> usize;
783 /// The preferred alignment of a type.
785 /// This intrinsic does not have a stable counterpart.
786 #[rustc_const_unstable(feature = "const_pref_align_of", issue = "none")]
787 pub fn pref_align_of<T>() -> usize;
789 /// The size of the referenced value in bytes.
791 /// The stabilized version of this intrinsic is [`mem::size_of_val`].
792 #[rustc_const_unstable(feature = "const_size_of_val", issue = "46571")]
793 pub fn size_of_val<T: ?Sized>(_: *const T) -> usize;
794 /// The required alignment of the referenced value.
796 /// The stabilized version of this intrinsic is [`core::mem::align_of_val`](crate::mem::align_of_val).
797 #[rustc_const_unstable(feature = "const_align_of_val", issue = "46571")]
798 pub fn min_align_of_val<T: ?Sized>(_: *const T) -> usize;
800 /// Gets a static string slice containing the name of a type.
802 /// The stabilized version of this intrinsic is [`core::any::type_name`](crate::any::type_name).
803 #[rustc_const_unstable(feature = "const_type_name", issue = "63084")]
804 pub fn type_name<T: ?Sized>() -> &'static str;
806 /// Gets an identifier which is globally unique to the specified type. This
807 /// function will return the same value for a type regardless of whichever
808 /// crate it is invoked in.
810 /// The stabilized version of this intrinsic is [`core::any::TypeId::of`](crate::any::TypeId::of).
811 #[rustc_const_unstable(feature = "const_type_id", issue = "77125")]
812 pub fn type_id<T: ?Sized + 'static>() -> u64;
814 /// A guard for unsafe functions that cannot ever be executed if `T` is uninhabited:
815 /// This will statically either panic, or do nothing.
817 /// This intrinsic does not have a stable counterpart.
818 #[rustc_const_unstable(feature = "const_maybe_assume_init", issue = "none")]
819 pub fn assert_inhabited<T>();
821 /// A guard for unsafe functions that cannot ever be executed if `T` does not permit
822 /// zero-initialization: This will statically either panic, or do nothing.
824 /// This intrinsic does not have a stable counterpart.
825 pub fn assert_zero_valid<T>();
827 /// A guard for unsafe functions that cannot ever be executed if `T` has invalid
828 /// bit patterns: This will statically either panic, or do nothing.
830 /// This intrinsic does not have a stable counterpart.
831 pub fn assert_uninit_valid<T>();
833 /// Gets a reference to a static `Location` indicating where it was called.
835 /// Consider using [`core::panic::Location::caller`](crate::panic::Location::caller) instead.
836 #[rustc_const_unstable(feature = "const_caller_location", issue = "76156")]
837 pub fn caller_location() -> &'static crate::panic::Location<'static>;
839 /// Moves a value out of scope without running drop glue.
841 /// This exists solely for [`mem::forget_unsized`]; normal `forget` uses
842 /// `ManuallyDrop` instead.
843 pub fn forget<T: ?Sized>(_: T);
845 /// Reinterprets the bits of a value of one type as another type.
847 /// Both types must have the same size. Neither the original, nor the result,
848 /// may be an [invalid value](../../nomicon/what-unsafe-does.html).
850 /// `transmute` is semantically equivalent to a bitwise move of one type
851 /// into another. It copies the bits from the source value into the
852 /// destination value, then forgets the original. It's equivalent to C's
853 /// `memcpy` under the hood, just like `transmute_copy`.
855 /// `transmute` is **incredibly** unsafe. There are a vast number of ways to
856 /// cause [undefined behavior][ub] with this function. `transmute` should be
857 /// the absolute last resort.
859 /// The [nomicon](../../nomicon/transmutes.html) has additional
862 /// [ub]: ../../reference/behavior-considered-undefined.html
866 /// There are a few things that `transmute` is really useful for.
868 /// Turning a pointer into a function pointer. This is *not* portable to
869 /// machines where function pointers and data pointers have different sizes.
872 /// fn foo() -> i32 {
875 /// let pointer = foo as *const ();
876 /// let function = unsafe {
877 /// std::mem::transmute::<*const (), fn() -> i32>(pointer)
879 /// assert_eq!(function(), 0);
882 /// Extending a lifetime, or shortening an invariant lifetime. This is
883 /// advanced, very unsafe Rust!
886 /// struct R<'a>(&'a i32);
887 /// unsafe fn extend_lifetime<'b>(r: R<'b>) -> R<'static> {
888 /// std::mem::transmute::<R<'b>, R<'static>>(r)
891 /// unsafe fn shorten_invariant_lifetime<'b, 'c>(r: &'b mut R<'static>)
892 /// -> &'b mut R<'c> {
893 /// std::mem::transmute::<&'b mut R<'static>, &'b mut R<'c>>(r)
899 /// Don't despair: many uses of `transmute` can be achieved through other means.
900 /// Below are common applications of `transmute` which can be replaced with safer
903 /// Turning raw bytes(`&[u8]`) to `u32`, `f64`, etc.:
906 /// let raw_bytes = [0x78, 0x56, 0x34, 0x12];
908 /// let num = unsafe {
909 /// std::mem::transmute::<[u8; 4], u32>(raw_bytes)
912 /// // use `u32::from_ne_bytes` instead
913 /// let num = u32::from_ne_bytes(raw_bytes);
914 /// // or use `u32::from_le_bytes` or `u32::from_be_bytes` to specify the endianness
915 /// let num = u32::from_le_bytes(raw_bytes);
916 /// assert_eq!(num, 0x12345678);
917 /// let num = u32::from_be_bytes(raw_bytes);
918 /// assert_eq!(num, 0x78563412);
921 /// Turning a pointer into a `usize`:
925 /// let ptr_num_transmute = unsafe {
926 /// std::mem::transmute::<&i32, usize>(ptr)
929 /// // Use an `as` cast instead
930 /// let ptr_num_cast = ptr as *const i32 as usize;
933 /// Turning a `*mut T` into an `&mut T`:
936 /// let ptr: *mut i32 = &mut 0;
937 /// let ref_transmuted = unsafe {
938 /// std::mem::transmute::<*mut i32, &mut i32>(ptr)
941 /// // Use a reborrow instead
942 /// let ref_casted = unsafe { &mut *ptr };
945 /// Turning an `&mut T` into an `&mut U`:
948 /// let ptr = &mut 0;
949 /// let val_transmuted = unsafe {
950 /// std::mem::transmute::<&mut i32, &mut u32>(ptr)
953 /// // Now, put together `as` and reborrowing - note the chaining of `as`
954 /// // `as` is not transitive
955 /// let val_casts = unsafe { &mut *(ptr as *mut i32 as *mut u32) };
958 /// Turning an `&str` into an `&[u8]`:
961 /// // this is not a good way to do this.
962 /// let slice = unsafe { std::mem::transmute::<&str, &[u8]>("Rust") };
963 /// assert_eq!(slice, &[82, 117, 115, 116]);
965 /// // You could use `str::as_bytes`
966 /// let slice = "Rust".as_bytes();
967 /// assert_eq!(slice, &[82, 117, 115, 116]);
969 /// // Or, just use a byte string, if you have control over the string
971 /// assert_eq!(b"Rust", &[82, 117, 115, 116]);
974 /// Turning a `Vec<&T>` into a `Vec<Option<&T>>`:
977 /// let store = [0, 1, 2, 3];
978 /// let v_orig = store.iter().collect::<Vec<&i32>>();
980 /// // clone the vector as we will reuse them later
981 /// let v_clone = v_orig.clone();
983 /// // Using transmute: this relies on the unspecified data layout of `Vec`, which is a
984 /// // bad idea and could cause Undefined Behavior.
985 /// // However, it is no-copy.
986 /// let v_transmuted = unsafe {
987 /// std::mem::transmute::<Vec<&i32>, Vec<Option<&i32>>>(v_clone)
990 /// let v_clone = v_orig.clone();
992 /// // This is the suggested, safe way.
993 /// // It does copy the entire vector, though, into a new array.
994 /// let v_collected = v_clone.into_iter()
996 /// .collect::<Vec<Option<&i32>>>();
998 /// let v_clone = v_orig.clone();
1000 /// // The no-copy, unsafe way, still using transmute, but not relying on the data layout.
1001 /// // Like the first approach, this reuses the `Vec` internals.
1002 /// // Therefore, the new inner type must have the
1003 /// // exact same size, *and the same alignment*, as the old type.
1004 /// // The same caveats exist for this method as transmute, for
1005 /// // the original inner type (`&i32`) to the converted inner type
1006 /// // (`Option<&i32>`), so read the nomicon pages linked above and also
1007 /// // consult the [`from_raw_parts`] documentation.
1008 /// let v_from_raw = unsafe {
1009 // FIXME Update this when vec_into_raw_parts is stabilized
1010 /// // Ensure the original vector is not dropped.
1011 /// let mut v_clone = std::mem::ManuallyDrop::new(v_clone);
1012 /// Vec::from_raw_parts(v_clone.as_mut_ptr() as *mut Option<&i32>,
1014 /// v_clone.capacity())
1018 /// [`from_raw_parts`]: ../../std/vec/struct.Vec.html#method.from_raw_parts
1020 /// Implementing `split_at_mut`:
1023 /// use std::{slice, mem};
1025 /// // There are multiple ways to do this, and there are multiple problems
1026 /// // with the following (transmute) way.
1027 /// fn split_at_mut_transmute<T>(slice: &mut [T], mid: usize)
1028 /// -> (&mut [T], &mut [T]) {
1029 /// let len = slice.len();
1030 /// assert!(mid <= len);
1032 /// let slice2 = mem::transmute::<&mut [T], &mut [T]>(slice);
1033 /// // first: transmute is not type safe; all it checks is that T and
1034 /// // U are of the same size. Second, right here, you have two
1035 /// // mutable references pointing to the same memory.
1036 /// (&mut slice[0..mid], &mut slice2[mid..len])
1040 /// // This gets rid of the type safety problems; `&mut *` will *only* give
1041 /// // you an `&mut T` from an `&mut T` or `*mut T`.
1042 /// fn split_at_mut_casts<T>(slice: &mut [T], mid: usize)
1043 /// -> (&mut [T], &mut [T]) {
1044 /// let len = slice.len();
1045 /// assert!(mid <= len);
1047 /// let slice2 = &mut *(slice as *mut [T]);
1048 /// // however, you still have two mutable references pointing to
1049 /// // the same memory.
1050 /// (&mut slice[0..mid], &mut slice2[mid..len])
1054 /// // This is how the standard library does it. This is the best method, if
1055 /// // you need to do something like this
1056 /// fn split_at_stdlib<T>(slice: &mut [T], mid: usize)
1057 /// -> (&mut [T], &mut [T]) {
1058 /// let len = slice.len();
1059 /// assert!(mid <= len);
1061 /// let ptr = slice.as_mut_ptr();
1062 /// // This now has three mutable references pointing at the same
1063 /// // memory. `slice`, the rvalue ret.0, and the rvalue ret.1.
1064 /// // `slice` is never used after `let ptr = ...`, and so one can
1065 /// // treat it as "dead", and therefore, you only have two real
1066 /// // mutable slices.
1067 /// (slice::from_raw_parts_mut(ptr, mid),
1068 /// slice::from_raw_parts_mut(ptr.add(mid), len - mid))
1072 #[stable(feature = "rust1", since = "1.0.0")]
1073 // NOTE: While this makes the intrinsic const stable, we have some custom code in const fn
1074 // checks that prevent its use within `const fn`.
1075 #[rustc_const_stable(feature = "const_transmute", since = "1.46.0")]
1076 #[rustc_diagnostic_item = "transmute"]
1077 pub fn transmute<T, U>(e: T) -> U;
1079 /// Returns `true` if the actual type given as `T` requires drop
1080 /// glue; returns `false` if the actual type provided for `T`
1081 /// implements `Copy`.
1083 /// If the actual type neither requires drop glue nor implements
1084 /// `Copy`, then the return value of this function is unspecified.
1086 /// The stabilized version of this intrinsic is [`mem::needs_drop`](crate::mem::needs_drop).
1087 #[rustc_const_stable(feature = "const_needs_drop", since = "1.40.0")]
1088 pub fn needs_drop<T>() -> bool;
1090 /// Calculates the offset from a pointer.
1092 /// This is implemented as an intrinsic to avoid converting to and from an
1093 /// integer, since the conversion would throw away aliasing information.
1097 /// Both the starting and resulting pointer must be either in bounds or one
1098 /// byte past the end of an allocated object. If either pointer is out of
1099 /// bounds or arithmetic overflow occurs then any further use of the
1100 /// returned value will result in undefined behavior.
1102 /// The stabilized version of this intrinsic is
1103 /// [`std::pointer::offset`](../../std/primitive.pointer.html#method.offset).
1104 #[must_use = "returns a new pointer rather than modifying its argument"]
1105 #[rustc_const_unstable(feature = "const_ptr_offset", issue = "71499")]
1106 pub fn offset<T>(dst: *const T, offset: isize) -> *const T;
1108 /// Calculates the offset from a pointer, potentially wrapping.
1110 /// This is implemented as an intrinsic to avoid converting to and from an
1111 /// integer, since the conversion inhibits certain optimizations.
1115 /// Unlike the `offset` intrinsic, this intrinsic does not restrict the
1116 /// resulting pointer to point into or one byte past the end of an allocated
1117 /// object, and it wraps with two's complement arithmetic. The resulting
1118 /// value is not necessarily valid to be used to actually access memory.
1120 /// The stabilized version of this intrinsic is
1121 /// [`std::pointer::wrapping_offset`](../../std/primitive.pointer.html#method.wrapping_offset).
1122 #[must_use = "returns a new pointer rather than modifying its argument"]
1123 #[rustc_const_unstable(feature = "const_ptr_offset", issue = "71499")]
1124 pub fn arith_offset<T>(dst: *const T, offset: isize) -> *const T;
1126 /// Equivalent to the appropriate `llvm.memcpy.p0i8.0i8.*` intrinsic, with
1127 /// a size of `count` * `size_of::<T>()` and an alignment of
1128 /// `min_align_of::<T>()`
1130 /// The volatile parameter is set to `true`, so it will not be optimized out
1131 /// unless size is equal to zero.
1133 /// This intrinsic does not have a stable counterpart.
1134 pub fn volatile_copy_nonoverlapping_memory<T>(dst: *mut T, src: *const T, count: usize);
1135 /// Equivalent to the appropriate `llvm.memmove.p0i8.0i8.*` intrinsic, with
1136 /// a size of `count` * `size_of::<T>()` and an alignment of
1137 /// `min_align_of::<T>()`
1139 /// The volatile parameter is set to `true`, so it will not be optimized out
1140 /// unless size is equal to zero.
1142 /// This intrinsic does not have a stable counterpart.
1143 pub fn volatile_copy_memory<T>(dst: *mut T, src: *const T, count: usize);
1144 /// Equivalent to the appropriate `llvm.memset.p0i8.*` intrinsic, with a
1145 /// size of `count` * `size_of::<T>()` and an alignment of
1146 /// `min_align_of::<T>()`.
1148 /// The volatile parameter is set to `true`, so it will not be optimized out
1149 /// unless size is equal to zero.
1151 /// This intrinsic does not have a stable counterpart.
1152 pub fn volatile_set_memory<T>(dst: *mut T, val: u8, count: usize);
1154 /// Performs a volatile load from the `src` pointer.
1156 /// The stabilized version of this intrinsic is [`core::ptr::read_volatile`](crate::ptr::read_volatile).
1157 pub fn volatile_load<T>(src: *const T) -> T;
1158 /// Performs a volatile store to the `dst` pointer.
1160 /// The stabilized version of this intrinsic is [`core::ptr::write_volatile`](crate::ptr::write_volatile).
1161 pub fn volatile_store<T>(dst: *mut T, val: T);
1163 /// Performs a volatile load from the `src` pointer
1164 /// The pointer is not required to be aligned.
1166 /// This intrinsic does not have a stable counterpart.
1167 pub fn unaligned_volatile_load<T>(src: *const T) -> T;
1168 /// Performs a volatile store to the `dst` pointer.
1169 /// The pointer is not required to be aligned.
1171 /// This intrinsic does not have a stable counterpart.
1172 pub fn unaligned_volatile_store<T>(dst: *mut T, val: T);
1174 /// Returns the square root of an `f32`
1176 /// The stabilized version of this intrinsic is
1177 /// [`f32::sqrt`](../../std/primitive.f32.html#method.sqrt)
1178 pub fn sqrtf32(x: f32) -> f32;
1179 /// Returns the square root of an `f64`
1181 /// The stabilized version of this intrinsic is
1182 /// [`f64::sqrt`](../../std/primitive.f64.html#method.sqrt)
1183 pub fn sqrtf64(x: f64) -> f64;
1185 /// Raises an `f32` to an integer power.
1187 /// The stabilized version of this intrinsic is
1188 /// [`f32::powi`](../../std/primitive.f32.html#method.powi)
1189 pub fn powif32(a: f32, x: i32) -> f32;
1190 /// Raises an `f64` to an integer power.
1192 /// The stabilized version of this intrinsic is
1193 /// [`f64::powi`](../../std/primitive.f64.html#method.powi)
1194 pub fn powif64(a: f64, x: i32) -> f64;
1196 /// Returns the sine of an `f32`.
1198 /// The stabilized version of this intrinsic is
1199 /// [`f32::sin`](../../std/primitive.f32.html#method.sin)
1200 pub fn sinf32(x: f32) -> f32;
1201 /// Returns the sine of an `f64`.
1203 /// The stabilized version of this intrinsic is
1204 /// [`f64::sin`](../../std/primitive.f64.html#method.sin)
1205 pub fn sinf64(x: f64) -> f64;
1207 /// Returns the cosine of an `f32`.
1209 /// The stabilized version of this intrinsic is
1210 /// [`f32::cos`](../../std/primitive.f32.html#method.cos)
1211 pub fn cosf32(x: f32) -> f32;
1212 /// Returns the cosine of an `f64`.
1214 /// The stabilized version of this intrinsic is
1215 /// [`f64::cos`](../../std/primitive.f64.html#method.cos)
1216 pub fn cosf64(x: f64) -> f64;
1218 /// Raises an `f32` to an `f32` power.
1220 /// The stabilized version of this intrinsic is
1221 /// [`f32::powf`](../../std/primitive.f32.html#method.powf)
1222 pub fn powf32(a: f32, x: f32) -> f32;
1223 /// Raises an `f64` to an `f64` power.
1225 /// The stabilized version of this intrinsic is
1226 /// [`f64::powf`](../../std/primitive.f64.html#method.powf)
1227 pub fn powf64(a: f64, x: f64) -> f64;
1229 /// Returns the exponential of an `f32`.
1231 /// The stabilized version of this intrinsic is
1232 /// [`f32::exp`](../../std/primitive.f32.html#method.exp)
1233 pub fn expf32(x: f32) -> f32;
1234 /// Returns the exponential of an `f64`.
1236 /// The stabilized version of this intrinsic is
1237 /// [`f64::exp`](../../std/primitive.f64.html#method.exp)
1238 pub fn expf64(x: f64) -> f64;
1240 /// Returns 2 raised to the power of an `f32`.
1242 /// The stabilized version of this intrinsic is
1243 /// [`f32::exp2`](../../std/primitive.f32.html#method.exp2)
1244 pub fn exp2f32(x: f32) -> f32;
1245 /// Returns 2 raised to the power of an `f64`.
1247 /// The stabilized version of this intrinsic is
1248 /// [`f64::exp2`](../../std/primitive.f64.html#method.exp2)
1249 pub fn exp2f64(x: f64) -> f64;
1251 /// Returns the natural logarithm of an `f32`.
1253 /// The stabilized version of this intrinsic is
1254 /// [`f32::ln`](../../std/primitive.f32.html#method.ln)
1255 pub fn logf32(x: f32) -> f32;
1256 /// Returns the natural logarithm of an `f64`.
1258 /// The stabilized version of this intrinsic is
1259 /// [`f64::ln`](../../std/primitive.f64.html#method.ln)
1260 pub fn logf64(x: f64) -> f64;
1262 /// Returns the base 10 logarithm of an `f32`.
1264 /// The stabilized version of this intrinsic is
1265 /// [`f32::log10`](../../std/primitive.f32.html#method.log10)
1266 pub fn log10f32(x: f32) -> f32;
1267 /// Returns the base 10 logarithm of an `f64`.
1269 /// The stabilized version of this intrinsic is
1270 /// [`f64::log10`](../../std/primitive.f64.html#method.log10)
1271 pub fn log10f64(x: f64) -> f64;
1273 /// Returns the base 2 logarithm of an `f32`.
1275 /// The stabilized version of this intrinsic is
1276 /// [`f32::log2`](../../std/primitive.f32.html#method.log2)
1277 pub fn log2f32(x: f32) -> f32;
1278 /// Returns the base 2 logarithm of an `f64`.
1280 /// The stabilized version of this intrinsic is
1281 /// [`f64::log2`](../../std/primitive.f64.html#method.log2)
1282 pub fn log2f64(x: f64) -> f64;
1284 /// Returns `a * b + c` for `f32` values.
1286 /// The stabilized version of this intrinsic is
1287 /// [`f32::mul_add`](../../std/primitive.f32.html#method.mul_add)
1288 pub fn fmaf32(a: f32, b: f32, c: f32) -> f32;
1289 /// Returns `a * b + c` for `f64` values.
1291 /// The stabilized version of this intrinsic is
1292 /// [`f64::mul_add`](../../std/primitive.f64.html#method.mul_add)
1293 pub fn fmaf64(a: f64, b: f64, c: f64) -> f64;
1295 /// Returns the absolute value of an `f32`.
1297 /// The stabilized version of this intrinsic is
1298 /// [`f32::abs`](../../std/primitive.f32.html#method.abs)
1299 pub fn fabsf32(x: f32) -> f32;
1300 /// Returns the absolute value of an `f64`.
1302 /// The stabilized version of this intrinsic is
1303 /// [`f64::abs`](../../std/primitive.f64.html#method.abs)
1304 pub fn fabsf64(x: f64) -> f64;
1306 /// Returns the minimum of two `f32` values.
1308 /// The stabilized version of this intrinsic is
1310 pub fn minnumf32(x: f32, y: f32) -> f32;
1311 /// Returns the minimum of two `f64` values.
1313 /// The stabilized version of this intrinsic is
1315 pub fn minnumf64(x: f64, y: f64) -> f64;
1316 /// Returns the maximum of two `f32` values.
1318 /// The stabilized version of this intrinsic is
1320 pub fn maxnumf32(x: f32, y: f32) -> f32;
1321 /// Returns the maximum of two `f64` values.
1323 /// The stabilized version of this intrinsic is
1325 pub fn maxnumf64(x: f64, y: f64) -> f64;
1327 /// Copies the sign from `y` to `x` for `f32` values.
1329 /// The stabilized version of this intrinsic is
1330 /// [`f32::copysign`](../../std/primitive.f32.html#method.copysign)
1331 pub fn copysignf32(x: f32, y: f32) -> f32;
1332 /// Copies the sign from `y` to `x` for `f64` values.
1334 /// The stabilized version of this intrinsic is
1335 /// [`f64::copysign`](../../std/primitive.f64.html#method.copysign)
1336 pub fn copysignf64(x: f64, y: f64) -> f64;
1338 /// Returns the largest integer less than or equal to an `f32`.
1340 /// The stabilized version of this intrinsic is
1341 /// [`f32::floor`](../../std/primitive.f32.html#method.floor)
1342 pub fn floorf32(x: f32) -> f32;
1343 /// Returns the largest integer less than or equal to an `f64`.
1345 /// The stabilized version of this intrinsic is
1346 /// [`f64::floor`](../../std/primitive.f64.html#method.floor)
1347 pub fn floorf64(x: f64) -> f64;
1349 /// Returns the smallest integer greater than or equal to an `f32`.
1351 /// The stabilized version of this intrinsic is
1352 /// [`f32::ceil`](../../std/primitive.f32.html#method.ceil)
1353 pub fn ceilf32(x: f32) -> f32;
1354 /// Returns the smallest integer greater than or equal to an `f64`.
1356 /// The stabilized version of this intrinsic is
1357 /// [`f64::ceil`](../../std/primitive.f64.html#method.ceil)
1358 pub fn ceilf64(x: f64) -> f64;
1360 /// Returns the integer part of an `f32`.
1362 /// The stabilized version of this intrinsic is
1363 /// [`f32::trunc`](../../std/primitive.f32.html#method.trunc)
1364 pub fn truncf32(x: f32) -> f32;
1365 /// Returns the integer part of an `f64`.
1367 /// The stabilized version of this intrinsic is
1368 /// [`f64::trunc`](../../std/primitive.f64.html#method.trunc)
1369 pub fn truncf64(x: f64) -> f64;
1371 /// Returns the nearest integer to an `f32`. May raise an inexact floating-point exception
1372 /// if the argument is not an integer.
1373 pub fn rintf32(x: f32) -> f32;
1374 /// Returns the nearest integer to an `f64`. May raise an inexact floating-point exception
1375 /// if the argument is not an integer.
1376 pub fn rintf64(x: f64) -> f64;
1378 /// Returns the nearest integer to an `f32`.
1380 /// This intrinsic does not have a stable counterpart.
1381 pub fn nearbyintf32(x: f32) -> f32;
1382 /// Returns the nearest integer to an `f64`.
1384 /// This intrinsic does not have a stable counterpart.
1385 pub fn nearbyintf64(x: f64) -> f64;
1387 /// Returns the nearest integer to an `f32`. Rounds half-way cases away from zero.
1389 /// The stabilized version of this intrinsic is
1390 /// [`f32::round`](../../std/primitive.f32.html#method.round)
1391 pub fn roundf32(x: f32) -> f32;
1392 /// Returns the nearest integer to an `f64`. Rounds half-way cases away from zero.
1394 /// The stabilized version of this intrinsic is
1395 /// [`f64::round`](../../std/primitive.f64.html#method.round)
1396 pub fn roundf64(x: f64) -> f64;
1398 /// Float addition that allows optimizations based on algebraic rules.
1399 /// May assume inputs are finite.
1401 /// This intrinsic does not have a stable counterpart.
1402 pub fn fadd_fast<T: Copy>(a: T, b: T) -> T;
1404 /// Float subtraction that allows optimizations based on algebraic rules.
1405 /// May assume inputs are finite.
1407 /// This intrinsic does not have a stable counterpart.
1408 pub fn fsub_fast<T: Copy>(a: T, b: T) -> T;
1410 /// Float multiplication that allows optimizations based on algebraic rules.
1411 /// May assume inputs are finite.
1413 /// This intrinsic does not have a stable counterpart.
1414 pub fn fmul_fast<T: Copy>(a: T, b: T) -> T;
1416 /// Float division that allows optimizations based on algebraic rules.
1417 /// May assume inputs are finite.
1419 /// This intrinsic does not have a stable counterpart.
1420 pub fn fdiv_fast<T: Copy>(a: T, b: T) -> T;
1422 /// Float remainder that allows optimizations based on algebraic rules.
1423 /// May assume inputs are finite.
1425 /// This intrinsic does not have a stable counterpart.
1426 pub fn frem_fast<T: Copy>(a: T, b: T) -> T;
1428 /// Convert with LLVM’s fptoui/fptosi, which may return undef for values out of range
1429 /// (<https://github.com/rust-lang/rust/issues/10184>)
1431 /// Stabilized as [`f32::to_int_unchecked`] and [`f64::to_int_unchecked`].
1432 pub fn float_to_int_unchecked<Float: Copy, Int: Copy>(value: Float) -> Int;
1434 /// Returns the number of bits set in an integer type `T`
1436 /// The stabilized versions of this intrinsic are available on the integer
1437 /// primitives via the `count_ones` method. For example,
1438 /// [`u32::count_ones`]
1439 #[rustc_const_stable(feature = "const_ctpop", since = "1.40.0")]
1440 pub fn ctpop<T: Copy>(x: T) -> T;
1442 /// Returns the number of leading unset bits (zeroes) in an integer type `T`.
1444 /// The stabilized versions of this intrinsic are available on the integer
1445 /// primitives via the `leading_zeros` method. For example,
1446 /// [`u32::leading_zeros`]
1451 /// #![feature(core_intrinsics)]
1453 /// use std::intrinsics::ctlz;
1455 /// let x = 0b0001_1100_u8;
1456 /// let num_leading = ctlz(x);
1457 /// assert_eq!(num_leading, 3);
1460 /// An `x` with value `0` will return the bit width of `T`.
1463 /// #![feature(core_intrinsics)]
1465 /// use std::intrinsics::ctlz;
1468 /// let num_leading = ctlz(x);
1469 /// assert_eq!(num_leading, 16);
1471 #[rustc_const_stable(feature = "const_ctlz", since = "1.40.0")]
1472 pub fn ctlz<T: Copy>(x: T) -> T;
1474 /// Like `ctlz`, but extra-unsafe as it returns `undef` when
1475 /// given an `x` with value `0`.
1477 /// This intrinsic does not have a stable counterpart.
1482 /// #![feature(core_intrinsics)]
1484 /// use std::intrinsics::ctlz_nonzero;
1486 /// let x = 0b0001_1100_u8;
1487 /// let num_leading = unsafe { ctlz_nonzero(x) };
1488 /// assert_eq!(num_leading, 3);
1490 #[rustc_const_stable(feature = "constctlz", since = "1.50.0")]
1491 pub fn ctlz_nonzero<T: Copy>(x: T) -> T;
1493 /// Returns the number of trailing unset bits (zeroes) in an integer type `T`.
1495 /// The stabilized versions of this intrinsic are available on the integer
1496 /// primitives via the `trailing_zeros` method. For example,
1497 /// [`u32::trailing_zeros`]
1502 /// #![feature(core_intrinsics)]
1504 /// use std::intrinsics::cttz;
1506 /// let x = 0b0011_1000_u8;
1507 /// let num_trailing = cttz(x);
1508 /// assert_eq!(num_trailing, 3);
1511 /// An `x` with value `0` will return the bit width of `T`:
1514 /// #![feature(core_intrinsics)]
1516 /// use std::intrinsics::cttz;
1519 /// let num_trailing = cttz(x);
1520 /// assert_eq!(num_trailing, 16);
1522 #[rustc_const_stable(feature = "const_cttz", since = "1.40.0")]
1523 pub fn cttz<T: Copy>(x: T) -> T;
1525 /// Like `cttz`, but extra-unsafe as it returns `undef` when
1526 /// given an `x` with value `0`.
1528 /// This intrinsic does not have a stable counterpart.
1533 /// #![feature(core_intrinsics)]
1535 /// use std::intrinsics::cttz_nonzero;
1537 /// let x = 0b0011_1000_u8;
1538 /// let num_trailing = unsafe { cttz_nonzero(x) };
1539 /// assert_eq!(num_trailing, 3);
1541 #[rustc_const_unstable(feature = "const_cttz", issue = "none")]
1542 pub fn cttz_nonzero<T: Copy>(x: T) -> T;
1544 /// Reverses the bytes in an integer type `T`.
1546 /// The stabilized versions of this intrinsic are available on the integer
1547 /// primitives via the `swap_bytes` method. For example,
1548 /// [`u32::swap_bytes`]
1549 #[rustc_const_stable(feature = "const_bswap", since = "1.40.0")]
1550 pub fn bswap<T: Copy>(x: T) -> T;
1552 /// Reverses the bits in an integer type `T`.
1554 /// The stabilized versions of this intrinsic are available on the integer
1555 /// primitives via the `reverse_bits` method. For example,
1556 /// [`u32::reverse_bits`]
1557 #[rustc_const_stable(feature = "const_bitreverse", since = "1.40.0")]
1558 pub fn bitreverse<T: Copy>(x: T) -> T;
1560 /// Performs checked integer addition.
1562 /// The stabilized versions of this intrinsic are available on the integer
1563 /// primitives via the `overflowing_add` method. For example,
1564 /// [`u32::overflowing_add`]
1565 #[rustc_const_stable(feature = "const_int_overflow", since = "1.40.0")]
1566 pub fn add_with_overflow<T: Copy>(x: T, y: T) -> (T, bool);
1568 /// Performs checked integer subtraction
1570 /// The stabilized versions of this intrinsic are available on the integer
1571 /// primitives via the `overflowing_sub` method. For example,
1572 /// [`u32::overflowing_sub`]
1573 #[rustc_const_stable(feature = "const_int_overflow", since = "1.40.0")]
1574 pub fn sub_with_overflow<T: Copy>(x: T, y: T) -> (T, bool);
1576 /// Performs checked integer multiplication
1578 /// The stabilized versions of this intrinsic are available on the integer
1579 /// primitives via the `overflowing_mul` method. For example,
1580 /// [`u32::overflowing_mul`]
1581 #[rustc_const_stable(feature = "const_int_overflow", since = "1.40.0")]
1582 pub fn mul_with_overflow<T: Copy>(x: T, y: T) -> (T, bool);
1584 /// Performs an exact division, resulting in undefined behavior where
1585 /// `x % y != 0` or `y == 0` or `x == T::MIN && y == -1`
1587 /// This intrinsic does not have a stable counterpart.
1588 pub fn exact_div<T: Copy>(x: T, y: T) -> T;
1590 /// Performs an unchecked division, resulting in undefined behavior
1591 /// where y = 0 or x = `T::MIN` and y = -1
1593 /// Safe wrappers for this intrinsic are available on the integer
1594 /// primitives via the `checked_div` method. For example,
1595 /// [`u32::checked_div`]
1596 #[rustc_const_unstable(feature = "const_int_unchecked_arith", issue = "none")]
1597 pub fn unchecked_div<T: Copy>(x: T, y: T) -> T;
1598 /// Returns the remainder of an unchecked division, resulting in
1599 /// undefined behavior where y = 0 or x = `T::MIN` and y = -1
1601 /// Safe wrappers for this intrinsic are available on the integer
1602 /// primitives via the `checked_rem` method. For example,
1603 /// [`u32::checked_rem`]
1604 #[rustc_const_unstable(feature = "const_int_unchecked_arith", issue = "none")]
1605 pub fn unchecked_rem<T: Copy>(x: T, y: T) -> T;
1607 /// Performs an unchecked left shift, resulting in undefined behavior when
1608 /// y < 0 or y >= N, where N is the width of T in bits.
1610 /// Safe wrappers for this intrinsic are available on the integer
1611 /// primitives via the `checked_shl` method. For example,
1612 /// [`u32::checked_shl`]
1613 #[rustc_const_stable(feature = "const_int_unchecked", since = "1.40.0")]
1614 pub fn unchecked_shl<T: Copy>(x: T, y: T) -> T;
1615 /// Performs an unchecked right shift, resulting in undefined behavior when
1616 /// y < 0 or y >= N, where N is the width of T in bits.
1618 /// Safe wrappers for this intrinsic are available on the integer
1619 /// primitives via the `checked_shr` method. For example,
1620 /// [`u32::checked_shr`]
1621 #[rustc_const_stable(feature = "const_int_unchecked", since = "1.40.0")]
1622 pub fn unchecked_shr<T: Copy>(x: T, y: T) -> T;
1624 /// Returns the result of an unchecked addition, resulting in
1625 /// undefined behavior when `x + y > T::MAX` or `x + y < T::MIN`.
1627 /// This intrinsic does not have a stable counterpart.
1628 #[rustc_const_unstable(feature = "const_int_unchecked_arith", issue = "none")]
1629 pub fn unchecked_add<T: Copy>(x: T, y: T) -> T;
1631 /// Returns the result of an unchecked subtraction, resulting in
1632 /// undefined behavior when `x - y > T::MAX` or `x - y < T::MIN`.
1634 /// This intrinsic does not have a stable counterpart.
1635 #[rustc_const_unstable(feature = "const_int_unchecked_arith", issue = "none")]
1636 pub fn unchecked_sub<T: Copy>(x: T, y: T) -> T;
1638 /// Returns the result of an unchecked multiplication, resulting in
1639 /// undefined behavior when `x * y > T::MAX` or `x * y < T::MIN`.
1641 /// This intrinsic does not have a stable counterpart.
1642 #[rustc_const_unstable(feature = "const_int_unchecked_arith", issue = "none")]
1643 pub fn unchecked_mul<T: Copy>(x: T, y: T) -> T;
1645 /// Performs rotate left.
1647 /// The stabilized versions of this intrinsic are available on the integer
1648 /// primitives via the `rotate_left` method. For example,
1649 /// [`u32::rotate_left`]
1650 #[rustc_const_stable(feature = "const_int_rotate", since = "1.40.0")]
1651 pub fn rotate_left<T: Copy>(x: T, y: T) -> T;
1653 /// Performs rotate right.
1655 /// The stabilized versions of this intrinsic are available on the integer
1656 /// primitives via the `rotate_right` method. For example,
1657 /// [`u32::rotate_right`]
1658 #[rustc_const_stable(feature = "const_int_rotate", since = "1.40.0")]
1659 pub fn rotate_right<T: Copy>(x: T, y: T) -> T;
1661 /// Returns (a + b) mod 2<sup>N</sup>, where N is the width of T in bits.
1663 /// The stabilized versions of this intrinsic are available on the integer
1664 /// primitives via the `wrapping_add` method. For example,
1665 /// [`u32::wrapping_add`]
1666 #[rustc_const_stable(feature = "const_int_wrapping", since = "1.40.0")]
1667 pub fn wrapping_add<T: Copy>(a: T, b: T) -> T;
1668 /// Returns (a - b) mod 2<sup>N</sup>, where N is the width of T in bits.
1670 /// The stabilized versions of this intrinsic are available on the integer
1671 /// primitives via the `wrapping_sub` method. For example,
1672 /// [`u32::wrapping_sub`]
1673 #[rustc_const_stable(feature = "const_int_wrapping", since = "1.40.0")]
1674 pub fn wrapping_sub<T: Copy>(a: T, b: T) -> T;
1675 /// Returns (a * b) mod 2<sup>N</sup>, where N is the width of T in bits.
1677 /// The stabilized versions of this intrinsic are available on the integer
1678 /// primitives via the `wrapping_mul` method. For example,
1679 /// [`u32::wrapping_mul`]
1680 #[rustc_const_stable(feature = "const_int_wrapping", since = "1.40.0")]
1681 pub fn wrapping_mul<T: Copy>(a: T, b: T) -> T;
1683 /// Computes `a + b`, while saturating at numeric bounds.
1685 /// The stabilized versions of this intrinsic are available on the integer
1686 /// primitives via the `saturating_add` method. For example,
1687 /// [`u32::saturating_add`]
1688 #[rustc_const_stable(feature = "const_int_saturating", since = "1.40.0")]
1689 pub fn saturating_add<T: Copy>(a: T, b: T) -> T;
1690 /// Computes `a - b`, while saturating at numeric bounds.
1692 /// The stabilized versions of this intrinsic are available on the integer
1693 /// primitives via the `saturating_sub` method. For example,
1694 /// [`u32::saturating_sub`]
1695 #[rustc_const_stable(feature = "const_int_saturating", since = "1.40.0")]
1696 pub fn saturating_sub<T: Copy>(a: T, b: T) -> T;
1698 /// Returns the value of the discriminant for the variant in 'v',
1699 /// cast to a `u64`; if `T` has no discriminant, returns 0.
1701 /// The stabilized version of this intrinsic is [`core::mem::discriminant`](crate::mem::discriminant).
1702 #[rustc_const_unstable(feature = "const_discriminant", issue = "69821")]
1703 pub fn discriminant_value<T>(v: &T) -> <T as DiscriminantKind>::Discriminant;
1705 /// Returns the number of variants of the type `T` cast to a `usize`;
1706 /// if `T` has no variants, returns 0. Uninhabited variants will be counted.
1708 /// The to-be-stabilized version of this intrinsic is [`mem::variant_count`].
1709 #[rustc_const_unstable(feature = "variant_count", issue = "73662")]
1710 pub fn variant_count<T>() -> usize;
1712 /// Rust's "try catch" construct which invokes the function pointer `try_fn`
1713 /// with the data pointer `data`.
1715 /// The third argument is a function called if a panic occurs. This function
1716 /// takes the data pointer and a pointer to the target-specific exception
1717 /// object that was caught. For more information see the compiler's
1718 /// source as well as std's catch implementation.
1719 pub fn r#try(try_fn: fn(*mut u8), data: *mut u8, catch_fn: fn(*mut u8, *mut u8)) -> i32;
1721 /// Emits a `!nontemporal` store according to LLVM (see their docs).
1722 /// Probably will never become stable.
1723 pub fn nontemporal_store<T>(ptr: *mut T, val: T);
1725 /// See documentation of `<*const T>::offset_from` for details.
1726 #[rustc_const_unstable(feature = "const_ptr_offset_from", issue = "41079")]
1727 pub fn ptr_offset_from<T>(ptr: *const T, base: *const T) -> isize;
1729 /// See documentation of `<*const T>::guaranteed_eq` for details.
1730 #[rustc_const_unstable(feature = "const_raw_ptr_comparison", issue = "53020")]
1731 pub fn ptr_guaranteed_eq<T>(ptr: *const T, other: *const T) -> bool;
1733 /// See documentation of `<*const T>::guaranteed_ne` for details.
1734 #[rustc_const_unstable(feature = "const_raw_ptr_comparison", issue = "53020")]
1735 pub fn ptr_guaranteed_ne<T>(ptr: *const T, other: *const T) -> bool;
1738 // Some functions are defined here because they accidentally got made
1739 // available in this module on stable. See <https://github.com/rust-lang/rust/issues/15702>.
1740 // (`transmute` also falls into this category, but it cannot be wrapped due to the
1741 // check that `T` and `U` have the same size.)
1743 /// Checks whether `ptr` is properly aligned with respect to
1744 /// `align_of::<T>()`.
1745 pub(crate) fn is_aligned_and_not_null<T>(ptr: *const T) -> bool {
1746 !ptr.is_null() && ptr as usize % mem::align_of::<T>() == 0
1749 /// Checks whether the regions of memory starting at `src` and `dst` of size
1750 /// `count * size_of::<T>()` do *not* overlap.
1751 pub(crate) fn is_nonoverlapping<T>(src: *const T, dst: *const T, count: usize) -> bool {
1752 let src_usize = src as usize;
1753 let dst_usize = dst as usize;
1754 let size = mem::size_of::<T>().checked_mul(count).unwrap();
1755 let diff = if src_usize > dst_usize { src_usize - dst_usize } else { dst_usize - src_usize };
1756 // If the absolute distance between the ptrs is at least as big as the size of the buffer,
1757 // they do not overlap.
1761 /// Copies `count * size_of::<T>()` bytes from `src` to `dst`. The source
1762 /// and destination must *not* overlap.
1764 /// For regions of memory which might overlap, use [`copy`] instead.
1766 /// `copy_nonoverlapping` is semantically equivalent to C's [`memcpy`], but
1767 /// with the argument order swapped.
1769 /// [`memcpy`]: https://en.cppreference.com/w/c/string/byte/memcpy
1773 /// Behavior is undefined if any of the following conditions are violated:
1775 /// * `src` must be [valid] for reads of `count * size_of::<T>()` bytes.
1777 /// * `dst` must be [valid] for writes of `count * size_of::<T>()` bytes.
1779 /// * Both `src` and `dst` must be properly aligned.
1781 /// * The region of memory beginning at `src` with a size of `count *
1782 /// size_of::<T>()` bytes must *not* overlap with the region of memory
1783 /// beginning at `dst` with the same size.
1785 /// Like [`read`], `copy_nonoverlapping` creates a bitwise copy of `T`, regardless of
1786 /// whether `T` is [`Copy`]. If `T` is not [`Copy`], using *both* the values
1787 /// in the region beginning at `*src` and the region beginning at `*dst` can
1788 /// [violate memory safety][read-ownership].
1790 /// Note that even if the effectively copied size (`count * size_of::<T>()`) is
1791 /// `0`, the pointers must be non-NULL and properly aligned.
1793 /// [`read`]: crate::ptr::read
1794 /// [read-ownership]: crate::ptr::read#ownership-of-the-returned-value
1795 /// [valid]: crate::ptr#safety
1799 /// Manually implement [`Vec::append`]:
1804 /// /// Moves all the elements of `src` into `dst`, leaving `src` empty.
1805 /// fn append<T>(dst: &mut Vec<T>, src: &mut Vec<T>) {
1806 /// let src_len = src.len();
1807 /// let dst_len = dst.len();
1809 /// // Ensure that `dst` has enough capacity to hold all of `src`.
1810 /// dst.reserve(src_len);
1813 /// // The call to offset is always safe because `Vec` will never
1814 /// // allocate more than `isize::MAX` bytes.
1815 /// let dst_ptr = dst.as_mut_ptr().offset(dst_len as isize);
1816 /// let src_ptr = src.as_ptr();
1818 /// // Truncate `src` without dropping its contents. We do this first,
1819 /// // to avoid problems in case something further down panics.
1822 /// // The two regions cannot overlap because mutable references do
1823 /// // not alias, and two different vectors cannot own the same
1825 /// ptr::copy_nonoverlapping(src_ptr, dst_ptr, src_len);
1827 /// // Notify `dst` that it now holds the contents of `src`.
1828 /// dst.set_len(dst_len + src_len);
1832 /// let mut a = vec!['r'];
1833 /// let mut b = vec!['u', 's', 't'];
1835 /// append(&mut a, &mut b);
1837 /// assert_eq!(a, &['r', 'u', 's', 't']);
1838 /// assert!(b.is_empty());
1841 /// [`Vec::append`]: ../../std/vec/struct.Vec.html#method.append
1842 #[doc(alias = "memcpy")]
1843 #[stable(feature = "rust1", since = "1.0.0")]
1845 pub unsafe fn copy_nonoverlapping<T>(src: *const T, dst: *mut T, count: usize) {
1846 extern "rust-intrinsic" {
1847 fn copy_nonoverlapping<T>(src: *const T, dst: *mut T, count: usize);
1850 if cfg!(debug_assertions)
1851 && !(is_aligned_and_not_null(src)
1852 && is_aligned_and_not_null(dst)
1853 && is_nonoverlapping(src, dst, count))
1855 // Not panicking to keep codegen impact smaller.
1859 // SAFETY: the safety contract for `copy_nonoverlapping` must be
1860 // upheld by the caller.
1861 unsafe { copy_nonoverlapping(src, dst, count) }
1864 /// Copies `count * size_of::<T>()` bytes from `src` to `dst`. The source
1865 /// and destination may overlap.
1867 /// If the source and destination will *never* overlap,
1868 /// [`copy_nonoverlapping`] can be used instead.
1870 /// `copy` is semantically equivalent to C's [`memmove`], but with the argument
1871 /// order swapped. Copying takes place as if the bytes were copied from `src`
1872 /// to a temporary array and then copied from the array to `dst`.
1874 /// [`memmove`]: https://en.cppreference.com/w/c/string/byte/memmove
1878 /// Behavior is undefined if any of the following conditions are violated:
1880 /// * `src` must be [valid] for reads of `count * size_of::<T>()` bytes.
1882 /// * `dst` must be [valid] for writes of `count * size_of::<T>()` bytes.
1884 /// * Both `src` and `dst` must be properly aligned.
1886 /// Like [`read`], `copy` creates a bitwise copy of `T`, regardless of
1887 /// whether `T` is [`Copy`]. If `T` is not [`Copy`], using both the values
1888 /// in the region beginning at `*src` and the region beginning at `*dst` can
1889 /// [violate memory safety][read-ownership].
1891 /// Note that even if the effectively copied size (`count * size_of::<T>()`) is
1892 /// `0`, the pointers must be non-NULL and properly aligned.
1894 /// [`read`]: crate::ptr::read
1895 /// [read-ownership]: crate::ptr::read#ownership-of-the-returned-value
1896 /// [valid]: crate::ptr#safety
1900 /// Efficiently create a Rust vector from an unsafe buffer:
1907 /// /// * `ptr` must be correctly aligned for its type and non-zero.
1908 /// /// * `ptr` must be valid for reads of `elts` contiguous elements of type `T`.
1909 /// /// * Those elements must not be used after calling this function unless `T: Copy`.
1910 /// # #[allow(dead_code)]
1911 /// unsafe fn from_buf_raw<T>(ptr: *const T, elts: usize) -> Vec<T> {
1912 /// let mut dst = Vec::with_capacity(elts);
1914 /// // SAFETY: Our precondition ensures the source is aligned and valid,
1915 /// // and `Vec::with_capacity` ensures that we have usable space to write them.
1916 /// ptr::copy(ptr, dst.as_mut_ptr(), elts);
1918 /// // SAFETY: We created it with this much capacity earlier,
1919 /// // and the previous `copy` has initialized these elements.
1920 /// dst.set_len(elts);
1924 #[doc(alias = "memmove")]
1925 #[stable(feature = "rust1", since = "1.0.0")]
1927 pub unsafe fn copy<T>(src: *const T, dst: *mut T, count: usize) {
1928 extern "rust-intrinsic" {
1929 fn copy<T>(src: *const T, dst: *mut T, count: usize);
1932 if cfg!(debug_assertions) && !(is_aligned_and_not_null(src) && is_aligned_and_not_null(dst)) {
1933 // Not panicking to keep codegen impact smaller.
1937 // SAFETY: the safety contract for `copy` must be upheld by the caller.
1938 unsafe { copy(src, dst, count) }
1941 /// Sets `count * size_of::<T>()` bytes of memory starting at `dst` to
1944 /// `write_bytes` is similar to C's [`memset`], but sets `count *
1945 /// size_of::<T>()` bytes to `val`.
1947 /// [`memset`]: https://en.cppreference.com/w/c/string/byte/memset
1951 /// Behavior is undefined if any of the following conditions are violated:
1953 /// * `dst` must be [valid] for writes of `count * size_of::<T>()` bytes.
1955 /// * `dst` must be properly aligned.
1957 /// Additionally, the caller must ensure that writing `count *
1958 /// size_of::<T>()` bytes to the given region of memory results in a valid
1959 /// value of `T`. Using a region of memory typed as a `T` that contains an
1960 /// invalid value of `T` is undefined behavior.
1962 /// Note that even if the effectively copied size (`count * size_of::<T>()`) is
1963 /// `0`, the pointer must be non-NULL and properly aligned.
1965 /// [valid]: crate::ptr#safety
1974 /// let mut vec = vec![0u32; 4];
1976 /// let vec_ptr = vec.as_mut_ptr();
1977 /// ptr::write_bytes(vec_ptr, 0xfe, 2);
1979 /// assert_eq!(vec, [0xfefefefe, 0xfefefefe, 0, 0]);
1982 /// Creating an invalid value:
1987 /// let mut v = Box::new(0i32);
1990 /// // Leaks the previously held value by overwriting the `Box<T>` with
1991 /// // a null pointer.
1992 /// ptr::write_bytes(&mut v as *mut Box<i32>, 0, 1);
1995 /// // At this point, using or dropping `v` results in undefined behavior.
1996 /// // drop(v); // ERROR
1998 /// // Even leaking `v` "uses" it, and hence is undefined behavior.
1999 /// // mem::forget(v); // ERROR
2001 /// // In fact, `v` is invalid according to basic type layout invariants, so *any*
2002 /// // operation touching it is undefined behavior.
2003 /// // let v2 = v; // ERROR
2006 /// // Let us instead put in a valid value
2007 /// ptr::write(&mut v as *mut Box<i32>, Box::new(42i32));
2010 /// // Now the box is fine
2011 /// assert_eq!(*v, 42);
2013 #[stable(feature = "rust1", since = "1.0.0")]
2015 pub unsafe fn write_bytes<T>(dst: *mut T, val: u8, count: usize) {
2016 extern "rust-intrinsic" {
2017 fn write_bytes<T>(dst: *mut T, val: u8, count: usize);
2020 debug_assert!(is_aligned_and_not_null(dst), "attempt to write to unaligned or null pointer");
2022 // SAFETY: the safety contract for `write_bytes` must be upheld by the caller.
2023 unsafe { write_bytes(dst, val, count) }