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 /// A guard for unsafe functions that cannot ever be executed if `T` does not permit
725 /// zero-initialization: This will statically either panic, or do nothing.
726 #[cfg(not(bootstrap))]
727 pub fn panic_if_zero_invalid<T>();
729 /// A guard for unsafe functions that cannot ever be executed if `T` has invalid
730 /// bit patterns: This will statically either panic, or do nothing.
731 #[cfg(not(bootstrap))]
732 pub fn panic_if_any_invalid<T>();
734 /// Gets a reference to a static `Location` indicating where it was called.
735 #[rustc_const_unstable(feature = "const_caller_location", issue = "47809")]
736 pub fn caller_location() -> &'static crate::panic::Location<'static>;
738 /// Creates a value initialized to zero.
740 /// `init` is unsafe because it returns a zeroed-out datum,
741 /// which is unsafe unless `T` is `Copy`. Also, even if T is
742 /// `Copy`, an all-zero value may not correspond to any legitimate
743 /// state for the type in question.
745 feature = "core_intrinsics",
746 reason = "intrinsics are unlikely to ever be stabilized, instead \
747 they should be used through stabilized interfaces \
748 in the rest of the standard library",
751 #[rustc_deprecated(reason = "superseded by MaybeUninit, removal planned", since = "1.38.0")]
752 pub fn init<T>() -> T;
754 /// Creates an uninitialized value.
756 /// `uninit` is unsafe because there is no guarantee of what its
757 /// contents are. In particular its drop-flag may be set to any
758 /// state, which means it may claim either dropped or
759 /// undropped. In the general case one must use `ptr::write` to
760 /// initialize memory previous set to the result of `uninit`.
762 feature = "core_intrinsics",
763 reason = "intrinsics are unlikely to ever be stabilized, instead \
764 they should be used through stabilized interfaces \
765 in the rest of the standard library",
768 #[rustc_deprecated(reason = "superseded by MaybeUninit, removal planned", since = "1.38.0")]
769 pub fn uninit<T>() -> T;
771 /// Moves a value out of scope without running drop glue.
772 pub fn forget<T: ?Sized>(_: T);
774 /// Reinterprets the bits of a value of one type as another type.
776 /// Both types must have the same size. Neither the original, nor the result,
777 /// may be an [invalid value](../../nomicon/what-unsafe-does.html).
779 /// `transmute` is semantically equivalent to a bitwise move of one type
780 /// into another. It copies the bits from the source value into the
781 /// destination value, then forgets the original. It's equivalent to C's
782 /// `memcpy` under the hood, just like `transmute_copy`.
784 /// `transmute` is **incredibly** unsafe. There are a vast number of ways to
785 /// cause [undefined behavior][ub] with this function. `transmute` should be
786 /// the absolute last resort.
788 /// The [nomicon](../../nomicon/transmutes.html) has additional
791 /// [ub]: ../../reference/behavior-considered-undefined.html
795 /// There are a few things that `transmute` is really useful for.
797 /// Turning a pointer into a function pointer. This is *not* portable to
798 /// machines where function pointers and data pointers have different sizes.
801 /// fn foo() -> i32 {
804 /// let pointer = foo as *const ();
805 /// let function = unsafe {
806 /// std::mem::transmute::<*const (), fn() -> i32>(pointer)
808 /// assert_eq!(function(), 0);
811 /// Extending a lifetime, or shortening an invariant lifetime. This is
812 /// advanced, very unsafe Rust!
815 /// struct R<'a>(&'a i32);
816 /// unsafe fn extend_lifetime<'b>(r: R<'b>) -> R<'static> {
817 /// std::mem::transmute::<R<'b>, R<'static>>(r)
820 /// unsafe fn shorten_invariant_lifetime<'b, 'c>(r: &'b mut R<'static>)
821 /// -> &'b mut R<'c> {
822 /// std::mem::transmute::<&'b mut R<'static>, &'b mut R<'c>>(r)
828 /// Don't despair: many uses of `transmute` can be achieved through other means.
829 /// Below are common applications of `transmute` which can be replaced with safer
832 /// Turning a pointer into a `usize`:
836 /// let ptr_num_transmute = unsafe {
837 /// std::mem::transmute::<&i32, usize>(ptr)
840 /// // Use an `as` cast instead
841 /// let ptr_num_cast = ptr as *const i32 as usize;
844 /// Turning a `*mut T` into an `&mut T`:
847 /// let ptr: *mut i32 = &mut 0;
848 /// let ref_transmuted = unsafe {
849 /// std::mem::transmute::<*mut i32, &mut i32>(ptr)
852 /// // Use a reborrow instead
853 /// let ref_casted = unsafe { &mut *ptr };
856 /// Turning an `&mut T` into an `&mut U`:
859 /// let ptr = &mut 0;
860 /// let val_transmuted = unsafe {
861 /// std::mem::transmute::<&mut i32, &mut u32>(ptr)
864 /// // Now, put together `as` and reborrowing - note the chaining of `as`
865 /// // `as` is not transitive
866 /// let val_casts = unsafe { &mut *(ptr as *mut i32 as *mut u32) };
869 /// Turning an `&str` into an `&[u8]`:
872 /// // this is not a good way to do this.
873 /// let slice = unsafe { std::mem::transmute::<&str, &[u8]>("Rust") };
874 /// assert_eq!(slice, &[82, 117, 115, 116]);
876 /// // You could use `str::as_bytes`
877 /// let slice = "Rust".as_bytes();
878 /// assert_eq!(slice, &[82, 117, 115, 116]);
880 /// // Or, just use a byte string, if you have control over the string
882 /// assert_eq!(b"Rust", &[82, 117, 115, 116]);
885 /// Turning a `Vec<&T>` into a `Vec<Option<&T>>`:
888 /// let store = [0, 1, 2, 3];
889 /// let v_orig = store.iter().collect::<Vec<&i32>>();
891 /// // clone the vector as we will reuse them later
892 /// let v_clone = v_orig.clone();
894 /// // Using transmute: this is Undefined Behavior, and a bad idea.
895 /// // However, it is no-copy.
896 /// let v_transmuted = unsafe {
897 /// std::mem::transmute::<Vec<&i32>, Vec<Option<&i32>>>(v_clone)
900 /// let v_clone = v_orig.clone();
902 /// // This is the suggested, safe way.
903 /// // It does copy the entire vector, though, into a new array.
904 /// let v_collected = v_clone.into_iter()
906 /// .collect::<Vec<Option<&i32>>>();
908 /// let v_clone = v_orig.clone();
910 /// // The no-copy, unsafe way, still using transmute, but not UB.
911 /// // This is equivalent to the original, but safer, and reuses the
912 /// // same `Vec` internals. Therefore, the new inner type must have the
913 /// // exact same size, and the same alignment, as the old type.
914 /// // The same caveats exist for this method as transmute, for
915 /// // the original inner type (`&i32`) to the converted inner type
916 /// // (`Option<&i32>`), so read the nomicon pages linked above.
917 /// let v_from_raw = unsafe {
918 // FIXME Update this when vec_into_raw_parts is stabilized
919 /// // Ensure the original vector is not dropped.
920 /// let mut v_clone = std::mem::ManuallyDrop::new(v_clone);
921 /// Vec::from_raw_parts(v_clone.as_mut_ptr() as *mut Option<&i32>,
923 /// v_clone.capacity())
927 /// Implementing `split_at_mut`:
930 /// use std::{slice, mem};
932 /// // There are multiple ways to do this, and there are multiple problems
933 /// // with the following (transmute) way.
934 /// fn split_at_mut_transmute<T>(slice: &mut [T], mid: usize)
935 /// -> (&mut [T], &mut [T]) {
936 /// let len = slice.len();
937 /// assert!(mid <= len);
939 /// let slice2 = mem::transmute::<&mut [T], &mut [T]>(slice);
940 /// // first: transmute is not typesafe; all it checks is that T and
941 /// // U are of the same size. Second, right here, you have two
942 /// // mutable references pointing to the same memory.
943 /// (&mut slice[0..mid], &mut slice2[mid..len])
947 /// // This gets rid of the typesafety problems; `&mut *` will *only* give
948 /// // you an `&mut T` from an `&mut T` or `*mut T`.
949 /// fn split_at_mut_casts<T>(slice: &mut [T], mid: usize)
950 /// -> (&mut [T], &mut [T]) {
951 /// let len = slice.len();
952 /// assert!(mid <= len);
954 /// let slice2 = &mut *(slice as *mut [T]);
955 /// // however, you still have two mutable references pointing to
956 /// // the same memory.
957 /// (&mut slice[0..mid], &mut slice2[mid..len])
961 /// // This is how the standard library does it. This is the best method, if
962 /// // you need to do something like this
963 /// fn split_at_stdlib<T>(slice: &mut [T], mid: usize)
964 /// -> (&mut [T], &mut [T]) {
965 /// let len = slice.len();
966 /// assert!(mid <= len);
968 /// let ptr = slice.as_mut_ptr();
969 /// // This now has three mutable references pointing at the same
970 /// // memory. `slice`, the rvalue ret.0, and the rvalue ret.1.
971 /// // `slice` is never used after `let ptr = ...`, and so one can
972 /// // treat it as "dead", and therefore, you only have two real
973 /// // mutable slices.
974 /// (slice::from_raw_parts_mut(ptr, mid),
975 /// slice::from_raw_parts_mut(ptr.add(mid), len - mid))
979 #[stable(feature = "rust1", since = "1.0.0")]
980 #[rustc_const_unstable(feature = "const_transmute", issue = "53605")]
981 pub fn transmute<T, U>(e: T) -> U;
983 /// Returns `true` if the actual type given as `T` requires drop
984 /// glue; returns `false` if the actual type provided for `T`
985 /// implements `Copy`.
987 /// If the actual type neither requires drop glue nor implements
988 /// `Copy`, then may return `true` or `false`.
990 /// The stabilized version of this intrinsic is
991 /// [`std::mem::needs_drop`](../../std/mem/fn.needs_drop.html).
992 #[rustc_const_stable(feature = "const_needs_drop", since = "1.40.0")]
993 pub fn needs_drop<T>() -> bool;
995 /// Calculates the offset from a pointer.
997 /// This is implemented as an intrinsic to avoid converting to and from an
998 /// integer, since the conversion would throw away aliasing information.
1002 /// Both the starting and resulting pointer must be either in bounds or one
1003 /// byte past the end of an allocated object. If either pointer is out of
1004 /// bounds or arithmetic overflow occurs then any further use of the
1005 /// returned value will result in undefined behavior.
1006 pub fn offset<T>(dst: *const T, offset: isize) -> *const T;
1008 /// Calculates the offset from a pointer, potentially wrapping.
1010 /// This is implemented as an intrinsic to avoid converting to and from an
1011 /// integer, since the conversion inhibits certain optimizations.
1015 /// Unlike the `offset` intrinsic, this intrinsic does not restrict the
1016 /// resulting pointer to point into or one byte past the end of an allocated
1017 /// object, and it wraps with two's complement arithmetic. The resulting
1018 /// value is not necessarily valid to be used to actually access memory.
1019 pub fn arith_offset<T>(dst: *const T, offset: isize) -> *const T;
1021 /// Equivalent to the appropriate `llvm.memcpy.p0i8.0i8.*` intrinsic, with
1022 /// a size of `count` * `size_of::<T>()` and an alignment of
1023 /// `min_align_of::<T>()`
1025 /// The volatile parameter is set to `true`, so it will not be optimized out
1026 /// unless size is equal to zero.
1027 pub fn volatile_copy_nonoverlapping_memory<T>(dst: *mut T, src: *const T, count: usize);
1028 /// Equivalent to the appropriate `llvm.memmove.p0i8.0i8.*` intrinsic, with
1029 /// a size of `count` * `size_of::<T>()` and an alignment of
1030 /// `min_align_of::<T>()`
1032 /// The volatile parameter is set to `true`, so it will not be optimized out
1033 /// unless size is equal to zero.
1034 pub fn volatile_copy_memory<T>(dst: *mut T, src: *const T, count: usize);
1035 /// Equivalent to the appropriate `llvm.memset.p0i8.*` intrinsic, with a
1036 /// size of `count` * `size_of::<T>()` and an alignment of
1037 /// `min_align_of::<T>()`.
1039 /// The volatile parameter is set to `true`, so it will not be optimized out
1040 /// unless size is equal to zero.
1041 pub fn volatile_set_memory<T>(dst: *mut T, val: u8, count: usize);
1043 /// Performs a volatile load from the `src` pointer.
1044 /// The stabilized version of this intrinsic is
1045 /// [`std::ptr::read_volatile`](../../std/ptr/fn.read_volatile.html).
1046 pub fn volatile_load<T>(src: *const T) -> T;
1047 /// Performs a volatile store to the `dst` pointer.
1048 /// The stabilized version of this intrinsic is
1049 /// [`std::ptr::write_volatile`](../../std/ptr/fn.write_volatile.html).
1050 pub fn volatile_store<T>(dst: *mut T, val: T);
1052 /// Performs a volatile load from the `src` pointer
1053 /// The pointer is not required to be aligned.
1054 pub fn unaligned_volatile_load<T>(src: *const T) -> T;
1055 /// Performs a volatile store to the `dst` pointer.
1056 /// The pointer is not required to be aligned.
1057 pub fn unaligned_volatile_store<T>(dst: *mut T, val: T);
1059 /// Returns the square root of an `f32`
1060 pub fn sqrtf32(x: f32) -> f32;
1061 /// Returns the square root of an `f64`
1062 pub fn sqrtf64(x: f64) -> f64;
1064 /// Raises an `f32` to an integer power.
1065 pub fn powif32(a: f32, x: i32) -> f32;
1066 /// Raises an `f64` to an integer power.
1067 pub fn powif64(a: f64, x: i32) -> f64;
1069 /// Returns the sine of an `f32`.
1070 pub fn sinf32(x: f32) -> f32;
1071 /// Returns the sine of an `f64`.
1072 pub fn sinf64(x: f64) -> f64;
1074 /// Returns the cosine of an `f32`.
1075 pub fn cosf32(x: f32) -> f32;
1076 /// Returns the cosine of an `f64`.
1077 pub fn cosf64(x: f64) -> f64;
1079 /// Raises an `f32` to an `f32` power.
1080 pub fn powf32(a: f32, x: f32) -> f32;
1081 /// Raises an `f64` to an `f64` power.
1082 pub fn powf64(a: f64, x: f64) -> f64;
1084 /// Returns the exponential of an `f32`.
1085 pub fn expf32(x: f32) -> f32;
1086 /// Returns the exponential of an `f64`.
1087 pub fn expf64(x: f64) -> f64;
1089 /// Returns 2 raised to the power of an `f32`.
1090 pub fn exp2f32(x: f32) -> f32;
1091 /// Returns 2 raised to the power of an `f64`.
1092 pub fn exp2f64(x: f64) -> f64;
1094 /// Returns the natural logarithm of an `f32`.
1095 pub fn logf32(x: f32) -> f32;
1096 /// Returns the natural logarithm of an `f64`.
1097 pub fn logf64(x: f64) -> f64;
1099 /// Returns the base 10 logarithm of an `f32`.
1100 pub fn log10f32(x: f32) -> f32;
1101 /// Returns the base 10 logarithm of an `f64`.
1102 pub fn log10f64(x: f64) -> f64;
1104 /// Returns the base 2 logarithm of an `f32`.
1105 pub fn log2f32(x: f32) -> f32;
1106 /// Returns the base 2 logarithm of an `f64`.
1107 pub fn log2f64(x: f64) -> f64;
1109 /// Returns `a * b + c` for `f32` values.
1110 pub fn fmaf32(a: f32, b: f32, c: f32) -> f32;
1111 /// Returns `a * b + c` for `f64` values.
1112 pub fn fmaf64(a: f64, b: f64, c: f64) -> f64;
1114 /// Returns the absolute value of an `f32`.
1115 pub fn fabsf32(x: f32) -> f32;
1116 /// Returns the absolute value of an `f64`.
1117 pub fn fabsf64(x: f64) -> f64;
1119 /// Returns the minimum of two `f32` values.
1120 pub fn minnumf32(x: f32, y: f32) -> f32;
1121 /// Returns the minimum of two `f64` values.
1122 pub fn minnumf64(x: f64, y: f64) -> f64;
1123 /// Returns the maximum of two `f32` values.
1124 pub fn maxnumf32(x: f32, y: f32) -> f32;
1125 /// Returns the maximum of two `f64` values.
1126 pub fn maxnumf64(x: f64, y: f64) -> f64;
1128 /// Copies the sign from `y` to `x` for `f32` values.
1129 pub fn copysignf32(x: f32, y: f32) -> f32;
1130 /// Copies the sign from `y` to `x` for `f64` values.
1131 pub fn copysignf64(x: f64, y: f64) -> f64;
1133 /// Returns the largest integer less than or equal to an `f32`.
1134 pub fn floorf32(x: f32) -> f32;
1135 /// Returns the largest integer less than or equal to an `f64`.
1136 pub fn floorf64(x: f64) -> f64;
1138 /// Returns the smallest integer greater than or equal to an `f32`.
1139 pub fn ceilf32(x: f32) -> f32;
1140 /// Returns the smallest integer greater than or equal to an `f64`.
1141 pub fn ceilf64(x: f64) -> f64;
1143 /// Returns the integer part of an `f32`.
1144 pub fn truncf32(x: f32) -> f32;
1145 /// Returns the integer part of an `f64`.
1146 pub fn truncf64(x: f64) -> f64;
1148 /// Returns the nearest integer to an `f32`. May raise an inexact floating-point exception
1149 /// if the argument is not an integer.
1150 pub fn rintf32(x: f32) -> f32;
1151 /// Returns the nearest integer to an `f64`. May raise an inexact floating-point exception
1152 /// if the argument is not an integer.
1153 pub fn rintf64(x: f64) -> f64;
1155 /// Returns the nearest integer to an `f32`.
1156 pub fn nearbyintf32(x: f32) -> f32;
1157 /// Returns the nearest integer to an `f64`.
1158 pub fn nearbyintf64(x: f64) -> f64;
1160 /// Returns the nearest integer to an `f32`. Rounds half-way cases away from zero.
1161 pub fn roundf32(x: f32) -> f32;
1162 /// Returns the nearest integer to an `f64`. Rounds half-way cases away from zero.
1163 pub fn roundf64(x: f64) -> f64;
1165 /// Float addition that allows optimizations based on algebraic rules.
1166 /// May assume inputs are finite.
1167 pub fn fadd_fast<T>(a: T, b: T) -> T;
1169 /// Float subtraction that allows optimizations based on algebraic rules.
1170 /// May assume inputs are finite.
1171 pub fn fsub_fast<T>(a: T, b: T) -> T;
1173 /// Float multiplication that allows optimizations based on algebraic rules.
1174 /// May assume inputs are finite.
1175 pub fn fmul_fast<T>(a: T, b: T) -> T;
1177 /// Float division that allows optimizations based on algebraic rules.
1178 /// May assume inputs are finite.
1179 pub fn fdiv_fast<T>(a: T, b: T) -> T;
1181 /// Float remainder that allows optimizations based on algebraic rules.
1182 /// May assume inputs are finite.
1183 pub fn frem_fast<T>(a: T, b: T) -> T;
1185 /// Convert with LLVM’s fptoui/fptosi, which may return undef for values out of range
1186 /// https://github.com/rust-lang/rust/issues/10184
1187 pub fn float_to_int_approx_unchecked<Float, Int>(value: Float) -> Int;
1189 /// Returns the number of bits set in an integer type `T`
1190 #[rustc_const_stable(feature = "const_ctpop", since = "1.40.0")]
1191 pub fn ctpop<T>(x: T) -> T;
1193 /// Returns the number of leading unset bits (zeroes) in an integer type `T`.
1198 /// #![feature(core_intrinsics)]
1200 /// use std::intrinsics::ctlz;
1202 /// let x = 0b0001_1100_u8;
1203 /// let num_leading = ctlz(x);
1204 /// assert_eq!(num_leading, 3);
1207 /// An `x` with value `0` will return the bit width of `T`.
1210 /// #![feature(core_intrinsics)]
1212 /// use std::intrinsics::ctlz;
1215 /// let num_leading = ctlz(x);
1216 /// assert_eq!(num_leading, 16);
1218 #[rustc_const_stable(feature = "const_ctlz", since = "1.40.0")]
1219 pub fn ctlz<T>(x: T) -> T;
1221 /// Like `ctlz`, but extra-unsafe as it returns `undef` when
1222 /// given an `x` with value `0`.
1227 /// #![feature(core_intrinsics)]
1229 /// use std::intrinsics::ctlz_nonzero;
1231 /// let x = 0b0001_1100_u8;
1232 /// let num_leading = unsafe { ctlz_nonzero(x) };
1233 /// assert_eq!(num_leading, 3);
1235 #[rustc_const_unstable(feature = "constctlz", issue = "none")]
1236 pub fn ctlz_nonzero<T>(x: T) -> T;
1238 /// Returns the number of trailing unset bits (zeroes) in an integer type `T`.
1243 /// #![feature(core_intrinsics)]
1245 /// use std::intrinsics::cttz;
1247 /// let x = 0b0011_1000_u8;
1248 /// let num_trailing = cttz(x);
1249 /// assert_eq!(num_trailing, 3);
1252 /// An `x` with value `0` will return the bit width of `T`:
1255 /// #![feature(core_intrinsics)]
1257 /// use std::intrinsics::cttz;
1260 /// let num_trailing = cttz(x);
1261 /// assert_eq!(num_trailing, 16);
1263 #[rustc_const_stable(feature = "const_cttz", since = "1.40.0")]
1264 pub fn cttz<T>(x: T) -> T;
1266 /// Like `cttz`, but extra-unsafe as it returns `undef` when
1267 /// given an `x` with value `0`.
1272 /// #![feature(core_intrinsics)]
1274 /// use std::intrinsics::cttz_nonzero;
1276 /// let x = 0b0011_1000_u8;
1277 /// let num_trailing = unsafe { cttz_nonzero(x) };
1278 /// assert_eq!(num_trailing, 3);
1280 #[rustc_const_unstable(feature = "const_cttz", issue = "none")]
1281 pub fn cttz_nonzero<T>(x: T) -> T;
1283 /// Reverses the bytes in an integer type `T`.
1284 #[rustc_const_stable(feature = "const_bswap", since = "1.40.0")]
1285 pub fn bswap<T>(x: T) -> T;
1287 /// Reverses the bits in an integer type `T`.
1288 #[rustc_const_stable(feature = "const_bitreverse", since = "1.40.0")]
1289 pub fn bitreverse<T>(x: T) -> T;
1291 /// Performs checked integer addition.
1292 /// The stabilized versions of this intrinsic are available on the integer
1293 /// primitives via the `overflowing_add` method. For example,
1294 /// [`std::u32::overflowing_add`](../../std/primitive.u32.html#method.overflowing_add)
1295 #[rustc_const_stable(feature = "const_int_overflow", since = "1.40.0")]
1296 pub fn add_with_overflow<T>(x: T, y: T) -> (T, bool);
1298 /// Performs checked integer subtraction
1299 /// The stabilized versions of this intrinsic are available on the integer
1300 /// primitives via the `overflowing_sub` method. For example,
1301 /// [`std::u32::overflowing_sub`](../../std/primitive.u32.html#method.overflowing_sub)
1302 #[rustc_const_stable(feature = "const_int_overflow", since = "1.40.0")]
1303 pub fn sub_with_overflow<T>(x: T, y: T) -> (T, bool);
1305 /// Performs checked integer multiplication
1306 /// The stabilized versions of this intrinsic are available on the integer
1307 /// primitives via the `overflowing_mul` method. For example,
1308 /// [`std::u32::overflowing_mul`](../../std/primitive.u32.html#method.overflowing_mul)
1309 #[rustc_const_stable(feature = "const_int_overflow", since = "1.40.0")]
1310 pub fn mul_with_overflow<T>(x: T, y: T) -> (T, bool);
1312 /// Performs an exact division, resulting in undefined behavior where
1313 /// `x % y != 0` or `y == 0` or `x == T::min_value() && y == -1`
1314 pub fn exact_div<T>(x: T, y: T) -> T;
1316 /// Performs an unchecked division, resulting in undefined behavior
1317 /// where y = 0 or x = `T::min_value()` and y = -1
1318 #[rustc_const_unstable(feature = "const_int_unchecked_arith", issue = "none")]
1319 pub fn unchecked_div<T>(x: T, y: T) -> T;
1320 /// Returns the remainder of an unchecked division, resulting in
1321 /// undefined behavior where y = 0 or x = `T::min_value()` and y = -1
1322 #[rustc_const_unstable(feature = "const_int_unchecked_arith", issue = "none")]
1323 pub fn unchecked_rem<T>(x: T, y: T) -> T;
1325 /// Performs an unchecked left shift, resulting in undefined behavior when
1326 /// y < 0 or y >= N, where N is the width of T in bits.
1327 #[rustc_const_stable(feature = "const_int_unchecked", since = "1.40.0")]
1328 pub fn unchecked_shl<T>(x: T, y: T) -> T;
1329 /// Performs an unchecked right shift, resulting in undefined behavior when
1330 /// y < 0 or y >= N, where N is the width of T in bits.
1331 #[rustc_const_stable(feature = "const_int_unchecked", since = "1.40.0")]
1332 pub fn unchecked_shr<T>(x: T, y: T) -> T;
1334 /// Returns the result of an unchecked addition, resulting in
1335 /// undefined behavior when `x + y > T::max_value()` or `x + y < T::min_value()`.
1336 #[rustc_const_unstable(feature = "const_int_unchecked_arith", issue = "none")]
1337 pub fn unchecked_add<T>(x: T, y: T) -> T;
1339 /// Returns the result of an unchecked subtraction, resulting in
1340 /// undefined behavior when `x - y > T::max_value()` or `x - y < T::min_value()`.
1341 #[rustc_const_unstable(feature = "const_int_unchecked_arith", issue = "none")]
1342 pub fn unchecked_sub<T>(x: T, y: T) -> T;
1344 /// Returns the result of an unchecked multiplication, resulting in
1345 /// undefined behavior when `x * y > T::max_value()` or `x * y < T::min_value()`.
1346 #[rustc_const_unstable(feature = "const_int_unchecked_arith", issue = "none")]
1347 pub fn unchecked_mul<T>(x: T, y: T) -> T;
1349 /// Performs rotate left.
1350 /// The stabilized versions of this intrinsic are available on the integer
1351 /// primitives via the `rotate_left` method. For example,
1352 /// [`std::u32::rotate_left`](../../std/primitive.u32.html#method.rotate_left)
1353 #[rustc_const_stable(feature = "const_int_rotate", since = "1.40.0")]
1354 pub fn rotate_left<T>(x: T, y: T) -> T;
1356 /// Performs rotate right.
1357 /// The stabilized versions of this intrinsic are available on the integer
1358 /// primitives via the `rotate_right` method. For example,
1359 /// [`std::u32::rotate_right`](../../std/primitive.u32.html#method.rotate_right)
1360 #[rustc_const_stable(feature = "const_int_rotate", since = "1.40.0")]
1361 pub fn rotate_right<T>(x: T, y: 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_add` method. For example,
1366 /// [`std::u32::wrapping_add`](../../std/primitive.u32.html#method.wrapping_add)
1367 #[rustc_const_stable(feature = "const_int_wrapping", since = "1.40.0")]
1368 pub fn wrapping_add<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_sub` method. For example,
1372 /// [`std::u32::wrapping_sub`](../../std/primitive.u32.html#method.wrapping_sub)
1373 #[rustc_const_stable(feature = "const_int_wrapping", since = "1.40.0")]
1374 pub fn wrapping_sub<T>(a: T, b: T) -> T;
1375 /// Returns (a * b) mod 2<sup>N</sup>, where N is the width of T in bits.
1376 /// The stabilized versions of this intrinsic are available on the integer
1377 /// primitives via the `wrapping_mul` method. For example,
1378 /// [`std::u32::wrapping_mul`](../../std/primitive.u32.html#method.wrapping_mul)
1379 #[rustc_const_stable(feature = "const_int_wrapping", since = "1.40.0")]
1380 pub fn wrapping_mul<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_add` method. For example,
1385 /// [`std::u32::saturating_add`](../../std/primitive.u32.html#method.saturating_add)
1386 #[rustc_const_stable(feature = "const_int_saturating", since = "1.40.0")]
1387 pub fn saturating_add<T>(a: T, b: T) -> T;
1388 /// Computes `a - b`, while saturating at numeric bounds.
1389 /// The stabilized versions of this intrinsic are available on the integer
1390 /// primitives via the `saturating_sub` method. For example,
1391 /// [`std::u32::saturating_sub`](../../std/primitive.u32.html#method.saturating_sub)
1392 #[rustc_const_stable(feature = "const_int_saturating", since = "1.40.0")]
1393 pub fn saturating_sub<T>(a: T, b: T) -> T;
1395 /// Returns the value of the discriminant for the variant in 'v',
1396 /// cast to a `u64`; if `T` has no discriminant, returns 0.
1397 pub fn discriminant_value<T>(v: &T) -> u64;
1399 /// Rust's "try catch" construct which invokes the function pointer `f` with
1400 /// the data pointer `data`.
1402 /// The third pointer is a target-specific data pointer which is filled in
1403 /// with the specifics of the exception that occurred. For examples on Unix
1404 /// platforms this is a `*mut *mut T` which is filled in by the compiler and
1405 /// on MSVC it's `*mut [usize; 2]`. For more information see the compiler's
1406 /// source as well as std's catch implementation.
1407 pub fn r#try(f: fn(*mut u8), data: *mut u8, local_ptr: *mut u8) -> i32;
1409 /// Emits a `!nontemporal` store according to LLVM (see their docs).
1410 /// Probably will never become stable.
1411 pub fn nontemporal_store<T>(ptr: *mut T, val: T);
1413 /// See documentation of `<*const T>::offset_from` for details.
1414 #[rustc_const_unstable(feature = "const_ptr_offset_from", issue = "none")]
1415 pub fn ptr_offset_from<T>(ptr: *const T, base: *const T) -> isize;
1417 /// Internal hook used by Miri to implement unwinding.
1418 /// Compiles to a NOP during non-Miri codegen.
1420 /// Perma-unstable: do not use
1421 pub fn miri_start_panic(data: *mut (dyn crate::any::Any + crate::marker::Send)) -> ();
1424 // Some functions are defined here because they accidentally got made
1425 // available in this module on stable. See <https://github.com/rust-lang/rust/issues/15702>.
1426 // (`transmute` also falls into this category, but it cannot be wrapped due to the
1427 // check that `T` and `U` have the same size.)
1429 /// Checks whether `ptr` is properly aligned with respect to
1430 /// `align_of::<T>()`.
1431 pub(crate) fn is_aligned_and_not_null<T>(ptr: *const T) -> bool {
1432 !ptr.is_null() && ptr as usize % mem::align_of::<T>() == 0
1435 /// Checks whether the regions of memory starting at `src` and `dst` of size
1436 /// `count * size_of::<T>()` do *not* overlap.
1437 pub(crate) fn is_nonoverlapping<T>(src: *const T, dst: *const T, count: usize) -> bool {
1438 let src_usize = src as usize;
1439 let dst_usize = dst as usize;
1440 let size = mem::size_of::<T>().checked_mul(count).unwrap();
1441 let diff = if src_usize > dst_usize { src_usize - dst_usize } else { dst_usize - src_usize };
1442 // If the absolute distance between the ptrs is at least as big as the size of the buffer,
1443 // they do not overlap.
1447 /// Copies `count * size_of::<T>()` bytes from `src` to `dst`. The source
1448 /// and destination must *not* overlap.
1450 /// For regions of memory which might overlap, use [`copy`] instead.
1452 /// `copy_nonoverlapping` is semantically equivalent to C's [`memcpy`], but
1453 /// with the argument order swapped.
1455 /// [`copy`]: ./fn.copy.html
1456 /// [`memcpy`]: https://en.cppreference.com/w/c/string/byte/memcpy
1460 /// Behavior is undefined if any of the following conditions are violated:
1462 /// * `src` must be [valid] for reads of `count * size_of::<T>()` bytes.
1464 /// * `dst` must be [valid] for writes of `count * size_of::<T>()` bytes.
1466 /// * Both `src` and `dst` must be properly aligned.
1468 /// * The region of memory beginning at `src` with a size of `count *
1469 /// size_of::<T>()` bytes must *not* overlap with the region of memory
1470 /// beginning at `dst` with the same size.
1472 /// Like [`read`], `copy_nonoverlapping` creates a bitwise copy of `T`, regardless of
1473 /// whether `T` is [`Copy`]. If `T` is not [`Copy`], using *both* the values
1474 /// in the region beginning at `*src` and the region beginning at `*dst` can
1475 /// [violate memory safety][read-ownership].
1477 /// Note that even if the effectively copied size (`count * size_of::<T>()`) is
1478 /// `0`, the pointers must be non-NULL and properly aligned.
1480 /// [`Copy`]: ../marker/trait.Copy.html
1481 /// [`read`]: ../ptr/fn.read.html
1482 /// [read-ownership]: ../ptr/fn.read.html#ownership-of-the-returned-value
1483 /// [valid]: ../ptr/index.html#safety
1487 /// Manually implement [`Vec::append`]:
1492 /// /// Moves all the elements of `src` into `dst`, leaving `src` empty.
1493 /// fn append<T>(dst: &mut Vec<T>, src: &mut Vec<T>) {
1494 /// let src_len = src.len();
1495 /// let dst_len = dst.len();
1497 /// // Ensure that `dst` has enough capacity to hold all of `src`.
1498 /// dst.reserve(src_len);
1501 /// // The call to offset is always safe because `Vec` will never
1502 /// // allocate more than `isize::MAX` bytes.
1503 /// let dst_ptr = dst.as_mut_ptr().offset(dst_len as isize);
1504 /// let src_ptr = src.as_ptr();
1506 /// // Truncate `src` without dropping its contents. We do this first,
1507 /// // to avoid problems in case something further down panics.
1510 /// // The two regions cannot overlap because mutable references do
1511 /// // not alias, and two different vectors cannot own the same
1513 /// ptr::copy_nonoverlapping(src_ptr, dst_ptr, src_len);
1515 /// // Notify `dst` that it now holds the contents of `src`.
1516 /// dst.set_len(dst_len + src_len);
1520 /// let mut a = vec!['r'];
1521 /// let mut b = vec!['u', 's', 't'];
1523 /// append(&mut a, &mut b);
1525 /// assert_eq!(a, &['r', 'u', 's', 't']);
1526 /// assert!(b.is_empty());
1529 /// [`Vec::append`]: ../../std/vec/struct.Vec.html#method.append
1530 #[doc(alias = "memcpy")]
1531 #[stable(feature = "rust1", since = "1.0.0")]
1533 pub unsafe fn copy_nonoverlapping<T>(src: *const T, dst: *mut T, count: usize) {
1534 extern "rust-intrinsic" {
1535 fn copy_nonoverlapping<T>(src: *const T, dst: *mut T, count: usize);
1538 debug_assert!(is_aligned_and_not_null(src), "attempt to copy from unaligned or null pointer");
1539 debug_assert!(is_aligned_and_not_null(dst), "attempt to copy to unaligned or null pointer");
1540 debug_assert!(is_nonoverlapping(src, dst, count), "attempt to copy to overlapping memory");
1541 copy_nonoverlapping(src, dst, count)
1544 /// Copies `count * size_of::<T>()` bytes from `src` to `dst`. The source
1545 /// and destination may overlap.
1547 /// If the source and destination will *never* overlap,
1548 /// [`copy_nonoverlapping`] can be used instead.
1550 /// `copy` is semantically equivalent to C's [`memmove`], but with the argument
1551 /// order swapped. Copying takes place as if the bytes were copied from `src`
1552 /// to a temporary array and then copied from the array to `dst`.
1554 /// [`copy_nonoverlapping`]: ./fn.copy_nonoverlapping.html
1555 /// [`memmove`]: https://en.cppreference.com/w/c/string/byte/memmove
1559 /// Behavior is undefined if any of the following conditions are violated:
1561 /// * `src` must be [valid] for reads of `count * size_of::<T>()` bytes.
1563 /// * `dst` must be [valid] for writes of `count * size_of::<T>()` bytes.
1565 /// * Both `src` and `dst` must be properly aligned.
1567 /// Like [`read`], `copy` creates a bitwise copy of `T`, regardless of
1568 /// whether `T` is [`Copy`]. If `T` is not [`Copy`], using both the values
1569 /// in the region beginning at `*src` and the region beginning at `*dst` can
1570 /// [violate memory safety][read-ownership].
1572 /// Note that even if the effectively copied size (`count * size_of::<T>()`) is
1573 /// `0`, the pointers must be non-NULL and properly aligned.
1575 /// [`Copy`]: ../marker/trait.Copy.html
1576 /// [`read`]: ../ptr/fn.read.html
1577 /// [read-ownership]: ../ptr/fn.read.html#ownership-of-the-returned-value
1578 /// [valid]: ../ptr/index.html#safety
1582 /// Efficiently create a Rust vector from an unsafe buffer:
1587 /// # #[allow(dead_code)]
1588 /// unsafe fn from_buf_raw<T>(ptr: *const T, elts: usize) -> Vec<T> {
1589 /// let mut dst = Vec::with_capacity(elts);
1590 /// dst.set_len(elts);
1591 /// ptr::copy(ptr, dst.as_mut_ptr(), elts);
1595 #[doc(alias = "memmove")]
1596 #[stable(feature = "rust1", since = "1.0.0")]
1598 pub unsafe fn copy<T>(src: *const T, dst: *mut T, count: usize) {
1599 extern "rust-intrinsic" {
1600 fn copy<T>(src: *const T, dst: *mut T, count: usize);
1603 debug_assert!(is_aligned_and_not_null(src), "attempt to copy from unaligned or null pointer");
1604 debug_assert!(is_aligned_and_not_null(dst), "attempt to copy to unaligned or null pointer");
1605 copy(src, dst, count)
1608 /// Sets `count * size_of::<T>()` bytes of memory starting at `dst` to
1611 /// `write_bytes` is similar to C's [`memset`], but sets `count *
1612 /// size_of::<T>()` bytes to `val`.
1614 /// [`memset`]: https://en.cppreference.com/w/c/string/byte/memset
1618 /// Behavior is undefined if any of the following conditions are violated:
1620 /// * `dst` must be [valid] for writes of `count * size_of::<T>()` bytes.
1622 /// * `dst` must be properly aligned.
1624 /// Additionally, the caller must ensure that writing `count *
1625 /// size_of::<T>()` bytes to the given region of memory results in a valid
1626 /// value of `T`. Using a region of memory typed as a `T` that contains an
1627 /// invalid value of `T` is undefined behavior.
1629 /// Note that even if the effectively copied size (`count * size_of::<T>()`) is
1630 /// `0`, the pointer must be non-NULL and properly aligned.
1632 /// [valid]: ../ptr/index.html#safety
1641 /// let mut vec = vec![0u32; 4];
1643 /// let vec_ptr = vec.as_mut_ptr();
1644 /// ptr::write_bytes(vec_ptr, 0xfe, 2);
1646 /// assert_eq!(vec, [0xfefefefe, 0xfefefefe, 0, 0]);
1649 /// Creating an invalid value:
1654 /// let mut v = Box::new(0i32);
1657 /// // Leaks the previously held value by overwriting the `Box<T>` with
1658 /// // a null pointer.
1659 /// ptr::write_bytes(&mut v as *mut Box<i32>, 0, 1);
1662 /// // At this point, using or dropping `v` results in undefined behavior.
1663 /// // drop(v); // ERROR
1665 /// // Even leaking `v` "uses" it, and hence is undefined behavior.
1666 /// // mem::forget(v); // ERROR
1668 /// // In fact, `v` is invalid according to basic type layout invariants, so *any*
1669 /// // operation touching it is undefined behavior.
1670 /// // let v2 = v; // ERROR
1673 /// // Let us instead put in a valid value
1674 /// ptr::write(&mut v as *mut Box<i32>, Box::new(42i32));
1677 /// // Now the box is fine
1678 /// assert_eq!(*v, 42);
1680 #[stable(feature = "rust1", since = "1.0.0")]
1682 pub unsafe fn write_bytes<T>(dst: *mut T, val: u8, count: usize) {
1683 extern "rust-intrinsic" {
1684 fn write_bytes<T>(dst: *mut T, val: u8, count: usize);
1687 debug_assert!(is_aligned_and_not_null(dst), "attempt to write to unaligned or null pointer");
1688 write_bytes(dst, val, count)