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 is Undefined Behavior, and a bad idea.
885 /// // However, it is no-copy.
886 /// let v_transmuted = unsafe {
887 /// std::mem::transmute::<Vec<&i32>, Vec<Option<&i32>>>(v_clone)
890 /// let v_clone = v_orig.clone();
892 /// // This is the suggested, safe way.
893 /// // It does copy the entire vector, though, into a new array.
894 /// let v_collected = v_clone.into_iter()
896 /// .collect::<Vec<Option<&i32>>>();
898 /// let v_clone = v_orig.clone();
900 /// // The no-copy, unsafe way, still using transmute, but not UB.
901 /// // This is equivalent to the original, but safer, and reuses the
902 /// // same `Vec` internals. Therefore, the new inner type must have the
903 /// // exact same size, and the same alignment, as the old type.
904 /// // The same caveats exist for this method as transmute, for
905 /// // the original inner type (`&i32`) to the converted inner type
906 /// // (`Option<&i32>`), so read the nomicon pages linked above.
907 /// let v_from_raw = unsafe {
908 // FIXME Update this when vec_into_raw_parts is stabilized
909 /// // Ensure the original vector is not dropped.
910 /// let mut v_clone = std::mem::ManuallyDrop::new(v_clone);
911 /// Vec::from_raw_parts(v_clone.as_mut_ptr() as *mut Option<&i32>,
913 /// v_clone.capacity())
917 /// Implementing `split_at_mut`:
920 /// use std::{slice, mem};
922 /// // There are multiple ways to do this, and there are multiple problems
923 /// // with the following (transmute) way.
924 /// fn split_at_mut_transmute<T>(slice: &mut [T], mid: usize)
925 /// -> (&mut [T], &mut [T]) {
926 /// let len = slice.len();
927 /// assert!(mid <= len);
929 /// let slice2 = mem::transmute::<&mut [T], &mut [T]>(slice);
930 /// // first: transmute is not typesafe; all it checks is that T and
931 /// // U are of the same size. Second, right here, you have two
932 /// // mutable references pointing to the same memory.
933 /// (&mut slice[0..mid], &mut slice2[mid..len])
937 /// // This gets rid of the typesafety problems; `&mut *` will *only* give
938 /// // you an `&mut T` from an `&mut T` or `*mut T`.
939 /// fn split_at_mut_casts<T>(slice: &mut [T], mid: usize)
940 /// -> (&mut [T], &mut [T]) {
941 /// let len = slice.len();
942 /// assert!(mid <= len);
944 /// let slice2 = &mut *(slice as *mut [T]);
945 /// // however, you still have two mutable references pointing to
946 /// // the same memory.
947 /// (&mut slice[0..mid], &mut slice2[mid..len])
951 /// // This is how the standard library does it. This is the best method, if
952 /// // you need to do something like this
953 /// fn split_at_stdlib<T>(slice: &mut [T], mid: usize)
954 /// -> (&mut [T], &mut [T]) {
955 /// let len = slice.len();
956 /// assert!(mid <= len);
958 /// let ptr = slice.as_mut_ptr();
959 /// // This now has three mutable references pointing at the same
960 /// // memory. `slice`, the rvalue ret.0, and the rvalue ret.1.
961 /// // `slice` is never used after `let ptr = ...`, and so one can
962 /// // treat it as "dead", and therefore, you only have two real
963 /// // mutable slices.
964 /// (slice::from_raw_parts_mut(ptr, mid),
965 /// slice::from_raw_parts_mut(ptr.add(mid), len - mid))
969 #[stable(feature = "rust1", since = "1.0.0")]
970 #[rustc_const_unstable(feature = "const_transmute", issue = "53605")]
971 pub fn transmute<T, U>(e: T) -> U;
973 /// Returns `true` if the actual type given as `T` requires drop
974 /// glue; returns `false` if the actual type provided for `T`
975 /// implements `Copy`.
977 /// If the actual type neither requires drop glue nor implements
978 /// `Copy`, then may return `true` or `false`.
980 /// The stabilized version of this intrinsic is
981 /// [`std::mem::needs_drop`](../../std/mem/fn.needs_drop.html).
982 #[rustc_const_stable(feature = "const_needs_drop", since = "1.40.0")]
983 pub fn needs_drop<T>() -> bool;
985 /// Calculates the offset from a pointer.
987 /// This is implemented as an intrinsic to avoid converting to and from an
988 /// integer, since the conversion would throw away aliasing information.
992 /// Both the starting and resulting pointer must be either in bounds or one
993 /// byte past the end of an allocated object. If either pointer is out of
994 /// bounds or arithmetic overflow occurs then any further use of the
995 /// returned value will result in undefined behavior.
996 pub fn offset<T>(dst: *const T, offset: isize) -> *const T;
998 /// Calculates the offset from a pointer, potentially wrapping.
1000 /// This is implemented as an intrinsic to avoid converting to and from an
1001 /// integer, since the conversion inhibits certain optimizations.
1005 /// Unlike the `offset` intrinsic, this intrinsic does not restrict the
1006 /// resulting pointer to point into or one byte past the end of an allocated
1007 /// object, and it wraps with two's complement arithmetic. The resulting
1008 /// value is not necessarily valid to be used to actually access memory.
1009 pub fn arith_offset<T>(dst: *const T, offset: isize) -> *const T;
1011 /// Equivalent to the appropriate `llvm.memcpy.p0i8.0i8.*` intrinsic, with
1012 /// a size of `count` * `size_of::<T>()` and an alignment of
1013 /// `min_align_of::<T>()`
1015 /// The volatile parameter is set to `true`, so it will not be optimized out
1016 /// unless size is equal to zero.
1017 pub fn volatile_copy_nonoverlapping_memory<T>(dst: *mut T, src: *const T, count: usize);
1018 /// Equivalent to the appropriate `llvm.memmove.p0i8.0i8.*` intrinsic, with
1019 /// a size of `count` * `size_of::<T>()` and an alignment of
1020 /// `min_align_of::<T>()`
1022 /// The volatile parameter is set to `true`, so it will not be optimized out
1023 /// unless size is equal to zero.
1024 pub fn volatile_copy_memory<T>(dst: *mut T, src: *const T, count: usize);
1025 /// Equivalent to the appropriate `llvm.memset.p0i8.*` intrinsic, with a
1026 /// size of `count` * `size_of::<T>()` and an alignment of
1027 /// `min_align_of::<T>()`.
1029 /// The volatile parameter is set to `true`, so it will not be optimized out
1030 /// unless size is equal to zero.
1031 pub fn volatile_set_memory<T>(dst: *mut T, val: u8, count: usize);
1033 /// Performs a volatile load from the `src` pointer.
1034 /// The stabilized version of this intrinsic is
1035 /// [`std::ptr::read_volatile`](../../std/ptr/fn.read_volatile.html).
1036 pub fn volatile_load<T>(src: *const T) -> T;
1037 /// Performs a volatile store to the `dst` pointer.
1038 /// The stabilized version of this intrinsic is
1039 /// [`std::ptr::write_volatile`](../../std/ptr/fn.write_volatile.html).
1040 pub fn volatile_store<T>(dst: *mut T, val: T);
1042 /// Performs a volatile load from the `src` pointer
1043 /// The pointer is not required to be aligned.
1044 pub fn unaligned_volatile_load<T>(src: *const T) -> T;
1045 /// Performs a volatile store to the `dst` pointer.
1046 /// The pointer is not required to be aligned.
1047 pub fn unaligned_volatile_store<T>(dst: *mut T, val: T);
1049 /// Returns the square root of an `f32`
1050 pub fn sqrtf32(x: f32) -> f32;
1051 /// Returns the square root of an `f64`
1052 pub fn sqrtf64(x: f64) -> f64;
1054 /// Raises an `f32` to an integer power.
1055 pub fn powif32(a: f32, x: i32) -> f32;
1056 /// Raises an `f64` to an integer power.
1057 pub fn powif64(a: f64, x: i32) -> f64;
1059 /// Returns the sine of an `f32`.
1060 pub fn sinf32(x: f32) -> f32;
1061 /// Returns the sine of an `f64`.
1062 pub fn sinf64(x: f64) -> f64;
1064 /// Returns the cosine of an `f32`.
1065 pub fn cosf32(x: f32) -> f32;
1066 /// Returns the cosine of an `f64`.
1067 pub fn cosf64(x: f64) -> f64;
1069 /// Raises an `f32` to an `f32` power.
1070 pub fn powf32(a: f32, x: f32) -> f32;
1071 /// Raises an `f64` to an `f64` power.
1072 pub fn powf64(a: f64, x: f64) -> f64;
1074 /// Returns the exponential of an `f32`.
1075 pub fn expf32(x: f32) -> f32;
1076 /// Returns the exponential of an `f64`.
1077 pub fn expf64(x: f64) -> f64;
1079 /// Returns 2 raised to the power of an `f32`.
1080 pub fn exp2f32(x: f32) -> f32;
1081 /// Returns 2 raised to the power of an `f64`.
1082 pub fn exp2f64(x: f64) -> f64;
1084 /// Returns the natural logarithm of an `f32`.
1085 pub fn logf32(x: f32) -> f32;
1086 /// Returns the natural logarithm of an `f64`.
1087 pub fn logf64(x: f64) -> f64;
1089 /// Returns the base 10 logarithm of an `f32`.
1090 pub fn log10f32(x: f32) -> f32;
1091 /// Returns the base 10 logarithm of an `f64`.
1092 pub fn log10f64(x: f64) -> f64;
1094 /// Returns the base 2 logarithm of an `f32`.
1095 pub fn log2f32(x: f32) -> f32;
1096 /// Returns the base 2 logarithm of an `f64`.
1097 pub fn log2f64(x: f64) -> f64;
1099 /// Returns `a * b + c` for `f32` values.
1100 pub fn fmaf32(a: f32, b: f32, c: f32) -> f32;
1101 /// Returns `a * b + c` for `f64` values.
1102 pub fn fmaf64(a: f64, b: f64, c: f64) -> f64;
1104 /// Returns the absolute value of an `f32`.
1105 pub fn fabsf32(x: f32) -> f32;
1106 /// Returns the absolute value of an `f64`.
1107 pub fn fabsf64(x: f64) -> f64;
1109 /// Returns the minimum of two `f32` values.
1110 pub fn minnumf32(x: f32, y: f32) -> f32;
1111 /// Returns the minimum of two `f64` values.
1112 pub fn minnumf64(x: f64, y: f64) -> f64;
1113 /// Returns the maximum of two `f32` values.
1114 pub fn maxnumf32(x: f32, y: f32) -> f32;
1115 /// Returns the maximum of two `f64` values.
1116 pub fn maxnumf64(x: f64, y: f64) -> f64;
1118 /// Copies the sign from `y` to `x` for `f32` values.
1119 pub fn copysignf32(x: f32, y: f32) -> f32;
1120 /// Copies the sign from `y` to `x` for `f64` values.
1121 pub fn copysignf64(x: f64, y: f64) -> f64;
1123 /// Returns the largest integer less than or equal to an `f32`.
1124 pub fn floorf32(x: f32) -> f32;
1125 /// Returns the largest integer less than or equal to an `f64`.
1126 pub fn floorf64(x: f64) -> f64;
1128 /// Returns the smallest integer greater than or equal to an `f32`.
1129 pub fn ceilf32(x: f32) -> f32;
1130 /// Returns the smallest integer greater than or equal to an `f64`.
1131 pub fn ceilf64(x: f64) -> f64;
1133 /// Returns the integer part of an `f32`.
1134 pub fn truncf32(x: f32) -> f32;
1135 /// Returns the integer part of an `f64`.
1136 pub fn truncf64(x: f64) -> f64;
1138 /// Returns the nearest integer to an `f32`. May raise an inexact floating-point exception
1139 /// if the argument is not an integer.
1140 pub fn rintf32(x: f32) -> f32;
1141 /// Returns the nearest integer to an `f64`. May raise an inexact floating-point exception
1142 /// if the argument is not an integer.
1143 pub fn rintf64(x: f64) -> f64;
1145 /// Returns the nearest integer to an `f32`.
1146 pub fn nearbyintf32(x: f32) -> f32;
1147 /// Returns the nearest integer to an `f64`.
1148 pub fn nearbyintf64(x: f64) -> f64;
1150 /// Returns the nearest integer to an `f32`. Rounds half-way cases away from zero.
1151 pub fn roundf32(x: f32) -> f32;
1152 /// Returns the nearest integer to an `f64`. Rounds half-way cases away from zero.
1153 pub fn roundf64(x: f64) -> f64;
1155 /// Float addition that allows optimizations based on algebraic rules.
1156 /// May assume inputs are finite.
1157 pub fn fadd_fast<T>(a: T, b: T) -> T;
1159 /// Float subtraction that allows optimizations based on algebraic rules.
1160 /// May assume inputs are finite.
1161 pub fn fsub_fast<T>(a: T, b: T) -> T;
1163 /// Float multiplication that allows optimizations based on algebraic rules.
1164 /// May assume inputs are finite.
1165 pub fn fmul_fast<T>(a: T, b: T) -> T;
1167 /// Float division that allows optimizations based on algebraic rules.
1168 /// May assume inputs are finite.
1169 pub fn fdiv_fast<T>(a: T, b: T) -> T;
1171 /// Float remainder that allows optimizations based on algebraic rules.
1172 /// May assume inputs are finite.
1173 pub fn frem_fast<T>(a: T, b: T) -> T;
1175 /// Convert with LLVM’s fptoui/fptosi, which may return undef for values out of range
1176 /// https://github.com/rust-lang/rust/issues/10184
1177 pub fn float_to_int_approx_unchecked<Float, Int>(value: Float) -> Int;
1179 /// Returns the number of bits set in an integer type `T`
1180 #[rustc_const_stable(feature = "const_ctpop", since = "1.40.0")]
1181 pub fn ctpop<T>(x: T) -> T;
1183 /// Returns the number of leading unset bits (zeroes) in an integer type `T`.
1188 /// #![feature(core_intrinsics)]
1190 /// use std::intrinsics::ctlz;
1192 /// let x = 0b0001_1100_u8;
1193 /// let num_leading = ctlz(x);
1194 /// assert_eq!(num_leading, 3);
1197 /// An `x` with value `0` will return the bit width of `T`.
1200 /// #![feature(core_intrinsics)]
1202 /// use std::intrinsics::ctlz;
1205 /// let num_leading = ctlz(x);
1206 /// assert_eq!(num_leading, 16);
1208 #[rustc_const_stable(feature = "const_ctlz", since = "1.40.0")]
1209 pub fn ctlz<T>(x: T) -> T;
1211 /// Like `ctlz`, but extra-unsafe as it returns `undef` when
1212 /// given an `x` with value `0`.
1217 /// #![feature(core_intrinsics)]
1219 /// use std::intrinsics::ctlz_nonzero;
1221 /// let x = 0b0001_1100_u8;
1222 /// let num_leading = unsafe { ctlz_nonzero(x) };
1223 /// assert_eq!(num_leading, 3);
1225 #[rustc_const_unstable(feature = "constctlz", issue = "none")]
1226 pub fn ctlz_nonzero<T>(x: T) -> T;
1228 /// Returns the number of trailing unset bits (zeroes) in an integer type `T`.
1233 /// #![feature(core_intrinsics)]
1235 /// use std::intrinsics::cttz;
1237 /// let x = 0b0011_1000_u8;
1238 /// let num_trailing = cttz(x);
1239 /// assert_eq!(num_trailing, 3);
1242 /// An `x` with value `0` will return the bit width of `T`:
1245 /// #![feature(core_intrinsics)]
1247 /// use std::intrinsics::cttz;
1250 /// let num_trailing = cttz(x);
1251 /// assert_eq!(num_trailing, 16);
1253 #[rustc_const_stable(feature = "const_cttz", since = "1.40.0")]
1254 pub fn cttz<T>(x: T) -> T;
1256 /// Like `cttz`, but extra-unsafe as it returns `undef` when
1257 /// given an `x` with value `0`.
1262 /// #![feature(core_intrinsics)]
1264 /// use std::intrinsics::cttz_nonzero;
1266 /// let x = 0b0011_1000_u8;
1267 /// let num_trailing = unsafe { cttz_nonzero(x) };
1268 /// assert_eq!(num_trailing, 3);
1270 #[rustc_const_unstable(feature = "const_cttz", issue = "none")]
1271 pub fn cttz_nonzero<T>(x: T) -> T;
1273 /// Reverses the bytes in an integer type `T`.
1274 #[rustc_const_stable(feature = "const_bswap", since = "1.40.0")]
1275 pub fn bswap<T>(x: T) -> T;
1277 /// Reverses the bits in an integer type `T`.
1278 #[rustc_const_stable(feature = "const_bitreverse", since = "1.40.0")]
1279 pub fn bitreverse<T>(x: T) -> T;
1281 /// Performs checked integer addition.
1282 /// The stabilized versions of this intrinsic are available on the integer
1283 /// primitives via the `overflowing_add` method. For example,
1284 /// [`std::u32::overflowing_add`](../../std/primitive.u32.html#method.overflowing_add)
1285 #[rustc_const_stable(feature = "const_int_overflow", since = "1.40.0")]
1286 pub fn add_with_overflow<T>(x: T, y: T) -> (T, bool);
1288 /// Performs checked integer subtraction
1289 /// The stabilized versions of this intrinsic are available on the integer
1290 /// primitives via the `overflowing_sub` method. For example,
1291 /// [`std::u32::overflowing_sub`](../../std/primitive.u32.html#method.overflowing_sub)
1292 #[rustc_const_stable(feature = "const_int_overflow", since = "1.40.0")]
1293 pub fn sub_with_overflow<T>(x: T, y: T) -> (T, bool);
1295 /// Performs checked integer multiplication
1296 /// The stabilized versions of this intrinsic are available on the integer
1297 /// primitives via the `overflowing_mul` method. For example,
1298 /// [`std::u32::overflowing_mul`](../../std/primitive.u32.html#method.overflowing_mul)
1299 #[rustc_const_stable(feature = "const_int_overflow", since = "1.40.0")]
1300 pub fn mul_with_overflow<T>(x: T, y: T) -> (T, bool);
1302 /// Performs an exact division, resulting in undefined behavior where
1303 /// `x % y != 0` or `y == 0` or `x == T::min_value() && y == -1`
1304 pub fn exact_div<T>(x: T, y: T) -> T;
1306 /// Performs an unchecked division, resulting in undefined behavior
1307 /// where y = 0 or x = `T::min_value()` and y = -1
1308 #[rustc_const_unstable(feature = "const_int_unchecked_arith", issue = "none")]
1309 pub fn unchecked_div<T>(x: T, y: T) -> T;
1310 /// Returns the remainder of an unchecked division, resulting in
1311 /// undefined behavior 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_rem<T>(x: T, y: T) -> T;
1315 /// Performs an unchecked left shift, resulting in undefined behavior when
1316 /// y < 0 or y >= N, where N is the width of T in bits.
1317 #[rustc_const_stable(feature = "const_int_unchecked", since = "1.40.0")]
1318 pub fn unchecked_shl<T>(x: T, y: T) -> T;
1319 /// Performs an unchecked right 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_shr<T>(x: T, y: T) -> T;
1324 /// Returns the result of an unchecked addition, resulting in
1325 /// undefined behavior when `x + y > T::max_value()` or `x + y < T::min_value()`.
1326 #[rustc_const_unstable(feature = "const_int_unchecked_arith", issue = "none")]
1327 pub fn unchecked_add<T>(x: T, y: T) -> T;
1329 /// Returns the result of an unchecked subtraction, resulting in
1330 /// undefined behavior when `x - y > T::max_value()` or `x - y < T::min_value()`.
1331 #[rustc_const_unstable(feature = "const_int_unchecked_arith", issue = "none")]
1332 pub fn unchecked_sub<T>(x: T, y: T) -> T;
1334 /// Returns the result of an unchecked multiplication, 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_mul<T>(x: T, y: T) -> T;
1339 /// Performs rotate left.
1340 /// The stabilized versions of this intrinsic are available on the integer
1341 /// primitives via the `rotate_left` method. For example,
1342 /// [`std::u32::rotate_left`](../../std/primitive.u32.html#method.rotate_left)
1343 #[rustc_const_stable(feature = "const_int_rotate", since = "1.40.0")]
1344 pub fn rotate_left<T>(x: T, y: T) -> T;
1346 /// Performs rotate right.
1347 /// The stabilized versions of this intrinsic are available on the integer
1348 /// primitives via the `rotate_right` method. For example,
1349 /// [`std::u32::rotate_right`](../../std/primitive.u32.html#method.rotate_right)
1350 #[rustc_const_stable(feature = "const_int_rotate", since = "1.40.0")]
1351 pub fn rotate_right<T>(x: T, y: T) -> T;
1353 /// Returns (a + b) mod 2<sup>N</sup>, where N is the width of T in bits.
1354 /// The stabilized versions of this intrinsic are available on the integer
1355 /// primitives via the `wrapping_add` method. For example,
1356 /// [`std::u32::wrapping_add`](../../std/primitive.u32.html#method.wrapping_add)
1357 #[rustc_const_stable(feature = "const_int_wrapping", since = "1.40.0")]
1358 pub fn wrapping_add<T>(a: T, b: T) -> T;
1359 /// Returns (a - b) mod 2<sup>N</sup>, where N is the width of T in bits.
1360 /// The stabilized versions of this intrinsic are available on the integer
1361 /// primitives via the `wrapping_sub` method. For example,
1362 /// [`std::u32::wrapping_sub`](../../std/primitive.u32.html#method.wrapping_sub)
1363 #[rustc_const_stable(feature = "const_int_wrapping", since = "1.40.0")]
1364 pub fn wrapping_sub<T>(a: T, b: T) -> T;
1365 /// Returns (a * b) mod 2<sup>N</sup>, where N is the width of T in bits.
1366 /// The stabilized versions of this intrinsic are available on the integer
1367 /// primitives via the `wrapping_mul` method. For example,
1368 /// [`std::u32::wrapping_mul`](../../std/primitive.u32.html#method.wrapping_mul)
1369 #[rustc_const_stable(feature = "const_int_wrapping", since = "1.40.0")]
1370 pub fn wrapping_mul<T>(a: T, b: T) -> T;
1372 /// Computes `a + b`, while saturating at numeric bounds.
1373 /// The stabilized versions of this intrinsic are available on the integer
1374 /// primitives via the `saturating_add` method. For example,
1375 /// [`std::u32::saturating_add`](../../std/primitive.u32.html#method.saturating_add)
1376 #[rustc_const_stable(feature = "const_int_saturating", since = "1.40.0")]
1377 pub fn saturating_add<T>(a: T, b: T) -> T;
1378 /// Computes `a - b`, while saturating at numeric bounds.
1379 /// The stabilized versions of this intrinsic are available on the integer
1380 /// primitives via the `saturating_sub` method. For example,
1381 /// [`std::u32::saturating_sub`](../../std/primitive.u32.html#method.saturating_sub)
1382 #[rustc_const_stable(feature = "const_int_saturating", since = "1.40.0")]
1383 pub fn saturating_sub<T>(a: T, b: T) -> T;
1385 /// Returns the value of the discriminant for the variant in 'v',
1386 /// cast to a `u64`; if `T` has no discriminant, returns 0.
1387 pub fn discriminant_value<T>(v: &T) -> u64;
1389 /// Rust's "try catch" construct which invokes the function pointer `f` with
1390 /// the data pointer `data`.
1392 /// The third pointer is a target-specific data pointer which is filled in
1393 /// with the specifics of the exception that occurred. For examples on Unix
1394 /// platforms this is a `*mut *mut T` which is filled in by the compiler and
1395 /// on MSVC it's `*mut [usize; 2]`. For more information see the compiler's
1396 /// source as well as std's catch implementation.
1397 pub fn r#try(f: fn(*mut u8), data: *mut u8, local_ptr: *mut u8) -> i32;
1399 /// Emits a `!nontemporal` store according to LLVM (see their docs).
1400 /// Probably will never become stable.
1401 pub fn nontemporal_store<T>(ptr: *mut T, val: T);
1403 /// See documentation of `<*const T>::offset_from` for details.
1404 #[rustc_const_unstable(feature = "const_ptr_offset_from", issue = "none")]
1405 pub fn ptr_offset_from<T>(ptr: *const T, base: *const T) -> isize;
1407 /// Internal hook used by Miri to implement unwinding.
1408 /// Compiles to a NOP during non-Miri codegen.
1410 /// Perma-unstable: do not use
1411 pub fn miri_start_panic(data: *mut (dyn crate::any::Any + crate::marker::Send)) -> ();
1414 // Some functions are defined here because they accidentally got made
1415 // available in this module on stable. See <https://github.com/rust-lang/rust/issues/15702>.
1416 // (`transmute` also falls into this category, but it cannot be wrapped due to the
1417 // check that `T` and `U` have the same size.)
1419 /// Checks whether `ptr` is properly aligned with respect to
1420 /// `align_of::<T>()`.
1421 pub(crate) fn is_aligned_and_not_null<T>(ptr: *const T) -> bool {
1422 !ptr.is_null() && ptr as usize % mem::align_of::<T>() == 0
1425 /// Checks whether the regions of memory starting at `src` and `dst` of size
1426 /// `count * size_of::<T>()` do *not* overlap.
1427 pub(crate) fn is_nonoverlapping<T>(src: *const T, dst: *const T, count: usize) -> bool {
1428 let src_usize = src as usize;
1429 let dst_usize = dst as usize;
1430 let size = mem::size_of::<T>().checked_mul(count).unwrap();
1431 let diff = if src_usize > dst_usize { src_usize - dst_usize } else { dst_usize - src_usize };
1432 // If the absolute distance between the ptrs is at least as big as the size of the buffer,
1433 // they do not overlap.
1437 /// Copies `count * size_of::<T>()` bytes from `src` to `dst`. The source
1438 /// and destination must *not* overlap.
1440 /// For regions of memory which might overlap, use [`copy`] instead.
1442 /// `copy_nonoverlapping` is semantically equivalent to C's [`memcpy`], but
1443 /// with the argument order swapped.
1445 /// [`copy`]: ./fn.copy.html
1446 /// [`memcpy`]: https://en.cppreference.com/w/c/string/byte/memcpy
1450 /// Behavior is undefined if any of the following conditions are violated:
1452 /// * `src` must be [valid] for reads of `count * size_of::<T>()` bytes.
1454 /// * `dst` must be [valid] for writes of `count * size_of::<T>()` bytes.
1456 /// * Both `src` and `dst` must be properly aligned.
1458 /// * The region of memory beginning at `src` with a size of `count *
1459 /// size_of::<T>()` bytes must *not* overlap with the region of memory
1460 /// beginning at `dst` with the same size.
1462 /// Like [`read`], `copy_nonoverlapping` creates a bitwise copy of `T`, regardless of
1463 /// whether `T` is [`Copy`]. If `T` is not [`Copy`], using *both* the values
1464 /// in the region beginning at `*src` and the region beginning at `*dst` can
1465 /// [violate memory safety][read-ownership].
1467 /// Note that even if the effectively copied size (`count * size_of::<T>()`) is
1468 /// `0`, the pointers must be non-NULL and properly aligned.
1470 /// [`Copy`]: ../marker/trait.Copy.html
1471 /// [`read`]: ../ptr/fn.read.html
1472 /// [read-ownership]: ../ptr/fn.read.html#ownership-of-the-returned-value
1473 /// [valid]: ../ptr/index.html#safety
1477 /// Manually implement [`Vec::append`]:
1482 /// /// Moves all the elements of `src` into `dst`, leaving `src` empty.
1483 /// fn append<T>(dst: &mut Vec<T>, src: &mut Vec<T>) {
1484 /// let src_len = src.len();
1485 /// let dst_len = dst.len();
1487 /// // Ensure that `dst` has enough capacity to hold all of `src`.
1488 /// dst.reserve(src_len);
1491 /// // The call to offset is always safe because `Vec` will never
1492 /// // allocate more than `isize::MAX` bytes.
1493 /// let dst_ptr = dst.as_mut_ptr().offset(dst_len as isize);
1494 /// let src_ptr = src.as_ptr();
1496 /// // Truncate `src` without dropping its contents. We do this first,
1497 /// // to avoid problems in case something further down panics.
1500 /// // The two regions cannot overlap because mutable references do
1501 /// // not alias, and two different vectors cannot own the same
1503 /// ptr::copy_nonoverlapping(src_ptr, dst_ptr, src_len);
1505 /// // Notify `dst` that it now holds the contents of `src`.
1506 /// dst.set_len(dst_len + src_len);
1510 /// let mut a = vec!['r'];
1511 /// let mut b = vec!['u', 's', 't'];
1513 /// append(&mut a, &mut b);
1515 /// assert_eq!(a, &['r', 'u', 's', 't']);
1516 /// assert!(b.is_empty());
1519 /// [`Vec::append`]: ../../std/vec/struct.Vec.html#method.append
1520 #[stable(feature = "rust1", since = "1.0.0")]
1522 pub unsafe fn copy_nonoverlapping<T>(src: *const T, dst: *mut T, count: usize) {
1523 extern "rust-intrinsic" {
1524 fn copy_nonoverlapping<T>(src: *const T, dst: *mut T, count: usize);
1527 debug_assert!(is_aligned_and_not_null(src), "attempt to copy from unaligned or null pointer");
1528 debug_assert!(is_aligned_and_not_null(dst), "attempt to copy to unaligned or null pointer");
1529 debug_assert!(is_nonoverlapping(src, dst, count), "attempt to copy to overlapping memory");
1530 copy_nonoverlapping(src, dst, count)
1533 /// Copies `count * size_of::<T>()` bytes from `src` to `dst`. The source
1534 /// and destination may overlap.
1536 /// If the source and destination will *never* overlap,
1537 /// [`copy_nonoverlapping`] can be used instead.
1539 /// `copy` is semantically equivalent to C's [`memmove`], but with the argument
1540 /// order swapped. Copying takes place as if the bytes were copied from `src`
1541 /// to a temporary array and then copied from the array to `dst`.
1543 /// [`copy_nonoverlapping`]: ./fn.copy_nonoverlapping.html
1544 /// [`memmove`]: https://en.cppreference.com/w/c/string/byte/memmove
1548 /// Behavior is undefined if any of the following conditions are violated:
1550 /// * `src` must be [valid] for reads of `count * size_of::<T>()` bytes.
1552 /// * `dst` must be [valid] for writes of `count * size_of::<T>()` bytes.
1554 /// * Both `src` and `dst` must be properly aligned.
1556 /// Like [`read`], `copy` creates a bitwise copy of `T`, regardless of
1557 /// whether `T` is [`Copy`]. If `T` is not [`Copy`], using both the values
1558 /// in the region beginning at `*src` and the region beginning at `*dst` can
1559 /// [violate memory safety][read-ownership].
1561 /// Note that even if the effectively copied size (`count * size_of::<T>()`) is
1562 /// `0`, the pointers must be non-NULL and properly aligned.
1564 /// [`Copy`]: ../marker/trait.Copy.html
1565 /// [`read`]: ../ptr/fn.read.html
1566 /// [read-ownership]: ../ptr/fn.read.html#ownership-of-the-returned-value
1567 /// [valid]: ../ptr/index.html#safety
1571 /// Efficiently create a Rust vector from an unsafe buffer:
1576 /// # #[allow(dead_code)]
1577 /// unsafe fn from_buf_raw<T>(ptr: *const T, elts: usize) -> Vec<T> {
1578 /// let mut dst = Vec::with_capacity(elts);
1579 /// dst.set_len(elts);
1580 /// ptr::copy(ptr, dst.as_mut_ptr(), elts);
1584 #[stable(feature = "rust1", since = "1.0.0")]
1586 pub unsafe fn copy<T>(src: *const T, dst: *mut T, count: usize) {
1587 extern "rust-intrinsic" {
1588 fn copy<T>(src: *const T, dst: *mut T, count: usize);
1591 debug_assert!(is_aligned_and_not_null(src), "attempt to copy from unaligned or null pointer");
1592 debug_assert!(is_aligned_and_not_null(dst), "attempt to copy to unaligned or null pointer");
1593 copy(src, dst, count)
1596 /// Sets `count * size_of::<T>()` bytes of memory starting at `dst` to
1599 /// `write_bytes` is similar to C's [`memset`], but sets `count *
1600 /// size_of::<T>()` bytes to `val`.
1602 /// [`memset`]: https://en.cppreference.com/w/c/string/byte/memset
1606 /// Behavior is undefined if any of the following conditions are violated:
1608 /// * `dst` must be [valid] for writes of `count * size_of::<T>()` bytes.
1610 /// * `dst` must be properly aligned.
1612 /// Additionally, the caller must ensure that writing `count *
1613 /// size_of::<T>()` bytes to the given region of memory results in a valid
1614 /// value of `T`. Using a region of memory typed as a `T` that contains an
1615 /// invalid value of `T` is undefined behavior.
1617 /// Note that even if the effectively copied size (`count * size_of::<T>()`) is
1618 /// `0`, the pointer must be non-NULL and properly aligned.
1620 /// [valid]: ../ptr/index.html#safety
1629 /// let mut vec = vec![0u32; 4];
1631 /// let vec_ptr = vec.as_mut_ptr();
1632 /// ptr::write_bytes(vec_ptr, 0xfe, 2);
1634 /// assert_eq!(vec, [0xfefefefe, 0xfefefefe, 0, 0]);
1637 /// Creating an invalid value:
1642 /// let mut v = Box::new(0i32);
1645 /// // Leaks the previously held value by overwriting the `Box<T>` with
1646 /// // a null pointer.
1647 /// ptr::write_bytes(&mut v as *mut Box<i32>, 0, 1);
1650 /// // At this point, using or dropping `v` results in undefined behavior.
1651 /// // drop(v); // ERROR
1653 /// // Even leaking `v` "uses" it, and hence is undefined behavior.
1654 /// // mem::forget(v); // ERROR
1656 /// // In fact, `v` is invalid according to basic type layout invariants, so *any*
1657 /// // operation touching it is undefined behavior.
1658 /// // let v2 = v; // ERROR
1661 /// // Let us instead put in a valid value
1662 /// ptr::write(&mut v as *mut Box<i32>, Box::new(42i32));
1665 /// // Now the box is fine
1666 /// assert_eq!(*v, 42);
1668 #[stable(feature = "rust1", since = "1.0.0")]
1670 pub unsafe fn write_bytes<T>(dst: *mut T, val: u8, count: usize) {
1671 extern "rust-intrinsic" {
1672 fn write_bytes<T>(dst: *mut T, val: u8, count: usize);
1675 debug_assert!(is_aligned_and_not_null(dst), "attempt to write to unaligned or null pointer");
1676 write_bytes(dst, val, count)