1 //! Compiler intrinsics.
3 //! The corresponding definitions are in <https://github.com/rust-lang/rust/blob/master/compiler/rustc_codegen_llvm/src/intrinsic.rs>.
4 //! The corresponding const implementations are in <https://github.com/rust-lang/rust/blob/master/compiler/rustc_const_eval/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 //! <https://github.com/rust-lang/rust/blob/master/compiler/rustc_const_eval/src/interpret/intrinsics.rs> and add a
14 //! `#[rustc_const_unstable(feature = "const_such_and_such", issue = "01234")]` to the intrinsic declaration.
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]: https://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]: https://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::{Destruct, 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")]
66 #[deprecated(note = "no longer an intrinsic - use `ptr::drop_in_place` directly", since = "1.52.0")]
68 pub unsafe fn drop_in_place<T: ?Sized>(to_drop: *mut T) {
69 // SAFETY: see `ptr::drop_in_place`
70 unsafe { crate::ptr::drop_in_place(to_drop) }
73 extern "rust-intrinsic" {
74 // N.B., these intrinsics take raw pointers because they mutate aliased
75 // memory, which is not valid for either `&` or `&mut`.
77 /// Stores a value if the current value is the same as the `old` value.
79 /// The stabilized version of this intrinsic is available on the
80 /// [`atomic`] types via the `compare_exchange` method by passing
81 /// [`Ordering::SeqCst`] as both the `success` and `failure` parameters.
82 /// For example, [`AtomicBool::compare_exchange`].
83 pub fn atomic_cxchg<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
84 /// Stores a value if the current value is the same as the `old` value.
86 /// The stabilized version of this intrinsic is available on the
87 /// [`atomic`] types via the `compare_exchange` method by passing
88 /// [`Ordering::Acquire`] as both the `success` and `failure` parameters.
89 /// For example, [`AtomicBool::compare_exchange`].
90 pub fn atomic_cxchg_acq<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
91 /// Stores a value if the current value is the same as the `old` value.
93 /// The stabilized version of this intrinsic is available on the
94 /// [`atomic`] types via the `compare_exchange` method by passing
95 /// [`Ordering::Release`] as the `success` and [`Ordering::Relaxed`] as the
96 /// `failure` parameters. For example, [`AtomicBool::compare_exchange`].
97 pub fn atomic_cxchg_rel<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
98 /// Stores a value if the current value is the same as the `old` value.
100 /// The stabilized version of this intrinsic is available on the
101 /// [`atomic`] types via the `compare_exchange` method by passing
102 /// [`Ordering::AcqRel`] as the `success` and [`Ordering::Acquire`] as the
103 /// `failure` parameters. For example, [`AtomicBool::compare_exchange`].
104 pub fn atomic_cxchg_acqrel<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
105 /// 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 /// [`atomic`] types via the `compare_exchange` method by passing
109 /// [`Ordering::Relaxed`] as both the `success` and `failure` parameters.
110 /// For example, [`AtomicBool::compare_exchange`].
111 pub fn atomic_cxchg_relaxed<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
112 /// Stores a value if the current value is the same as the `old` value.
114 /// The stabilized version of this intrinsic is available on the
115 /// [`atomic`] types via the `compare_exchange` method by passing
116 /// [`Ordering::SeqCst`] as the `success` and [`Ordering::Relaxed`] as the
117 /// `failure` parameters. For example, [`AtomicBool::compare_exchange`].
118 pub fn atomic_cxchg_failrelaxed<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
119 /// Stores a value if the current value is the same as the `old` value.
121 /// The stabilized version of this intrinsic is available on the
122 /// [`atomic`] types via the `compare_exchange` method by passing
123 /// [`Ordering::SeqCst`] as the `success` and [`Ordering::Acquire`] as the
124 /// `failure` parameters. For example, [`AtomicBool::compare_exchange`].
125 pub fn atomic_cxchg_failacq<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
126 /// Stores a value if the current value is the same as the `old` value.
128 /// The stabilized version of this intrinsic is available on the
129 /// [`atomic`] types via the `compare_exchange` method by passing
130 /// [`Ordering::Acquire`] as the `success` and [`Ordering::Relaxed`] as the
131 /// `failure` parameters. For example, [`AtomicBool::compare_exchange`].
132 pub fn atomic_cxchg_acq_failrelaxed<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
133 /// Stores a value if the current value is the same as the `old` value.
135 /// The stabilized version of this intrinsic is available on the
136 /// [`atomic`] types via the `compare_exchange` method by passing
137 /// [`Ordering::AcqRel`] as the `success` and [`Ordering::Relaxed`] as the
138 /// `failure` parameters. For example, [`AtomicBool::compare_exchange`].
139 pub fn atomic_cxchg_acqrel_failrelaxed<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
141 /// Stores a value if the current value is the same as the `old` value.
143 /// The stabilized version of this intrinsic is available on the
144 /// [`atomic`] types via the `compare_exchange_weak` method by passing
145 /// [`Ordering::SeqCst`] as both the `success` and `failure` parameters.
146 /// For example, [`AtomicBool::compare_exchange_weak`].
147 pub fn atomic_cxchgweak<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
148 /// Stores a value if the current value is the same as the `old` value.
150 /// The stabilized version of this intrinsic is available on the
151 /// [`atomic`] types via the `compare_exchange_weak` method by passing
152 /// [`Ordering::Acquire`] as both the `success` and `failure` parameters.
153 /// For example, [`AtomicBool::compare_exchange_weak`].
154 pub fn atomic_cxchgweak_acq<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
155 /// Stores a value if the current value is the same as the `old` value.
157 /// The stabilized version of this intrinsic is available on the
158 /// [`atomic`] types via the `compare_exchange_weak` method by passing
159 /// [`Ordering::Release`] as the `success` and [`Ordering::Relaxed`] as the
160 /// `failure` parameters. For example, [`AtomicBool::compare_exchange_weak`].
161 pub fn atomic_cxchgweak_rel<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
162 /// Stores a value if the current value is the same as the `old` value.
164 /// The stabilized version of this intrinsic is available on the
165 /// [`atomic`] types via the `compare_exchange_weak` method by passing
166 /// [`Ordering::AcqRel`] as the `success` and [`Ordering::Acquire`] as the
167 /// `failure` parameters. For example, [`AtomicBool::compare_exchange_weak`].
168 pub fn atomic_cxchgweak_acqrel<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
169 /// Stores a value if the current value is the same as the `old` value.
171 /// The stabilized version of this intrinsic is available on the
172 /// [`atomic`] types via the `compare_exchange_weak` method by passing
173 /// [`Ordering::Relaxed`] as both the `success` and `failure` parameters.
174 /// For example, [`AtomicBool::compare_exchange_weak`].
175 pub fn atomic_cxchgweak_relaxed<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
176 /// Stores a value if the current value is the same as the `old` value.
178 /// The stabilized version of this intrinsic is available on the
179 /// [`atomic`] types via the `compare_exchange_weak` method by passing
180 /// [`Ordering::SeqCst`] as the `success` and [`Ordering::Relaxed`] as the
181 /// `failure` parameters. For example, [`AtomicBool::compare_exchange_weak`].
182 pub fn atomic_cxchgweak_failrelaxed<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
183 /// Stores a value if the current value is the same as the `old` value.
185 /// The stabilized version of this intrinsic is available on the
186 /// [`atomic`] types via the `compare_exchange_weak` method by passing
187 /// [`Ordering::SeqCst`] as the `success` and [`Ordering::Acquire`] as the
188 /// `failure` parameters. For example, [`AtomicBool::compare_exchange_weak`].
189 pub fn atomic_cxchgweak_failacq<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
190 /// Stores a value if the current value is the same as the `old` value.
192 /// The stabilized version of this intrinsic is available on the
193 /// [`atomic`] types via the `compare_exchange_weak` method by passing
194 /// [`Ordering::Acquire`] as the `success` and [`Ordering::Relaxed`] as the
195 /// `failure` parameters. For example, [`AtomicBool::compare_exchange_weak`].
196 pub fn atomic_cxchgweak_acq_failrelaxed<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
197 /// Stores a value if the current value is the same as the `old` value.
199 /// The stabilized version of this intrinsic is available on the
200 /// [`atomic`] types via the `compare_exchange_weak` method by passing
201 /// [`Ordering::AcqRel`] as the `success` and [`Ordering::Relaxed`] as the
202 /// `failure` parameters. For example, [`AtomicBool::compare_exchange_weak`].
203 pub fn atomic_cxchgweak_acqrel_failrelaxed<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
205 /// Loads the current value of the pointer.
207 /// The stabilized version of this intrinsic is available on the
208 /// [`atomic`] types via the `load` method by passing
209 /// [`Ordering::SeqCst`] as the `order`. For example, [`AtomicBool::load`].
210 pub fn atomic_load<T: Copy>(src: *const T) -> T;
211 /// Loads the current value of the pointer.
213 /// The stabilized version of this intrinsic is available on the
214 /// [`atomic`] types via the `load` method by passing
215 /// [`Ordering::Acquire`] as the `order`. For example, [`AtomicBool::load`].
216 pub fn atomic_load_acq<T: Copy>(src: *const T) -> T;
217 /// Loads the current value of the pointer.
219 /// The stabilized version of this intrinsic is available on the
220 /// [`atomic`] types via the `load` method by passing
221 /// [`Ordering::Relaxed`] as the `order`. For example, [`AtomicBool::load`].
222 pub fn atomic_load_relaxed<T: Copy>(src: *const T) -> T;
223 pub fn atomic_load_unordered<T: Copy>(src: *const T) -> T;
225 /// Stores the value at the specified memory location.
227 /// The stabilized version of this intrinsic is available on the
228 /// [`atomic`] types via the `store` method by passing
229 /// [`Ordering::SeqCst`] as the `order`. For example, [`AtomicBool::store`].
230 pub fn atomic_store<T: Copy>(dst: *mut T, val: T);
231 /// Stores the value at the specified memory location.
233 /// The stabilized version of this intrinsic is available on the
234 /// [`atomic`] types via the `store` method by passing
235 /// [`Ordering::Release`] as the `order`. For example, [`AtomicBool::store`].
236 pub fn atomic_store_rel<T: Copy>(dst: *mut T, val: T);
237 /// Stores the value at the specified memory location.
239 /// The stabilized version of this intrinsic is available on the
240 /// [`atomic`] types via the `store` method by passing
241 /// [`Ordering::Relaxed`] as the `order`. For example, [`AtomicBool::store`].
242 pub fn atomic_store_relaxed<T: Copy>(dst: *mut T, val: T);
243 pub fn atomic_store_unordered<T: Copy>(dst: *mut T, val: T);
245 /// Stores the value at the specified memory location, returning the old value.
247 /// The stabilized version of this intrinsic is available on the
248 /// [`atomic`] types via the `swap` method by passing
249 /// [`Ordering::SeqCst`] as the `order`. For example, [`AtomicBool::swap`].
250 pub fn atomic_xchg<T: Copy>(dst: *mut T, src: T) -> T;
251 /// Stores the value at the specified memory location, returning the old value.
253 /// The stabilized version of this intrinsic is available on the
254 /// [`atomic`] types via the `swap` method by passing
255 /// [`Ordering::Acquire`] as the `order`. For example, [`AtomicBool::swap`].
256 pub fn atomic_xchg_acq<T: Copy>(dst: *mut T, src: T) -> T;
257 /// Stores the value at the specified memory location, returning the old value.
259 /// The stabilized version of this intrinsic is available on the
260 /// [`atomic`] types via the `swap` method by passing
261 /// [`Ordering::Release`] as the `order`. For example, [`AtomicBool::swap`].
262 pub fn atomic_xchg_rel<T: Copy>(dst: *mut T, src: T) -> T;
263 /// Stores the value at the specified memory location, returning the old value.
265 /// The stabilized version of this intrinsic is available on the
266 /// [`atomic`] types via the `swap` method by passing
267 /// [`Ordering::AcqRel`] as the `order`. For example, [`AtomicBool::swap`].
268 pub fn atomic_xchg_acqrel<T: Copy>(dst: *mut T, src: T) -> T;
269 /// Stores the value at the specified memory location, returning the old value.
271 /// The stabilized version of this intrinsic is available on the
272 /// [`atomic`] types via the `swap` method by passing
273 /// [`Ordering::Relaxed`] as the `order`. For example, [`AtomicBool::swap`].
274 pub fn atomic_xchg_relaxed<T: Copy>(dst: *mut T, src: T) -> T;
276 /// Adds to the current value, returning the previous value.
278 /// The stabilized version of this intrinsic is available on the
279 /// [`atomic`] types via the `fetch_add` method by passing
280 /// [`Ordering::SeqCst`] as the `order`. For example, [`AtomicIsize::fetch_add`].
281 pub fn atomic_xadd<T: Copy>(dst: *mut T, src: T) -> T;
282 /// Adds to the current value, returning the previous value.
284 /// The stabilized version of this intrinsic is available on the
285 /// [`atomic`] types via the `fetch_add` method by passing
286 /// [`Ordering::Acquire`] as the `order`. For example, [`AtomicIsize::fetch_add`].
287 pub fn atomic_xadd_acq<T: Copy>(dst: *mut T, src: T) -> T;
288 /// Adds to the current value, returning the previous value.
290 /// The stabilized version of this intrinsic is available on the
291 /// [`atomic`] types via the `fetch_add` method by passing
292 /// [`Ordering::Release`] as the `order`. For example, [`AtomicIsize::fetch_add`].
293 pub fn atomic_xadd_rel<T: Copy>(dst: *mut T, src: T) -> T;
294 /// Adds to the current value, returning the previous value.
296 /// The stabilized version of this intrinsic is available on the
297 /// [`atomic`] types via the `fetch_add` method by passing
298 /// [`Ordering::AcqRel`] as the `order`. For example, [`AtomicIsize::fetch_add`].
299 pub fn atomic_xadd_acqrel<T: Copy>(dst: *mut T, src: T) -> T;
300 /// Adds to the current value, returning the previous value.
302 /// The stabilized version of this intrinsic is available on the
303 /// [`atomic`] types via the `fetch_add` method by passing
304 /// [`Ordering::Relaxed`] as the `order`. For example, [`AtomicIsize::fetch_add`].
305 pub fn atomic_xadd_relaxed<T: Copy>(dst: *mut T, src: T) -> T;
307 /// Subtract from the current value, returning the previous value.
309 /// The stabilized version of this intrinsic is available on the
310 /// [`atomic`] types via the `fetch_sub` method by passing
311 /// [`Ordering::SeqCst`] as the `order`. For example, [`AtomicIsize::fetch_sub`].
312 pub fn atomic_xsub<T: Copy>(dst: *mut T, src: T) -> T;
313 /// Subtract from the current value, returning the previous value.
315 /// The stabilized version of this intrinsic is available on the
316 /// [`atomic`] types via the `fetch_sub` method by passing
317 /// [`Ordering::Acquire`] as the `order`. For example, [`AtomicIsize::fetch_sub`].
318 pub fn atomic_xsub_acq<T: Copy>(dst: *mut T, src: T) -> T;
319 /// Subtract from the current value, returning the previous value.
321 /// The stabilized version of this intrinsic is available on the
322 /// [`atomic`] types via the `fetch_sub` method by passing
323 /// [`Ordering::Release`] as the `order`. For example, [`AtomicIsize::fetch_sub`].
324 pub fn atomic_xsub_rel<T: Copy>(dst: *mut T, src: T) -> T;
325 /// Subtract from the current value, returning the previous value.
327 /// The stabilized version of this intrinsic is available on the
328 /// [`atomic`] types via the `fetch_sub` method by passing
329 /// [`Ordering::AcqRel`] as the `order`. For example, [`AtomicIsize::fetch_sub`].
330 pub fn atomic_xsub_acqrel<T: Copy>(dst: *mut T, src: T) -> T;
331 /// Subtract from the current value, returning the previous value.
333 /// The stabilized version of this intrinsic is available on the
334 /// [`atomic`] types via the `fetch_sub` method by passing
335 /// [`Ordering::Relaxed`] as the `order`. For example, [`AtomicIsize::fetch_sub`].
336 pub fn atomic_xsub_relaxed<T: Copy>(dst: *mut T, src: T) -> T;
338 /// Bitwise and with the current value, returning the previous value.
340 /// The stabilized version of this intrinsic is available on the
341 /// [`atomic`] types via the `fetch_and` method by passing
342 /// [`Ordering::SeqCst`] as the `order`. For example, [`AtomicBool::fetch_and`].
343 pub fn atomic_and<T: Copy>(dst: *mut T, src: T) -> T;
344 /// Bitwise and with the current value, returning the previous value.
346 /// The stabilized version of this intrinsic is available on the
347 /// [`atomic`] types via the `fetch_and` method by passing
348 /// [`Ordering::Acquire`] as the `order`. For example, [`AtomicBool::fetch_and`].
349 pub fn atomic_and_acq<T: Copy>(dst: *mut T, src: T) -> T;
350 /// Bitwise and with the current value, returning the previous value.
352 /// The stabilized version of this intrinsic is available on the
353 /// [`atomic`] types via the `fetch_and` method by passing
354 /// [`Ordering::Release`] as the `order`. For example, [`AtomicBool::fetch_and`].
355 pub fn atomic_and_rel<T: Copy>(dst: *mut T, src: T) -> T;
356 /// Bitwise and with the current value, returning the previous value.
358 /// The stabilized version of this intrinsic is available on the
359 /// [`atomic`] types via the `fetch_and` method by passing
360 /// [`Ordering::AcqRel`] as the `order`. For example, [`AtomicBool::fetch_and`].
361 pub fn atomic_and_acqrel<T: Copy>(dst: *mut T, src: T) -> T;
362 /// Bitwise and with the current value, returning the previous value.
364 /// The stabilized version of this intrinsic is available on the
365 /// [`atomic`] types via the `fetch_and` method by passing
366 /// [`Ordering::Relaxed`] as the `order`. For example, [`AtomicBool::fetch_and`].
367 pub fn atomic_and_relaxed<T: Copy>(dst: *mut T, src: T) -> T;
369 /// Bitwise nand with the current value, returning the previous value.
371 /// The stabilized version of this intrinsic is available on the
372 /// [`AtomicBool`] type via the `fetch_nand` method by passing
373 /// [`Ordering::SeqCst`] as the `order`. For example, [`AtomicBool::fetch_nand`].
374 pub fn atomic_nand<T: Copy>(dst: *mut T, src: T) -> T;
375 /// Bitwise nand with the current value, returning the previous value.
377 /// The stabilized version of this intrinsic is available on the
378 /// [`AtomicBool`] type via the `fetch_nand` method by passing
379 /// [`Ordering::Acquire`] as the `order`. For example, [`AtomicBool::fetch_nand`].
380 pub fn atomic_nand_acq<T: Copy>(dst: *mut T, src: T) -> T;
381 /// Bitwise nand with the current value, returning the previous value.
383 /// The stabilized version of this intrinsic is available on the
384 /// [`AtomicBool`] type via the `fetch_nand` method by passing
385 /// [`Ordering::Release`] as the `order`. For example, [`AtomicBool::fetch_nand`].
386 pub fn atomic_nand_rel<T: Copy>(dst: *mut T, src: T) -> T;
387 /// Bitwise nand with the current value, returning the previous value.
389 /// The stabilized version of this intrinsic is available on the
390 /// [`AtomicBool`] type via the `fetch_nand` method by passing
391 /// [`Ordering::AcqRel`] as the `order`. For example, [`AtomicBool::fetch_nand`].
392 pub fn atomic_nand_acqrel<T: Copy>(dst: *mut T, src: T) -> T;
393 /// Bitwise nand with the current value, returning the previous value.
395 /// The stabilized version of this intrinsic is available on the
396 /// [`AtomicBool`] type via the `fetch_nand` method by passing
397 /// [`Ordering::Relaxed`] as the `order`. For example, [`AtomicBool::fetch_nand`].
398 pub fn atomic_nand_relaxed<T: Copy>(dst: *mut T, src: T) -> T;
400 /// Bitwise or with the current value, returning the previous value.
402 /// The stabilized version of this intrinsic is available on the
403 /// [`atomic`] types via the `fetch_or` method by passing
404 /// [`Ordering::SeqCst`] as the `order`. For example, [`AtomicBool::fetch_or`].
405 pub fn atomic_or<T: Copy>(dst: *mut T, src: T) -> T;
406 /// Bitwise or with the current value, returning the previous value.
408 /// The stabilized version of this intrinsic is available on the
409 /// [`atomic`] types via the `fetch_or` method by passing
410 /// [`Ordering::Acquire`] as the `order`. For example, [`AtomicBool::fetch_or`].
411 pub fn atomic_or_acq<T: Copy>(dst: *mut T, src: T) -> T;
412 /// Bitwise or with the current value, returning the previous value.
414 /// The stabilized version of this intrinsic is available on the
415 /// [`atomic`] types via the `fetch_or` method by passing
416 /// [`Ordering::Release`] as the `order`. For example, [`AtomicBool::fetch_or`].
417 pub fn atomic_or_rel<T: Copy>(dst: *mut T, src: T) -> T;
418 /// Bitwise or with the current value, returning the previous value.
420 /// The stabilized version of this intrinsic is available on the
421 /// [`atomic`] types via the `fetch_or` method by passing
422 /// [`Ordering::AcqRel`] as the `order`. For example, [`AtomicBool::fetch_or`].
423 pub fn atomic_or_acqrel<T: Copy>(dst: *mut T, src: T) -> T;
424 /// Bitwise or with the current value, returning the previous value.
426 /// The stabilized version of this intrinsic is available on the
427 /// [`atomic`] types via the `fetch_or` method by passing
428 /// [`Ordering::Relaxed`] as the `order`. For example, [`AtomicBool::fetch_or`].
429 pub fn atomic_or_relaxed<T: Copy>(dst: *mut T, src: T) -> T;
431 /// Bitwise xor with the current value, returning the previous value.
433 /// The stabilized version of this intrinsic is available on the
434 /// [`atomic`] types via the `fetch_xor` method by passing
435 /// [`Ordering::SeqCst`] as the `order`. For example, [`AtomicBool::fetch_xor`].
436 pub fn atomic_xor<T: Copy>(dst: *mut T, src: T) -> T;
437 /// Bitwise xor with the current value, returning the previous value.
439 /// The stabilized version of this intrinsic is available on the
440 /// [`atomic`] types via the `fetch_xor` method by passing
441 /// [`Ordering::Acquire`] as the `order`. For example, [`AtomicBool::fetch_xor`].
442 pub fn atomic_xor_acq<T: Copy>(dst: *mut T, src: T) -> T;
443 /// Bitwise xor with the current value, returning the previous value.
445 /// The stabilized version of this intrinsic is available on the
446 /// [`atomic`] types via the `fetch_xor` method by passing
447 /// [`Ordering::Release`] as the `order`. For example, [`AtomicBool::fetch_xor`].
448 pub fn atomic_xor_rel<T: Copy>(dst: *mut T, src: T) -> T;
449 /// Bitwise xor with the current value, returning the previous value.
451 /// The stabilized version of this intrinsic is available on the
452 /// [`atomic`] types via the `fetch_xor` method by passing
453 /// [`Ordering::AcqRel`] as the `order`. For example, [`AtomicBool::fetch_xor`].
454 pub fn atomic_xor_acqrel<T: Copy>(dst: *mut T, src: T) -> T;
455 /// Bitwise xor with the current value, returning the previous value.
457 /// The stabilized version of this intrinsic is available on the
458 /// [`atomic`] types via the `fetch_xor` method by passing
459 /// [`Ordering::Relaxed`] as the `order`. For example, [`AtomicBool::fetch_xor`].
460 pub fn atomic_xor_relaxed<T: Copy>(dst: *mut T, src: T) -> T;
462 /// Maximum with the current value using a signed comparison.
464 /// The stabilized version of this intrinsic is available on the
465 /// [`atomic`] signed integer types via the `fetch_max` method by passing
466 /// [`Ordering::SeqCst`] as the `order`. For example, [`AtomicI32::fetch_max`].
467 pub fn atomic_max<T: Copy>(dst: *mut T, src: T) -> T;
468 /// Maximum with the current value using a signed comparison.
470 /// The stabilized version of this intrinsic is available on the
471 /// [`atomic`] signed integer types via the `fetch_max` method by passing
472 /// [`Ordering::Acquire`] as the `order`. For example, [`AtomicI32::fetch_max`].
473 pub fn atomic_max_acq<T: Copy>(dst: *mut T, src: T) -> T;
474 /// Maximum with the current value using a signed comparison.
476 /// The stabilized version of this intrinsic is available on the
477 /// [`atomic`] signed integer types via the `fetch_max` method by passing
478 /// [`Ordering::Release`] as the `order`. For example, [`AtomicI32::fetch_max`].
479 pub fn atomic_max_rel<T: Copy>(dst: *mut T, src: T) -> T;
480 /// Maximum with the current value using a signed comparison.
482 /// The stabilized version of this intrinsic is available on the
483 /// [`atomic`] signed integer types via the `fetch_max` method by passing
484 /// [`Ordering::AcqRel`] as the `order`. For example, [`AtomicI32::fetch_max`].
485 pub fn atomic_max_acqrel<T: Copy>(dst: *mut T, src: T) -> T;
486 /// Maximum with the current value.
488 /// The stabilized version of this intrinsic is available on the
489 /// [`atomic`] signed integer types via the `fetch_max` method by passing
490 /// [`Ordering::Relaxed`] as the `order`. For example, [`AtomicI32::fetch_max`].
491 pub fn atomic_max_relaxed<T: Copy>(dst: *mut T, src: T) -> T;
493 /// Minimum with the current value using a signed comparison.
495 /// The stabilized version of this intrinsic is available on the
496 /// [`atomic`] signed integer types via the `fetch_min` method by passing
497 /// [`Ordering::SeqCst`] as the `order`. For example, [`AtomicI32::fetch_min`].
498 pub fn atomic_min<T: Copy>(dst: *mut T, src: T) -> T;
499 /// Minimum with the current value using a signed comparison.
501 /// The stabilized version of this intrinsic is available on the
502 /// [`atomic`] signed integer types via the `fetch_min` method by passing
503 /// [`Ordering::Acquire`] as the `order`. For example, [`AtomicI32::fetch_min`].
504 pub fn atomic_min_acq<T: Copy>(dst: *mut T, src: T) -> T;
505 /// Minimum with the current value using a signed comparison.
507 /// The stabilized version of this intrinsic is available on the
508 /// [`atomic`] signed integer types via the `fetch_min` method by passing
509 /// [`Ordering::Release`] as the `order`. For example, [`AtomicI32::fetch_min`].
510 pub fn atomic_min_rel<T: Copy>(dst: *mut T, src: T) -> T;
511 /// Minimum with the current value using a signed comparison.
513 /// The stabilized version of this intrinsic is available on the
514 /// [`atomic`] signed integer types via the `fetch_min` method by passing
515 /// [`Ordering::AcqRel`] as the `order`. For example, [`AtomicI32::fetch_min`].
516 pub fn atomic_min_acqrel<T: Copy>(dst: *mut T, src: T) -> T;
517 /// Minimum with the current value using a signed comparison.
519 /// The stabilized version of this intrinsic is available on the
520 /// [`atomic`] signed integer types via the `fetch_min` method by passing
521 /// [`Ordering::Relaxed`] as the `order`. For example, [`AtomicI32::fetch_min`].
522 pub fn atomic_min_relaxed<T: Copy>(dst: *mut T, src: T) -> T;
524 /// Minimum with the current value using an unsigned comparison.
526 /// The stabilized version of this intrinsic is available on the
527 /// [`atomic`] unsigned integer types via the `fetch_min` method by passing
528 /// [`Ordering::SeqCst`] as the `order`. For example, [`AtomicU32::fetch_min`].
529 pub fn atomic_umin<T: Copy>(dst: *mut T, src: T) -> T;
530 /// Minimum with the current value using an unsigned comparison.
532 /// The stabilized version of this intrinsic is available on the
533 /// [`atomic`] unsigned integer types via the `fetch_min` method by passing
534 /// [`Ordering::Acquire`] as the `order`. For example, [`AtomicU32::fetch_min`].
535 pub fn atomic_umin_acq<T: Copy>(dst: *mut T, src: T) -> T;
536 /// Minimum with the current value using an unsigned comparison.
538 /// The stabilized version of this intrinsic is available on the
539 /// [`atomic`] unsigned integer types via the `fetch_min` method by passing
540 /// [`Ordering::Release`] as the `order`. For example, [`AtomicU32::fetch_min`].
541 pub fn atomic_umin_rel<T: Copy>(dst: *mut T, src: T) -> T;
542 /// Minimum with the current value using an unsigned comparison.
544 /// The stabilized version of this intrinsic is available on the
545 /// [`atomic`] unsigned integer types via the `fetch_min` method by passing
546 /// [`Ordering::AcqRel`] as the `order`. For example, [`AtomicU32::fetch_min`].
547 pub fn atomic_umin_acqrel<T: Copy>(dst: *mut T, src: T) -> T;
548 /// Minimum with the current value using an unsigned comparison.
550 /// The stabilized version of this intrinsic is available on the
551 /// [`atomic`] unsigned integer types via the `fetch_min` method by passing
552 /// [`Ordering::Relaxed`] as the `order`. For example, [`AtomicU32::fetch_min`].
553 pub fn atomic_umin_relaxed<T: Copy>(dst: *mut T, src: T) -> T;
555 /// Maximum with the current value using an unsigned comparison.
557 /// The stabilized version of this intrinsic is available on the
558 /// [`atomic`] unsigned integer types via the `fetch_max` method by passing
559 /// [`Ordering::SeqCst`] as the `order`. For example, [`AtomicU32::fetch_max`].
560 pub fn atomic_umax<T: Copy>(dst: *mut T, src: T) -> T;
561 /// Maximum with the current value using an unsigned comparison.
563 /// The stabilized version of this intrinsic is available on the
564 /// [`atomic`] unsigned integer types via the `fetch_max` method by passing
565 /// [`Ordering::Acquire`] as the `order`. For example, [`AtomicU32::fetch_max`].
566 pub fn atomic_umax_acq<T: Copy>(dst: *mut T, src: T) -> T;
567 /// Maximum with the current value using an unsigned comparison.
569 /// The stabilized version of this intrinsic is available on the
570 /// [`atomic`] unsigned integer types via the `fetch_max` method by passing
571 /// [`Ordering::Release`] as the `order`. For example, [`AtomicU32::fetch_max`].
572 pub fn atomic_umax_rel<T: Copy>(dst: *mut T, src: T) -> T;
573 /// Maximum with the current value using an unsigned comparison.
575 /// The stabilized version of this intrinsic is available on the
576 /// [`atomic`] unsigned integer types via the `fetch_max` method by passing
577 /// [`Ordering::AcqRel`] as the `order`. For example, [`AtomicU32::fetch_max`].
578 pub fn atomic_umax_acqrel<T: Copy>(dst: *mut T, src: T) -> T;
579 /// Maximum with the current value using an unsigned comparison.
581 /// The stabilized version of this intrinsic is available on the
582 /// [`atomic`] unsigned integer types via the `fetch_max` method by passing
583 /// [`Ordering::Relaxed`] as the `order`. For example, [`AtomicU32::fetch_max`].
584 pub fn atomic_umax_relaxed<T: Copy>(dst: *mut T, src: T) -> T;
586 /// The `prefetch` intrinsic is a hint to the code generator to insert a prefetch instruction
587 /// if supported; otherwise, it is a no-op.
588 /// Prefetches have no effect on the behavior of the program but can change its performance
591 /// The `locality` argument must be a constant integer and is a temporal locality specifier
592 /// ranging from (0) - no locality, to (3) - extremely local keep in cache.
594 /// This intrinsic does not have a stable counterpart.
595 pub fn prefetch_read_data<T>(data: *const T, locality: i32);
596 /// The `prefetch` intrinsic is a hint to the code generator to insert a prefetch instruction
597 /// if supported; otherwise, it is a no-op.
598 /// Prefetches have no effect on the behavior of the program but can change its performance
601 /// The `locality` argument must be a constant integer and is a temporal locality specifier
602 /// ranging from (0) - no locality, to (3) - extremely local keep in cache.
604 /// This intrinsic does not have a stable counterpart.
605 pub fn prefetch_write_data<T>(data: *const T, locality: i32);
606 /// The `prefetch` intrinsic is a hint to the code generator to insert a prefetch instruction
607 /// if supported; otherwise, it is a no-op.
608 /// Prefetches have no effect on the behavior of the program but can change its performance
611 /// The `locality` argument must be a constant integer and is a temporal locality specifier
612 /// ranging from (0) - no locality, to (3) - extremely local keep in cache.
614 /// This intrinsic does not have a stable counterpart.
615 pub fn prefetch_read_instruction<T>(data: *const T, locality: i32);
616 /// The `prefetch` intrinsic is a hint to the code generator to insert a prefetch instruction
617 /// if supported; otherwise, it is a no-op.
618 /// Prefetches have no effect on the behavior of the program but can change its performance
621 /// The `locality` argument must be a constant integer and is a temporal locality specifier
622 /// ranging from (0) - no locality, to (3) - extremely local keep in cache.
624 /// This intrinsic does not have a stable counterpart.
625 pub fn prefetch_write_instruction<T>(data: *const T, locality: i32);
628 extern "rust-intrinsic" {
631 /// The stabilized version of this intrinsic is available in
632 /// [`atomic::fence`] by passing [`Ordering::SeqCst`]
634 pub fn atomic_fence();
637 /// The stabilized version of this intrinsic is available in
638 /// [`atomic::fence`] by passing [`Ordering::Acquire`]
640 pub fn atomic_fence_acq();
643 /// The stabilized version of this intrinsic is available in
644 /// [`atomic::fence`] by passing [`Ordering::Release`]
646 pub fn atomic_fence_rel();
649 /// The stabilized version of this intrinsic is available in
650 /// [`atomic::fence`] by passing [`Ordering::AcqRel`]
652 pub fn atomic_fence_acqrel();
654 /// A compiler-only memory barrier.
656 /// Memory accesses will never be reordered across this barrier by the
657 /// compiler, but no instructions will be emitted for it. This is
658 /// appropriate for operations on the same thread that may be preempted,
659 /// such as when interacting with signal handlers.
661 /// The stabilized version of this intrinsic is available in
662 /// [`atomic::compiler_fence`] by passing [`Ordering::SeqCst`]
664 pub fn atomic_singlethreadfence();
665 /// A compiler-only memory barrier.
667 /// Memory accesses will never be reordered across this barrier by the
668 /// compiler, but no instructions will be emitted for it. This is
669 /// appropriate for operations on the same thread that may be preempted,
670 /// such as when interacting with signal handlers.
672 /// The stabilized version of this intrinsic is available in
673 /// [`atomic::compiler_fence`] by passing [`Ordering::Acquire`]
675 pub fn atomic_singlethreadfence_acq();
676 /// A compiler-only memory barrier.
678 /// Memory accesses will never be reordered across this barrier by the
679 /// compiler, but no instructions will be emitted for it. This is
680 /// appropriate for operations on the same thread that may be preempted,
681 /// such as when interacting with signal handlers.
683 /// The stabilized version of this intrinsic is available in
684 /// [`atomic::compiler_fence`] by passing [`Ordering::Release`]
686 pub fn atomic_singlethreadfence_rel();
687 /// A compiler-only memory barrier.
689 /// Memory accesses will never be reordered across this barrier by the
690 /// compiler, but no instructions will be emitted for it. This is
691 /// appropriate for operations on the same thread that may be preempted,
692 /// such as when interacting with signal handlers.
694 /// The stabilized version of this intrinsic is available in
695 /// [`atomic::compiler_fence`] by passing [`Ordering::AcqRel`]
697 pub fn atomic_singlethreadfence_acqrel();
699 /// Magic intrinsic that derives its meaning from attributes
700 /// attached to the function.
702 /// For example, dataflow uses this to inject static assertions so
703 /// that `rustc_peek(potentially_uninitialized)` would actually
704 /// double-check that dataflow did indeed compute that it is
705 /// uninitialized at that point in the control flow.
707 /// This intrinsic should not be used outside of the compiler.
708 pub fn rustc_peek<T>(_: T) -> T;
710 /// Aborts the execution of the process.
712 /// Note that, unlike most intrinsics, this is safe to call;
713 /// it does not require an `unsafe` block.
714 /// Therefore, implementations must not require the user to uphold
715 /// any safety invariants.
717 /// [`std::process::abort`](../../std/process/fn.abort.html) is to be preferred if possible,
718 /// as its behavior is more user-friendly and more stable.
720 /// The current implementation of `intrinsics::abort` is to invoke an invalid instruction,
721 /// on most platforms.
723 /// process will probably terminate with a signal like `SIGABRT`, `SIGILL`, `SIGTRAP`, `SIGSEGV` or
724 /// `SIGBUS`. The precise behaviour is not guaranteed and not stable.
727 /// Informs the optimizer that this point in the code is not reachable,
728 /// enabling further optimizations.
730 /// N.B., this is very different from the `unreachable!()` macro: Unlike the
731 /// macro, which panics when it is executed, it is *undefined behavior* to
732 /// reach code marked with this function.
734 /// The stabilized version of this intrinsic is [`core::hint::unreachable_unchecked`].
735 #[rustc_const_stable(feature = "const_unreachable_unchecked", since = "1.57.0")]
736 pub fn unreachable() -> !;
738 /// Informs the optimizer that a condition is always true.
739 /// If the condition is false, the behavior is undefined.
741 /// No code is generated for this intrinsic, but the optimizer will try
742 /// to preserve it (and its condition) between passes, which may interfere
743 /// with optimization of surrounding code and reduce performance. It should
744 /// not be used if the invariant can be discovered by the optimizer on its
745 /// own, or if it does not enable any significant optimizations.
747 /// This intrinsic does not have a stable counterpart.
748 #[rustc_const_unstable(feature = "const_assume", issue = "76972")]
749 pub fn assume(b: bool);
751 /// Hints to the compiler that branch condition is likely to be true.
752 /// Returns the value passed to it.
754 /// Any use other than with `if` statements will probably not have an effect.
756 /// Note that, unlike most intrinsics, this is safe to call;
757 /// it does not require an `unsafe` block.
758 /// Therefore, implementations must not require the user to uphold
759 /// any safety invariants.
761 /// This intrinsic does not have a stable counterpart.
762 #[rustc_const_unstable(feature = "const_likely", issue = "none")]
763 pub fn likely(b: bool) -> bool;
765 /// Hints to the compiler that branch condition is likely to be false.
766 /// Returns the value passed to it.
768 /// Any use other than with `if` statements will probably not have an effect.
770 /// Note that, unlike most intrinsics, this is safe to call;
771 /// it does not require an `unsafe` block.
772 /// Therefore, implementations must not require the user to uphold
773 /// any safety invariants.
775 /// This intrinsic does not have a stable counterpart.
776 #[rustc_const_unstable(feature = "const_likely", issue = "none")]
777 pub fn unlikely(b: bool) -> bool;
779 /// Executes a breakpoint trap, for inspection by a debugger.
781 /// This intrinsic does not have a stable counterpart.
784 /// The size of a type in bytes.
786 /// Note that, unlike most intrinsics, this is safe to call;
787 /// it does not require an `unsafe` block.
788 /// Therefore, implementations must not require the user to uphold
789 /// any safety invariants.
791 /// More specifically, this is the offset in bytes between successive
792 /// items of the same type, including alignment padding.
794 /// The stabilized version of this intrinsic is [`core::mem::size_of`].
795 #[rustc_const_stable(feature = "const_size_of", since = "1.40.0")]
796 pub fn size_of<T>() -> usize;
798 /// The minimum alignment of a type.
800 /// Note that, unlike most intrinsics, this is safe to call;
801 /// it does not require an `unsafe` block.
802 /// Therefore, implementations must not require the user to uphold
803 /// any safety invariants.
805 /// The stabilized version of this intrinsic is [`core::mem::align_of`].
806 #[rustc_const_stable(feature = "const_min_align_of", since = "1.40.0")]
807 pub fn min_align_of<T>() -> usize;
808 /// The preferred alignment of a type.
810 /// This intrinsic does not have a stable counterpart.
811 /// It's "tracking issue" is [#91971](https://github.com/rust-lang/rust/issues/91971).
812 #[rustc_const_unstable(feature = "const_pref_align_of", issue = "91971")]
813 pub fn pref_align_of<T>() -> usize;
815 /// The size of the referenced value in bytes.
817 /// The stabilized version of this intrinsic is [`mem::size_of_val`].
818 #[rustc_const_unstable(feature = "const_size_of_val", issue = "46571")]
819 pub fn size_of_val<T: ?Sized>(_: *const T) -> usize;
820 /// The required alignment of the referenced value.
822 /// The stabilized version of this intrinsic is [`core::mem::align_of_val`].
823 #[rustc_const_unstable(feature = "const_align_of_val", issue = "46571")]
824 pub fn min_align_of_val<T: ?Sized>(_: *const T) -> usize;
826 /// Gets a static string slice containing the name of a type.
828 /// Note that, unlike most intrinsics, this is safe to call;
829 /// it does not require an `unsafe` block.
830 /// Therefore, implementations must not require the user to uphold
831 /// any safety invariants.
833 /// The stabilized version of this intrinsic is [`core::any::type_name`].
834 #[rustc_const_unstable(feature = "const_type_name", issue = "63084")]
835 pub fn type_name<T: ?Sized>() -> &'static str;
837 /// Gets an identifier which is globally unique to the specified type. This
838 /// function will return the same value for a type regardless of whichever
839 /// crate it is invoked in.
841 /// Note that, unlike most intrinsics, this is safe to call;
842 /// it does not require an `unsafe` block.
843 /// Therefore, implementations must not require the user to uphold
844 /// any safety invariants.
846 /// The stabilized version of this intrinsic is [`core::any::TypeId::of`].
847 #[rustc_const_unstable(feature = "const_type_id", issue = "77125")]
848 pub fn type_id<T: ?Sized + 'static>() -> u64;
850 /// A guard for unsafe functions that cannot ever be executed if `T` is uninhabited:
851 /// This will statically either panic, or do nothing.
853 /// This intrinsic does not have a stable counterpart.
854 #[rustc_const_stable(feature = "const_assert_type", since = "1.59.0")]
855 pub fn assert_inhabited<T>();
857 /// A guard for unsafe functions that cannot ever be executed if `T` does not permit
858 /// zero-initialization: This will statically either panic, or do nothing.
860 /// This intrinsic does not have a stable counterpart.
861 #[rustc_const_unstable(feature = "const_assert_type2", issue = "none")]
862 pub fn assert_zero_valid<T>();
864 /// A guard for unsafe functions that cannot ever be executed if `T` has invalid
865 /// bit patterns: This will statically either panic, or do nothing.
867 /// This intrinsic does not have a stable counterpart.
868 #[rustc_const_unstable(feature = "const_assert_type2", issue = "none")]
869 pub fn assert_uninit_valid<T>();
871 /// Gets a reference to a static `Location` indicating where it was called.
873 /// Note that, unlike most intrinsics, this is safe to call;
874 /// it does not require an `unsafe` block.
875 /// Therefore, implementations must not require the user to uphold
876 /// any safety invariants.
878 /// Consider using [`core::panic::Location::caller`] instead.
879 #[rustc_const_unstable(feature = "const_caller_location", issue = "76156")]
880 pub fn caller_location() -> &'static crate::panic::Location<'static>;
882 /// Moves a value out of scope without running drop glue.
884 /// This exists solely for [`mem::forget_unsized`]; normal `forget` uses
885 /// `ManuallyDrop` instead.
887 /// Note that, unlike most intrinsics, this is safe to call;
888 /// it does not require an `unsafe` block.
889 /// Therefore, implementations must not require the user to uphold
890 /// any safety invariants.
891 #[rustc_const_unstable(feature = "const_intrinsic_forget", issue = "none")]
892 pub fn forget<T: ?Sized>(_: T);
894 /// Reinterprets the bits of a value of one type as another type.
896 /// Both types must have the same size. Neither the original, nor the result,
897 /// may be an [invalid value](../../nomicon/what-unsafe-does.html).
899 /// `transmute` is semantically equivalent to a bitwise move of one type
900 /// into another. It copies the bits from the source value into the
901 /// destination value, then forgets the original. It's equivalent to C's
902 /// `memcpy` under the hood, just like `transmute_copy`.
904 /// Because `transmute` is a by-value operation, alignment of the *transmuted values
905 /// themselves* is not a concern. As with any other function, the compiler already ensures
906 /// both `T` and `U` are properly aligned. However, when transmuting values that *point
907 /// elsewhere* (such as pointers, references, boxes…), the caller has to ensure proper
908 /// alignment of the pointed-to values.
910 /// `transmute` is **incredibly** unsafe. There are a vast number of ways to
911 /// cause [undefined behavior][ub] with this function. `transmute` should be
912 /// the absolute last resort.
914 /// Transmuting pointers to integers in a `const` context is [undefined behavior][ub].
915 /// Any attempt to use the resulting value for integer operations will abort const-evaluation.
917 /// The [nomicon](../../nomicon/transmutes.html) has additional
920 /// [ub]: ../../reference/behavior-considered-undefined.html
924 /// There are a few things that `transmute` is really useful for.
926 /// Turning a pointer into a function pointer. This is *not* portable to
927 /// machines where function pointers and data pointers have different sizes.
930 /// fn foo() -> i32 {
933 /// let pointer = foo as *const ();
934 /// let function = unsafe {
935 /// std::mem::transmute::<*const (), fn() -> i32>(pointer)
937 /// assert_eq!(function(), 0);
940 /// Extending a lifetime, or shortening an invariant lifetime. This is
941 /// advanced, very unsafe Rust!
944 /// struct R<'a>(&'a i32);
945 /// unsafe fn extend_lifetime<'b>(r: R<'b>) -> R<'static> {
946 /// std::mem::transmute::<R<'b>, R<'static>>(r)
949 /// unsafe fn shorten_invariant_lifetime<'b, 'c>(r: &'b mut R<'static>)
950 /// -> &'b mut R<'c> {
951 /// std::mem::transmute::<&'b mut R<'static>, &'b mut R<'c>>(r)
957 /// Don't despair: many uses of `transmute` can be achieved through other means.
958 /// Below are common applications of `transmute` which can be replaced with safer
961 /// Turning raw bytes (`&[u8]`) into `u32`, `f64`, etc.:
964 /// let raw_bytes = [0x78, 0x56, 0x34, 0x12];
966 /// let num = unsafe {
967 /// std::mem::transmute::<[u8; 4], u32>(raw_bytes)
970 /// // use `u32::from_ne_bytes` instead
971 /// let num = u32::from_ne_bytes(raw_bytes);
972 /// // or use `u32::from_le_bytes` or `u32::from_be_bytes` to specify the endianness
973 /// let num = u32::from_le_bytes(raw_bytes);
974 /// assert_eq!(num, 0x12345678);
975 /// let num = u32::from_be_bytes(raw_bytes);
976 /// assert_eq!(num, 0x78563412);
979 /// Turning a pointer into a `usize`:
983 /// let ptr_num_transmute = unsafe {
984 /// std::mem::transmute::<&i32, usize>(ptr)
987 /// // Use an `as` cast instead
988 /// let ptr_num_cast = ptr as *const i32 as usize;
991 /// Note that using `transmute` to turn a pointer to a `usize` is (as noted above) [undefined
992 /// behavior][ub] in `const` contexts. Also outside of consts, this operation might not behave
993 /// as expected -- this is touching on many unspecified aspects of the Rust memory model.
994 /// Depending on what the code is doing, the following alternatives are preferrable to
995 /// pointer-to-integer transmutation:
996 /// - If the code just wants to store data of arbitrary type in some buffer and needs to pick a
997 /// type for that buffer, it can use [`MaybeUninit`][mem::MaybeUninit].
998 /// - If the code actually wants to work on the address the pointer points to, it can use `as`
999 /// casts or [`ptr.addr()`][pointer::addr].
1001 /// Turning a `*mut T` into an `&mut T`:
1004 /// let ptr: *mut i32 = &mut 0;
1005 /// let ref_transmuted = unsafe {
1006 /// std::mem::transmute::<*mut i32, &mut i32>(ptr)
1009 /// // Use a reborrow instead
1010 /// let ref_casted = unsafe { &mut *ptr };
1013 /// Turning an `&mut T` into an `&mut U`:
1016 /// let ptr = &mut 0;
1017 /// let val_transmuted = unsafe {
1018 /// std::mem::transmute::<&mut i32, &mut u32>(ptr)
1021 /// // Now, put together `as` and reborrowing - note the chaining of `as`
1022 /// // `as` is not transitive
1023 /// let val_casts = unsafe { &mut *(ptr as *mut i32 as *mut u32) };
1026 /// Turning an `&str` into a `&[u8]`:
1029 /// // this is not a good way to do this.
1030 /// let slice = unsafe { std::mem::transmute::<&str, &[u8]>("Rust") };
1031 /// assert_eq!(slice, &[82, 117, 115, 116]);
1033 /// // You could use `str::as_bytes`
1034 /// let slice = "Rust".as_bytes();
1035 /// assert_eq!(slice, &[82, 117, 115, 116]);
1037 /// // Or, just use a byte string, if you have control over the string
1039 /// assert_eq!(b"Rust", &[82, 117, 115, 116]);
1042 /// Turning a `Vec<&T>` into a `Vec<Option<&T>>`.
1044 /// To transmute the inner type of the contents of a container, you must make sure to not
1045 /// violate any of the container's invariants. For `Vec`, this means that both the size
1046 /// *and alignment* of the inner types have to match. Other containers might rely on the
1047 /// size of the type, alignment, or even the `TypeId`, in which case transmuting wouldn't
1048 /// be possible at all without violating the container invariants.
1051 /// let store = [0, 1, 2, 3];
1052 /// let v_orig = store.iter().collect::<Vec<&i32>>();
1054 /// // clone the vector as we will reuse them later
1055 /// let v_clone = v_orig.clone();
1057 /// // Using transmute: this relies on the unspecified data layout of `Vec`, which is a
1058 /// // bad idea and could cause Undefined Behavior.
1059 /// // However, it is no-copy.
1060 /// let v_transmuted = unsafe {
1061 /// std::mem::transmute::<Vec<&i32>, Vec<Option<&i32>>>(v_clone)
1064 /// let v_clone = v_orig.clone();
1066 /// // This is the suggested, safe way.
1067 /// // It does copy the entire vector, though, into a new array.
1068 /// let v_collected = v_clone.into_iter()
1070 /// .collect::<Vec<Option<&i32>>>();
1072 /// let v_clone = v_orig.clone();
1074 /// // This is the proper no-copy, unsafe way of "transmuting" a `Vec`, without relying on the
1075 /// // data layout. Instead of literally calling `transmute`, we perform a pointer cast, but
1076 /// // in terms of converting the original inner type (`&i32`) to the new one (`Option<&i32>`),
1077 /// // this has all the same caveats. Besides the information provided above, also consult the
1078 /// // [`from_raw_parts`] documentation.
1079 /// let v_from_raw = unsafe {
1080 // FIXME Update this when vec_into_raw_parts is stabilized
1081 /// // Ensure the original vector is not dropped.
1082 /// let mut v_clone = std::mem::ManuallyDrop::new(v_clone);
1083 /// Vec::from_raw_parts(v_clone.as_mut_ptr() as *mut Option<&i32>,
1085 /// v_clone.capacity())
1089 /// [`from_raw_parts`]: ../../std/vec/struct.Vec.html#method.from_raw_parts
1091 /// Implementing `split_at_mut`:
1094 /// use std::{slice, mem};
1096 /// // There are multiple ways to do this, and there are multiple problems
1097 /// // with the following (transmute) way.
1098 /// fn split_at_mut_transmute<T>(slice: &mut [T], mid: usize)
1099 /// -> (&mut [T], &mut [T]) {
1100 /// let len = slice.len();
1101 /// assert!(mid <= len);
1103 /// let slice2 = mem::transmute::<&mut [T], &mut [T]>(slice);
1104 /// // first: transmute is not type safe; all it checks is that T and
1105 /// // U are of the same size. Second, right here, you have two
1106 /// // mutable references pointing to the same memory.
1107 /// (&mut slice[0..mid], &mut slice2[mid..len])
1111 /// // This gets rid of the type safety problems; `&mut *` will *only* give
1112 /// // you an `&mut T` from an `&mut T` or `*mut T`.
1113 /// fn split_at_mut_casts<T>(slice: &mut [T], mid: usize)
1114 /// -> (&mut [T], &mut [T]) {
1115 /// let len = slice.len();
1116 /// assert!(mid <= len);
1118 /// let slice2 = &mut *(slice as *mut [T]);
1119 /// // however, you still have two mutable references pointing to
1120 /// // the same memory.
1121 /// (&mut slice[0..mid], &mut slice2[mid..len])
1125 /// // This is how the standard library does it. This is the best method, if
1126 /// // you need to do something like this
1127 /// fn split_at_stdlib<T>(slice: &mut [T], mid: usize)
1128 /// -> (&mut [T], &mut [T]) {
1129 /// let len = slice.len();
1130 /// assert!(mid <= len);
1132 /// let ptr = slice.as_mut_ptr();
1133 /// // This now has three mutable references pointing at the same
1134 /// // memory. `slice`, the rvalue ret.0, and the rvalue ret.1.
1135 /// // `slice` is never used after `let ptr = ...`, and so one can
1136 /// // treat it as "dead", and therefore, you only have two real
1137 /// // mutable slices.
1138 /// (slice::from_raw_parts_mut(ptr, mid),
1139 /// slice::from_raw_parts_mut(ptr.add(mid), len - mid))
1143 #[stable(feature = "rust1", since = "1.0.0")]
1144 #[rustc_const_stable(feature = "const_transmute", since = "1.46.0")]
1145 #[rustc_diagnostic_item = "transmute"]
1146 pub fn transmute<T, U>(e: T) -> U;
1148 /// Returns `true` if the actual type given as `T` requires drop
1149 /// glue; returns `false` if the actual type provided for `T`
1150 /// implements `Copy`.
1152 /// If the actual type neither requires drop glue nor implements
1153 /// `Copy`, then the return value of this function is unspecified.
1155 /// Note that, unlike most intrinsics, this is safe to call;
1156 /// it does not require an `unsafe` block.
1157 /// Therefore, implementations must not require the user to uphold
1158 /// any safety invariants.
1160 /// The stabilized version of this intrinsic is [`mem::needs_drop`](crate::mem::needs_drop).
1161 #[rustc_const_stable(feature = "const_needs_drop", since = "1.40.0")]
1162 pub fn needs_drop<T>() -> bool;
1164 /// Calculates the offset from a pointer.
1166 /// This is implemented as an intrinsic to avoid converting to and from an
1167 /// integer, since the conversion would throw away aliasing information.
1171 /// Both the starting and resulting pointer must be either in bounds or one
1172 /// byte past the end of an allocated object. If either pointer is out of
1173 /// bounds or arithmetic overflow occurs then any further use of the
1174 /// returned value will result in undefined behavior.
1176 /// The stabilized version of this intrinsic is [`pointer::offset`].
1177 #[must_use = "returns a new pointer rather than modifying its argument"]
1178 #[rustc_const_stable(feature = "const_ptr_offset", since = "1.61.0")]
1179 pub fn offset<T>(dst: *const T, offset: isize) -> *const T;
1181 /// Calculates the offset from a pointer, potentially wrapping.
1183 /// This is implemented as an intrinsic to avoid converting to and from an
1184 /// integer, since the conversion inhibits certain optimizations.
1188 /// Unlike the `offset` intrinsic, this intrinsic does not restrict the
1189 /// resulting pointer to point into or one byte past the end of an allocated
1190 /// object, and it wraps with two's complement arithmetic. The resulting
1191 /// value is not necessarily valid to be used to actually access memory.
1193 /// The stabilized version of this intrinsic is [`pointer::wrapping_offset`].
1194 #[must_use = "returns a new pointer rather than modifying its argument"]
1195 #[rustc_const_stable(feature = "const_ptr_offset", since = "1.61.0")]
1196 pub fn arith_offset<T>(dst: *const T, offset: isize) -> *const T;
1198 /// Equivalent to the appropriate `llvm.memcpy.p0i8.0i8.*` intrinsic, with
1199 /// a size of `count` * `size_of::<T>()` and an alignment of
1200 /// `min_align_of::<T>()`
1202 /// The volatile parameter is set to `true`, so it will not be optimized out
1203 /// unless size is equal to zero.
1205 /// This intrinsic does not have a stable counterpart.
1206 pub fn volatile_copy_nonoverlapping_memory<T>(dst: *mut T, src: *const T, count: usize);
1207 /// Equivalent to the appropriate `llvm.memmove.p0i8.0i8.*` intrinsic, with
1208 /// a size of `count * size_of::<T>()` and an alignment of
1209 /// `min_align_of::<T>()`
1211 /// The volatile parameter is set to `true`, so it will not be optimized out
1212 /// unless size is equal to zero.
1214 /// This intrinsic does not have a stable counterpart.
1215 pub fn volatile_copy_memory<T>(dst: *mut T, src: *const T, count: usize);
1216 /// Equivalent to the appropriate `llvm.memset.p0i8.*` intrinsic, with a
1217 /// size of `count * size_of::<T>()` and an alignment of
1218 /// `min_align_of::<T>()`.
1220 /// The volatile parameter is set to `true`, so it will not be optimized out
1221 /// unless size is equal to zero.
1223 /// This intrinsic does not have a stable counterpart.
1224 pub fn volatile_set_memory<T>(dst: *mut T, val: u8, count: usize);
1226 /// Performs a volatile load from the `src` pointer.
1228 /// The stabilized version of this intrinsic is [`core::ptr::read_volatile`].
1229 pub fn volatile_load<T>(src: *const T) -> T;
1230 /// Performs a volatile store to the `dst` pointer.
1232 /// The stabilized version of this intrinsic is [`core::ptr::write_volatile`].
1233 pub fn volatile_store<T>(dst: *mut T, val: T);
1235 /// Performs a volatile load from the `src` pointer
1236 /// The pointer is not required to be aligned.
1238 /// This intrinsic does not have a stable counterpart.
1239 pub fn unaligned_volatile_load<T>(src: *const T) -> T;
1240 /// Performs a volatile store to the `dst` pointer.
1241 /// The pointer is not required to be aligned.
1243 /// This intrinsic does not have a stable counterpart.
1244 pub fn unaligned_volatile_store<T>(dst: *mut T, val: T);
1246 /// Returns the square root of an `f32`
1248 /// The stabilized version of this intrinsic is
1249 /// [`f32::sqrt`](../../std/primitive.f32.html#method.sqrt)
1250 pub fn sqrtf32(x: f32) -> f32;
1251 /// Returns the square root of an `f64`
1253 /// The stabilized version of this intrinsic is
1254 /// [`f64::sqrt`](../../std/primitive.f64.html#method.sqrt)
1255 pub fn sqrtf64(x: f64) -> f64;
1257 /// Raises an `f32` to an integer power.
1259 /// The stabilized version of this intrinsic is
1260 /// [`f32::powi`](../../std/primitive.f32.html#method.powi)
1261 pub fn powif32(a: f32, x: i32) -> f32;
1262 /// Raises an `f64` to an integer power.
1264 /// The stabilized version of this intrinsic is
1265 /// [`f64::powi`](../../std/primitive.f64.html#method.powi)
1266 pub fn powif64(a: f64, x: i32) -> f64;
1268 /// Returns the sine of an `f32`.
1270 /// The stabilized version of this intrinsic is
1271 /// [`f32::sin`](../../std/primitive.f32.html#method.sin)
1272 pub fn sinf32(x: f32) -> f32;
1273 /// Returns the sine of an `f64`.
1275 /// The stabilized version of this intrinsic is
1276 /// [`f64::sin`](../../std/primitive.f64.html#method.sin)
1277 pub fn sinf64(x: f64) -> f64;
1279 /// Returns the cosine of an `f32`.
1281 /// The stabilized version of this intrinsic is
1282 /// [`f32::cos`](../../std/primitive.f32.html#method.cos)
1283 pub fn cosf32(x: f32) -> f32;
1284 /// Returns the cosine of an `f64`.
1286 /// The stabilized version of this intrinsic is
1287 /// [`f64::cos`](../../std/primitive.f64.html#method.cos)
1288 pub fn cosf64(x: f64) -> f64;
1290 /// Raises an `f32` to an `f32` power.
1292 /// The stabilized version of this intrinsic is
1293 /// [`f32::powf`](../../std/primitive.f32.html#method.powf)
1294 pub fn powf32(a: f32, x: f32) -> f32;
1295 /// Raises an `f64` to an `f64` power.
1297 /// The stabilized version of this intrinsic is
1298 /// [`f64::powf`](../../std/primitive.f64.html#method.powf)
1299 pub fn powf64(a: f64, x: f64) -> f64;
1301 /// Returns the exponential of an `f32`.
1303 /// The stabilized version of this intrinsic is
1304 /// [`f32::exp`](../../std/primitive.f32.html#method.exp)
1305 pub fn expf32(x: f32) -> f32;
1306 /// Returns the exponential of an `f64`.
1308 /// The stabilized version of this intrinsic is
1309 /// [`f64::exp`](../../std/primitive.f64.html#method.exp)
1310 pub fn expf64(x: f64) -> f64;
1312 /// Returns 2 raised to the power of an `f32`.
1314 /// The stabilized version of this intrinsic is
1315 /// [`f32::exp2`](../../std/primitive.f32.html#method.exp2)
1316 pub fn exp2f32(x: f32) -> f32;
1317 /// Returns 2 raised to the power of an `f64`.
1319 /// The stabilized version of this intrinsic is
1320 /// [`f64::exp2`](../../std/primitive.f64.html#method.exp2)
1321 pub fn exp2f64(x: f64) -> f64;
1323 /// Returns the natural logarithm of an `f32`.
1325 /// The stabilized version of this intrinsic is
1326 /// [`f32::ln`](../../std/primitive.f32.html#method.ln)
1327 pub fn logf32(x: f32) -> f32;
1328 /// Returns the natural logarithm of an `f64`.
1330 /// The stabilized version of this intrinsic is
1331 /// [`f64::ln`](../../std/primitive.f64.html#method.ln)
1332 pub fn logf64(x: f64) -> f64;
1334 /// Returns the base 10 logarithm of an `f32`.
1336 /// The stabilized version of this intrinsic is
1337 /// [`f32::log10`](../../std/primitive.f32.html#method.log10)
1338 pub fn log10f32(x: f32) -> f32;
1339 /// Returns the base 10 logarithm of an `f64`.
1341 /// The stabilized version of this intrinsic is
1342 /// [`f64::log10`](../../std/primitive.f64.html#method.log10)
1343 pub fn log10f64(x: f64) -> f64;
1345 /// Returns the base 2 logarithm of an `f32`.
1347 /// The stabilized version of this intrinsic is
1348 /// [`f32::log2`](../../std/primitive.f32.html#method.log2)
1349 pub fn log2f32(x: f32) -> f32;
1350 /// Returns the base 2 logarithm of an `f64`.
1352 /// The stabilized version of this intrinsic is
1353 /// [`f64::log2`](../../std/primitive.f64.html#method.log2)
1354 pub fn log2f64(x: f64) -> f64;
1356 /// Returns `a * b + c` for `f32` values.
1358 /// The stabilized version of this intrinsic is
1359 /// [`f32::mul_add`](../../std/primitive.f32.html#method.mul_add)
1360 pub fn fmaf32(a: f32, b: f32, c: f32) -> f32;
1361 /// Returns `a * b + c` for `f64` values.
1363 /// The stabilized version of this intrinsic is
1364 /// [`f64::mul_add`](../../std/primitive.f64.html#method.mul_add)
1365 pub fn fmaf64(a: f64, b: f64, c: f64) -> f64;
1367 /// Returns the absolute value of an `f32`.
1369 /// The stabilized version of this intrinsic is
1370 /// [`f32::abs`](../../std/primitive.f32.html#method.abs)
1371 pub fn fabsf32(x: f32) -> f32;
1372 /// Returns the absolute value of an `f64`.
1374 /// The stabilized version of this intrinsic is
1375 /// [`f64::abs`](../../std/primitive.f64.html#method.abs)
1376 pub fn fabsf64(x: f64) -> f64;
1378 /// Returns the minimum of two `f32` values.
1380 /// Note that, unlike most intrinsics, this is safe to call;
1381 /// it does not require an `unsafe` block.
1382 /// Therefore, implementations must not require the user to uphold
1383 /// any safety invariants.
1385 /// The stabilized version of this intrinsic is
1387 pub fn minnumf32(x: f32, y: f32) -> f32;
1388 /// Returns the minimum of two `f64` values.
1390 /// Note that, unlike most intrinsics, this is safe to call;
1391 /// it does not require an `unsafe` block.
1392 /// Therefore, implementations must not require the user to uphold
1393 /// any safety invariants.
1395 /// The stabilized version of this intrinsic is
1397 pub fn minnumf64(x: f64, y: f64) -> f64;
1398 /// Returns the maximum of two `f32` values.
1400 /// Note that, unlike most intrinsics, this is safe to call;
1401 /// it does not require an `unsafe` block.
1402 /// Therefore, implementations must not require the user to uphold
1403 /// any safety invariants.
1405 /// The stabilized version of this intrinsic is
1407 pub fn maxnumf32(x: f32, y: f32) -> f32;
1408 /// Returns the maximum of two `f64` values.
1410 /// Note that, unlike most intrinsics, this is safe to call;
1411 /// it does not require an `unsafe` block.
1412 /// Therefore, implementations must not require the user to uphold
1413 /// any safety invariants.
1415 /// The stabilized version of this intrinsic is
1417 pub fn maxnumf64(x: f64, y: f64) -> f64;
1419 /// Copies the sign from `y` to `x` for `f32` values.
1421 /// The stabilized version of this intrinsic is
1422 /// [`f32::copysign`](../../std/primitive.f32.html#method.copysign)
1423 pub fn copysignf32(x: f32, y: f32) -> f32;
1424 /// Copies the sign from `y` to `x` for `f64` values.
1426 /// The stabilized version of this intrinsic is
1427 /// [`f64::copysign`](../../std/primitive.f64.html#method.copysign)
1428 pub fn copysignf64(x: f64, y: f64) -> f64;
1430 /// Returns the largest integer less than or equal to an `f32`.
1432 /// The stabilized version of this intrinsic is
1433 /// [`f32::floor`](../../std/primitive.f32.html#method.floor)
1434 pub fn floorf32(x: f32) -> f32;
1435 /// Returns the largest integer less than or equal to an `f64`.
1437 /// The stabilized version of this intrinsic is
1438 /// [`f64::floor`](../../std/primitive.f64.html#method.floor)
1439 pub fn floorf64(x: f64) -> f64;
1441 /// Returns the smallest integer greater than or equal to an `f32`.
1443 /// The stabilized version of this intrinsic is
1444 /// [`f32::ceil`](../../std/primitive.f32.html#method.ceil)
1445 pub fn ceilf32(x: f32) -> f32;
1446 /// Returns the smallest integer greater than or equal to an `f64`.
1448 /// The stabilized version of this intrinsic is
1449 /// [`f64::ceil`](../../std/primitive.f64.html#method.ceil)
1450 pub fn ceilf64(x: f64) -> f64;
1452 /// Returns the integer part of an `f32`.
1454 /// The stabilized version of this intrinsic is
1455 /// [`f32::trunc`](../../std/primitive.f32.html#method.trunc)
1456 pub fn truncf32(x: f32) -> f32;
1457 /// Returns the integer part of an `f64`.
1459 /// The stabilized version of this intrinsic is
1460 /// [`f64::trunc`](../../std/primitive.f64.html#method.trunc)
1461 pub fn truncf64(x: f64) -> f64;
1463 /// Returns the nearest integer to an `f32`. May raise an inexact floating-point exception
1464 /// if the argument is not an integer.
1465 pub fn rintf32(x: f32) -> f32;
1466 /// Returns the nearest integer to an `f64`. May raise an inexact floating-point exception
1467 /// if the argument is not an integer.
1468 pub fn rintf64(x: f64) -> f64;
1470 /// Returns the nearest integer to an `f32`.
1472 /// This intrinsic does not have a stable counterpart.
1473 pub fn nearbyintf32(x: f32) -> f32;
1474 /// Returns the nearest integer to an `f64`.
1476 /// This intrinsic does not have a stable counterpart.
1477 pub fn nearbyintf64(x: f64) -> f64;
1479 /// Returns the nearest integer to an `f32`. Rounds half-way cases away from zero.
1481 /// The stabilized version of this intrinsic is
1482 /// [`f32::round`](../../std/primitive.f32.html#method.round)
1483 pub fn roundf32(x: f32) -> f32;
1484 /// Returns the nearest integer to an `f64`. Rounds half-way cases away from zero.
1486 /// The stabilized version of this intrinsic is
1487 /// [`f64::round`](../../std/primitive.f64.html#method.round)
1488 pub fn roundf64(x: f64) -> f64;
1490 /// Float addition that allows optimizations based on algebraic rules.
1491 /// May assume inputs are finite.
1493 /// This intrinsic does not have a stable counterpart.
1494 pub fn fadd_fast<T: Copy>(a: T, b: T) -> T;
1496 /// Float subtraction that allows optimizations based on algebraic rules.
1497 /// May assume inputs are finite.
1499 /// This intrinsic does not have a stable counterpart.
1500 pub fn fsub_fast<T: Copy>(a: T, b: T) -> T;
1502 /// Float multiplication that allows optimizations based on algebraic rules.
1503 /// May assume inputs are finite.
1505 /// This intrinsic does not have a stable counterpart.
1506 pub fn fmul_fast<T: Copy>(a: T, b: T) -> T;
1508 /// Float division that allows optimizations based on algebraic rules.
1509 /// May assume inputs are finite.
1511 /// This intrinsic does not have a stable counterpart.
1512 pub fn fdiv_fast<T: Copy>(a: T, b: T) -> T;
1514 /// Float remainder that allows optimizations based on algebraic rules.
1515 /// May assume inputs are finite.
1517 /// This intrinsic does not have a stable counterpart.
1518 pub fn frem_fast<T: Copy>(a: T, b: T) -> T;
1520 /// Convert with LLVM’s fptoui/fptosi, which may return undef for values out of range
1521 /// (<https://github.com/rust-lang/rust/issues/10184>)
1523 /// Stabilized as [`f32::to_int_unchecked`] and [`f64::to_int_unchecked`].
1524 pub fn float_to_int_unchecked<Float: Copy, Int: Copy>(value: Float) -> Int;
1526 /// Returns the number of bits set in an integer type `T`
1528 /// Note that, unlike most intrinsics, this is safe to call;
1529 /// it does not require an `unsafe` block.
1530 /// Therefore, implementations must not require the user to uphold
1531 /// any safety invariants.
1533 /// The stabilized versions of this intrinsic are available on the integer
1534 /// primitives via the `count_ones` method. For example,
1535 /// [`u32::count_ones`]
1536 #[rustc_const_stable(feature = "const_ctpop", since = "1.40.0")]
1537 pub fn ctpop<T: Copy>(x: T) -> T;
1539 /// Returns the number of leading unset bits (zeroes) in an integer type `T`.
1541 /// Note that, unlike most intrinsics, this is safe to call;
1542 /// it does not require an `unsafe` block.
1543 /// Therefore, implementations must not require the user to uphold
1544 /// any safety invariants.
1546 /// The stabilized versions of this intrinsic are available on the integer
1547 /// primitives via the `leading_zeros` method. For example,
1548 /// [`u32::leading_zeros`]
1553 /// #![feature(core_intrinsics)]
1555 /// use std::intrinsics::ctlz;
1557 /// let x = 0b0001_1100_u8;
1558 /// let num_leading = ctlz(x);
1559 /// assert_eq!(num_leading, 3);
1562 /// An `x` with value `0` will return the bit width of `T`.
1565 /// #![feature(core_intrinsics)]
1567 /// use std::intrinsics::ctlz;
1570 /// let num_leading = ctlz(x);
1571 /// assert_eq!(num_leading, 16);
1573 #[rustc_const_stable(feature = "const_ctlz", since = "1.40.0")]
1574 pub fn ctlz<T: Copy>(x: T) -> T;
1576 /// Like `ctlz`, but extra-unsafe as it returns `undef` when
1577 /// given an `x` with value `0`.
1579 /// This intrinsic does not have a stable counterpart.
1584 /// #![feature(core_intrinsics)]
1586 /// use std::intrinsics::ctlz_nonzero;
1588 /// let x = 0b0001_1100_u8;
1589 /// let num_leading = unsafe { ctlz_nonzero(x) };
1590 /// assert_eq!(num_leading, 3);
1592 #[rustc_const_stable(feature = "constctlz", since = "1.50.0")]
1593 pub fn ctlz_nonzero<T: Copy>(x: T) -> T;
1595 /// Returns the number of trailing unset bits (zeroes) in an integer type `T`.
1597 /// Note that, unlike most intrinsics, this is safe to call;
1598 /// it does not require an `unsafe` block.
1599 /// Therefore, implementations must not require the user to uphold
1600 /// any safety invariants.
1602 /// The stabilized versions of this intrinsic are available on the integer
1603 /// primitives via the `trailing_zeros` method. For example,
1604 /// [`u32::trailing_zeros`]
1609 /// #![feature(core_intrinsics)]
1611 /// use std::intrinsics::cttz;
1613 /// let x = 0b0011_1000_u8;
1614 /// let num_trailing = cttz(x);
1615 /// assert_eq!(num_trailing, 3);
1618 /// An `x` with value `0` will return the bit width of `T`:
1621 /// #![feature(core_intrinsics)]
1623 /// use std::intrinsics::cttz;
1626 /// let num_trailing = cttz(x);
1627 /// assert_eq!(num_trailing, 16);
1629 #[rustc_const_stable(feature = "const_cttz", since = "1.40.0")]
1630 pub fn cttz<T: Copy>(x: T) -> T;
1632 /// Like `cttz`, but extra-unsafe as it returns `undef` when
1633 /// given an `x` with value `0`.
1635 /// This intrinsic does not have a stable counterpart.
1640 /// #![feature(core_intrinsics)]
1642 /// use std::intrinsics::cttz_nonzero;
1644 /// let x = 0b0011_1000_u8;
1645 /// let num_trailing = unsafe { cttz_nonzero(x) };
1646 /// assert_eq!(num_trailing, 3);
1648 #[rustc_const_stable(feature = "const_cttz_nonzero", since = "1.53.0")]
1649 pub fn cttz_nonzero<T: Copy>(x: T) -> T;
1651 /// Reverses the bytes in an integer type `T`.
1653 /// Note that, unlike most intrinsics, this is safe to call;
1654 /// it does not require an `unsafe` block.
1655 /// Therefore, implementations must not require the user to uphold
1656 /// any safety invariants.
1658 /// The stabilized versions of this intrinsic are available on the integer
1659 /// primitives via the `swap_bytes` method. For example,
1660 /// [`u32::swap_bytes`]
1661 #[rustc_const_stable(feature = "const_bswap", since = "1.40.0")]
1662 pub fn bswap<T: Copy>(x: T) -> T;
1664 /// Reverses the bits in an integer type `T`.
1666 /// Note that, unlike most intrinsics, this is safe to call;
1667 /// it does not require an `unsafe` block.
1668 /// Therefore, implementations must not require the user to uphold
1669 /// any safety invariants.
1671 /// The stabilized versions of this intrinsic are available on the integer
1672 /// primitives via the `reverse_bits` method. For example,
1673 /// [`u32::reverse_bits`]
1674 #[rustc_const_stable(feature = "const_bitreverse", since = "1.40.0")]
1675 pub fn bitreverse<T: Copy>(x: T) -> T;
1677 /// Performs checked integer addition.
1679 /// Note that, unlike most intrinsics, this is safe to call;
1680 /// it does not require an `unsafe` block.
1681 /// Therefore, implementations must not require the user to uphold
1682 /// any safety invariants.
1684 /// The stabilized versions of this intrinsic are available on the integer
1685 /// primitives via the `overflowing_add` method. For example,
1686 /// [`u32::overflowing_add`]
1687 #[rustc_const_stable(feature = "const_int_overflow", since = "1.40.0")]
1688 pub fn add_with_overflow<T: Copy>(x: T, y: T) -> (T, bool);
1690 /// Performs checked integer subtraction
1692 /// Note that, unlike most intrinsics, this is safe to call;
1693 /// it does not require an `unsafe` block.
1694 /// Therefore, implementations must not require the user to uphold
1695 /// any safety invariants.
1697 /// The stabilized versions of this intrinsic are available on the integer
1698 /// primitives via the `overflowing_sub` method. For example,
1699 /// [`u32::overflowing_sub`]
1700 #[rustc_const_stable(feature = "const_int_overflow", since = "1.40.0")]
1701 pub fn sub_with_overflow<T: Copy>(x: T, y: T) -> (T, bool);
1703 /// Performs checked integer multiplication
1705 /// Note that, unlike most intrinsics, this is safe to call;
1706 /// it does not require an `unsafe` block.
1707 /// Therefore, implementations must not require the user to uphold
1708 /// any safety invariants.
1710 /// The stabilized versions of this intrinsic are available on the integer
1711 /// primitives via the `overflowing_mul` method. For example,
1712 /// [`u32::overflowing_mul`]
1713 #[rustc_const_stable(feature = "const_int_overflow", since = "1.40.0")]
1714 pub fn mul_with_overflow<T: Copy>(x: T, y: T) -> (T, bool);
1716 /// Performs an exact division, resulting in undefined behavior where
1717 /// `x % y != 0` or `y == 0` or `x == T::MIN && y == -1`
1719 /// This intrinsic does not have a stable counterpart.
1720 pub fn exact_div<T: Copy>(x: T, y: T) -> T;
1722 /// Performs an unchecked division, resulting in undefined behavior
1723 /// where `y == 0` or `x == T::MIN && y == -1`
1725 /// Safe wrappers for this intrinsic are available on the integer
1726 /// primitives via the `checked_div` method. For example,
1727 /// [`u32::checked_div`]
1728 #[rustc_const_stable(feature = "const_int_unchecked_div", since = "1.52.0")]
1729 pub fn unchecked_div<T: Copy>(x: T, y: T) -> T;
1730 /// Returns the remainder of an unchecked division, resulting in
1731 /// undefined behavior when `y == 0` or `x == T::MIN && y == -1`
1733 /// Safe wrappers for this intrinsic are available on the integer
1734 /// primitives via the `checked_rem` method. For example,
1735 /// [`u32::checked_rem`]
1736 #[rustc_const_stable(feature = "const_int_unchecked_rem", since = "1.52.0")]
1737 pub fn unchecked_rem<T: Copy>(x: T, y: T) -> T;
1739 /// Performs an unchecked left shift, resulting in undefined behavior when
1740 /// `y < 0` or `y >= N`, where N is the width of T in bits.
1742 /// Safe wrappers for this intrinsic are available on the integer
1743 /// primitives via the `checked_shl` method. For example,
1744 /// [`u32::checked_shl`]
1745 #[rustc_const_stable(feature = "const_int_unchecked", since = "1.40.0")]
1746 pub fn unchecked_shl<T: Copy>(x: T, y: T) -> T;
1747 /// Performs an unchecked right shift, resulting in undefined behavior when
1748 /// `y < 0` or `y >= N`, where N is the width of T in bits.
1750 /// Safe wrappers for this intrinsic are available on the integer
1751 /// primitives via the `checked_shr` method. For example,
1752 /// [`u32::checked_shr`]
1753 #[rustc_const_stable(feature = "const_int_unchecked", since = "1.40.0")]
1754 pub fn unchecked_shr<T: Copy>(x: T, y: T) -> T;
1756 /// Returns the result of an unchecked addition, resulting in
1757 /// undefined behavior when `x + y > T::MAX` or `x + y < T::MIN`.
1759 /// This intrinsic does not have a stable counterpart.
1760 #[rustc_const_unstable(feature = "const_int_unchecked_arith", issue = "none")]
1761 pub fn unchecked_add<T: Copy>(x: T, y: T) -> T;
1763 /// Returns the result of an unchecked subtraction, resulting in
1764 /// undefined behavior when `x - y > T::MAX` or `x - y < T::MIN`.
1766 /// This intrinsic does not have a stable counterpart.
1767 #[rustc_const_unstable(feature = "const_int_unchecked_arith", issue = "none")]
1768 pub fn unchecked_sub<T: Copy>(x: T, y: T) -> T;
1770 /// Returns the result of an unchecked multiplication, resulting in
1771 /// undefined behavior when `x * y > T::MAX` or `x * y < T::MIN`.
1773 /// This intrinsic does not have a stable counterpart.
1774 #[rustc_const_unstable(feature = "const_int_unchecked_arith", issue = "none")]
1775 pub fn unchecked_mul<T: Copy>(x: T, y: T) -> T;
1777 /// Performs rotate left.
1779 /// Note that, unlike most intrinsics, this is safe to call;
1780 /// it does not require an `unsafe` block.
1781 /// Therefore, implementations must not require the user to uphold
1782 /// any safety invariants.
1784 /// The stabilized versions of this intrinsic are available on the integer
1785 /// primitives via the `rotate_left` method. For example,
1786 /// [`u32::rotate_left`]
1787 #[rustc_const_stable(feature = "const_int_rotate", since = "1.40.0")]
1788 pub fn rotate_left<T: Copy>(x: T, y: T) -> T;
1790 /// Performs rotate right.
1792 /// Note that, unlike most intrinsics, this is safe to call;
1793 /// it does not require an `unsafe` block.
1794 /// Therefore, implementations must not require the user to uphold
1795 /// any safety invariants.
1797 /// The stabilized versions of this intrinsic are available on the integer
1798 /// primitives via the `rotate_right` method. For example,
1799 /// [`u32::rotate_right`]
1800 #[rustc_const_stable(feature = "const_int_rotate", since = "1.40.0")]
1801 pub fn rotate_right<T: Copy>(x: T, y: T) -> T;
1803 /// Returns (a + b) mod 2<sup>N</sup>, where N is the width of T in bits.
1805 /// Note that, unlike most intrinsics, this is safe to call;
1806 /// it does not require an `unsafe` block.
1807 /// Therefore, implementations must not require the user to uphold
1808 /// any safety invariants.
1810 /// The stabilized versions of this intrinsic are available on the integer
1811 /// primitives via the `wrapping_add` method. For example,
1812 /// [`u32::wrapping_add`]
1813 #[rustc_const_stable(feature = "const_int_wrapping", since = "1.40.0")]
1814 pub fn wrapping_add<T: Copy>(a: T, b: T) -> T;
1815 /// Returns (a - b) mod 2<sup>N</sup>, where N is the width of T in bits.
1817 /// Note that, unlike most intrinsics, this is safe to call;
1818 /// it does not require an `unsafe` block.
1819 /// Therefore, implementations must not require the user to uphold
1820 /// any safety invariants.
1822 /// The stabilized versions of this intrinsic are available on the integer
1823 /// primitives via the `wrapping_sub` method. For example,
1824 /// [`u32::wrapping_sub`]
1825 #[rustc_const_stable(feature = "const_int_wrapping", since = "1.40.0")]
1826 pub fn wrapping_sub<T: Copy>(a: T, b: T) -> T;
1827 /// Returns (a * b) mod 2<sup>N</sup>, where N is the width of T in bits.
1829 /// Note that, unlike most intrinsics, this is safe to call;
1830 /// it does not require an `unsafe` block.
1831 /// Therefore, implementations must not require the user to uphold
1832 /// any safety invariants.
1834 /// The stabilized versions of this intrinsic are available on the integer
1835 /// primitives via the `wrapping_mul` method. For example,
1836 /// [`u32::wrapping_mul`]
1837 #[rustc_const_stable(feature = "const_int_wrapping", since = "1.40.0")]
1838 pub fn wrapping_mul<T: Copy>(a: T, b: T) -> T;
1840 /// Computes `a + b`, saturating at numeric bounds.
1842 /// Note that, unlike most intrinsics, this is safe to call;
1843 /// it does not require an `unsafe` block.
1844 /// Therefore, implementations must not require the user to uphold
1845 /// any safety invariants.
1847 /// The stabilized versions of this intrinsic are available on the integer
1848 /// primitives via the `saturating_add` method. For example,
1849 /// [`u32::saturating_add`]
1850 #[rustc_const_stable(feature = "const_int_saturating", since = "1.40.0")]
1851 pub fn saturating_add<T: Copy>(a: T, b: T) -> T;
1852 /// Computes `a - b`, saturating at numeric bounds.
1854 /// Note that, unlike most intrinsics, this is safe to call;
1855 /// it does not require an `unsafe` block.
1856 /// Therefore, implementations must not require the user to uphold
1857 /// any safety invariants.
1859 /// The stabilized versions of this intrinsic are available on the integer
1860 /// primitives via the `saturating_sub` method. For example,
1861 /// [`u32::saturating_sub`]
1862 #[rustc_const_stable(feature = "const_int_saturating", since = "1.40.0")]
1863 pub fn saturating_sub<T: Copy>(a: T, b: T) -> T;
1865 /// Returns the value of the discriminant for the variant in 'v';
1866 /// if `T` has no discriminant, returns `0`.
1868 /// Note that, unlike most intrinsics, this is safe to call;
1869 /// it does not require an `unsafe` block.
1870 /// Therefore, implementations must not require the user to uphold
1871 /// any safety invariants.
1873 /// The stabilized version of this intrinsic is [`core::mem::discriminant`].
1874 #[rustc_const_unstable(feature = "const_discriminant", issue = "69821")]
1875 pub fn discriminant_value<T>(v: &T) -> <T as DiscriminantKind>::Discriminant;
1877 /// Returns the number of variants of the type `T` cast to a `usize`;
1878 /// if `T` has no variants, returns `0`. Uninhabited variants will be counted.
1880 /// Note that, unlike most intrinsics, this is safe to call;
1881 /// it does not require an `unsafe` block.
1882 /// Therefore, implementations must not require the user to uphold
1883 /// any safety invariants.
1885 /// The to-be-stabilized version of this intrinsic is [`mem::variant_count`].
1886 #[rustc_const_unstable(feature = "variant_count", issue = "73662")]
1887 pub fn variant_count<T>() -> usize;
1889 /// Rust's "try catch" construct which invokes the function pointer `try_fn`
1890 /// with the data pointer `data`.
1892 /// The third argument is a function called if a panic occurs. This function
1893 /// takes the data pointer and a pointer to the target-specific exception
1894 /// object that was caught. For more information see the compiler's
1895 /// source as well as std's catch implementation.
1896 pub fn r#try(try_fn: fn(*mut u8), data: *mut u8, catch_fn: fn(*mut u8, *mut u8)) -> i32;
1898 /// Emits a `!nontemporal` store according to LLVM (see their docs).
1899 /// Probably will never become stable.
1900 pub fn nontemporal_store<T>(ptr: *mut T, val: T);
1902 /// See documentation of `<*const T>::offset_from` for details.
1903 #[rustc_const_unstable(feature = "const_ptr_offset_from", issue = "92980")]
1904 pub fn ptr_offset_from<T>(ptr: *const T, base: *const T) -> isize;
1906 /// See documentation of `<*const T>::sub_ptr` for details.
1907 #[rustc_const_unstable(feature = "const_ptr_offset_from", issue = "92980")]
1908 #[cfg(not(bootstrap))]
1909 pub fn ptr_offset_from_unsigned<T>(ptr: *const T, base: *const T) -> usize;
1911 /// See documentation of `<*const T>::guaranteed_eq` for details.
1913 /// Note that, unlike most intrinsics, this is safe to call;
1914 /// it does not require an `unsafe` block.
1915 /// Therefore, implementations must not require the user to uphold
1916 /// any safety invariants.
1917 #[rustc_const_unstable(feature = "const_raw_ptr_comparison", issue = "53020")]
1918 pub fn ptr_guaranteed_eq<T>(ptr: *const T, other: *const T) -> bool;
1920 /// See documentation of `<*const T>::guaranteed_ne` for details.
1922 /// Note that, unlike most intrinsics, this is safe to call;
1923 /// it does not require an `unsafe` block.
1924 /// Therefore, implementations must not require the user to uphold
1925 /// any safety invariants.
1926 #[rustc_const_unstable(feature = "const_raw_ptr_comparison", issue = "53020")]
1927 pub fn ptr_guaranteed_ne<T>(ptr: *const T, other: *const T) -> bool;
1929 /// Allocates a block of memory at compile time.
1930 /// At runtime, just returns a null pointer.
1934 /// - The `align` argument must be a power of two.
1935 /// - At compile time, a compile error occurs if this constraint is violated.
1936 /// - At runtime, it is not checked.
1937 #[rustc_const_unstable(feature = "const_heap", issue = "79597")]
1938 pub fn const_allocate(size: usize, align: usize) -> *mut u8;
1940 /// Deallocates a memory which allocated by `intrinsics::const_allocate` at compile time.
1941 /// At runtime, does nothing.
1945 /// - The `align` argument must be a power of two.
1946 /// - At compile time, a compile error occurs if this constraint is violated.
1947 /// - At runtime, it is not checked.
1948 /// - If the `ptr` is created in an another const, this intrinsic doesn't deallocate it.
1949 /// - If the `ptr` is pointing to a local variable, this intrinsic doesn't deallocate it.
1950 #[rustc_const_unstable(feature = "const_heap", issue = "79597")]
1951 pub fn const_deallocate(ptr: *mut u8, size: usize, align: usize);
1953 /// Determines whether the raw bytes of the two values are equal.
1955 /// This is particularly handy for arrays, since it allows things like just
1956 /// comparing `i96`s instead of forcing `alloca`s for `[6 x i16]`.
1958 /// Above some backend-decided threshold this will emit calls to `memcmp`,
1959 /// like slice equality does, instead of causing massive code size.
1963 /// It's UB to call this if any of the *bytes* in `*a` or `*b` are uninitialized.
1964 /// Note that this is a stricter criterion than just the *values* being
1965 /// fully-initialized: if `T` has padding, it's UB to call this intrinsic.
1967 /// (The implementation is allowed to branch on the results of comparisons,
1968 /// which is UB if any of their inputs are `undef`.)
1969 #[rustc_const_unstable(feature = "const_intrinsic_raw_eq", issue = "none")]
1970 pub fn raw_eq<T>(a: &T, b: &T) -> bool;
1972 /// See documentation of [`std::hint::black_box`] for details.
1974 /// [`std::hint::black_box`]: crate::hint::black_box
1975 #[rustc_const_unstable(feature = "const_black_box", issue = "none")]
1976 pub fn black_box<T>(dummy: T) -> T;
1979 // Some functions are defined here because they accidentally got made
1980 // available in this module on stable. See <https://github.com/rust-lang/rust/issues/15702>.
1981 // (`transmute` also falls into this category, but it cannot be wrapped due to the
1982 // check that `T` and `U` have the same size.)
1984 /// Check that the preconditions of an unsafe function are followed, if debug_assertions are on,
1985 /// and only at runtime.
1989 /// Invoking this macro is only sound if the following code is already UB when the passed
1990 /// expression evaluates to false.
1992 /// This macro expands to a check at runtime if debug_assertions is set. It has no effect at
1993 /// compile time, but the semantics of the contained `const_eval_select` must be the same at
1994 /// runtime and at compile time. Thus if the expression evaluates to false, this macro produces
1995 /// different behavior at compile time and at runtime, and invoking it is incorrect.
1997 /// So in a sense it is UB if this macro is useful, but we expect callers of `unsafe fn` to make
1998 /// the occasional mistake, and this check should help them figure things out.
1999 #[allow_internal_unstable(const_eval_select)] // permit this to be called in stably-const fn
2000 macro_rules! assert_unsafe_precondition {
2002 if cfg!(debug_assertions) {
2003 // Use a closure so that we can capture arbitrary expressions from the invocation
2006 // abort instead of panicking to reduce impact on code size
2007 ::core::intrinsics::abort();
2010 const fn comptime() {}
2012 ::core::intrinsics::const_eval_select((), comptime, runtime);
2016 pub(crate) use assert_unsafe_precondition;
2018 /// Checks whether `ptr` is properly aligned with respect to
2019 /// `align_of::<T>()`.
2020 pub(crate) fn is_aligned_and_not_null<T>(ptr: *const T) -> bool {
2021 !ptr.is_null() && ptr.addr() % mem::align_of::<T>() == 0
2024 /// Checks whether the regions of memory starting at `src` and `dst` of size
2025 /// `count * size_of::<T>()` do *not* overlap.
2026 pub(crate) fn is_nonoverlapping<T>(src: *const T, dst: *const T, count: usize) -> bool {
2027 let src_usize = src.addr();
2028 let dst_usize = dst.addr();
2029 let size = mem::size_of::<T>().checked_mul(count).unwrap();
2030 let diff = if src_usize > dst_usize { src_usize - dst_usize } else { dst_usize - src_usize };
2031 // If the absolute distance between the ptrs is at least as big as the size of the buffer,
2032 // they do not overlap.
2036 /// Copies `count * size_of::<T>()` bytes from `src` to `dst`. The source
2037 /// and destination must *not* overlap.
2039 /// For regions of memory which might overlap, use [`copy`] instead.
2041 /// `copy_nonoverlapping` is semantically equivalent to C's [`memcpy`], but
2042 /// with the argument order swapped.
2044 /// [`memcpy`]: https://en.cppreference.com/w/c/string/byte/memcpy
2048 /// Behavior is undefined if any of the following conditions are violated:
2050 /// * `src` must be [valid] for reads of `count * size_of::<T>()` bytes.
2052 /// * `dst` must be [valid] for writes of `count * size_of::<T>()` bytes.
2054 /// * Both `src` and `dst` must be properly aligned.
2056 /// * The region of memory beginning at `src` with a size of `count *
2057 /// size_of::<T>()` bytes must *not* overlap with the region of memory
2058 /// beginning at `dst` with the same size.
2060 /// Like [`read`], `copy_nonoverlapping` creates a bitwise copy of `T`, regardless of
2061 /// whether `T` is [`Copy`]. If `T` is not [`Copy`], using *both* the values
2062 /// in the region beginning at `*src` and the region beginning at `*dst` can
2063 /// [violate memory safety][read-ownership].
2065 /// Note that even if the effectively copied size (`count * size_of::<T>()`) is
2066 /// `0`, the pointers must be non-null and properly aligned.
2068 /// [`read`]: crate::ptr::read
2069 /// [read-ownership]: crate::ptr::read#ownership-of-the-returned-value
2070 /// [valid]: crate::ptr#safety
2074 /// Manually implement [`Vec::append`]:
2079 /// /// Moves all the elements of `src` into `dst`, leaving `src` empty.
2080 /// fn append<T>(dst: &mut Vec<T>, src: &mut Vec<T>) {
2081 /// let src_len = src.len();
2082 /// let dst_len = dst.len();
2084 /// // Ensure that `dst` has enough capacity to hold all of `src`.
2085 /// dst.reserve(src_len);
2088 /// // The call to offset is always safe because `Vec` will never
2089 /// // allocate more than `isize::MAX` bytes.
2090 /// let dst_ptr = dst.as_mut_ptr().offset(dst_len as isize);
2091 /// let src_ptr = src.as_ptr();
2093 /// // Truncate `src` without dropping its contents. We do this first,
2094 /// // to avoid problems in case something further down panics.
2097 /// // The two regions cannot overlap because mutable references do
2098 /// // not alias, and two different vectors cannot own the same
2100 /// ptr::copy_nonoverlapping(src_ptr, dst_ptr, src_len);
2102 /// // Notify `dst` that it now holds the contents of `src`.
2103 /// dst.set_len(dst_len + src_len);
2107 /// let mut a = vec!['r'];
2108 /// let mut b = vec!['u', 's', 't'];
2110 /// append(&mut a, &mut b);
2112 /// assert_eq!(a, &['r', 'u', 's', 't']);
2113 /// assert!(b.is_empty());
2116 /// [`Vec::append`]: ../../std/vec/struct.Vec.html#method.append
2117 #[doc(alias = "memcpy")]
2118 #[stable(feature = "rust1", since = "1.0.0")]
2119 #[rustc_const_unstable(feature = "const_intrinsic_copy", issue = "80697")]
2121 pub const unsafe fn copy_nonoverlapping<T>(src: *const T, dst: *mut T, count: usize) {
2122 extern "rust-intrinsic" {
2123 #[rustc_const_unstable(feature = "const_intrinsic_copy", issue = "80697")]
2124 pub fn copy_nonoverlapping<T>(src: *const T, dst: *mut T, count: usize);
2127 // SAFETY: the safety contract for `copy_nonoverlapping` must be
2128 // upheld by the caller.
2130 assert_unsafe_precondition!(
2131 is_aligned_and_not_null(src)
2132 && is_aligned_and_not_null(dst)
2133 && is_nonoverlapping(src, dst, count)
2135 copy_nonoverlapping(src, dst, count)
2139 /// Copies `count * size_of::<T>()` bytes from `src` to `dst`. The source
2140 /// and destination may overlap.
2142 /// If the source and destination will *never* overlap,
2143 /// [`copy_nonoverlapping`] can be used instead.
2145 /// `copy` is semantically equivalent to C's [`memmove`], but with the argument
2146 /// order swapped. Copying takes place as if the bytes were copied from `src`
2147 /// to a temporary array and then copied from the array to `dst`.
2149 /// [`memmove`]: https://en.cppreference.com/w/c/string/byte/memmove
2153 /// Behavior is undefined if any of the following conditions are violated:
2155 /// * `src` must be [valid] for reads of `count * size_of::<T>()` bytes.
2157 /// * `dst` must be [valid] for writes of `count * size_of::<T>()` bytes.
2159 /// * Both `src` and `dst` must be properly aligned.
2161 /// Like [`read`], `copy` creates a bitwise copy of `T`, regardless of
2162 /// whether `T` is [`Copy`]. If `T` is not [`Copy`], using both the values
2163 /// in the region beginning at `*src` and the region beginning at `*dst` can
2164 /// [violate memory safety][read-ownership].
2166 /// Note that even if the effectively copied size (`count * size_of::<T>()`) is
2167 /// `0`, the pointers must be non-null and properly aligned.
2169 /// [`read`]: crate::ptr::read
2170 /// [read-ownership]: crate::ptr::read#ownership-of-the-returned-value
2171 /// [valid]: crate::ptr#safety
2175 /// Efficiently create a Rust vector from an unsafe buffer:
2182 /// /// * `ptr` must be correctly aligned for its type and non-zero.
2183 /// /// * `ptr` must be valid for reads of `elts` contiguous elements of type `T`.
2184 /// /// * Those elements must not be used after calling this function unless `T: Copy`.
2185 /// # #[allow(dead_code)]
2186 /// unsafe fn from_buf_raw<T>(ptr: *const T, elts: usize) -> Vec<T> {
2187 /// let mut dst = Vec::with_capacity(elts);
2189 /// // SAFETY: Our precondition ensures the source is aligned and valid,
2190 /// // and `Vec::with_capacity` ensures that we have usable space to write them.
2191 /// ptr::copy(ptr, dst.as_mut_ptr(), elts);
2193 /// // SAFETY: We created it with this much capacity earlier,
2194 /// // and the previous `copy` has initialized these elements.
2195 /// dst.set_len(elts);
2199 #[doc(alias = "memmove")]
2200 #[stable(feature = "rust1", since = "1.0.0")]
2201 #[rustc_const_unstable(feature = "const_intrinsic_copy", issue = "80697")]
2203 pub const unsafe fn copy<T>(src: *const T, dst: *mut T, count: usize) {
2204 extern "rust-intrinsic" {
2205 #[rustc_const_unstable(feature = "const_intrinsic_copy", issue = "80697")]
2206 fn copy<T>(src: *const T, dst: *mut T, count: usize);
2209 // SAFETY: the safety contract for `copy` must be upheld by the caller.
2211 assert_unsafe_precondition!(is_aligned_and_not_null(src) && is_aligned_and_not_null(dst));
2212 copy(src, dst, count)
2216 /// Sets `count * size_of::<T>()` bytes of memory starting at `dst` to
2219 /// `write_bytes` is similar to C's [`memset`], but sets `count *
2220 /// size_of::<T>()` bytes to `val`.
2222 /// [`memset`]: https://en.cppreference.com/w/c/string/byte/memset
2226 /// Behavior is undefined if any of the following conditions are violated:
2228 /// * `dst` must be [valid] for writes of `count * size_of::<T>()` bytes.
2230 /// * `dst` must be properly aligned.
2232 /// Additionally, the caller must ensure that writing `count *
2233 /// size_of::<T>()` bytes to the given region of memory results in a valid
2234 /// value of `T`. Using a region of memory typed as a `T` that contains an
2235 /// invalid value of `T` is undefined behavior.
2237 /// Note that even if the effectively copied size (`count * size_of::<T>()`) is
2238 /// `0`, the pointer must be non-null and properly aligned.
2240 /// [valid]: crate::ptr#safety
2249 /// let mut vec = vec![0u32; 4];
2251 /// let vec_ptr = vec.as_mut_ptr();
2252 /// ptr::write_bytes(vec_ptr, 0xfe, 2);
2254 /// assert_eq!(vec, [0xfefefefe, 0xfefefefe, 0, 0]);
2257 /// Creating an invalid value:
2262 /// let mut v = Box::new(0i32);
2265 /// // Leaks the previously held value by overwriting the `Box<T>` with
2266 /// // a null pointer.
2267 /// ptr::write_bytes(&mut v as *mut Box<i32>, 0, 1);
2270 /// // At this point, using or dropping `v` results in undefined behavior.
2271 /// // drop(v); // ERROR
2273 /// // Even leaking `v` "uses" it, and hence is undefined behavior.
2274 /// // mem::forget(v); // ERROR
2276 /// // In fact, `v` is invalid according to basic type layout invariants, so *any*
2277 /// // operation touching it is undefined behavior.
2278 /// // let v2 = v; // ERROR
2281 /// // Let us instead put in a valid value
2282 /// ptr::write(&mut v as *mut Box<i32>, Box::new(42i32));
2285 /// // Now the box is fine
2286 /// assert_eq!(*v, 42);
2288 #[stable(feature = "rust1", since = "1.0.0")]
2289 #[rustc_const_unstable(feature = "const_ptr_write", issue = "86302")]
2291 pub const unsafe fn write_bytes<T>(dst: *mut T, val: u8, count: usize) {
2292 extern "rust-intrinsic" {
2293 #[rustc_const_unstable(feature = "const_ptr_write", issue = "86302")]
2294 fn write_bytes<T>(dst: *mut T, val: u8, count: usize);
2297 // SAFETY: the safety contract for `write_bytes` must be upheld by the caller.
2299 assert_unsafe_precondition!(is_aligned_and_not_null(dst));
2300 write_bytes(dst, val, count)
2304 /// Selects which function to call depending on the context.
2306 /// If this function is evaluated at compile-time, then a call to this
2307 /// intrinsic will be replaced with a call to `called_in_const`. It gets
2308 /// replaced with a call to `called_at_rt` otherwise.
2310 /// # Type Requirements
2312 /// The two functions must be both function items. They cannot be function
2313 /// pointers or closures.
2315 /// `arg` will be the arguments that will be passed to either one of the
2316 /// two functions, therefore, both functions must accept the same type of
2317 /// arguments. Both functions must return RET.
2321 /// The two functions must behave observably equivalent. Safe code in other
2322 /// crates may assume that calling a `const fn` at compile-time and at run-time
2323 /// produces the same result. A function that produces a different result when
2324 /// evaluated at run-time, or has any other observable side-effects, is
2327 /// Here is an example of how this could cause a problem:
2329 /// #![feature(const_eval_select)]
2330 /// use std::hint::unreachable_unchecked;
2331 /// use std::intrinsics::const_eval_select;
2334 /// pub const fn inconsistent() -> i32 {
2335 /// fn runtime() -> i32 { 1 }
2336 /// const fn compiletime() -> i32 { 2 }
2339 // // ⚠ This code violates the required equivalence of `compiletime`
2340 /// // and `runtime`.
2341 /// const_eval_select((), compiletime, runtime)
2346 /// const X: i32 = inconsistent();
2347 /// let x = inconsistent();
2348 /// if x != X { unsafe { unreachable_unchecked(); }}
2351 /// This code causes Undefined Behavior when being run, since the
2352 /// `unreachable_unchecked` is actually being reached. The bug is in *crate A*,
2353 /// which violates the principle that a `const fn` must behave the same at
2354 /// compile-time and at run-time. The unsafe code in crate B is fine.
2356 feature = "const_eval_select",
2358 reason = "const_eval_select will never be stable"
2360 #[rustc_const_unstable(feature = "const_eval_select", issue = "none")]
2361 #[lang = "const_eval_select"]
2362 #[rustc_do_not_const_check]
2363 pub const unsafe fn const_eval_select<ARG, F, G, RET>(
2365 _called_in_const: F,
2369 F: ~const FnOnce<ARG, Output = RET>,
2370 G: FnOnce<ARG, Output = RET> + ~const Destruct,
2372 called_at_rt.call_once(arg)
2376 feature = "const_eval_select",
2378 reason = "const_eval_select will never be stable"
2380 #[rustc_const_unstable(feature = "const_eval_select", issue = "none")]
2381 #[lang = "const_eval_select_ct"]
2382 pub const unsafe fn const_eval_select_ct<ARG, F, G, RET>(
2388 F: ~const FnOnce<ARG, Output = RET>,
2389 G: FnOnce<ARG, Output = RET> + ~const Destruct,
2391 called_in_const.call_once(arg)
2394 /// Bootstrap polyfill
2396 pub const unsafe fn ptr_offset_from_unsigned<T>(ptr: *const T, base: *const T) -> usize {
2397 // SAFETY: we have stricter preconditions than `ptr_offset_from`, so can
2398 // call it, and its output has to be positive, so we can just cast.
2399 unsafe { ptr_offset_from(ptr, base) as _ }