1 //! Compiler intrinsics.
3 //! The corresponding definitions are in `librustc_codegen_llvm/intrinsic.rs`.
4 //! The corresponding const implementations are in `librustc_mir/interpret/intrinsics.rs`
8 //! Note: any changes to the constness of intrinsics should be discussed with the language team.
9 //! This includes changes in the stability of the constness.
11 //! In order to make an intrinsic usable at compile-time, one needs to copy the implementation
12 //! from https://github.com/rust-lang/miri/blob/master/src/shims/intrinsics.rs to
13 //! `librustc_mir/interpret/intrinsics.rs` and add a
14 //! `#[rustc_const_unstable(feature = "foo", issue = "01234")]` to the intrinsic.
16 //! If an intrinsic is supposed to be used from a `const fn` with a `rustc_const_stable` attribute,
17 //! the intrinsic's attribute must be `rustc_const_stable`, too. Such a change should not be done
18 //! without T-lang consulation, because it bakes a feature into the language that cannot be
19 //! replicated in user code without compiler support.
23 //! The volatile intrinsics provide operations intended to act on I/O
24 //! memory, which are guaranteed to not be reordered by the compiler
25 //! across other volatile intrinsics. See the LLVM documentation on
28 //! [volatile]: http://llvm.org/docs/LangRef.html#volatile-memory-accesses
32 //! The atomic intrinsics provide common atomic operations on machine
33 //! words, with multiple possible memory orderings. They obey the same
34 //! semantics as C++11. See the LLVM documentation on [[atomics]].
36 //! [atomics]: http://llvm.org/docs/Atomics.html
38 //! A quick refresher on memory ordering:
40 //! * Acquire - a barrier for acquiring a lock. Subsequent reads and writes
41 //! take place after the barrier.
42 //! * Release - a barrier for releasing a lock. Preceding reads and writes
43 //! take place before the barrier.
44 //! * Sequentially consistent - sequentially consistent operations are
45 //! guaranteed to happen in order. This is the standard mode for working
46 //! with atomic types and is equivalent to Java's `volatile`.
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)]
59 #[stable(feature = "drop_in_place", since = "1.8.0")]
61 reason = "no longer an intrinsic - use `ptr::drop_in_place` directly",
64 pub use crate::ptr::drop_in_place;
66 extern "rust-intrinsic" {
67 // N.B., these intrinsics take raw pointers because they mutate aliased
68 // memory, which is not valid for either `&` or `&mut`.
70 /// Stores a value if the current value is the same as the `old` value.
71 /// The stabilized version of this intrinsic is available on the
72 /// `std::sync::atomic` types via the `compare_exchange` method by passing
73 /// [`Ordering::SeqCst`](../../std/sync/atomic/enum.Ordering.html)
74 /// as both the `success` and `failure` parameters. For example,
75 /// [`AtomicBool::compare_exchange`][compare_exchange].
77 /// [compare_exchange]: ../../std/sync/atomic/struct.AtomicBool.html#method.compare_exchange
78 pub fn atomic_cxchg<T>(dst: *mut T, old: T, src: T) -> (T, bool);
79 /// Stores a value if the current value is the same as the `old` value.
80 /// The stabilized version of this intrinsic is available on the
81 /// `std::sync::atomic` types via the `compare_exchange` method by passing
82 /// [`Ordering::Acquire`](../../std/sync/atomic/enum.Ordering.html)
83 /// as both the `success` and `failure` parameters. For example,
84 /// [`AtomicBool::compare_exchange`][compare_exchange].
86 /// [compare_exchange]: ../../std/sync/atomic/struct.AtomicBool.html#method.compare_exchange
87 pub fn atomic_cxchg_acq<T>(dst: *mut T, old: T, src: T) -> (T, bool);
88 /// Stores a value if the current value is the same as the `old` value.
89 /// The stabilized version of this intrinsic is available on the
90 /// `std::sync::atomic` types via the `compare_exchange` method by passing
91 /// [`Ordering::Release`](../../std/sync/atomic/enum.Ordering.html)
92 /// as the `success` and
93 /// [`Ordering::Relaxed`](../../std/sync/atomic/enum.Ordering.html)
94 /// as the `failure` parameters. For example,
95 /// [`AtomicBool::compare_exchange`][compare_exchange].
97 /// [compare_exchange]: ../../std/sync/atomic/struct.AtomicBool.html#method.compare_exchange
98 pub fn atomic_cxchg_rel<T>(dst: *mut T, old: T, src: T) -> (T, bool);
99 /// Stores a value if the current value is the same as the `old` value.
100 /// The stabilized version of this intrinsic is available on the
101 /// `std::sync::atomic` types via the `compare_exchange` method by passing
102 /// [`Ordering::AcqRel`](../../std/sync/atomic/enum.Ordering.html)
103 /// as the `success` and
104 /// [`Ordering::Acquire`](../../std/sync/atomic/enum.Ordering.html)
105 /// as the `failure` parameters. For example,
106 /// [`AtomicBool::compare_exchange`][compare_exchange].
108 /// [compare_exchange]: ../../std/sync/atomic/struct.AtomicBool.html#method.compare_exchange
109 pub fn atomic_cxchg_acqrel<T>(dst: *mut T, old: T, src: T) -> (T, bool);
110 /// Stores a value if the current value is the same as the `old` value.
111 /// The stabilized version of this intrinsic is available on the
112 /// `std::sync::atomic` types via the `compare_exchange` method by passing
113 /// [`Ordering::Relaxed`](../../std/sync/atomic/enum.Ordering.html)
114 /// as both the `success` and `failure` parameters. For example,
115 /// [`AtomicBool::compare_exchange`][compare_exchange].
117 /// [compare_exchange]: ../../std/sync/atomic/struct.AtomicBool.html#method.compare_exchange
118 pub fn atomic_cxchg_relaxed<T>(dst: *mut T, old: T, src: T) -> (T, bool);
119 /// Stores a value if the current value is the same as the `old` value.
120 /// The stabilized version of this intrinsic is available on the
121 /// `std::sync::atomic` types via the `compare_exchange` method by passing
122 /// [`Ordering::SeqCst`](../../std/sync/atomic/enum.Ordering.html)
123 /// as the `success` and
124 /// [`Ordering::Relaxed`](../../std/sync/atomic/enum.Ordering.html)
125 /// as the `failure` parameters. For example,
126 /// [`AtomicBool::compare_exchange`][compare_exchange].
128 /// [compare_exchange]: ../../std/sync/atomic/struct.AtomicBool.html#method.compare_exchange
129 pub fn atomic_cxchg_failrelaxed<T>(dst: *mut T, old: T, src: T) -> (T, bool);
130 /// Stores a value if the current value is the same as the `old` value.
131 /// The stabilized version of this intrinsic is available on the
132 /// `std::sync::atomic` types via the `compare_exchange` method by passing
133 /// [`Ordering::SeqCst`](../../std/sync/atomic/enum.Ordering.html)
134 /// as the `success` and
135 /// [`Ordering::Acquire`](../../std/sync/atomic/enum.Ordering.html)
136 /// as the `failure` parameters. For example,
137 /// [`AtomicBool::compare_exchange`][compare_exchange].
139 /// [compare_exchange]: ../../std/sync/atomic/struct.AtomicBool.html#method.compare_exchange
140 pub fn atomic_cxchg_failacq<T>(dst: *mut T, old: T, src: T) -> (T, bool);
141 /// Stores a value if the current value is the same as the `old` value.
142 /// The stabilized version of this intrinsic is available on the
143 /// `std::sync::atomic` types via the `compare_exchange` method by passing
144 /// [`Ordering::Acquire`](../../std/sync/atomic/enum.Ordering.html)
145 /// as the `success` and
146 /// [`Ordering::Relaxed`](../../std/sync/atomic/enum.Ordering.html)
147 /// as the `failure` parameters. For example,
148 /// [`AtomicBool::compare_exchange`][compare_exchange].
150 /// [compare_exchange]: ../../std/sync/atomic/struct.AtomicBool.html#method.compare_exchange
151 pub fn atomic_cxchg_acq_failrelaxed<T>(dst: *mut T, old: T, src: T) -> (T, bool);
152 /// Stores a value if the current value is the same as the `old` value.
153 /// The stabilized version of this intrinsic is available on the
154 /// `std::sync::atomic` types via the `compare_exchange` method by passing
155 /// [`Ordering::AcqRel`](../../std/sync/atomic/enum.Ordering.html)
156 /// as the `success` and
157 /// [`Ordering::Relaxed`](../../std/sync/atomic/enum.Ordering.html)
158 /// as the `failure` parameters. For example,
159 /// [`AtomicBool::compare_exchange`][compare_exchange].
161 /// [compare_exchange]: ../../std/sync/atomic/struct.AtomicBool.html#method.compare_exchange
162 pub fn atomic_cxchg_acqrel_failrelaxed<T>(dst: *mut T, old: T, src: T) -> (T, bool);
164 /// Stores a value if the current value is the same as the `old` value.
165 /// The stabilized version of this intrinsic is available on the
166 /// `std::sync::atomic` types via the `compare_exchange_weak` method by passing
167 /// [`Ordering::SeqCst`](../../std/sync/atomic/enum.Ordering.html)
168 /// as both the `success` and `failure` parameters. For example,
169 /// [`AtomicBool::compare_exchange_weak`][cew].
171 /// [cew]: ../../std/sync/atomic/struct.AtomicBool.html#method.compare_exchange_weak
172 pub fn atomic_cxchgweak<T>(dst: *mut T, old: T, src: T) -> (T, bool);
173 /// Stores a value if the current value is the same as the `old` value.
174 /// The stabilized version of this intrinsic is available on the
175 /// `std::sync::atomic` types via the `compare_exchange_weak` method by passing
176 /// [`Ordering::Acquire`](../../std/sync/atomic/enum.Ordering.html)
177 /// as both the `success` and `failure` parameters. For example,
178 /// [`AtomicBool::compare_exchange_weak`][cew].
180 /// [cew]: ../../std/sync/atomic/struct.AtomicBool.html#method.compare_exchange_weak
181 pub fn atomic_cxchgweak_acq<T>(dst: *mut T, old: T, src: T) -> (T, bool);
182 /// Stores a value if the current value is the same as the `old` value.
183 /// The stabilized version of this intrinsic is available on the
184 /// `std::sync::atomic` types via the `compare_exchange_weak` method by passing
185 /// [`Ordering::Release`](../../std/sync/atomic/enum.Ordering.html)
186 /// as the `success` and
187 /// [`Ordering::Relaxed`](../../std/sync/atomic/enum.Ordering.html)
188 /// as the `failure` parameters. For example,
189 /// [`AtomicBool::compare_exchange_weak`][cew].
191 /// [cew]: ../../std/sync/atomic/struct.AtomicBool.html#method.compare_exchange_weak
192 pub fn atomic_cxchgweak_rel<T>(dst: *mut T, old: T, src: T) -> (T, bool);
193 /// Stores a value if the current value is the same as the `old` value.
194 /// The stabilized version of this intrinsic is available on the
195 /// `std::sync::atomic` types via the `compare_exchange_weak` method by passing
196 /// [`Ordering::AcqRel`](../../std/sync/atomic/enum.Ordering.html)
197 /// as the `success` and
198 /// [`Ordering::Acquire`](../../std/sync/atomic/enum.Ordering.html)
199 /// as the `failure` parameters. For example,
200 /// [`AtomicBool::compare_exchange_weak`][cew].
202 /// [cew]: ../../std/sync/atomic/struct.AtomicBool.html#method.compare_exchange_weak
203 pub fn atomic_cxchgweak_acqrel<T>(dst: *mut T, old: T, src: T) -> (T, bool);
204 /// Stores a value if the current value is the same as the `old` value.
205 /// The stabilized version of this intrinsic is available on the
206 /// `std::sync::atomic` types via the `compare_exchange_weak` method by passing
207 /// [`Ordering::Relaxed`](../../std/sync/atomic/enum.Ordering.html)
208 /// as both the `success` and `failure` parameters. For example,
209 /// [`AtomicBool::compare_exchange_weak`][cew].
211 /// [cew]: ../../std/sync/atomic/struct.AtomicBool.html#method.compare_exchange_weak
212 pub fn atomic_cxchgweak_relaxed<T>(dst: *mut T, old: T, src: T) -> (T, bool);
213 /// Stores a value if the current value is the same as the `old` value.
214 /// The stabilized version of this intrinsic is available on the
215 /// `std::sync::atomic` types via the `compare_exchange_weak` method by passing
216 /// [`Ordering::SeqCst`](../../std/sync/atomic/enum.Ordering.html)
217 /// as the `success` and
218 /// [`Ordering::Relaxed`](../../std/sync/atomic/enum.Ordering.html)
219 /// as the `failure` parameters. For example,
220 /// [`AtomicBool::compare_exchange_weak`][cew].
222 /// [cew]: ../../std/sync/atomic/struct.AtomicBool.html#method.compare_exchange_weak
223 pub fn atomic_cxchgweak_failrelaxed<T>(dst: *mut T, old: T, src: T) -> (T, bool);
224 /// Stores a value if the current value is the same as the `old` value.
225 /// The stabilized version of this intrinsic is available on the
226 /// `std::sync::atomic` types via the `compare_exchange_weak` method by passing
227 /// [`Ordering::SeqCst`](../../std/sync/atomic/enum.Ordering.html)
228 /// as the `success` and
229 /// [`Ordering::Acquire`](../../std/sync/atomic/enum.Ordering.html)
230 /// as the `failure` parameters. For example,
231 /// [`AtomicBool::compare_exchange_weak`][cew].
233 /// [cew]: ../../std/sync/atomic/struct.AtomicBool.html#method.compare_exchange_weak
234 pub fn atomic_cxchgweak_failacq<T>(dst: *mut T, old: T, src: T) -> (T, bool);
235 /// Stores a value if the current value is the same as the `old` value.
236 /// The stabilized version of this intrinsic is available on the
237 /// `std::sync::atomic` types via the `compare_exchange_weak` method by passing
238 /// [`Ordering::Acquire`](../../std/sync/atomic/enum.Ordering.html)
239 /// as the `success` and
240 /// [`Ordering::Relaxed`](../../std/sync/atomic/enum.Ordering.html)
241 /// as the `failure` parameters. For example,
242 /// [`AtomicBool::compare_exchange_weak`][cew].
244 /// [cew]: ../../std/sync/atomic/struct.AtomicBool.html#method.compare_exchange_weak
245 pub fn atomic_cxchgweak_acq_failrelaxed<T>(dst: *mut T, old: T, src: T) -> (T, bool);
246 /// Stores a value if the current value is the same as the `old` value.
247 /// The stabilized version of this intrinsic is available on the
248 /// `std::sync::atomic` types via the `compare_exchange_weak` method by passing
249 /// [`Ordering::AcqRel`](../../std/sync/atomic/enum.Ordering.html)
250 /// as the `success` and
251 /// [`Ordering::Relaxed`](../../std/sync/atomic/enum.Ordering.html)
252 /// as the `failure` parameters. For example,
253 /// [`AtomicBool::compare_exchange_weak`][cew].
255 /// [cew]: ../../std/sync/atomic/struct.AtomicBool.html#method.compare_exchange_weak
256 pub fn atomic_cxchgweak_acqrel_failrelaxed<T>(dst: *mut T, old: T, src: T) -> (T, bool);
258 /// Loads the current value of the pointer.
259 /// The stabilized version of this intrinsic is available on the
260 /// `std::sync::atomic` types via the `load` method by passing
261 /// [`Ordering::SeqCst`](../../std/sync/atomic/enum.Ordering.html)
262 /// as the `order`. For example,
263 /// [`AtomicBool::load`](../../std/sync/atomic/struct.AtomicBool.html#method.load).
264 pub fn atomic_load<T>(src: *const T) -> T;
265 /// Loads the current value of the pointer.
266 /// The stabilized version of this intrinsic is available on the
267 /// `std::sync::atomic` types via the `load` method by passing
268 /// [`Ordering::Acquire`](../../std/sync/atomic/enum.Ordering.html)
269 /// as the `order`. For example,
270 /// [`AtomicBool::load`](../../std/sync/atomic/struct.AtomicBool.html#method.load).
271 pub fn atomic_load_acq<T>(src: *const T) -> T;
272 /// Loads the current value of the pointer.
273 /// The stabilized version of this intrinsic is available on the
274 /// `std::sync::atomic` types via the `load` method by passing
275 /// [`Ordering::Relaxed`](../../std/sync/atomic/enum.Ordering.html)
276 /// as the `order`. For example,
277 /// [`AtomicBool::load`](../../std/sync/atomic/struct.AtomicBool.html#method.load).
278 pub fn atomic_load_relaxed<T>(src: *const T) -> T;
279 pub fn atomic_load_unordered<T>(src: *const T) -> T;
281 /// Stores the value at the specified memory location.
282 /// The stabilized version of this intrinsic is available on the
283 /// `std::sync::atomic` types via the `store` method by passing
284 /// [`Ordering::SeqCst`](../../std/sync/atomic/enum.Ordering.html)
285 /// as the `order`. For example,
286 /// [`AtomicBool::store`](../../std/sync/atomic/struct.AtomicBool.html#method.store).
287 pub fn atomic_store<T>(dst: *mut T, val: T);
288 /// Stores the value at the specified memory location.
289 /// The stabilized version of this intrinsic is available on the
290 /// `std::sync::atomic` types via the `store` method by passing
291 /// [`Ordering::Release`](../../std/sync/atomic/enum.Ordering.html)
292 /// as the `order`. For example,
293 /// [`AtomicBool::store`](../../std/sync/atomic/struct.AtomicBool.html#method.store).
294 pub fn atomic_store_rel<T>(dst: *mut T, val: T);
295 /// Stores the value at the specified memory location.
296 /// The stabilized version of this intrinsic is available on the
297 /// `std::sync::atomic` types via the `store` method by passing
298 /// [`Ordering::Relaxed`](../../std/sync/atomic/enum.Ordering.html)
299 /// as the `order`. For example,
300 /// [`AtomicBool::store`](../../std/sync/atomic/struct.AtomicBool.html#method.store).
301 pub fn atomic_store_relaxed<T>(dst: *mut T, val: T);
302 pub fn atomic_store_unordered<T>(dst: *mut T, val: T);
304 /// Stores the value at the specified memory location, returning the old value.
305 /// The stabilized version of this intrinsic is available on the
306 /// `std::sync::atomic` types via the `swap` method by passing
307 /// [`Ordering::SeqCst`](../../std/sync/atomic/enum.Ordering.html)
308 /// as the `order`. For example,
309 /// [`AtomicBool::swap`](../../std/sync/atomic/struct.AtomicBool.html#method.swap).
310 pub fn atomic_xchg<T>(dst: *mut T, src: T) -> T;
311 /// Stores the value at the specified memory location, returning the old value.
312 /// The stabilized version of this intrinsic is available on the
313 /// `std::sync::atomic` types via the `swap` method by passing
314 /// [`Ordering::Acquire`](../../std/sync/atomic/enum.Ordering.html)
315 /// as the `order`. For example,
316 /// [`AtomicBool::swap`](../../std/sync/atomic/struct.AtomicBool.html#method.swap).
317 pub fn atomic_xchg_acq<T>(dst: *mut T, src: T) -> T;
318 /// Stores the value at the specified memory location, returning the old value.
319 /// The stabilized version of this intrinsic is available on the
320 /// `std::sync::atomic` types via the `swap` method by passing
321 /// [`Ordering::Release`](../../std/sync/atomic/enum.Ordering.html)
322 /// as the `order`. For example,
323 /// [`AtomicBool::swap`](../../std/sync/atomic/struct.AtomicBool.html#method.swap).
324 pub fn atomic_xchg_rel<T>(dst: *mut T, src: T) -> T;
325 /// Stores the value at the specified memory location, returning the old value.
326 /// The stabilized version of this intrinsic is available on the
327 /// `std::sync::atomic` types via the `swap` method by passing
328 /// [`Ordering::AcqRel`](../../std/sync/atomic/enum.Ordering.html)
329 /// as the `order`. For example,
330 /// [`AtomicBool::swap`](../../std/sync/atomic/struct.AtomicBool.html#method.swap).
331 pub fn atomic_xchg_acqrel<T>(dst: *mut T, src: T) -> T;
332 /// Stores the value at the specified memory location, returning the old value.
333 /// The stabilized version of this intrinsic is available on the
334 /// `std::sync::atomic` types via the `swap` method by passing
335 /// [`Ordering::Relaxed`](../../std/sync/atomic/enum.Ordering.html)
336 /// as the `order`. For example,
337 /// [`AtomicBool::swap`](../../std/sync/atomic/struct.AtomicBool.html#method.swap).
338 pub fn atomic_xchg_relaxed<T>(dst: *mut T, src: T) -> T;
340 /// Adds to the current value, returning the previous value.
341 /// The stabilized version of this intrinsic is available on the
342 /// `std::sync::atomic` types via the `fetch_add` method by passing
343 /// [`Ordering::SeqCst`](../../std/sync/atomic/enum.Ordering.html)
344 /// as the `order`. For example,
345 /// [`AtomicIsize::fetch_add`](../../std/sync/atomic/struct.AtomicIsize.html#method.fetch_add).
346 pub fn atomic_xadd<T>(dst: *mut T, src: T) -> T;
347 /// Adds to the current value, returning the previous value.
348 /// The stabilized version of this intrinsic is available on the
349 /// `std::sync::atomic` types via the `fetch_add` method by passing
350 /// [`Ordering::Acquire`](../../std/sync/atomic/enum.Ordering.html)
351 /// as the `order`. For example,
352 /// [`AtomicIsize::fetch_add`](../../std/sync/atomic/struct.AtomicIsize.html#method.fetch_add).
353 pub fn atomic_xadd_acq<T>(dst: *mut T, src: T) -> T;
354 /// Adds to the current value, returning the previous value.
355 /// The stabilized version of this intrinsic is available on the
356 /// `std::sync::atomic` types via the `fetch_add` method by passing
357 /// [`Ordering::Release`](../../std/sync/atomic/enum.Ordering.html)
358 /// as the `order`. For example,
359 /// [`AtomicIsize::fetch_add`](../../std/sync/atomic/struct.AtomicIsize.html#method.fetch_add).
360 pub fn atomic_xadd_rel<T>(dst: *mut T, src: T) -> T;
361 /// Adds to the current value, returning the previous value.
362 /// The stabilized version of this intrinsic is available on the
363 /// `std::sync::atomic` types via the `fetch_add` method by passing
364 /// [`Ordering::AcqRel`](../../std/sync/atomic/enum.Ordering.html)
365 /// as the `order`. For example,
366 /// [`AtomicIsize::fetch_add`](../../std/sync/atomic/struct.AtomicIsize.html#method.fetch_add).
367 pub fn atomic_xadd_acqrel<T>(dst: *mut T, src: T) -> T;
368 /// Adds to the current value, returning the previous value.
369 /// The stabilized version of this intrinsic is available on the
370 /// `std::sync::atomic` types via the `fetch_add` method by passing
371 /// [`Ordering::Relaxed`](../../std/sync/atomic/enum.Ordering.html)
372 /// as the `order`. For example,
373 /// [`AtomicIsize::fetch_add`](../../std/sync/atomic/struct.AtomicIsize.html#method.fetch_add).
374 pub fn atomic_xadd_relaxed<T>(dst: *mut T, src: T) -> T;
376 /// Subtract from the current value, returning the previous value.
377 /// The stabilized version of this intrinsic is available on the
378 /// `std::sync::atomic` types via the `fetch_sub` method by passing
379 /// [`Ordering::SeqCst`](../../std/sync/atomic/enum.Ordering.html)
380 /// as the `order`. For example,
381 /// [`AtomicIsize::fetch_sub`](../../std/sync/atomic/struct.AtomicIsize.html#method.fetch_sub).
382 pub fn atomic_xsub<T>(dst: *mut T, src: T) -> T;
383 /// Subtract from the current value, returning the previous value.
384 /// The stabilized version of this intrinsic is available on the
385 /// `std::sync::atomic` types via the `fetch_sub` method by passing
386 /// [`Ordering::Acquire`](../../std/sync/atomic/enum.Ordering.html)
387 /// as the `order`. For example,
388 /// [`AtomicIsize::fetch_sub`](../../std/sync/atomic/struct.AtomicIsize.html#method.fetch_sub).
389 pub fn atomic_xsub_acq<T>(dst: *mut T, src: T) -> T;
390 /// Subtract from the current value, returning the previous value.
391 /// The stabilized version of this intrinsic is available on the
392 /// `std::sync::atomic` types via the `fetch_sub` method by passing
393 /// [`Ordering::Release`](../../std/sync/atomic/enum.Ordering.html)
394 /// as the `order`. For example,
395 /// [`AtomicIsize::fetch_sub`](../../std/sync/atomic/struct.AtomicIsize.html#method.fetch_sub).
396 pub fn atomic_xsub_rel<T>(dst: *mut T, src: T) -> T;
397 /// Subtract from the current value, returning the previous value.
398 /// The stabilized version of this intrinsic is available on the
399 /// `std::sync::atomic` types via the `fetch_sub` method by passing
400 /// [`Ordering::AcqRel`](../../std/sync/atomic/enum.Ordering.html)
401 /// as the `order`. For example,
402 /// [`AtomicIsize::fetch_sub`](../../std/sync/atomic/struct.AtomicIsize.html#method.fetch_sub).
403 pub fn atomic_xsub_acqrel<T>(dst: *mut T, src: T) -> T;
404 /// Subtract from the current value, returning the previous value.
405 /// The stabilized version of this intrinsic is available on the
406 /// `std::sync::atomic` types via the `fetch_sub` method by passing
407 /// [`Ordering::Relaxed`](../../std/sync/atomic/enum.Ordering.html)
408 /// as the `order`. For example,
409 /// [`AtomicIsize::fetch_sub`](../../std/sync/atomic/struct.AtomicIsize.html#method.fetch_sub).
410 pub fn atomic_xsub_relaxed<T>(dst: *mut T, src: T) -> T;
412 /// Bitwise and with the current value, returning the previous value.
413 /// The stabilized version of this intrinsic is available on the
414 /// `std::sync::atomic` types via the `fetch_and` method by passing
415 /// [`Ordering::SeqCst`](../../std/sync/atomic/enum.Ordering.html)
416 /// as the `order`. For example,
417 /// [`AtomicBool::fetch_and`](../../std/sync/atomic/struct.AtomicBool.html#method.fetch_and).
418 pub fn atomic_and<T>(dst: *mut T, src: T) -> T;
419 /// Bitwise and with the current value, returning the previous value.
420 /// The stabilized version of this intrinsic is available on the
421 /// `std::sync::atomic` types via the `fetch_and` method by passing
422 /// [`Ordering::Acquire`](../../std/sync/atomic/enum.Ordering.html)
423 /// as the `order`. For example,
424 /// [`AtomicBool::fetch_and`](../../std/sync/atomic/struct.AtomicBool.html#method.fetch_and).
425 pub fn atomic_and_acq<T>(dst: *mut T, src: T) -> T;
426 /// Bitwise and with the current value, returning the previous value.
427 /// The stabilized version of this intrinsic is available on the
428 /// `std::sync::atomic` types via the `fetch_and` method by passing
429 /// [`Ordering::Release`](../../std/sync/atomic/enum.Ordering.html)
430 /// as the `order`. For example,
431 /// [`AtomicBool::fetch_and`](../../std/sync/atomic/struct.AtomicBool.html#method.fetch_and).
432 pub fn atomic_and_rel<T>(dst: *mut T, src: T) -> T;
433 /// Bitwise and with the current value, returning the previous value.
434 /// The stabilized version of this intrinsic is available on the
435 /// `std::sync::atomic` types via the `fetch_and` method by passing
436 /// [`Ordering::AcqRel`](../../std/sync/atomic/enum.Ordering.html)
437 /// as the `order`. For example,
438 /// [`AtomicBool::fetch_and`](../../std/sync/atomic/struct.AtomicBool.html#method.fetch_and).
439 pub fn atomic_and_acqrel<T>(dst: *mut T, src: T) -> T;
440 /// Bitwise and with the current value, returning the previous value.
441 /// The stabilized version of this intrinsic is available on the
442 /// `std::sync::atomic` types via the `fetch_and` method by passing
443 /// [`Ordering::Relaxed`](../../std/sync/atomic/enum.Ordering.html)
444 /// as the `order`. For example,
445 /// [`AtomicBool::fetch_and`](../../std/sync/atomic/struct.AtomicBool.html#method.fetch_and).
446 pub fn atomic_and_relaxed<T>(dst: *mut T, src: T) -> T;
448 /// Bitwise nand with the current value, returning the previous value.
449 /// The stabilized version of this intrinsic is available on the
450 /// `std::sync::atomic::AtomicBool` type via the `fetch_nand` method by passing
451 /// [`Ordering::SeqCst`](../../std/sync/atomic/enum.Ordering.html)
452 /// as the `order`. For example,
453 /// [`AtomicBool::fetch_nand`](../../std/sync/atomic/struct.AtomicBool.html#method.fetch_nand).
454 pub fn atomic_nand<T>(dst: *mut T, src: T) -> T;
455 /// Bitwise nand with the current value, returning the previous value.
456 /// The stabilized version of this intrinsic is available on the
457 /// `std::sync::atomic::AtomicBool` type via the `fetch_nand` method by passing
458 /// [`Ordering::Acquire`](../../std/sync/atomic/enum.Ordering.html)
459 /// as the `order`. For example,
460 /// [`AtomicBool::fetch_nand`](../../std/sync/atomic/struct.AtomicBool.html#method.fetch_nand).
461 pub fn atomic_nand_acq<T>(dst: *mut T, src: T) -> T;
462 /// Bitwise nand with the current value, returning the previous value.
463 /// The stabilized version of this intrinsic is available on the
464 /// `std::sync::atomic::AtomicBool` type via the `fetch_nand` method by passing
465 /// [`Ordering::Release`](../../std/sync/atomic/enum.Ordering.html)
466 /// as the `order`. For example,
467 /// [`AtomicBool::fetch_nand`](../../std/sync/atomic/struct.AtomicBool.html#method.fetch_nand).
468 pub fn atomic_nand_rel<T>(dst: *mut T, src: T) -> T;
469 /// Bitwise nand with the current value, returning the previous value.
470 /// The stabilized version of this intrinsic is available on the
471 /// `std::sync::atomic::AtomicBool` type via the `fetch_nand` method by passing
472 /// [`Ordering::AcqRel`](../../std/sync/atomic/enum.Ordering.html)
473 /// as the `order`. For example,
474 /// [`AtomicBool::fetch_nand`](../../std/sync/atomic/struct.AtomicBool.html#method.fetch_nand).
475 pub fn atomic_nand_acqrel<T>(dst: *mut T, src: T) -> T;
476 /// Bitwise nand with the current value, returning the previous value.
477 /// The stabilized version of this intrinsic is available on the
478 /// `std::sync::atomic::AtomicBool` type via the `fetch_nand` method by passing
479 /// [`Ordering::Relaxed`](../../std/sync/atomic/enum.Ordering.html)
480 /// as the `order`. For example,
481 /// [`AtomicBool::fetch_nand`](../../std/sync/atomic/struct.AtomicBool.html#method.fetch_nand).
482 pub fn atomic_nand_relaxed<T>(dst: *mut T, src: T) -> T;
484 /// Bitwise or with the current value, returning the previous value.
485 /// The stabilized version of this intrinsic is available on the
486 /// `std::sync::atomic` types via the `fetch_or` method by passing
487 /// [`Ordering::SeqCst`](../../std/sync/atomic/enum.Ordering.html)
488 /// as the `order`. For example,
489 /// [`AtomicBool::fetch_or`](../../std/sync/atomic/struct.AtomicBool.html#method.fetch_or).
490 pub fn atomic_or<T>(dst: *mut T, src: T) -> T;
491 /// Bitwise or with the current value, returning the previous value.
492 /// The stabilized version of this intrinsic is available on the
493 /// `std::sync::atomic` types via the `fetch_or` method by passing
494 /// [`Ordering::Acquire`](../../std/sync/atomic/enum.Ordering.html)
495 /// as the `order`. For example,
496 /// [`AtomicBool::fetch_or`](../../std/sync/atomic/struct.AtomicBool.html#method.fetch_or).
497 pub fn atomic_or_acq<T>(dst: *mut T, src: T) -> T;
498 /// Bitwise or with the current value, returning the previous value.
499 /// The stabilized version of this intrinsic is available on the
500 /// `std::sync::atomic` types via the `fetch_or` method by passing
501 /// [`Ordering::Release`](../../std/sync/atomic/enum.Ordering.html)
502 /// as the `order`. For example,
503 /// [`AtomicBool::fetch_or`](../../std/sync/atomic/struct.AtomicBool.html#method.fetch_or).
504 pub fn atomic_or_rel<T>(dst: *mut T, src: T) -> T;
505 /// Bitwise or with the current value, returning the previous value.
506 /// The stabilized version of this intrinsic is available on the
507 /// `std::sync::atomic` types via the `fetch_or` method by passing
508 /// [`Ordering::AcqRel`](../../std/sync/atomic/enum.Ordering.html)
509 /// as the `order`. For example,
510 /// [`AtomicBool::fetch_or`](../../std/sync/atomic/struct.AtomicBool.html#method.fetch_or).
511 pub fn atomic_or_acqrel<T>(dst: *mut T, src: T) -> T;
512 /// Bitwise or with the current value, returning the previous value.
513 /// The stabilized version of this intrinsic is available on the
514 /// `std::sync::atomic` types via the `fetch_or` method by passing
515 /// [`Ordering::Relaxed`](../../std/sync/atomic/enum.Ordering.html)
516 /// as the `order`. For example,
517 /// [`AtomicBool::fetch_or`](../../std/sync/atomic/struct.AtomicBool.html#method.fetch_or).
518 pub fn atomic_or_relaxed<T>(dst: *mut T, src: T) -> T;
520 /// Bitwise xor with the current value, returning the previous value.
521 /// The stabilized version of this intrinsic is available on the
522 /// `std::sync::atomic` types via the `fetch_xor` method by passing
523 /// [`Ordering::SeqCst`](../../std/sync/atomic/enum.Ordering.html)
524 /// as the `order`. For example,
525 /// [`AtomicBool::fetch_xor`](../../std/sync/atomic/struct.AtomicBool.html#method.fetch_xor).
526 pub fn atomic_xor<T>(dst: *mut T, src: T) -> T;
527 /// Bitwise xor with the current value, returning the previous value.
528 /// The stabilized version of this intrinsic is available on the
529 /// `std::sync::atomic` types via the `fetch_xor` method by passing
530 /// [`Ordering::Acquire`](../../std/sync/atomic/enum.Ordering.html)
531 /// as the `order`. For example,
532 /// [`AtomicBool::fetch_xor`](../../std/sync/atomic/struct.AtomicBool.html#method.fetch_xor).
533 pub fn atomic_xor_acq<T>(dst: *mut T, src: T) -> T;
534 /// Bitwise xor with the current value, returning the previous value.
535 /// The stabilized version of this intrinsic is available on the
536 /// `std::sync::atomic` types via the `fetch_xor` method by passing
537 /// [`Ordering::Release`](../../std/sync/atomic/enum.Ordering.html)
538 /// as the `order`. For example,
539 /// [`AtomicBool::fetch_xor`](../../std/sync/atomic/struct.AtomicBool.html#method.fetch_xor).
540 pub fn atomic_xor_rel<T>(dst: *mut T, src: T) -> T;
541 /// Bitwise xor with the current value, returning the previous value.
542 /// The stabilized version of this intrinsic is available on the
543 /// `std::sync::atomic` types via the `fetch_xor` method by passing
544 /// [`Ordering::AcqRel`](../../std/sync/atomic/enum.Ordering.html)
545 /// as the `order`. For example,
546 /// [`AtomicBool::fetch_xor`](../../std/sync/atomic/struct.AtomicBool.html#method.fetch_xor).
547 pub fn atomic_xor_acqrel<T>(dst: *mut T, src: T) -> T;
548 /// Bitwise xor with the current value, returning the previous value.
549 /// The stabilized version of this intrinsic is available on the
550 /// `std::sync::atomic` types via the `fetch_xor` method by passing
551 /// [`Ordering::Relaxed`](../../std/sync/atomic/enum.Ordering.html)
552 /// as the `order`. For example,
553 /// [`AtomicBool::fetch_xor`](../../std/sync/atomic/struct.AtomicBool.html#method.fetch_xor).
554 pub fn atomic_xor_relaxed<T>(dst: *mut T, src: T) -> T;
556 pub fn atomic_max<T>(dst: *mut T, src: T) -> T;
557 pub fn atomic_max_acq<T>(dst: *mut T, src: T) -> T;
558 pub fn atomic_max_rel<T>(dst: *mut T, src: T) -> T;
559 pub fn atomic_max_acqrel<T>(dst: *mut T, src: T) -> T;
560 pub fn atomic_max_relaxed<T>(dst: *mut T, src: T) -> T;
562 pub fn atomic_min<T>(dst: *mut T, src: T) -> T;
563 pub fn atomic_min_acq<T>(dst: *mut T, src: T) -> T;
564 pub fn atomic_min_rel<T>(dst: *mut T, src: T) -> T;
565 pub fn atomic_min_acqrel<T>(dst: *mut T, src: T) -> T;
566 pub fn atomic_min_relaxed<T>(dst: *mut T, src: T) -> T;
568 pub fn atomic_umin<T>(dst: *mut T, src: T) -> T;
569 pub fn atomic_umin_acq<T>(dst: *mut T, src: T) -> T;
570 pub fn atomic_umin_rel<T>(dst: *mut T, src: T) -> T;
571 pub fn atomic_umin_acqrel<T>(dst: *mut T, src: T) -> T;
572 pub fn atomic_umin_relaxed<T>(dst: *mut T, src: T) -> T;
574 pub fn atomic_umax<T>(dst: *mut T, src: T) -> T;
575 pub fn atomic_umax_acq<T>(dst: *mut T, src: T) -> T;
576 pub fn atomic_umax_rel<T>(dst: *mut T, src: T) -> T;
577 pub fn atomic_umax_acqrel<T>(dst: *mut T, src: T) -> T;
578 pub fn atomic_umax_relaxed<T>(dst: *mut T, src: T) -> T;
580 /// The `prefetch` intrinsic is a hint to the code generator to insert a prefetch instruction
581 /// if supported; otherwise, it is a no-op.
582 /// Prefetches have no effect on the behavior of the program but can change its performance
585 /// The `locality` argument must be a constant integer and is a temporal locality specifier
586 /// ranging from (0) - no locality, to (3) - extremely local keep in cache
587 pub fn prefetch_read_data<T>(data: *const T, locality: i32);
588 /// The `prefetch` intrinsic is a hint to the code generator to insert a prefetch instruction
589 /// if supported; otherwise, it is a no-op.
590 /// Prefetches have no effect on the behavior of the program but can change its performance
593 /// The `locality` argument must be a constant integer and is a temporal locality specifier
594 /// ranging from (0) - no locality, to (3) - extremely local keep in cache
595 pub fn prefetch_write_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
603 pub fn prefetch_read_instruction<T>(data: *const T, locality: i32);
604 /// The `prefetch` intrinsic is a hint to the code generator to insert a prefetch instruction
605 /// if supported; otherwise, it is a no-op.
606 /// Prefetches have no effect on the behavior of the program but can change its performance
609 /// The `locality` argument must be a constant integer and is a temporal locality specifier
610 /// ranging from (0) - no locality, to (3) - extremely local keep in cache
611 pub fn prefetch_write_instruction<T>(data: *const T, locality: i32);
614 extern "rust-intrinsic" {
616 pub fn atomic_fence();
617 pub fn atomic_fence_acq();
618 pub fn atomic_fence_rel();
619 pub fn atomic_fence_acqrel();
621 /// A compiler-only memory barrier.
623 /// Memory accesses will never be reordered across this barrier by the
624 /// compiler, but no instructions will be emitted for it. This is
625 /// appropriate for operations on the same thread that may be preempted,
626 /// such as when interacting with signal handlers.
627 pub fn atomic_singlethreadfence();
628 pub fn atomic_singlethreadfence_acq();
629 pub fn atomic_singlethreadfence_rel();
630 pub fn atomic_singlethreadfence_acqrel();
632 /// Magic intrinsic that derives its meaning from attributes
633 /// attached to the function.
635 /// For example, dataflow uses this to inject static assertions so
636 /// that `rustc_peek(potentially_uninitialized)` would actually
637 /// double-check that dataflow did indeed compute that it is
638 /// uninitialized at that point in the control flow.
639 pub fn rustc_peek<T>(_: T) -> T;
641 /// Aborts the execution of the process.
643 /// The stabilized version of this intrinsic is
644 /// [`std::process::abort`](../../std/process/fn.abort.html)
647 /// Tells LLVM that this point in the code is not reachable, enabling
648 /// further optimizations.
650 /// N.B., this is very different from the `unreachable!()` macro: Unlike the
651 /// macro, which panics when it is executed, it is *undefined behavior* to
652 /// reach code marked with this function.
654 /// The stabilized version of this intrinsic is
655 /// [`std::hint::unreachable_unchecked`](../../std/hint/fn.unreachable_unchecked.html).
656 pub fn unreachable() -> !;
658 /// Informs the optimizer that a condition is always true.
659 /// If the condition is false, the behavior is undefined.
661 /// No code is generated for this intrinsic, but the optimizer will try
662 /// to preserve it (and its condition) between passes, which may interfere
663 /// with optimization of surrounding code and reduce performance. It should
664 /// not be used if the invariant can be discovered by the optimizer on its
665 /// own, or if it does not enable any significant optimizations.
666 pub fn assume(b: bool);
668 /// Hints to the compiler that branch condition is likely to be true.
669 /// Returns the value passed to it.
671 /// Any use other than with `if` statements will probably not have an effect.
672 pub fn likely(b: bool) -> bool;
674 /// Hints to the compiler that branch condition is likely to be false.
675 /// Returns the value passed to it.
677 /// Any use other than with `if` statements will probably not have an effect.
678 pub fn unlikely(b: bool) -> bool;
680 /// Executes a breakpoint trap, for inspection by a debugger.
683 /// The size of a type in bytes.
685 /// More specifically, this is the offset in bytes between successive
686 /// items of the same type, including alignment padding.
688 /// The stabilized version of this intrinsic is
689 /// [`std::mem::size_of`](../../std/mem/fn.size_of.html).
690 #[rustc_const_stable(feature = "const_size_of", since = "1.40.0")]
691 pub fn size_of<T>() -> usize;
693 /// Moves a value to an uninitialized memory location.
695 /// Drop glue is not run on the destination.
696 pub fn move_val_init<T>(dst: *mut T, src: T);
698 #[rustc_const_stable(feature = "const_min_align_of", since = "1.40.0")]
699 pub fn min_align_of<T>() -> usize;
700 #[rustc_const_unstable(feature = "const_pref_align_of", issue = "none")]
701 pub fn pref_align_of<T>() -> usize;
703 /// The size of the referenced value in bytes.
705 /// The stabilized version of this intrinsic is
706 /// [`std::mem::size_of_val`](../../std/mem/fn.size_of_val.html).
707 pub fn size_of_val<T: ?Sized>(_: &T) -> usize;
708 pub fn min_align_of_val<T: ?Sized>(_: &T) -> usize;
710 /// Gets a static string slice containing the name of a type.
711 #[rustc_const_unstable(feature = "const_type_name", issue = "none")]
712 pub fn type_name<T: ?Sized>() -> &'static str;
714 /// Gets an identifier which is globally unique to the specified type. This
715 /// function will return the same value for a type regardless of whichever
716 /// crate it is invoked in.
717 #[rustc_const_unstable(feature = "const_type_id", issue = "none")]
718 pub fn type_id<T: ?Sized + 'static>() -> u64;
720 /// A guard for unsafe functions that cannot ever be executed if `T` is uninhabited:
721 /// This will statically either panic, or do nothing.
722 pub fn panic_if_uninhabited<T>();
724 /// Gets a reference to a static `Location` indicating where it was called.
725 #[rustc_const_unstable(feature = "const_caller_location", issue = "47809")]
726 pub fn caller_location() -> &'static crate::panic::Location<'static>;
728 /// Creates a value initialized to zero.
730 /// `init` is unsafe because it returns a zeroed-out datum,
731 /// which is unsafe unless `T` is `Copy`. Also, even if T is
732 /// `Copy`, an all-zero value may not correspond to any legitimate
733 /// state for the type in question.
735 feature = "core_intrinsics",
736 reason = "intrinsics are unlikely to ever be stabilized, instead \
737 they should be used through stabilized interfaces \
738 in the rest of the standard library",
741 #[rustc_deprecated(reason = "superseded by MaybeUninit, removal planned", since = "1.38.0")]
742 pub fn init<T>() -> T;
744 /// Creates an uninitialized value.
746 /// `uninit` is unsafe because there is no guarantee of what its
747 /// contents are. In particular its drop-flag may be set to any
748 /// state, which means it may claim either dropped or
749 /// undropped. In the general case one must use `ptr::write` to
750 /// initialize memory previous set to the result of `uninit`.
752 feature = "core_intrinsics",
753 reason = "intrinsics are unlikely to ever be stabilized, instead \
754 they should be used through stabilized interfaces \
755 in the rest of the standard library",
758 #[rustc_deprecated(reason = "superseded by MaybeUninit, removal planned", since = "1.38.0")]
759 pub fn uninit<T>() -> T;
761 /// Moves a value out of scope without running drop glue.
762 pub fn forget<T: ?Sized>(_: T);
764 /// Reinterprets the bits of a value of one type as another type.
766 /// Both types must have the same size. Neither the original, nor the result,
767 /// may be an [invalid value](../../nomicon/what-unsafe-does.html).
769 /// `transmute` is semantically equivalent to a bitwise move of one type
770 /// into another. It copies the bits from the source value into the
771 /// destination value, then forgets the original. It's equivalent to C's
772 /// `memcpy` under the hood, just like `transmute_copy`.
774 /// `transmute` is **incredibly** unsafe. There are a vast number of ways to
775 /// cause [undefined behavior][ub] with this function. `transmute` should be
776 /// the absolute last resort.
778 /// The [nomicon](../../nomicon/transmutes.html) has additional
781 /// [ub]: ../../reference/behavior-considered-undefined.html
785 /// There are a few things that `transmute` is really useful for.
787 /// Turning a pointer into a function pointer. This is *not* portable to
788 /// machines where function pointers and data pointers have different sizes.
791 /// fn foo() -> i32 {
794 /// let pointer = foo as *const ();
795 /// let function = unsafe {
796 /// std::mem::transmute::<*const (), fn() -> i32>(pointer)
798 /// assert_eq!(function(), 0);
801 /// Extending a lifetime, or shortening an invariant lifetime. This is
802 /// advanced, very unsafe Rust!
805 /// struct R<'a>(&'a i32);
806 /// unsafe fn extend_lifetime<'b>(r: R<'b>) -> R<'static> {
807 /// std::mem::transmute::<R<'b>, R<'static>>(r)
810 /// unsafe fn shorten_invariant_lifetime<'b, 'c>(r: &'b mut R<'static>)
811 /// -> &'b mut R<'c> {
812 /// std::mem::transmute::<&'b mut R<'static>, &'b mut R<'c>>(r)
818 /// Don't despair: many uses of `transmute` can be achieved through other means.
819 /// Below are common applications of `transmute` which can be replaced with safer
822 /// Turning a pointer into a `usize`:
826 /// let ptr_num_transmute = unsafe {
827 /// std::mem::transmute::<&i32, usize>(ptr)
830 /// // Use an `as` cast instead
831 /// let ptr_num_cast = ptr as *const i32 as usize;
834 /// Turning a `*mut T` into an `&mut T`:
837 /// let ptr: *mut i32 = &mut 0;
838 /// let ref_transmuted = unsafe {
839 /// std::mem::transmute::<*mut i32, &mut i32>(ptr)
842 /// // Use a reborrow instead
843 /// let ref_casted = unsafe { &mut *ptr };
846 /// Turning an `&mut T` into an `&mut U`:
849 /// let ptr = &mut 0;
850 /// let val_transmuted = unsafe {
851 /// std::mem::transmute::<&mut i32, &mut u32>(ptr)
854 /// // Now, put together `as` and reborrowing - note the chaining of `as`
855 /// // `as` is not transitive
856 /// let val_casts = unsafe { &mut *(ptr as *mut i32 as *mut u32) };
859 /// Turning an `&str` into an `&[u8]`:
862 /// // this is not a good way to do this.
863 /// let slice = unsafe { std::mem::transmute::<&str, &[u8]>("Rust") };
864 /// assert_eq!(slice, &[82, 117, 115, 116]);
866 /// // You could use `str::as_bytes`
867 /// let slice = "Rust".as_bytes();
868 /// assert_eq!(slice, &[82, 117, 115, 116]);
870 /// // Or, just use a byte string, if you have control over the string
872 /// assert_eq!(b"Rust", &[82, 117, 115, 116]);
875 /// Turning a `Vec<&T>` into a `Vec<Option<&T>>`:
878 /// let store = [0, 1, 2, 3];
879 /// let v_orig = store.iter().collect::<Vec<&i32>>();
881 /// // clone the vector as we will reuse them later
882 /// let v_clone = v_orig.clone();
884 /// // Using transmute: this relies on the unspecified data layout of `Vec`, which is a
885 /// // bad idea and could cause Undefined Behavior
886 /// // However, it is no-copy.
887 /// let v_transmuted = unsafe {
888 /// std::mem::transmute::<Vec<&i32>, Vec<Option<&i32>>>(v_clone)
891 /// let v_clone = v_orig.clone();
893 /// // This is the suggested, safe way.
894 /// // It does copy the entire vector, though, into a new array.
895 /// let v_collected = v_clone.into_iter()
897 /// .collect::<Vec<Option<&i32>>>();
899 /// let v_clone = v_orig.clone();
901 /// // The no-copy, unsafe way, still using transmute, but not relying on the data layout.
902 /// // Like the first approach, this reuses the `Vec` internals.
903 /// // Therefore, the new inner type must have the
904 /// // exact same size, *and the same alignment*, as the old type.
905 /// // The same caveats exist for this method as transmute, for
906 /// // the original inner type (`&i32`) to the converted inner type
907 /// // (`Option<&i32>`), so read the nomicon pages linked above and also
908 /// // consult the [`from_raw_parts`] documentation.
909 /// let v_from_raw = unsafe {
910 // FIXME Update this when vec_into_raw_parts is stabilized
911 /// // Ensure the original vector is not dropped.
912 /// let mut v_clone = std::mem::ManuallyDrop::new(v_clone);
913 /// Vec::from_raw_parts(v_clone.as_mut_ptr() as *mut Option<&i32>,
915 /// v_clone.capacity())
919 /// [`from_raw_parts`]: ../../std/vec/struct.Vec.html#method.from_raw_parts
921 /// Implementing `split_at_mut`:
924 /// use std::{slice, mem};
926 /// // There are multiple ways to do this, and there are multiple problems
927 /// // with the following (transmute) way.
928 /// fn split_at_mut_transmute<T>(slice: &mut [T], mid: usize)
929 /// -> (&mut [T], &mut [T]) {
930 /// let len = slice.len();
931 /// assert!(mid <= len);
933 /// let slice2 = mem::transmute::<&mut [T], &mut [T]>(slice);
934 /// // first: transmute is not typesafe; all it checks is that T and
935 /// // U are of the same size. Second, right here, you have two
936 /// // mutable references pointing to the same memory.
937 /// (&mut slice[0..mid], &mut slice2[mid..len])
941 /// // This gets rid of the typesafety problems; `&mut *` will *only* give
942 /// // you an `&mut T` from an `&mut T` or `*mut T`.
943 /// fn split_at_mut_casts<T>(slice: &mut [T], mid: usize)
944 /// -> (&mut [T], &mut [T]) {
945 /// let len = slice.len();
946 /// assert!(mid <= len);
948 /// let slice2 = &mut *(slice as *mut [T]);
949 /// // however, you still have two mutable references pointing to
950 /// // the same memory.
951 /// (&mut slice[0..mid], &mut slice2[mid..len])
955 /// // This is how the standard library does it. This is the best method, if
956 /// // you need to do something like this
957 /// fn split_at_stdlib<T>(slice: &mut [T], mid: usize)
958 /// -> (&mut [T], &mut [T]) {
959 /// let len = slice.len();
960 /// assert!(mid <= len);
962 /// let ptr = slice.as_mut_ptr();
963 /// // This now has three mutable references pointing at the same
964 /// // memory. `slice`, the rvalue ret.0, and the rvalue ret.1.
965 /// // `slice` is never used after `let ptr = ...`, and so one can
966 /// // treat it as "dead", and therefore, you only have two real
967 /// // mutable slices.
968 /// (slice::from_raw_parts_mut(ptr, mid),
969 /// slice::from_raw_parts_mut(ptr.add(mid), len - mid))
973 #[stable(feature = "rust1", since = "1.0.0")]
974 #[rustc_const_unstable(feature = "const_transmute", issue = "53605")]
975 pub fn transmute<T, U>(e: T) -> U;
977 /// Returns `true` if the actual type given as `T` requires drop
978 /// glue; returns `false` if the actual type provided for `T`
979 /// implements `Copy`.
981 /// If the actual type neither requires drop glue nor implements
982 /// `Copy`, then may return `true` or `false`.
984 /// The stabilized version of this intrinsic is
985 /// [`std::mem::needs_drop`](../../std/mem/fn.needs_drop.html).
986 #[rustc_const_stable(feature = "const_needs_drop", since = "1.40.0")]
987 pub fn needs_drop<T>() -> bool;
989 /// Calculates the offset from a pointer.
991 /// This is implemented as an intrinsic to avoid converting to and from an
992 /// integer, since the conversion would throw away aliasing information.
996 /// Both the starting and resulting pointer must be either in bounds or one
997 /// byte past the end of an allocated object. If either pointer is out of
998 /// bounds or arithmetic overflow occurs then any further use of the
999 /// returned value will result in undefined behavior.
1000 pub fn offset<T>(dst: *const T, offset: isize) -> *const T;
1002 /// Calculates the offset from a pointer, potentially wrapping.
1004 /// This is implemented as an intrinsic to avoid converting to and from an
1005 /// integer, since the conversion inhibits certain optimizations.
1009 /// Unlike the `offset` intrinsic, this intrinsic does not restrict the
1010 /// resulting pointer to point into or one byte past the end of an allocated
1011 /// object, and it wraps with two's complement arithmetic. The resulting
1012 /// value is not necessarily valid to be used to actually access memory.
1013 pub fn arith_offset<T>(dst: *const T, offset: isize) -> *const T;
1015 /// Equivalent to the appropriate `llvm.memcpy.p0i8.0i8.*` intrinsic, with
1016 /// a size of `count` * `size_of::<T>()` and an alignment of
1017 /// `min_align_of::<T>()`
1019 /// The volatile parameter is set to `true`, so it will not be optimized out
1020 /// unless size is equal to zero.
1021 pub fn volatile_copy_nonoverlapping_memory<T>(dst: *mut T, src: *const T, count: usize);
1022 /// Equivalent to the appropriate `llvm.memmove.p0i8.0i8.*` intrinsic, with
1023 /// a size of `count` * `size_of::<T>()` and an alignment of
1024 /// `min_align_of::<T>()`
1026 /// The volatile parameter is set to `true`, so it will not be optimized out
1027 /// unless size is equal to zero.
1028 pub fn volatile_copy_memory<T>(dst: *mut T, src: *const T, count: usize);
1029 /// Equivalent to the appropriate `llvm.memset.p0i8.*` intrinsic, with a
1030 /// size of `count` * `size_of::<T>()` and an alignment of
1031 /// `min_align_of::<T>()`.
1033 /// The volatile parameter is set to `true`, so it will not be optimized out
1034 /// unless size is equal to zero.
1035 pub fn volatile_set_memory<T>(dst: *mut T, val: u8, count: usize);
1037 /// Performs a volatile load from the `src` pointer.
1038 /// The stabilized version of this intrinsic is
1039 /// [`std::ptr::read_volatile`](../../std/ptr/fn.read_volatile.html).
1040 pub fn volatile_load<T>(src: *const T) -> T;
1041 /// Performs a volatile store to the `dst` pointer.
1042 /// The stabilized version of this intrinsic is
1043 /// [`std::ptr::write_volatile`](../../std/ptr/fn.write_volatile.html).
1044 pub fn volatile_store<T>(dst: *mut T, val: T);
1046 /// Performs a volatile load from the `src` pointer
1047 /// The pointer is not required to be aligned.
1048 pub fn unaligned_volatile_load<T>(src: *const T) -> T;
1049 /// Performs a volatile store to the `dst` pointer.
1050 /// The pointer is not required to be aligned.
1051 pub fn unaligned_volatile_store<T>(dst: *mut T, val: T);
1053 /// Returns the square root of an `f32`
1054 pub fn sqrtf32(x: f32) -> f32;
1055 /// Returns the square root of an `f64`
1056 pub fn sqrtf64(x: f64) -> f64;
1058 /// Raises an `f32` to an integer power.
1059 pub fn powif32(a: f32, x: i32) -> f32;
1060 /// Raises an `f64` to an integer power.
1061 pub fn powif64(a: f64, x: i32) -> f64;
1063 /// Returns the sine of an `f32`.
1064 pub fn sinf32(x: f32) -> f32;
1065 /// Returns the sine of an `f64`.
1066 pub fn sinf64(x: f64) -> f64;
1068 /// Returns the cosine of an `f32`.
1069 pub fn cosf32(x: f32) -> f32;
1070 /// Returns the cosine of an `f64`.
1071 pub fn cosf64(x: f64) -> f64;
1073 /// Raises an `f32` to an `f32` power.
1074 pub fn powf32(a: f32, x: f32) -> f32;
1075 /// Raises an `f64` to an `f64` power.
1076 pub fn powf64(a: f64, x: f64) -> f64;
1078 /// Returns the exponential of an `f32`.
1079 pub fn expf32(x: f32) -> f32;
1080 /// Returns the exponential of an `f64`.
1081 pub fn expf64(x: f64) -> f64;
1083 /// Returns 2 raised to the power of an `f32`.
1084 pub fn exp2f32(x: f32) -> f32;
1085 /// Returns 2 raised to the power of an `f64`.
1086 pub fn exp2f64(x: f64) -> f64;
1088 /// Returns the natural logarithm of an `f32`.
1089 pub fn logf32(x: f32) -> f32;
1090 /// Returns the natural logarithm of an `f64`.
1091 pub fn logf64(x: f64) -> f64;
1093 /// Returns the base 10 logarithm of an `f32`.
1094 pub fn log10f32(x: f32) -> f32;
1095 /// Returns the base 10 logarithm of an `f64`.
1096 pub fn log10f64(x: f64) -> f64;
1098 /// Returns the base 2 logarithm of an `f32`.
1099 pub fn log2f32(x: f32) -> f32;
1100 /// Returns the base 2 logarithm of an `f64`.
1101 pub fn log2f64(x: f64) -> f64;
1103 /// Returns `a * b + c` for `f32` values.
1104 pub fn fmaf32(a: f32, b: f32, c: f32) -> f32;
1105 /// Returns `a * b + c` for `f64` values.
1106 pub fn fmaf64(a: f64, b: f64, c: f64) -> f64;
1108 /// Returns the absolute value of an `f32`.
1109 pub fn fabsf32(x: f32) -> f32;
1110 /// Returns the absolute value of an `f64`.
1111 pub fn fabsf64(x: f64) -> f64;
1113 /// Returns the minimum of two `f32` values.
1114 pub fn minnumf32(x: f32, y: f32) -> f32;
1115 /// Returns the minimum of two `f64` values.
1116 pub fn minnumf64(x: f64, y: f64) -> f64;
1117 /// Returns the maximum of two `f32` values.
1118 pub fn maxnumf32(x: f32, y: f32) -> f32;
1119 /// Returns the maximum of two `f64` values.
1120 pub fn maxnumf64(x: f64, y: f64) -> f64;
1122 /// Copies the sign from `y` to `x` for `f32` values.
1123 pub fn copysignf32(x: f32, y: f32) -> f32;
1124 /// Copies the sign from `y` to `x` for `f64` values.
1125 pub fn copysignf64(x: f64, y: f64) -> f64;
1127 /// Returns the largest integer less than or equal to an `f32`.
1128 pub fn floorf32(x: f32) -> f32;
1129 /// Returns the largest integer less than or equal to an `f64`.
1130 pub fn floorf64(x: f64) -> f64;
1132 /// Returns the smallest integer greater than or equal to an `f32`.
1133 pub fn ceilf32(x: f32) -> f32;
1134 /// Returns the smallest integer greater than or equal to an `f64`.
1135 pub fn ceilf64(x: f64) -> f64;
1137 /// Returns the integer part of an `f32`.
1138 pub fn truncf32(x: f32) -> f32;
1139 /// Returns the integer part of an `f64`.
1140 pub fn truncf64(x: f64) -> f64;
1142 /// Returns the nearest integer to an `f32`. May raise an inexact floating-point exception
1143 /// if the argument is not an integer.
1144 pub fn rintf32(x: f32) -> f32;
1145 /// Returns the nearest integer to an `f64`. May raise an inexact floating-point exception
1146 /// if the argument is not an integer.
1147 pub fn rintf64(x: f64) -> f64;
1149 /// Returns the nearest integer to an `f32`.
1150 pub fn nearbyintf32(x: f32) -> f32;
1151 /// Returns the nearest integer to an `f64`.
1152 pub fn nearbyintf64(x: f64) -> f64;
1154 /// Returns the nearest integer to an `f32`. Rounds half-way cases away from zero.
1155 pub fn roundf32(x: f32) -> f32;
1156 /// Returns the nearest integer to an `f64`. Rounds half-way cases away from zero.
1157 pub fn roundf64(x: f64) -> f64;
1159 /// Float addition that allows optimizations based on algebraic rules.
1160 /// May assume inputs are finite.
1161 pub fn fadd_fast<T>(a: T, b: T) -> T;
1163 /// Float subtraction that allows optimizations based on algebraic rules.
1164 /// May assume inputs are finite.
1165 pub fn fsub_fast<T>(a: T, b: T) -> T;
1167 /// Float multiplication that allows optimizations based on algebraic rules.
1168 /// May assume inputs are finite.
1169 pub fn fmul_fast<T>(a: T, b: T) -> T;
1171 /// Float division that allows optimizations based on algebraic rules.
1172 /// May assume inputs are finite.
1173 pub fn fdiv_fast<T>(a: T, b: T) -> T;
1175 /// Float remainder that allows optimizations based on algebraic rules.
1176 /// May assume inputs are finite.
1177 pub fn frem_fast<T>(a: T, b: T) -> T;
1179 /// Convert with LLVM’s fptoui/fptosi, which may return undef for values out of range
1180 /// https://github.com/rust-lang/rust/issues/10184
1181 pub fn float_to_int_approx_unchecked<Float, Int>(value: Float) -> Int;
1183 /// Returns the number of bits set in an integer type `T`
1184 #[rustc_const_stable(feature = "const_ctpop", since = "1.40.0")]
1185 pub fn ctpop<T>(x: T) -> T;
1187 /// Returns the number of leading unset bits (zeroes) in an integer type `T`.
1192 /// #![feature(core_intrinsics)]
1194 /// use std::intrinsics::ctlz;
1196 /// let x = 0b0001_1100_u8;
1197 /// let num_leading = ctlz(x);
1198 /// assert_eq!(num_leading, 3);
1201 /// An `x` with value `0` will return the bit width of `T`.
1204 /// #![feature(core_intrinsics)]
1206 /// use std::intrinsics::ctlz;
1209 /// let num_leading = ctlz(x);
1210 /// assert_eq!(num_leading, 16);
1212 #[rustc_const_stable(feature = "const_ctlz", since = "1.40.0")]
1213 pub fn ctlz<T>(x: T) -> T;
1215 /// Like `ctlz`, but extra-unsafe as it returns `undef` when
1216 /// given an `x` with value `0`.
1221 /// #![feature(core_intrinsics)]
1223 /// use std::intrinsics::ctlz_nonzero;
1225 /// let x = 0b0001_1100_u8;
1226 /// let num_leading = unsafe { ctlz_nonzero(x) };
1227 /// assert_eq!(num_leading, 3);
1229 #[rustc_const_unstable(feature = "constctlz", issue = "none")]
1230 pub fn ctlz_nonzero<T>(x: T) -> T;
1232 /// Returns the number of trailing unset bits (zeroes) in an integer type `T`.
1237 /// #![feature(core_intrinsics)]
1239 /// use std::intrinsics::cttz;
1241 /// let x = 0b0011_1000_u8;
1242 /// let num_trailing = cttz(x);
1243 /// assert_eq!(num_trailing, 3);
1246 /// An `x` with value `0` will return the bit width of `T`:
1249 /// #![feature(core_intrinsics)]
1251 /// use std::intrinsics::cttz;
1254 /// let num_trailing = cttz(x);
1255 /// assert_eq!(num_trailing, 16);
1257 #[rustc_const_stable(feature = "const_cttz", since = "1.40.0")]
1258 pub fn cttz<T>(x: T) -> T;
1260 /// Like `cttz`, but extra-unsafe as it returns `undef` when
1261 /// given an `x` with value `0`.
1266 /// #![feature(core_intrinsics)]
1268 /// use std::intrinsics::cttz_nonzero;
1270 /// let x = 0b0011_1000_u8;
1271 /// let num_trailing = unsafe { cttz_nonzero(x) };
1272 /// assert_eq!(num_trailing, 3);
1274 #[rustc_const_unstable(feature = "const_cttz", issue = "none")]
1275 pub fn cttz_nonzero<T>(x: T) -> T;
1277 /// Reverses the bytes in an integer type `T`.
1278 #[rustc_const_stable(feature = "const_bswap", since = "1.40.0")]
1279 pub fn bswap<T>(x: T) -> T;
1281 /// Reverses the bits in an integer type `T`.
1282 #[rustc_const_stable(feature = "const_bitreverse", since = "1.40.0")]
1283 pub fn bitreverse<T>(x: T) -> T;
1285 /// Performs checked integer addition.
1286 /// The stabilized versions of this intrinsic are available on the integer
1287 /// primitives via the `overflowing_add` method. For example,
1288 /// [`std::u32::overflowing_add`](../../std/primitive.u32.html#method.overflowing_add)
1289 #[rustc_const_stable(feature = "const_int_overflow", since = "1.40.0")]
1290 pub fn add_with_overflow<T>(x: T, y: T) -> (T, bool);
1292 /// Performs checked integer subtraction
1293 /// The stabilized versions of this intrinsic are available on the integer
1294 /// primitives via the `overflowing_sub` method. For example,
1295 /// [`std::u32::overflowing_sub`](../../std/primitive.u32.html#method.overflowing_sub)
1296 #[rustc_const_stable(feature = "const_int_overflow", since = "1.40.0")]
1297 pub fn sub_with_overflow<T>(x: T, y: T) -> (T, bool);
1299 /// Performs checked integer multiplication
1300 /// The stabilized versions of this intrinsic are available on the integer
1301 /// primitives via the `overflowing_mul` method. For example,
1302 /// [`std::u32::overflowing_mul`](../../std/primitive.u32.html#method.overflowing_mul)
1303 #[rustc_const_stable(feature = "const_int_overflow", since = "1.40.0")]
1304 pub fn mul_with_overflow<T>(x: T, y: T) -> (T, bool);
1306 /// Performs an exact division, resulting in undefined behavior where
1307 /// `x % y != 0` or `y == 0` or `x == T::min_value() && y == -1`
1308 pub fn exact_div<T>(x: T, y: T) -> T;
1310 /// Performs an unchecked division, resulting in undefined behavior
1311 /// where y = 0 or x = `T::min_value()` and y = -1
1312 #[rustc_const_unstable(feature = "const_int_unchecked_arith", issue = "none")]
1313 pub fn unchecked_div<T>(x: T, y: T) -> T;
1314 /// Returns the remainder of an unchecked division, resulting in
1315 /// undefined behavior where y = 0 or x = `T::min_value()` and y = -1
1316 #[rustc_const_unstable(feature = "const_int_unchecked_arith", issue = "none")]
1317 pub fn unchecked_rem<T>(x: T, y: T) -> T;
1319 /// Performs an unchecked left shift, resulting in undefined behavior when
1320 /// y < 0 or y >= N, where N is the width of T in bits.
1321 #[rustc_const_stable(feature = "const_int_unchecked", since = "1.40.0")]
1322 pub fn unchecked_shl<T>(x: T, y: T) -> T;
1323 /// Performs an unchecked right shift, resulting in undefined behavior when
1324 /// y < 0 or y >= N, where N is the width of T in bits.
1325 #[rustc_const_stable(feature = "const_int_unchecked", since = "1.40.0")]
1326 pub fn unchecked_shr<T>(x: T, y: T) -> T;
1328 /// Returns the result of an unchecked addition, resulting in
1329 /// undefined behavior when `x + y > T::max_value()` or `x + y < T::min_value()`.
1330 #[rustc_const_unstable(feature = "const_int_unchecked_arith", issue = "none")]
1331 pub fn unchecked_add<T>(x: T, y: T) -> T;
1333 /// Returns the result of an unchecked subtraction, resulting in
1334 /// undefined behavior when `x - y > T::max_value()` or `x - y < T::min_value()`.
1335 #[rustc_const_unstable(feature = "const_int_unchecked_arith", issue = "none")]
1336 pub fn unchecked_sub<T>(x: T, y: T) -> T;
1338 /// Returns the result of an unchecked multiplication, resulting in
1339 /// undefined behavior when `x * y > T::max_value()` or `x * y < T::min_value()`.
1340 #[rustc_const_unstable(feature = "const_int_unchecked_arith", issue = "none")]
1341 pub fn unchecked_mul<T>(x: T, y: T) -> T;
1343 /// Performs rotate left.
1344 /// The stabilized versions of this intrinsic are available on the integer
1345 /// primitives via the `rotate_left` method. For example,
1346 /// [`std::u32::rotate_left`](../../std/primitive.u32.html#method.rotate_left)
1347 #[rustc_const_stable(feature = "const_int_rotate", since = "1.40.0")]
1348 pub fn rotate_left<T>(x: T, y: T) -> T;
1350 /// Performs rotate right.
1351 /// The stabilized versions of this intrinsic are available on the integer
1352 /// primitives via the `rotate_right` method. For example,
1353 /// [`std::u32::rotate_right`](../../std/primitive.u32.html#method.rotate_right)
1354 #[rustc_const_stable(feature = "const_int_rotate", since = "1.40.0")]
1355 pub fn rotate_right<T>(x: T, y: T) -> T;
1357 /// Returns (a + b) mod 2<sup>N</sup>, where N is the width of T in bits.
1358 /// The stabilized versions of this intrinsic are available on the integer
1359 /// primitives via the `wrapping_add` method. For example,
1360 /// [`std::u32::wrapping_add`](../../std/primitive.u32.html#method.wrapping_add)
1361 #[rustc_const_stable(feature = "const_int_wrapping", since = "1.40.0")]
1362 pub fn wrapping_add<T>(a: T, b: T) -> T;
1363 /// Returns (a - b) mod 2<sup>N</sup>, where N is the width of T in bits.
1364 /// The stabilized versions of this intrinsic are available on the integer
1365 /// primitives via the `wrapping_sub` method. For example,
1366 /// [`std::u32::wrapping_sub`](../../std/primitive.u32.html#method.wrapping_sub)
1367 #[rustc_const_stable(feature = "const_int_wrapping", since = "1.40.0")]
1368 pub fn wrapping_sub<T>(a: T, b: T) -> T;
1369 /// Returns (a * b) mod 2<sup>N</sup>, where N is the width of T in bits.
1370 /// The stabilized versions of this intrinsic are available on the integer
1371 /// primitives via the `wrapping_mul` method. For example,
1372 /// [`std::u32::wrapping_mul`](../../std/primitive.u32.html#method.wrapping_mul)
1373 #[rustc_const_stable(feature = "const_int_wrapping", since = "1.40.0")]
1374 pub fn wrapping_mul<T>(a: T, b: T) -> T;
1376 /// Computes `a + b`, while saturating at numeric bounds.
1377 /// The stabilized versions of this intrinsic are available on the integer
1378 /// primitives via the `saturating_add` method. For example,
1379 /// [`std::u32::saturating_add`](../../std/primitive.u32.html#method.saturating_add)
1380 #[rustc_const_stable(feature = "const_int_saturating", since = "1.40.0")]
1381 pub fn saturating_add<T>(a: T, b: T) -> T;
1382 /// Computes `a - b`, while saturating at numeric bounds.
1383 /// The stabilized versions of this intrinsic are available on the integer
1384 /// primitives via the `saturating_sub` method. For example,
1385 /// [`std::u32::saturating_sub`](../../std/primitive.u32.html#method.saturating_sub)
1386 #[rustc_const_stable(feature = "const_int_saturating", since = "1.40.0")]
1387 pub fn saturating_sub<T>(a: T, b: T) -> T;
1389 /// Returns the value of the discriminant for the variant in 'v',
1390 /// cast to a `u64`; if `T` has no discriminant, returns 0.
1391 pub fn discriminant_value<T>(v: &T) -> u64;
1393 /// Rust's "try catch" construct which invokes the function pointer `f` with
1394 /// the data pointer `data`.
1396 /// The third pointer is a target-specific data pointer which is filled in
1397 /// with the specifics of the exception that occurred. For examples on Unix
1398 /// platforms this is a `*mut *mut T` which is filled in by the compiler and
1399 /// on MSVC it's `*mut [usize; 2]`. For more information see the compiler's
1400 /// source as well as std's catch implementation.
1401 pub fn r#try(f: fn(*mut u8), data: *mut u8, local_ptr: *mut u8) -> i32;
1403 /// Emits a `!nontemporal` store according to LLVM (see their docs).
1404 /// Probably will never become stable.
1405 pub fn nontemporal_store<T>(ptr: *mut T, val: T);
1407 /// See documentation of `<*const T>::offset_from` for details.
1408 #[rustc_const_unstable(feature = "const_ptr_offset_from", issue = "none")]
1409 pub fn ptr_offset_from<T>(ptr: *const T, base: *const T) -> isize;
1411 /// Internal hook used by Miri to implement unwinding.
1412 /// Compiles to a NOP during non-Miri codegen.
1414 /// Perma-unstable: do not use
1415 pub fn miri_start_panic(data: *mut (dyn crate::any::Any + crate::marker::Send)) -> ();
1418 // Some functions are defined here because they accidentally got made
1419 // available in this module on stable. See <https://github.com/rust-lang/rust/issues/15702>.
1420 // (`transmute` also falls into this category, but it cannot be wrapped due to the
1421 // check that `T` and `U` have the same size.)
1423 /// Checks whether `ptr` is properly aligned with respect to
1424 /// `align_of::<T>()`.
1425 pub(crate) fn is_aligned_and_not_null<T>(ptr: *const T) -> bool {
1426 !ptr.is_null() && ptr as usize % mem::align_of::<T>() == 0
1429 /// Checks whether the regions of memory starting at `src` and `dst` of size
1430 /// `count * size_of::<T>()` do *not* overlap.
1431 pub(crate) fn is_nonoverlapping<T>(src: *const T, dst: *const T, count: usize) -> bool {
1432 let src_usize = src as usize;
1433 let dst_usize = dst as usize;
1434 let size = mem::size_of::<T>().checked_mul(count).unwrap();
1435 let diff = if src_usize > dst_usize { src_usize - dst_usize } else { dst_usize - src_usize };
1436 // If the absolute distance between the ptrs is at least as big as the size of the buffer,
1437 // they do not overlap.
1441 /// Copies `count * size_of::<T>()` bytes from `src` to `dst`. The source
1442 /// and destination must *not* overlap.
1444 /// For regions of memory which might overlap, use [`copy`] instead.
1446 /// `copy_nonoverlapping` is semantically equivalent to C's [`memcpy`], but
1447 /// with the argument order swapped.
1449 /// [`copy`]: ./fn.copy.html
1450 /// [`memcpy`]: https://en.cppreference.com/w/c/string/byte/memcpy
1454 /// Behavior is undefined if any of the following conditions are violated:
1456 /// * `src` must be [valid] for reads of `count * size_of::<T>()` bytes.
1458 /// * `dst` must be [valid] for writes of `count * size_of::<T>()` bytes.
1460 /// * Both `src` and `dst` must be properly aligned.
1462 /// * The region of memory beginning at `src` with a size of `count *
1463 /// size_of::<T>()` bytes must *not* overlap with the region of memory
1464 /// beginning at `dst` with the same size.
1466 /// Like [`read`], `copy_nonoverlapping` creates a bitwise copy of `T`, regardless of
1467 /// whether `T` is [`Copy`]. If `T` is not [`Copy`], using *both* the values
1468 /// in the region beginning at `*src` and the region beginning at `*dst` can
1469 /// [violate memory safety][read-ownership].
1471 /// Note that even if the effectively copied size (`count * size_of::<T>()`) is
1472 /// `0`, the pointers must be non-NULL and properly aligned.
1474 /// [`Copy`]: ../marker/trait.Copy.html
1475 /// [`read`]: ../ptr/fn.read.html
1476 /// [read-ownership]: ../ptr/fn.read.html#ownership-of-the-returned-value
1477 /// [valid]: ../ptr/index.html#safety
1481 /// Manually implement [`Vec::append`]:
1486 /// /// Moves all the elements of `src` into `dst`, leaving `src` empty.
1487 /// fn append<T>(dst: &mut Vec<T>, src: &mut Vec<T>) {
1488 /// let src_len = src.len();
1489 /// let dst_len = dst.len();
1491 /// // Ensure that `dst` has enough capacity to hold all of `src`.
1492 /// dst.reserve(src_len);
1495 /// // The call to offset is always safe because `Vec` will never
1496 /// // allocate more than `isize::MAX` bytes.
1497 /// let dst_ptr = dst.as_mut_ptr().offset(dst_len as isize);
1498 /// let src_ptr = src.as_ptr();
1500 /// // Truncate `src` without dropping its contents. We do this first,
1501 /// // to avoid problems in case something further down panics.
1504 /// // The two regions cannot overlap because mutable references do
1505 /// // not alias, and two different vectors cannot own the same
1507 /// ptr::copy_nonoverlapping(src_ptr, dst_ptr, src_len);
1509 /// // Notify `dst` that it now holds the contents of `src`.
1510 /// dst.set_len(dst_len + src_len);
1514 /// let mut a = vec!['r'];
1515 /// let mut b = vec!['u', 's', 't'];
1517 /// append(&mut a, &mut b);
1519 /// assert_eq!(a, &['r', 'u', 's', 't']);
1520 /// assert!(b.is_empty());
1523 /// [`Vec::append`]: ../../std/vec/struct.Vec.html#method.append
1524 #[doc(alias = "memcpy")]
1525 #[stable(feature = "rust1", since = "1.0.0")]
1527 pub unsafe fn copy_nonoverlapping<T>(src: *const T, dst: *mut T, count: usize) {
1528 extern "rust-intrinsic" {
1529 fn copy_nonoverlapping<T>(src: *const T, dst: *mut T, count: usize);
1532 debug_assert!(is_aligned_and_not_null(src), "attempt to copy from unaligned or null pointer");
1533 debug_assert!(is_aligned_and_not_null(dst), "attempt to copy to unaligned or null pointer");
1534 debug_assert!(is_nonoverlapping(src, dst, count), "attempt to copy to overlapping memory");
1535 copy_nonoverlapping(src, dst, count)
1538 /// Copies `count * size_of::<T>()` bytes from `src` to `dst`. The source
1539 /// and destination may overlap.
1541 /// If the source and destination will *never* overlap,
1542 /// [`copy_nonoverlapping`] can be used instead.
1544 /// `copy` is semantically equivalent to C's [`memmove`], but with the argument
1545 /// order swapped. Copying takes place as if the bytes were copied from `src`
1546 /// to a temporary array and then copied from the array to `dst`.
1548 /// [`copy_nonoverlapping`]: ./fn.copy_nonoverlapping.html
1549 /// [`memmove`]: https://en.cppreference.com/w/c/string/byte/memmove
1553 /// Behavior is undefined if any of the following conditions are violated:
1555 /// * `src` must be [valid] for reads of `count * size_of::<T>()` bytes.
1557 /// * `dst` must be [valid] for writes of `count * size_of::<T>()` bytes.
1559 /// * Both `src` and `dst` must be properly aligned.
1561 /// Like [`read`], `copy` creates a bitwise copy of `T`, regardless of
1562 /// whether `T` is [`Copy`]. If `T` is not [`Copy`], using both the values
1563 /// in the region beginning at `*src` and the region beginning at `*dst` can
1564 /// [violate memory safety][read-ownership].
1566 /// Note that even if the effectively copied size (`count * size_of::<T>()`) is
1567 /// `0`, the pointers must be non-NULL and properly aligned.
1569 /// [`Copy`]: ../marker/trait.Copy.html
1570 /// [`read`]: ../ptr/fn.read.html
1571 /// [read-ownership]: ../ptr/fn.read.html#ownership-of-the-returned-value
1572 /// [valid]: ../ptr/index.html#safety
1576 /// Efficiently create a Rust vector from an unsafe buffer:
1581 /// # #[allow(dead_code)]
1582 /// unsafe fn from_buf_raw<T>(ptr: *const T, elts: usize) -> Vec<T> {
1583 /// let mut dst = Vec::with_capacity(elts);
1584 /// dst.set_len(elts);
1585 /// ptr::copy(ptr, dst.as_mut_ptr(), elts);
1589 #[doc(alias = "memmove")]
1590 #[stable(feature = "rust1", since = "1.0.0")]
1592 pub unsafe fn copy<T>(src: *const T, dst: *mut T, count: usize) {
1593 extern "rust-intrinsic" {
1594 fn copy<T>(src: *const T, dst: *mut T, count: usize);
1597 debug_assert!(is_aligned_and_not_null(src), "attempt to copy from unaligned or null pointer");
1598 debug_assert!(is_aligned_and_not_null(dst), "attempt to copy to unaligned or null pointer");
1599 copy(src, dst, count)
1602 /// Sets `count * size_of::<T>()` bytes of memory starting at `dst` to
1605 /// `write_bytes` is similar to C's [`memset`], but sets `count *
1606 /// size_of::<T>()` bytes to `val`.
1608 /// [`memset`]: https://en.cppreference.com/w/c/string/byte/memset
1612 /// Behavior is undefined if any of the following conditions are violated:
1614 /// * `dst` must be [valid] for writes of `count * size_of::<T>()` bytes.
1616 /// * `dst` must be properly aligned.
1618 /// Additionally, the caller must ensure that writing `count *
1619 /// size_of::<T>()` bytes to the given region of memory results in a valid
1620 /// value of `T`. Using a region of memory typed as a `T` that contains an
1621 /// invalid value of `T` is undefined behavior.
1623 /// Note that even if the effectively copied size (`count * size_of::<T>()`) is
1624 /// `0`, the pointer must be non-NULL and properly aligned.
1626 /// [valid]: ../ptr/index.html#safety
1635 /// let mut vec = vec![0u32; 4];
1637 /// let vec_ptr = vec.as_mut_ptr();
1638 /// ptr::write_bytes(vec_ptr, 0xfe, 2);
1640 /// assert_eq!(vec, [0xfefefefe, 0xfefefefe, 0, 0]);
1643 /// Creating an invalid value:
1648 /// let mut v = Box::new(0i32);
1651 /// // Leaks the previously held value by overwriting the `Box<T>` with
1652 /// // a null pointer.
1653 /// ptr::write_bytes(&mut v as *mut Box<i32>, 0, 1);
1656 /// // At this point, using or dropping `v` results in undefined behavior.
1657 /// // drop(v); // ERROR
1659 /// // Even leaking `v` "uses" it, and hence is undefined behavior.
1660 /// // mem::forget(v); // ERROR
1662 /// // In fact, `v` is invalid according to basic type layout invariants, so *any*
1663 /// // operation touching it is undefined behavior.
1664 /// // let v2 = v; // ERROR
1667 /// // Let us instead put in a valid value
1668 /// ptr::write(&mut v as *mut Box<i32>, Box::new(42i32));
1671 /// // Now the box is fine
1672 /// assert_eq!(*v, 42);
1674 #[stable(feature = "rust1", since = "1.0.0")]
1676 pub unsafe fn write_bytes<T>(dst: *mut T, val: u8, count: usize) {
1677 extern "rust-intrinsic" {
1678 fn write_bytes<T>(dst: *mut T, val: u8, count: usize);
1681 debug_assert!(is_aligned_and_not_null(dst), "attempt to write to unaligned or null pointer");
1682 write_bytes(dst, val, count)