1 //! Compiler intrinsics.
3 //! The corresponding definitions are in `librustc_codegen_llvm/intrinsic.rs`.
7 //! The volatile intrinsics provide operations intended to act on I/O
8 //! memory, which are guaranteed to not be reordered by the compiler
9 //! across other volatile intrinsics. See the LLVM documentation on
12 //! [volatile]: http://llvm.org/docs/LangRef.html#volatile-memory-accesses
16 //! The atomic intrinsics provide common atomic operations on machine
17 //! words, with multiple possible memory orderings. They obey the same
18 //! semantics as C++11. See the LLVM documentation on [[atomics]].
20 //! [atomics]: http://llvm.org/docs/Atomics.html
22 //! A quick refresher on memory ordering:
24 //! * Acquire - a barrier for acquiring a lock. Subsequent reads and writes
25 //! take place after the barrier.
26 //! * Release - a barrier for releasing a lock. Preceding reads and writes
27 //! take place before the barrier.
28 //! * Sequentially consistent - sequentially consistent operations are
29 //! guaranteed to happen in order. This is the standard mode for working
30 //! with atomic types and is equivalent to Java's `volatile`.
32 #![unstable(feature = "core_intrinsics",
33 reason = "intrinsics are unlikely to ever be stabilized, instead \
34 they should be used through stabilized interfaces \
35 in the rest of the standard library",
37 #![allow(missing_docs)]
41 #[stable(feature = "drop_in_place", since = "1.8.0")]
42 #[rustc_deprecated(reason = "no longer an intrinsic - use `ptr::drop_in_place` directly",
44 pub use crate::ptr::drop_in_place;
46 extern "rust-intrinsic" {
47 // N.B., these intrinsics take raw pointers because they mutate aliased
48 // memory, which is not valid for either `&` or `&mut`.
50 /// Stores a value if the current value is the same as the `old` value.
51 /// The stabilized version of this intrinsic is available on the
52 /// `std::sync::atomic` types via the `compare_exchange` method by passing
53 /// [`Ordering::SeqCst`](../../std/sync/atomic/enum.Ordering.html)
54 /// as both the `success` and `failure` parameters. For example,
55 /// [`AtomicBool::compare_exchange`][compare_exchange].
57 /// [compare_exchange]: ../../std/sync/atomic/struct.AtomicBool.html#method.compare_exchange
58 pub fn atomic_cxchg<T>(dst: *mut T, old: T, src: T) -> (T, bool);
59 /// Stores a value if the current value is the same as the `old` value.
60 /// The stabilized version of this intrinsic is available on the
61 /// `std::sync::atomic` types via the `compare_exchange` method by passing
62 /// [`Ordering::Acquire`](../../std/sync/atomic/enum.Ordering.html)
63 /// as both the `success` and `failure` parameters. For example,
64 /// [`AtomicBool::compare_exchange`][compare_exchange].
66 /// [compare_exchange]: ../../std/sync/atomic/struct.AtomicBool.html#method.compare_exchange
67 pub fn atomic_cxchg_acq<T>(dst: *mut T, old: T, src: T) -> (T, bool);
68 /// Stores a value if the current value is the same as the `old` value.
69 /// The stabilized version of this intrinsic is available on the
70 /// `std::sync::atomic` types via the `compare_exchange` method by passing
71 /// [`Ordering::Release`](../../std/sync/atomic/enum.Ordering.html)
72 /// as the `success` and
73 /// [`Ordering::Relaxed`](../../std/sync/atomic/enum.Ordering.html)
74 /// as the `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_rel<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::AcqRel`](../../std/sync/atomic/enum.Ordering.html)
83 /// as the `success` and
84 /// [`Ordering::Acquire`](../../std/sync/atomic/enum.Ordering.html)
85 /// as the `failure` parameters. For example,
86 /// [`AtomicBool::compare_exchange`][compare_exchange].
88 /// [compare_exchange]: ../../std/sync/atomic/struct.AtomicBool.html#method.compare_exchange
89 pub fn atomic_cxchg_acqrel<T>(dst: *mut T, old: T, src: T) -> (T, bool);
90 /// Stores a value if the current value is the same as the `old` value.
91 /// The stabilized version of this intrinsic is available on the
92 /// `std::sync::atomic` types via the `compare_exchange` method by passing
93 /// [`Ordering::Relaxed`](../../std/sync/atomic/enum.Ordering.html)
94 /// as both the `success` and `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_relaxed<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::SeqCst`](../../std/sync/atomic/enum.Ordering.html)
103 /// as the `success` and
104 /// [`Ordering::Relaxed`](../../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_failrelaxed<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::SeqCst`](../../std/sync/atomic/enum.Ordering.html)
114 /// as the `success` and
115 /// [`Ordering::Acquire`](../../std/sync/atomic/enum.Ordering.html)
116 /// as the `failure` parameters. For example,
117 /// [`AtomicBool::compare_exchange`][compare_exchange].
119 /// [compare_exchange]: ../../std/sync/atomic/struct.AtomicBool.html#method.compare_exchange
120 pub fn atomic_cxchg_failacq<T>(dst: *mut T, old: T, src: T) -> (T, bool);
121 /// Stores a value if the current value is the same as the `old` value.
122 /// The stabilized version of this intrinsic is available on the
123 /// `std::sync::atomic` types via the `compare_exchange` method by passing
124 /// [`Ordering::Acquire`](../../std/sync/atomic/enum.Ordering.html)
125 /// as the `success` and
126 /// [`Ordering::Relaxed`](../../std/sync/atomic/enum.Ordering.html)
127 /// as the `failure` parameters. For example,
128 /// [`AtomicBool::compare_exchange`][compare_exchange].
130 /// [compare_exchange]: ../../std/sync/atomic/struct.AtomicBool.html#method.compare_exchange
131 pub fn atomic_cxchg_acq_failrelaxed<T>(dst: *mut T, old: T, src: T) -> (T, bool);
132 /// Stores a value if the current value is the same as the `old` value.
133 /// The stabilized version of this intrinsic is available on the
134 /// `std::sync::atomic` types via the `compare_exchange` method by passing
135 /// [`Ordering::AcqRel`](../../std/sync/atomic/enum.Ordering.html)
136 /// as the `success` and
137 /// [`Ordering::Relaxed`](../../std/sync/atomic/enum.Ordering.html)
138 /// as the `failure` parameters. For example,
139 /// [`AtomicBool::compare_exchange`][compare_exchange].
141 /// [compare_exchange]: ../../std/sync/atomic/struct.AtomicBool.html#method.compare_exchange
142 pub fn atomic_cxchg_acqrel_failrelaxed<T>(dst: *mut T, old: T, src: T) -> (T, bool);
144 /// Stores a value if the current value is the same as the `old` value.
145 /// The stabilized version of this intrinsic is available on the
146 /// `std::sync::atomic` types via the `compare_exchange_weak` method by passing
147 /// [`Ordering::SeqCst`](../../std/sync/atomic/enum.Ordering.html)
148 /// as both the `success` and `failure` parameters. For example,
149 /// [`AtomicBool::compare_exchange_weak`][cew].
151 /// [cew]: ../../std/sync/atomic/struct.AtomicBool.html#method.compare_exchange_weak
152 pub fn atomic_cxchgweak<T>(dst: *mut T, old: T, src: T) -> (T, bool);
153 /// Stores a value if the current value is the same as the `old` value.
154 /// The stabilized version of this intrinsic is available on the
155 /// `std::sync::atomic` types via the `compare_exchange_weak` method by passing
156 /// [`Ordering::Acquire`](../../std/sync/atomic/enum.Ordering.html)
157 /// as both the `success` and `failure` parameters. For example,
158 /// [`AtomicBool::compare_exchange_weak`][cew].
160 /// [cew]: ../../std/sync/atomic/struct.AtomicBool.html#method.compare_exchange_weak
161 pub fn atomic_cxchgweak_acq<T>(dst: *mut T, old: T, src: T) -> (T, bool);
162 /// Stores a value if the current value is the same as the `old` value.
163 /// The stabilized version of this intrinsic is available on the
164 /// `std::sync::atomic` types via the `compare_exchange_weak` method by passing
165 /// [`Ordering::Release`](../../std/sync/atomic/enum.Ordering.html)
166 /// as the `success` and
167 /// [`Ordering::Relaxed`](../../std/sync/atomic/enum.Ordering.html)
168 /// as the `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_rel<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::AcqRel`](../../std/sync/atomic/enum.Ordering.html)
177 /// as the `success` and
178 /// [`Ordering::Acquire`](../../std/sync/atomic/enum.Ordering.html)
179 /// as the `failure` parameters. For example,
180 /// [`AtomicBool::compare_exchange_weak`][cew].
182 /// [cew]: ../../std/sync/atomic/struct.AtomicBool.html#method.compare_exchange_weak
183 pub fn atomic_cxchgweak_acqrel<T>(dst: *mut T, old: T, src: T) -> (T, bool);
184 /// Stores a value if the current value is the same as the `old` value.
185 /// The stabilized version of this intrinsic is available on the
186 /// `std::sync::atomic` types via the `compare_exchange_weak` method by passing
187 /// [`Ordering::Relaxed`](../../std/sync/atomic/enum.Ordering.html)
188 /// as both the `success` and `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_relaxed<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::SeqCst`](../../std/sync/atomic/enum.Ordering.html)
197 /// as the `success` and
198 /// [`Ordering::Relaxed`](../../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_failrelaxed<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::SeqCst`](../../std/sync/atomic/enum.Ordering.html)
208 /// as the `success` and
209 /// [`Ordering::Acquire`](../../std/sync/atomic/enum.Ordering.html)
210 /// as the `failure` parameters. For example,
211 /// [`AtomicBool::compare_exchange_weak`][cew].
213 /// [cew]: ../../std/sync/atomic/struct.AtomicBool.html#method.compare_exchange_weak
214 pub fn atomic_cxchgweak_failacq<T>(dst: *mut T, old: T, src: T) -> (T, bool);
215 /// Stores a value if the current value is the same as the `old` value.
216 /// The stabilized version of this intrinsic is available on the
217 /// `std::sync::atomic` types via the `compare_exchange_weak` method by passing
218 /// [`Ordering::Acquire`](../../std/sync/atomic/enum.Ordering.html)
219 /// as the `success` and
220 /// [`Ordering::Relaxed`](../../std/sync/atomic/enum.Ordering.html)
221 /// as the `failure` parameters. For example,
222 /// [`AtomicBool::compare_exchange_weak`][cew].
224 /// [cew]: ../../std/sync/atomic/struct.AtomicBool.html#method.compare_exchange_weak
225 pub fn atomic_cxchgweak_acq_failrelaxed<T>(dst: *mut T, old: T, src: T) -> (T, bool);
226 /// Stores a value if the current value is the same as the `old` value.
227 /// The stabilized version of this intrinsic is available on the
228 /// `std::sync::atomic` types via the `compare_exchange_weak` method by passing
229 /// [`Ordering::AcqRel`](../../std/sync/atomic/enum.Ordering.html)
230 /// as the `success` and
231 /// [`Ordering::Relaxed`](../../std/sync/atomic/enum.Ordering.html)
232 /// as the `failure` parameters. For example,
233 /// [`AtomicBool::compare_exchange_weak`][cew].
235 /// [cew]: ../../std/sync/atomic/struct.AtomicBool.html#method.compare_exchange_weak
236 pub fn atomic_cxchgweak_acqrel_failrelaxed<T>(dst: *mut T, old: T, src: T) -> (T, bool);
238 /// Loads the current value of the pointer.
239 /// The stabilized version of this intrinsic is available on the
240 /// `std::sync::atomic` types via the `load` method by passing
241 /// [`Ordering::SeqCst`](../../std/sync/atomic/enum.Ordering.html)
242 /// as the `order`. For example,
243 /// [`AtomicBool::load`](../../std/sync/atomic/struct.AtomicBool.html#method.load).
244 pub fn atomic_load<T>(src: *const T) -> T;
245 /// Loads the current value of the pointer.
246 /// The stabilized version of this intrinsic is available on the
247 /// `std::sync::atomic` types via the `load` method by passing
248 /// [`Ordering::Acquire`](../../std/sync/atomic/enum.Ordering.html)
249 /// as the `order`. For example,
250 /// [`AtomicBool::load`](../../std/sync/atomic/struct.AtomicBool.html#method.load).
251 pub fn atomic_load_acq<T>(src: *const T) -> T;
252 /// Loads the current value of the pointer.
253 /// The stabilized version of this intrinsic is available on the
254 /// `std::sync::atomic` types via the `load` method by passing
255 /// [`Ordering::Relaxed`](../../std/sync/atomic/enum.Ordering.html)
256 /// as the `order`. For example,
257 /// [`AtomicBool::load`](../../std/sync/atomic/struct.AtomicBool.html#method.load).
258 pub fn atomic_load_relaxed<T>(src: *const T) -> T;
259 pub fn atomic_load_unordered<T>(src: *const T) -> T;
261 /// Stores the value at the specified memory location.
262 /// The stabilized version of this intrinsic is available on the
263 /// `std::sync::atomic` types via the `store` method by passing
264 /// [`Ordering::SeqCst`](../../std/sync/atomic/enum.Ordering.html)
265 /// as the `order`. For example,
266 /// [`AtomicBool::store`](../../std/sync/atomic/struct.AtomicBool.html#method.store).
267 pub fn atomic_store<T>(dst: *mut T, val: T);
268 /// Stores the value at the specified memory location.
269 /// The stabilized version of this intrinsic is available on the
270 /// `std::sync::atomic` types via the `store` method by passing
271 /// [`Ordering::Release`](../../std/sync/atomic/enum.Ordering.html)
272 /// as the `order`. For example,
273 /// [`AtomicBool::store`](../../std/sync/atomic/struct.AtomicBool.html#method.store).
274 pub fn atomic_store_rel<T>(dst: *mut T, val: T);
275 /// Stores the value at the specified memory location.
276 /// The stabilized version of this intrinsic is available on the
277 /// `std::sync::atomic` types via the `store` method by passing
278 /// [`Ordering::Relaxed`](../../std/sync/atomic/enum.Ordering.html)
279 /// as the `order`. For example,
280 /// [`AtomicBool::store`](../../std/sync/atomic/struct.AtomicBool.html#method.store).
281 pub fn atomic_store_relaxed<T>(dst: *mut T, val: T);
282 pub fn atomic_store_unordered<T>(dst: *mut T, val: T);
284 /// Stores the value at the specified memory location, returning the old value.
285 /// The stabilized version of this intrinsic is available on the
286 /// `std::sync::atomic` types via the `swap` method by passing
287 /// [`Ordering::SeqCst`](../../std/sync/atomic/enum.Ordering.html)
288 /// as the `order`. For example,
289 /// [`AtomicBool::swap`](../../std/sync/atomic/struct.AtomicBool.html#method.swap).
290 pub fn atomic_xchg<T>(dst: *mut T, src: T) -> T;
291 /// Stores the value at the specified memory location, returning the old value.
292 /// The stabilized version of this intrinsic is available on the
293 /// `std::sync::atomic` types via the `swap` method by passing
294 /// [`Ordering::Acquire`](../../std/sync/atomic/enum.Ordering.html)
295 /// as the `order`. For example,
296 /// [`AtomicBool::swap`](../../std/sync/atomic/struct.AtomicBool.html#method.swap).
297 pub fn atomic_xchg_acq<T>(dst: *mut T, src: T) -> T;
298 /// Stores the value at the specified memory location, returning the old value.
299 /// The stabilized version of this intrinsic is available on the
300 /// `std::sync::atomic` types via the `swap` method by passing
301 /// [`Ordering::Release`](../../std/sync/atomic/enum.Ordering.html)
302 /// as the `order`. For example,
303 /// [`AtomicBool::swap`](../../std/sync/atomic/struct.AtomicBool.html#method.swap).
304 pub fn atomic_xchg_rel<T>(dst: *mut T, src: T) -> T;
305 /// Stores the value at the specified memory location, returning the old value.
306 /// The stabilized version of this intrinsic is available on the
307 /// `std::sync::atomic` types via the `swap` method by passing
308 /// [`Ordering::AcqRel`](../../std/sync/atomic/enum.Ordering.html)
309 /// as the `order`. For example,
310 /// [`AtomicBool::swap`](../../std/sync/atomic/struct.AtomicBool.html#method.swap).
311 pub fn atomic_xchg_acqrel<T>(dst: *mut T, src: T) -> T;
312 /// Stores the value at the specified memory location, returning the old value.
313 /// The stabilized version of this intrinsic is available on the
314 /// `std::sync::atomic` types via the `swap` method by passing
315 /// [`Ordering::Relaxed`](../../std/sync/atomic/enum.Ordering.html)
316 /// as the `order`. For example,
317 /// [`AtomicBool::swap`](../../std/sync/atomic/struct.AtomicBool.html#method.swap).
318 pub fn atomic_xchg_relaxed<T>(dst: *mut T, src: T) -> T;
320 /// Adds to the current value, returning the previous value.
321 /// The stabilized version of this intrinsic is available on the
322 /// `std::sync::atomic` types via the `fetch_add` method by passing
323 /// [`Ordering::SeqCst`](../../std/sync/atomic/enum.Ordering.html)
324 /// as the `order`. For example,
325 /// [`AtomicIsize::fetch_add`](../../std/sync/atomic/struct.AtomicIsize.html#method.fetch_add).
326 pub fn atomic_xadd<T>(dst: *mut T, src: T) -> T;
327 /// Adds to the current value, returning the previous value.
328 /// The stabilized version of this intrinsic is available on the
329 /// `std::sync::atomic` types via the `fetch_add` method by passing
330 /// [`Ordering::Acquire`](../../std/sync/atomic/enum.Ordering.html)
331 /// as the `order`. For example,
332 /// [`AtomicIsize::fetch_add`](../../std/sync/atomic/struct.AtomicIsize.html#method.fetch_add).
333 pub fn atomic_xadd_acq<T>(dst: *mut T, src: T) -> T;
334 /// Adds to the current value, returning the previous value.
335 /// The stabilized version of this intrinsic is available on the
336 /// `std::sync::atomic` types via the `fetch_add` method by passing
337 /// [`Ordering::Release`](../../std/sync/atomic/enum.Ordering.html)
338 /// as the `order`. For example,
339 /// [`AtomicIsize::fetch_add`](../../std/sync/atomic/struct.AtomicIsize.html#method.fetch_add).
340 pub fn atomic_xadd_rel<T>(dst: *mut T, src: T) -> T;
341 /// Adds to the current value, returning the previous value.
342 /// The stabilized version of this intrinsic is available on the
343 /// `std::sync::atomic` types via the `fetch_add` method by passing
344 /// [`Ordering::AcqRel`](../../std/sync/atomic/enum.Ordering.html)
345 /// as the `order`. For example,
346 /// [`AtomicIsize::fetch_add`](../../std/sync/atomic/struct.AtomicIsize.html#method.fetch_add).
347 pub fn atomic_xadd_acqrel<T>(dst: *mut T, src: T) -> T;
348 /// Adds to the current value, returning the previous value.
349 /// The stabilized version of this intrinsic is available on the
350 /// `std::sync::atomic` types via the `fetch_add` method by passing
351 /// [`Ordering::Relaxed`](../../std/sync/atomic/enum.Ordering.html)
352 /// as the `order`. For example,
353 /// [`AtomicIsize::fetch_add`](../../std/sync/atomic/struct.AtomicIsize.html#method.fetch_add).
354 pub fn atomic_xadd_relaxed<T>(dst: *mut T, src: T) -> T;
356 /// Subtract from the current value, returning the previous value.
357 /// The stabilized version of this intrinsic is available on the
358 /// `std::sync::atomic` types via the `fetch_sub` method by passing
359 /// [`Ordering::SeqCst`](../../std/sync/atomic/enum.Ordering.html)
360 /// as the `order`. For example,
361 /// [`AtomicIsize::fetch_sub`](../../std/sync/atomic/struct.AtomicIsize.html#method.fetch_sub).
362 pub fn atomic_xsub<T>(dst: *mut T, src: T) -> T;
363 /// Subtract from the current value, returning the previous value.
364 /// The stabilized version of this intrinsic is available on the
365 /// `std::sync::atomic` types via the `fetch_sub` method by passing
366 /// [`Ordering::Acquire`](../../std/sync/atomic/enum.Ordering.html)
367 /// as the `order`. For example,
368 /// [`AtomicIsize::fetch_sub`](../../std/sync/atomic/struct.AtomicIsize.html#method.fetch_sub).
369 pub fn atomic_xsub_acq<T>(dst: *mut T, src: T) -> T;
370 /// Subtract from the current value, returning the previous value.
371 /// The stabilized version of this intrinsic is available on the
372 /// `std::sync::atomic` types via the `fetch_sub` method by passing
373 /// [`Ordering::Release`](../../std/sync/atomic/enum.Ordering.html)
374 /// as the `order`. For example,
375 /// [`AtomicIsize::fetch_sub`](../../std/sync/atomic/struct.AtomicIsize.html#method.fetch_sub).
376 pub fn atomic_xsub_rel<T>(dst: *mut T, src: T) -> T;
377 /// Subtract from the current value, returning the previous value.
378 /// The stabilized version of this intrinsic is available on the
379 /// `std::sync::atomic` types via the `fetch_sub` method by passing
380 /// [`Ordering::AcqRel`](../../std/sync/atomic/enum.Ordering.html)
381 /// as the `order`. For example,
382 /// [`AtomicIsize::fetch_sub`](../../std/sync/atomic/struct.AtomicIsize.html#method.fetch_sub).
383 pub fn atomic_xsub_acqrel<T>(dst: *mut T, src: T) -> T;
384 /// Subtract from the current value, returning the previous value.
385 /// The stabilized version of this intrinsic is available on the
386 /// `std::sync::atomic` types via the `fetch_sub` method by passing
387 /// [`Ordering::Relaxed`](../../std/sync/atomic/enum.Ordering.html)
388 /// as the `order`. For example,
389 /// [`AtomicIsize::fetch_sub`](../../std/sync/atomic/struct.AtomicIsize.html#method.fetch_sub).
390 pub fn atomic_xsub_relaxed<T>(dst: *mut T, src: T) -> T;
392 /// Bitwise and with the current value, returning the previous value.
393 /// The stabilized version of this intrinsic is available on the
394 /// `std::sync::atomic` types via the `fetch_and` method by passing
395 /// [`Ordering::SeqCst`](../../std/sync/atomic/enum.Ordering.html)
396 /// as the `order`. For example,
397 /// [`AtomicBool::fetch_and`](../../std/sync/atomic/struct.AtomicBool.html#method.fetch_and).
398 pub fn atomic_and<T>(dst: *mut T, src: T) -> T;
399 /// Bitwise and with the current value, returning the previous value.
400 /// The stabilized version of this intrinsic is available on the
401 /// `std::sync::atomic` types via the `fetch_and` method by passing
402 /// [`Ordering::Acquire`](../../std/sync/atomic/enum.Ordering.html)
403 /// as the `order`. For example,
404 /// [`AtomicBool::fetch_and`](../../std/sync/atomic/struct.AtomicBool.html#method.fetch_and).
405 pub fn atomic_and_acq<T>(dst: *mut T, src: T) -> T;
406 /// Bitwise and with the current value, returning the previous value.
407 /// The stabilized version of this intrinsic is available on the
408 /// `std::sync::atomic` types via the `fetch_and` method by passing
409 /// [`Ordering::Release`](../../std/sync/atomic/enum.Ordering.html)
410 /// as the `order`. For example,
411 /// [`AtomicBool::fetch_and`](../../std/sync/atomic/struct.AtomicBool.html#method.fetch_and).
412 pub fn atomic_and_rel<T>(dst: *mut T, src: T) -> T;
413 /// Bitwise and with the current value, returning the previous value.
414 /// The stabilized version of this intrinsic is available on the
415 /// `std::sync::atomic` types via the `fetch_and` method by passing
416 /// [`Ordering::AcqRel`](../../std/sync/atomic/enum.Ordering.html)
417 /// as the `order`. For example,
418 /// [`AtomicBool::fetch_and`](../../std/sync/atomic/struct.AtomicBool.html#method.fetch_and).
419 pub fn atomic_and_acqrel<T>(dst: *mut T, src: T) -> T;
420 /// Bitwise and with the current value, returning the previous value.
421 /// The stabilized version of this intrinsic is available on the
422 /// `std::sync::atomic` types via the `fetch_and` method by passing
423 /// [`Ordering::Relaxed`](../../std/sync/atomic/enum.Ordering.html)
424 /// as the `order`. For example,
425 /// [`AtomicBool::fetch_and`](../../std/sync/atomic/struct.AtomicBool.html#method.fetch_and).
426 pub fn atomic_and_relaxed<T>(dst: *mut T, src: T) -> T;
428 /// Bitwise nand with the current value, returning the previous value.
429 /// The stabilized version of this intrinsic is available on the
430 /// `std::sync::atomic::AtomicBool` type via the `fetch_nand` method by passing
431 /// [`Ordering::SeqCst`](../../std/sync/atomic/enum.Ordering.html)
432 /// as the `order`. For example,
433 /// [`AtomicBool::fetch_nand`](../../std/sync/atomic/struct.AtomicBool.html#method.fetch_nand).
434 pub fn atomic_nand<T>(dst: *mut T, src: T) -> T;
435 /// Bitwise nand with the current value, returning the previous value.
436 /// The stabilized version of this intrinsic is available on the
437 /// `std::sync::atomic::AtomicBool` type via the `fetch_nand` method by passing
438 /// [`Ordering::Acquire`](../../std/sync/atomic/enum.Ordering.html)
439 /// as the `order`. For example,
440 /// [`AtomicBool::fetch_nand`](../../std/sync/atomic/struct.AtomicBool.html#method.fetch_nand).
441 pub fn atomic_nand_acq<T>(dst: *mut T, src: T) -> T;
442 /// Bitwise nand with the current value, returning the previous value.
443 /// The stabilized version of this intrinsic is available on the
444 /// `std::sync::atomic::AtomicBool` type via the `fetch_nand` method by passing
445 /// [`Ordering::Release`](../../std/sync/atomic/enum.Ordering.html)
446 /// as the `order`. For example,
447 /// [`AtomicBool::fetch_nand`](../../std/sync/atomic/struct.AtomicBool.html#method.fetch_nand).
448 pub fn atomic_nand_rel<T>(dst: *mut T, src: T) -> T;
449 /// Bitwise nand with the current value, returning the previous value.
450 /// The stabilized version of this intrinsic is available on the
451 /// `std::sync::atomic::AtomicBool` type via the `fetch_nand` method by passing
452 /// [`Ordering::AcqRel`](../../std/sync/atomic/enum.Ordering.html)
453 /// as the `order`. For example,
454 /// [`AtomicBool::fetch_nand`](../../std/sync/atomic/struct.AtomicBool.html#method.fetch_nand).
455 pub fn atomic_nand_acqrel<T>(dst: *mut T, src: T) -> T;
456 /// Bitwise nand with the current value, returning the previous value.
457 /// The stabilized version of this intrinsic is available on the
458 /// `std::sync::atomic::AtomicBool` type via the `fetch_nand` method by passing
459 /// [`Ordering::Relaxed`](../../std/sync/atomic/enum.Ordering.html)
460 /// as the `order`. For example,
461 /// [`AtomicBool::fetch_nand`](../../std/sync/atomic/struct.AtomicBool.html#method.fetch_nand).
462 pub fn atomic_nand_relaxed<T>(dst: *mut T, src: T) -> T;
464 /// Bitwise or with the current value, returning the previous value.
465 /// The stabilized version of this intrinsic is available on the
466 /// `std::sync::atomic` types via the `fetch_or` method by passing
467 /// [`Ordering::SeqCst`](../../std/sync/atomic/enum.Ordering.html)
468 /// as the `order`. For example,
469 /// [`AtomicBool::fetch_or`](../../std/sync/atomic/struct.AtomicBool.html#method.fetch_or).
470 pub fn atomic_or<T>(dst: *mut T, src: T) -> T;
471 /// Bitwise or with the current value, returning the previous value.
472 /// The stabilized version of this intrinsic is available on the
473 /// `std::sync::atomic` types via the `fetch_or` method by passing
474 /// [`Ordering::Acquire`](../../std/sync/atomic/enum.Ordering.html)
475 /// as the `order`. For example,
476 /// [`AtomicBool::fetch_or`](../../std/sync/atomic/struct.AtomicBool.html#method.fetch_or).
477 pub fn atomic_or_acq<T>(dst: *mut T, src: T) -> T;
478 /// Bitwise or with the current value, returning the previous value.
479 /// The stabilized version of this intrinsic is available on the
480 /// `std::sync::atomic` types via the `fetch_or` method by passing
481 /// [`Ordering::Release`](../../std/sync/atomic/enum.Ordering.html)
482 /// as the `order`. For example,
483 /// [`AtomicBool::fetch_or`](../../std/sync/atomic/struct.AtomicBool.html#method.fetch_or).
484 pub fn atomic_or_rel<T>(dst: *mut T, src: T) -> T;
485 /// Bitwise or with the current value, returning the previous value.
486 /// The stabilized version of this intrinsic is available on the
487 /// `std::sync::atomic` types via the `fetch_or` method by passing
488 /// [`Ordering::AcqRel`](../../std/sync/atomic/enum.Ordering.html)
489 /// as the `order`. For example,
490 /// [`AtomicBool::fetch_or`](../../std/sync/atomic/struct.AtomicBool.html#method.fetch_or).
491 pub fn atomic_or_acqrel<T>(dst: *mut T, src: T) -> T;
492 /// Bitwise or with the current value, returning the previous value.
493 /// The stabilized version of this intrinsic is available on the
494 /// `std::sync::atomic` types via the `fetch_or` method by passing
495 /// [`Ordering::Relaxed`](../../std/sync/atomic/enum.Ordering.html)
496 /// as the `order`. For example,
497 /// [`AtomicBool::fetch_or`](../../std/sync/atomic/struct.AtomicBool.html#method.fetch_or).
498 pub fn atomic_or_relaxed<T>(dst: *mut T, src: T) -> T;
500 /// Bitwise xor with the current value, returning the previous value.
501 /// The stabilized version of this intrinsic is available on the
502 /// `std::sync::atomic` types via the `fetch_xor` method by passing
503 /// [`Ordering::SeqCst`](../../std/sync/atomic/enum.Ordering.html)
504 /// as the `order`. For example,
505 /// [`AtomicBool::fetch_xor`](../../std/sync/atomic/struct.AtomicBool.html#method.fetch_xor).
506 pub fn atomic_xor<T>(dst: *mut T, src: T) -> T;
507 /// Bitwise xor with the current value, returning the previous value.
508 /// The stabilized version of this intrinsic is available on the
509 /// `std::sync::atomic` types via the `fetch_xor` method by passing
510 /// [`Ordering::Acquire`](../../std/sync/atomic/enum.Ordering.html)
511 /// as the `order`. For example,
512 /// [`AtomicBool::fetch_xor`](../../std/sync/atomic/struct.AtomicBool.html#method.fetch_xor).
513 pub fn atomic_xor_acq<T>(dst: *mut T, src: T) -> T;
514 /// Bitwise xor with the current value, returning the previous value.
515 /// The stabilized version of this intrinsic is available on the
516 /// `std::sync::atomic` types via the `fetch_xor` method by passing
517 /// [`Ordering::Release`](../../std/sync/atomic/enum.Ordering.html)
518 /// as the `order`. For example,
519 /// [`AtomicBool::fetch_xor`](../../std/sync/atomic/struct.AtomicBool.html#method.fetch_xor).
520 pub fn atomic_xor_rel<T>(dst: *mut T, src: T) -> T;
521 /// Bitwise xor with the current value, returning the previous value.
522 /// The stabilized version of this intrinsic is available on the
523 /// `std::sync::atomic` types via the `fetch_xor` method by passing
524 /// [`Ordering::AcqRel`](../../std/sync/atomic/enum.Ordering.html)
525 /// as the `order`. For example,
526 /// [`AtomicBool::fetch_xor`](../../std/sync/atomic/struct.AtomicBool.html#method.fetch_xor).
527 pub fn atomic_xor_acqrel<T>(dst: *mut T, src: T) -> T;
528 /// Bitwise xor with the current value, returning the previous value.
529 /// The stabilized version of this intrinsic is available on the
530 /// `std::sync::atomic` types via the `fetch_xor` method by passing
531 /// [`Ordering::Relaxed`](../../std/sync/atomic/enum.Ordering.html)
532 /// as the `order`. For example,
533 /// [`AtomicBool::fetch_xor`](../../std/sync/atomic/struct.AtomicBool.html#method.fetch_xor).
534 pub fn atomic_xor_relaxed<T>(dst: *mut T, src: T) -> T;
536 pub fn atomic_max<T>(dst: *mut T, src: T) -> T;
537 pub fn atomic_max_acq<T>(dst: *mut T, src: T) -> T;
538 pub fn atomic_max_rel<T>(dst: *mut T, src: T) -> T;
539 pub fn atomic_max_acqrel<T>(dst: *mut T, src: T) -> T;
540 pub fn atomic_max_relaxed<T>(dst: *mut T, src: T) -> T;
542 pub fn atomic_min<T>(dst: *mut T, src: T) -> T;
543 pub fn atomic_min_acq<T>(dst: *mut T, src: T) -> T;
544 pub fn atomic_min_rel<T>(dst: *mut T, src: T) -> T;
545 pub fn atomic_min_acqrel<T>(dst: *mut T, src: T) -> T;
546 pub fn atomic_min_relaxed<T>(dst: *mut T, src: T) -> T;
548 pub fn atomic_umin<T>(dst: *mut T, src: T) -> T;
549 pub fn atomic_umin_acq<T>(dst: *mut T, src: T) -> T;
550 pub fn atomic_umin_rel<T>(dst: *mut T, src: T) -> T;
551 pub fn atomic_umin_acqrel<T>(dst: *mut T, src: T) -> T;
552 pub fn atomic_umin_relaxed<T>(dst: *mut T, src: T) -> T;
554 pub fn atomic_umax<T>(dst: *mut T, src: T) -> T;
555 pub fn atomic_umax_acq<T>(dst: *mut T, src: T) -> T;
556 pub fn atomic_umax_rel<T>(dst: *mut T, src: T) -> T;
557 pub fn atomic_umax_acqrel<T>(dst: *mut T, src: T) -> T;
558 pub fn atomic_umax_relaxed<T>(dst: *mut T, src: T) -> T;
560 /// The `prefetch` intrinsic is a hint to the code generator to insert a prefetch instruction
561 /// if supported; otherwise, it is a no-op.
562 /// Prefetches have no effect on the behavior of the program but can change its performance
565 /// The `locality` argument must be a constant integer and is a temporal locality specifier
566 /// ranging from (0) - no locality, to (3) - extremely local keep in cache
567 pub fn prefetch_read_data<T>(data: *const T, locality: i32);
568 /// The `prefetch` intrinsic is a hint to the code generator to insert a prefetch instruction
569 /// if supported; otherwise, it is a no-op.
570 /// Prefetches have no effect on the behavior of the program but can change its performance
573 /// The `locality` argument must be a constant integer and is a temporal locality specifier
574 /// ranging from (0) - no locality, to (3) - extremely local keep in cache
575 pub fn prefetch_write_data<T>(data: *const T, locality: i32);
576 /// The `prefetch` intrinsic is a hint to the code generator to insert a prefetch instruction
577 /// if supported; otherwise, it is a no-op.
578 /// Prefetches have no effect on the behavior of the program but can change its performance
581 /// The `locality` argument must be a constant integer and is a temporal locality specifier
582 /// ranging from (0) - no locality, to (3) - extremely local keep in cache
583 pub fn prefetch_read_instruction<T>(data: *const T, locality: i32);
584 /// The `prefetch` intrinsic is a hint to the code generator to insert a prefetch instruction
585 /// if supported; otherwise, it is a no-op.
586 /// Prefetches have no effect on the behavior of the program but can change its performance
589 /// The `locality` argument must be a constant integer and is a temporal locality specifier
590 /// ranging from (0) - no locality, to (3) - extremely local keep in cache
591 pub fn prefetch_write_instruction<T>(data: *const T, locality: i32);
594 extern "rust-intrinsic" {
596 pub fn atomic_fence();
597 pub fn atomic_fence_acq();
598 pub fn atomic_fence_rel();
599 pub fn atomic_fence_acqrel();
601 /// A compiler-only memory barrier.
603 /// Memory accesses will never be reordered across this barrier by the
604 /// compiler, but no instructions will be emitted for it. This is
605 /// appropriate for operations on the same thread that may be preempted,
606 /// such as when interacting with signal handlers.
607 pub fn atomic_singlethreadfence();
608 pub fn atomic_singlethreadfence_acq();
609 pub fn atomic_singlethreadfence_rel();
610 pub fn atomic_singlethreadfence_acqrel();
612 /// Magic intrinsic that derives its meaning from attributes
613 /// attached to the function.
615 /// For example, dataflow uses this to inject static assertions so
616 /// that `rustc_peek(potentially_uninitialized)` would actually
617 /// double-check that dataflow did indeed compute that it is
618 /// uninitialized at that point in the control flow.
619 pub fn rustc_peek<T>(_: T) -> T;
621 /// Aborts the execution of the process.
623 /// The stabilized version of this intrinsic is
624 /// [`std::process::abort`](../../std/process/fn.abort.html)
627 /// Tells LLVM that this point in the code is not reachable, enabling
628 /// further optimizations.
630 /// N.B., this is very different from the `unreachable!()` macro: Unlike the
631 /// macro, which panics when it is executed, it is *undefined behavior* to
632 /// reach code marked with this function.
634 /// The stabilized version of this intrinsic is
635 /// [`std::hint::unreachable_unchecked`](../../std/hint/fn.unreachable_unchecked.html).
636 pub fn unreachable() -> !;
638 /// Informs the optimizer that a condition is always true.
639 /// If the condition is false, the behavior is undefined.
641 /// No code is generated for this intrinsic, but the optimizer will try
642 /// to preserve it (and its condition) between passes, which may interfere
643 /// with optimization of surrounding code and reduce performance. It should
644 /// not be used if the invariant can be discovered by the optimizer on its
645 /// own, or if it does not enable any significant optimizations.
646 pub fn assume(b: bool);
648 /// Hints to the compiler that branch condition is likely to be true.
649 /// Returns the value passed to it.
651 /// Any use other than with `if` statements will probably not have an effect.
652 pub fn likely(b: bool) -> bool;
654 /// Hints to the compiler that branch condition is likely to be false.
655 /// Returns the value passed to it.
657 /// Any use other than with `if` statements will probably not have an effect.
658 pub fn unlikely(b: bool) -> bool;
660 /// Executes a breakpoint trap, for inspection by a debugger.
663 /// The size of a type in bytes.
665 /// More specifically, this is the offset in bytes between successive
666 /// items of the same type, including alignment padding.
668 /// The stabilized version of this intrinsic is
669 /// [`std::mem::size_of`](../../std/mem/fn.size_of.html).
670 pub fn size_of<T>() -> usize;
672 /// Moves a value to an uninitialized memory location.
674 /// Drop glue is not run on the destination.
675 pub fn move_val_init<T>(dst: *mut T, src: T);
677 pub fn min_align_of<T>() -> usize;
678 pub fn pref_align_of<T>() -> usize;
680 /// The size of the referenced value in bytes.
682 /// The stabilized version of this intrinsic is
683 /// [`std::mem::size_of_val`](../../std/mem/fn.size_of_val.html).
684 pub fn size_of_val<T: ?Sized>(_: &T) -> usize;
685 pub fn min_align_of_val<T: ?Sized>(_: &T) -> usize;
687 /// Gets a static string slice containing the name of a type.
688 pub fn type_name<T: ?Sized>() -> &'static str;
690 /// Gets an identifier which is globally unique to the specified type. This
691 /// function will return the same value for a type regardless of whichever
692 /// crate it is invoked in.
693 pub fn type_id<T: ?Sized + 'static>() -> u64;
695 /// A guard for unsafe functions that cannot ever be executed if `T` is uninhabited:
696 /// This will statically either panic, or do nothing.
697 pub fn panic_if_uninhabited<T>();
699 /// Creates a value initialized to zero.
701 /// `init` is unsafe because it returns a zeroed-out datum,
702 /// which is unsafe unless `T` is `Copy`. Also, even if T is
703 /// `Copy`, an all-zero value may not correspond to any legitimate
704 /// state for the type in question.
705 #[unstable(feature = "core_intrinsics",
706 reason = "intrinsics are unlikely to ever be stabilized, instead \
707 they should be used through stabilized interfaces \
708 in the rest of the standard library",
710 #[rustc_deprecated(reason = "no longer used by rustc, will be removed - use MaybeUninit \
713 pub fn init<T>() -> T;
715 /// Moves a value out of scope without running drop glue.
716 pub fn forget<T: ?Sized>(_: T);
718 /// Reinterprets the bits of a value of one type as another type.
720 /// Both types must have the same size. Neither the original, nor the result,
721 /// may be an [invalid value](../../nomicon/what-unsafe-does.html).
723 /// `transmute` is semantically equivalent to a bitwise move of one type
724 /// into another. It copies the bits from the source value into the
725 /// destination value, then forgets the original. It's equivalent to C's
726 /// `memcpy` under the hood, just like `transmute_copy`.
728 /// `transmute` is **incredibly** unsafe. There are a vast number of ways to
729 /// cause [undefined behavior][ub] with this function. `transmute` should be
730 /// the absolute last resort.
732 /// The [nomicon](../../nomicon/transmutes.html) has additional
735 /// [ub]: ../../reference/behavior-considered-undefined.html
739 /// There are a few things that `transmute` is really useful for.
741 /// Turning a pointer into a function pointer. This is *not* portable to
742 /// machines where function pointers and data pointers have different sizes.
745 /// fn foo() -> i32 {
748 /// let pointer = foo as *const ();
749 /// let function = unsafe {
750 /// std::mem::transmute::<*const (), fn() -> i32>(pointer)
752 /// assert_eq!(function(), 0);
755 /// Extending a lifetime, or shortening an invariant lifetime. This is
756 /// advanced, very unsafe Rust!
759 /// struct R<'a>(&'a i32);
760 /// unsafe fn extend_lifetime<'b>(r: R<'b>) -> R<'static> {
761 /// std::mem::transmute::<R<'b>, R<'static>>(r)
764 /// unsafe fn shorten_invariant_lifetime<'b, 'c>(r: &'b mut R<'static>)
765 /// -> &'b mut R<'c> {
766 /// std::mem::transmute::<&'b mut R<'static>, &'b mut R<'c>>(r)
772 /// Don't despair: many uses of `transmute` can be achieved through other means.
773 /// Below are common applications of `transmute` which can be replaced with safer
776 /// Turning a pointer into a `usize`:
780 /// let ptr_num_transmute = unsafe {
781 /// std::mem::transmute::<&i32, usize>(ptr)
784 /// // Use an `as` cast instead
785 /// let ptr_num_cast = ptr as *const i32 as usize;
788 /// Turning a `*mut T` into an `&mut T`:
791 /// let ptr: *mut i32 = &mut 0;
792 /// let ref_transmuted = unsafe {
793 /// std::mem::transmute::<*mut i32, &mut i32>(ptr)
796 /// // Use a reborrow instead
797 /// let ref_casted = unsafe { &mut *ptr };
800 /// Turning an `&mut T` into an `&mut U`:
803 /// let ptr = &mut 0;
804 /// let val_transmuted = unsafe {
805 /// std::mem::transmute::<&mut i32, &mut u32>(ptr)
808 /// // Now, put together `as` and reborrowing - note the chaining of `as`
809 /// // `as` is not transitive
810 /// let val_casts = unsafe { &mut *(ptr as *mut i32 as *mut u32) };
813 /// Turning an `&str` into an `&[u8]`:
816 /// // this is not a good way to do this.
817 /// let slice = unsafe { std::mem::transmute::<&str, &[u8]>("Rust") };
818 /// assert_eq!(slice, &[82, 117, 115, 116]);
820 /// // You could use `str::as_bytes`
821 /// let slice = "Rust".as_bytes();
822 /// assert_eq!(slice, &[82, 117, 115, 116]);
824 /// // Or, just use a byte string, if you have control over the string
826 /// assert_eq!(b"Rust", &[82, 117, 115, 116]);
829 /// Turning a `Vec<&T>` into a `Vec<Option<&T>>`:
832 /// let store = [0, 1, 2, 3];
833 /// let mut v_orig = store.iter().collect::<Vec<&i32>>();
835 /// // Using transmute: this is Undefined Behavior, and a bad idea.
836 /// // However, it is no-copy.
837 /// let v_transmuted = unsafe {
838 /// std::mem::transmute::<Vec<&i32>, Vec<Option<&i32>>>(
842 /// // This is the suggested, safe way.
843 /// // It does copy the entire vector, though, into a new array.
844 /// let v_collected = v_orig.clone()
846 /// .map(|r| Some(r))
847 /// .collect::<Vec<Option<&i32>>>();
849 /// // The no-copy, unsafe way, still using transmute, but not UB.
850 /// // This is equivalent to the original, but safer, and reuses the
851 /// // same `Vec` internals. Therefore, the new inner type must have the
852 /// // exact same size, and the same alignment, as the old type.
853 /// // The same caveats exist for this method as transmute, for
854 /// // the original inner type (`&i32`) to the converted inner type
855 /// // (`Option<&i32>`), so read the nomicon pages linked above.
856 /// let v_from_raw = unsafe {
857 /// Vec::from_raw_parts(v_orig.as_mut_ptr() as *mut Option<&i32>,
859 /// v_orig.capacity())
861 /// std::mem::forget(v_orig);
864 /// Implementing `split_at_mut`:
867 /// use std::{slice, mem};
869 /// // There are multiple ways to do this, and there are multiple problems
870 /// // with the following (transmute) way.
871 /// fn split_at_mut_transmute<T>(slice: &mut [T], mid: usize)
872 /// -> (&mut [T], &mut [T]) {
873 /// let len = slice.len();
874 /// assert!(mid <= len);
876 /// let slice2 = mem::transmute::<&mut [T], &mut [T]>(slice);
877 /// // first: transmute is not typesafe; all it checks is that T and
878 /// // U are of the same size. Second, right here, you have two
879 /// // mutable references pointing to the same memory.
880 /// (&mut slice[0..mid], &mut slice2[mid..len])
884 /// // This gets rid of the typesafety problems; `&mut *` will *only* give
885 /// // you an `&mut T` from an `&mut T` or `*mut T`.
886 /// fn split_at_mut_casts<T>(slice: &mut [T], mid: usize)
887 /// -> (&mut [T], &mut [T]) {
888 /// let len = slice.len();
889 /// assert!(mid <= len);
891 /// let slice2 = &mut *(slice as *mut [T]);
892 /// // however, you still have two mutable references pointing to
893 /// // the same memory.
894 /// (&mut slice[0..mid], &mut slice2[mid..len])
898 /// // This is how the standard library does it. This is the best method, if
899 /// // you need to do something like this
900 /// fn split_at_stdlib<T>(slice: &mut [T], mid: usize)
901 /// -> (&mut [T], &mut [T]) {
902 /// let len = slice.len();
903 /// assert!(mid <= len);
905 /// let ptr = slice.as_mut_ptr();
906 /// // This now has three mutable references pointing at the same
907 /// // memory. `slice`, the rvalue ret.0, and the rvalue ret.1.
908 /// // `slice` is never used after `let ptr = ...`, and so one can
909 /// // treat it as "dead", and therefore, you only have two real
910 /// // mutable slices.
911 /// (slice::from_raw_parts_mut(ptr, mid),
912 /// slice::from_raw_parts_mut(ptr.add(mid), len - mid))
916 #[stable(feature = "rust1", since = "1.0.0")]
917 pub fn transmute<T, U>(e: T) -> U;
919 /// Returns `true` if the actual type given as `T` requires drop
920 /// glue; returns `false` if the actual type provided for `T`
921 /// implements `Copy`.
923 /// If the actual type neither requires drop glue nor implements
924 /// `Copy`, then may return `true` or `false`.
926 /// The stabilized version of this intrinsic is
927 /// [`std::mem::needs_drop`](../../std/mem/fn.needs_drop.html).
928 pub fn needs_drop<T>() -> bool;
930 /// Calculates the offset from a pointer.
932 /// This is implemented as an intrinsic to avoid converting to and from an
933 /// integer, since the conversion would throw away aliasing information.
937 /// Both the starting and resulting pointer must be either in bounds or one
938 /// byte past the end of an allocated object. If either pointer is out of
939 /// bounds or arithmetic overflow occurs then any further use of the
940 /// returned value will result in undefined behavior.
941 pub fn offset<T>(dst: *const T, offset: isize) -> *const T;
943 /// Calculates the offset from a pointer, potentially wrapping.
945 /// This is implemented as an intrinsic to avoid converting to and from an
946 /// integer, since the conversion inhibits certain optimizations.
950 /// Unlike the `offset` intrinsic, this intrinsic does not restrict the
951 /// resulting pointer to point into or one byte past the end of an allocated
952 /// object, and it wraps with two's complement arithmetic. The resulting
953 /// value is not necessarily valid to be used to actually access memory.
954 pub fn arith_offset<T>(dst: *const T, offset: isize) -> *const T;
956 /// Equivalent to the appropriate `llvm.memcpy.p0i8.0i8.*` intrinsic, with
957 /// a size of `count` * `size_of::<T>()` and an alignment of
958 /// `min_align_of::<T>()`
960 /// The volatile parameter is set to `true`, so it will not be optimized out
961 /// unless size is equal to zero.
962 pub fn volatile_copy_nonoverlapping_memory<T>(dst: *mut T, src: *const T,
964 /// Equivalent to the appropriate `llvm.memmove.p0i8.0i8.*` intrinsic, with
965 /// a size of `count` * `size_of::<T>()` and an alignment of
966 /// `min_align_of::<T>()`
968 /// The volatile parameter is set to `true`, so it will not be optimized out
969 /// unless size is equal to zero.
970 pub fn volatile_copy_memory<T>(dst: *mut T, src: *const T, count: usize);
971 /// Equivalent to the appropriate `llvm.memset.p0i8.*` intrinsic, with a
972 /// size of `count` * `size_of::<T>()` and an alignment of
973 /// `min_align_of::<T>()`.
975 /// The volatile parameter is set to `true`, so it will not be optimized out
976 /// unless size is equal to zero.
977 pub fn volatile_set_memory<T>(dst: *mut T, val: u8, count: usize);
979 /// Performs a volatile load from the `src` pointer.
980 /// The stabilized version of this intrinsic is
981 /// [`std::ptr::read_volatile`](../../std/ptr/fn.read_volatile.html).
982 pub fn volatile_load<T>(src: *const T) -> T;
983 /// Performs a volatile store to the `dst` pointer.
984 /// The stabilized version of this intrinsic is
985 /// [`std::ptr::write_volatile`](../../std/ptr/fn.write_volatile.html).
986 pub fn volatile_store<T>(dst: *mut T, val: T);
988 /// Performs a volatile load from the `src` pointer
989 /// The pointer is not required to be aligned.
990 pub fn unaligned_volatile_load<T>(src: *const T) -> T;
991 /// Performs a volatile store to the `dst` pointer.
992 /// The pointer is not required to be aligned.
993 pub fn unaligned_volatile_store<T>(dst: *mut T, val: T);
995 /// Returns the square root of an `f32`
996 pub fn sqrtf32(x: f32) -> f32;
997 /// Returns the square root of an `f64`
998 pub fn sqrtf64(x: f64) -> f64;
1000 /// Raises an `f32` to an integer power.
1001 pub fn powif32(a: f32, x: i32) -> f32;
1002 /// Raises an `f64` to an integer power.
1003 pub fn powif64(a: f64, x: i32) -> f64;
1005 /// Returns the sine of an `f32`.
1006 pub fn sinf32(x: f32) -> f32;
1007 /// Returns the sine of an `f64`.
1008 pub fn sinf64(x: f64) -> f64;
1010 /// Returns the cosine of an `f32`.
1011 pub fn cosf32(x: f32) -> f32;
1012 /// Returns the cosine of an `f64`.
1013 pub fn cosf64(x: f64) -> f64;
1015 /// Raises an `f32` to an `f32` power.
1016 pub fn powf32(a: f32, x: f32) -> f32;
1017 /// Raises an `f64` to an `f64` power.
1018 pub fn powf64(a: f64, x: f64) -> f64;
1020 /// Returns the exponential of an `f32`.
1021 pub fn expf32(x: f32) -> f32;
1022 /// Returns the exponential of an `f64`.
1023 pub fn expf64(x: f64) -> f64;
1025 /// Returns 2 raised to the power of an `f32`.
1026 pub fn exp2f32(x: f32) -> f32;
1027 /// Returns 2 raised to the power of an `f64`.
1028 pub fn exp2f64(x: f64) -> f64;
1030 /// Returns the natural logarithm of an `f32`.
1031 pub fn logf32(x: f32) -> f32;
1032 /// Returns the natural logarithm of an `f64`.
1033 pub fn logf64(x: f64) -> f64;
1035 /// Returns the base 10 logarithm of an `f32`.
1036 pub fn log10f32(x: f32) -> f32;
1037 /// Returns the base 10 logarithm of an `f64`.
1038 pub fn log10f64(x: f64) -> f64;
1040 /// Returns the base 2 logarithm of an `f32`.
1041 pub fn log2f32(x: f32) -> f32;
1042 /// Returns the base 2 logarithm of an `f64`.
1043 pub fn log2f64(x: f64) -> f64;
1045 /// Returns `a * b + c` for `f32` values.
1046 pub fn fmaf32(a: f32, b: f32, c: f32) -> f32;
1047 /// Returns `a * b + c` for `f64` values.
1048 pub fn fmaf64(a: f64, b: f64, c: f64) -> f64;
1050 /// Returns the absolute value of an `f32`.
1051 pub fn fabsf32(x: f32) -> f32;
1052 /// Returns the absolute value of an `f64`.
1053 pub fn fabsf64(x: f64) -> f64;
1055 /// Returns the minimum of two `f32` values.
1056 pub fn minnumf32(x: f32, y: f32) -> f32;
1057 /// Returns the minimum of two `f64` values.
1058 pub fn minnumf64(x: f64, y: f64) -> f64;
1059 /// Returns the maximum of two `f32` values.
1060 pub fn maxnumf32(x: f32, y: f32) -> f32;
1061 /// Returns the maximum of two `f64` values.
1062 pub fn maxnumf64(x: f64, y: f64) -> f64;
1064 /// Copies the sign from `y` to `x` for `f32` values.
1065 pub fn copysignf32(x: f32, y: f32) -> f32;
1066 /// Copies the sign from `y` to `x` for `f64` values.
1067 pub fn copysignf64(x: f64, y: f64) -> f64;
1069 /// Returns the largest integer less than or equal to an `f32`.
1070 pub fn floorf32(x: f32) -> f32;
1071 /// Returns the largest integer less than or equal to an `f64`.
1072 pub fn floorf64(x: f64) -> f64;
1074 /// Returns the smallest integer greater than or equal to an `f32`.
1075 pub fn ceilf32(x: f32) -> f32;
1076 /// Returns the smallest integer greater than or equal to an `f64`.
1077 pub fn ceilf64(x: f64) -> f64;
1079 /// Returns the integer part of an `f32`.
1080 pub fn truncf32(x: f32) -> f32;
1081 /// Returns the integer part of an `f64`.
1082 pub fn truncf64(x: f64) -> f64;
1084 /// Returns the nearest integer to an `f32`. May raise an inexact floating-point exception
1085 /// if the argument is not an integer.
1086 pub fn rintf32(x: f32) -> f32;
1087 /// Returns the nearest integer to an `f64`. May raise an inexact floating-point exception
1088 /// if the argument is not an integer.
1089 pub fn rintf64(x: f64) -> f64;
1091 /// Returns the nearest integer to an `f32`.
1092 pub fn nearbyintf32(x: f32) -> f32;
1093 /// Returns the nearest integer to an `f64`.
1094 pub fn nearbyintf64(x: f64) -> f64;
1096 /// Returns the nearest integer to an `f32`. Rounds half-way cases away from zero.
1097 pub fn roundf32(x: f32) -> f32;
1098 /// Returns the nearest integer to an `f64`. Rounds half-way cases away from zero.
1099 pub fn roundf64(x: f64) -> f64;
1101 /// Float addition that allows optimizations based on algebraic rules.
1102 /// May assume inputs are finite.
1103 pub fn fadd_fast<T>(a: T, b: T) -> T;
1105 /// Float subtraction that allows optimizations based on algebraic rules.
1106 /// May assume inputs are finite.
1107 pub fn fsub_fast<T>(a: T, b: T) -> T;
1109 /// Float multiplication that allows optimizations based on algebraic rules.
1110 /// May assume inputs are finite.
1111 pub fn fmul_fast<T>(a: T, b: T) -> T;
1113 /// Float division that allows optimizations based on algebraic rules.
1114 /// May assume inputs are finite.
1115 pub fn fdiv_fast<T>(a: T, b: T) -> T;
1117 /// Float remainder that allows optimizations based on algebraic rules.
1118 /// May assume inputs are finite.
1119 pub fn frem_fast<T>(a: T, b: T) -> T;
1122 /// Returns the number of bits set in an integer type `T`
1123 pub fn ctpop<T>(x: T) -> T;
1125 /// Returns the number of leading unset bits (zeroes) in an integer type `T`.
1130 /// #![feature(core_intrinsics)]
1132 /// use std::intrinsics::ctlz;
1134 /// let x = 0b0001_1100_u8;
1135 /// let num_leading = ctlz(x);
1136 /// assert_eq!(num_leading, 3);
1139 /// An `x` with value `0` will return the bit width of `T`.
1142 /// #![feature(core_intrinsics)]
1144 /// use std::intrinsics::ctlz;
1147 /// let num_leading = ctlz(x);
1148 /// assert_eq!(num_leading, 16);
1150 pub fn ctlz<T>(x: T) -> T;
1152 /// Like `ctlz`, but extra-unsafe as it returns `undef` when
1153 /// given an `x` with value `0`.
1158 /// #![feature(core_intrinsics)]
1160 /// use std::intrinsics::ctlz_nonzero;
1162 /// let x = 0b0001_1100_u8;
1163 /// let num_leading = unsafe { ctlz_nonzero(x) };
1164 /// assert_eq!(num_leading, 3);
1166 pub fn ctlz_nonzero<T>(x: T) -> T;
1168 /// Returns the number of trailing unset bits (zeroes) in an integer type `T`.
1173 /// #![feature(core_intrinsics)]
1175 /// use std::intrinsics::cttz;
1177 /// let x = 0b0011_1000_u8;
1178 /// let num_trailing = cttz(x);
1179 /// assert_eq!(num_trailing, 3);
1182 /// An `x` with value `0` will return the bit width of `T`:
1185 /// #![feature(core_intrinsics)]
1187 /// use std::intrinsics::cttz;
1190 /// let num_trailing = cttz(x);
1191 /// assert_eq!(num_trailing, 16);
1193 pub fn cttz<T>(x: T) -> T;
1195 /// Like `cttz`, but extra-unsafe as it returns `undef` when
1196 /// given an `x` with value `0`.
1201 /// #![feature(core_intrinsics)]
1203 /// use std::intrinsics::cttz_nonzero;
1205 /// let x = 0b0011_1000_u8;
1206 /// let num_trailing = unsafe { cttz_nonzero(x) };
1207 /// assert_eq!(num_trailing, 3);
1209 pub fn cttz_nonzero<T>(x: T) -> T;
1211 /// Reverses the bytes in an integer type `T`.
1212 pub fn bswap<T>(x: T) -> T;
1214 /// Reverses the bits in an integer type `T`.
1215 pub fn bitreverse<T>(x: T) -> T;
1217 /// Performs checked integer addition.
1218 /// The stabilized versions of this intrinsic are available on the integer
1219 /// primitives via the `overflowing_add` method. For example,
1220 /// [`std::u32::overflowing_add`](../../std/primitive.u32.html#method.overflowing_add)
1221 pub fn add_with_overflow<T>(x: T, y: T) -> (T, bool);
1223 /// Performs checked integer subtraction
1224 /// The stabilized versions of this intrinsic are available on the integer
1225 /// primitives via the `overflowing_sub` method. For example,
1226 /// [`std::u32::overflowing_sub`](../../std/primitive.u32.html#method.overflowing_sub)
1227 pub fn sub_with_overflow<T>(x: T, y: T) -> (T, bool);
1229 /// Performs checked integer multiplication
1230 /// The stabilized versions of this intrinsic are available on the integer
1231 /// primitives via the `overflowing_mul` method. For example,
1232 /// [`std::u32::overflowing_mul`](../../std/primitive.u32.html#method.overflowing_mul)
1233 pub fn mul_with_overflow<T>(x: T, y: T) -> (T, bool);
1235 /// Performs an exact division, resulting in undefined behavior where
1236 /// `x % y != 0` or `y == 0` or `x == T::min_value() && y == -1`
1237 pub fn exact_div<T>(x: T, y: T) -> T;
1239 /// Performs an unchecked division, resulting in undefined behavior
1240 /// where y = 0 or x = `T::min_value()` and y = -1
1241 pub fn unchecked_div<T>(x: T, y: T) -> T;
1242 /// Returns the remainder of an unchecked division, resulting in
1243 /// undefined behavior where y = 0 or x = `T::min_value()` and y = -1
1244 pub fn unchecked_rem<T>(x: T, y: T) -> T;
1246 /// Performs an unchecked left shift, resulting in undefined behavior when
1247 /// y < 0 or y >= N, where N is the width of T in bits.
1248 pub fn unchecked_shl<T>(x: T, y: T) -> T;
1249 /// Performs an unchecked right shift, resulting in undefined behavior when
1250 /// y < 0 or y >= N, where N is the width of T in bits.
1251 pub fn unchecked_shr<T>(x: T, y: T) -> T;
1253 /// Returns the result of an unchecked addition, resulting in
1254 /// undefined behavior when `x + y > T::max_value()` or `x + y < T::min_value()`.
1255 pub fn unchecked_add<T>(x: T, y: T) -> T;
1257 /// Returns the result of an unchecked substraction, resulting in
1258 /// undefined behavior when `x - y > T::max_value()` or `x - y < T::min_value()`.
1259 pub fn unchecked_sub<T>(x: T, y: T) -> T;
1261 /// Returns the result of an unchecked multiplication, resulting in
1262 /// undefined behavior when `x * y > T::max_value()` or `x * y < T::min_value()`.
1263 pub fn unchecked_mul<T>(x: T, y: T) -> T;
1265 /// Performs rotate left.
1266 /// The stabilized versions of this intrinsic are available on the integer
1267 /// primitives via the `rotate_left` method. For example,
1268 /// [`std::u32::rotate_left`](../../std/primitive.u32.html#method.rotate_left)
1269 pub fn rotate_left<T>(x: T, y: T) -> T;
1271 /// Performs rotate right.
1272 /// The stabilized versions of this intrinsic are available on the integer
1273 /// primitives via the `rotate_right` method. For example,
1274 /// [`std::u32::rotate_right`](../../std/primitive.u32.html#method.rotate_right)
1275 pub fn rotate_right<T>(x: T, y: T) -> T;
1277 /// Returns (a + b) mod 2<sup>N</sup>, where N is the width of T in bits.
1278 /// The stabilized versions of this intrinsic are available on the integer
1279 /// primitives via the `wrapping_add` method. For example,
1280 /// [`std::u32::wrapping_add`](../../std/primitive.u32.html#method.wrapping_add)
1281 pub fn overflowing_add<T>(a: T, b: T) -> T;
1282 /// Returns (a - b) mod 2<sup>N</sup>, where N is the width of T in bits.
1283 /// The stabilized versions of this intrinsic are available on the integer
1284 /// primitives via the `wrapping_sub` method. For example,
1285 /// [`std::u32::wrapping_sub`](../../std/primitive.u32.html#method.wrapping_sub)
1286 pub fn overflowing_sub<T>(a: T, b: T) -> T;
1287 /// Returns (a * b) mod 2<sup>N</sup>, where N is the width of T in bits.
1288 /// The stabilized versions of this intrinsic are available on the integer
1289 /// primitives via the `wrapping_mul` method. For example,
1290 /// [`std::u32::wrapping_mul`](../../std/primitive.u32.html#method.wrapping_mul)
1291 pub fn overflowing_mul<T>(a: T, b: T) -> T;
1293 /// Computes `a + b`, while saturating at numeric bounds.
1294 /// The stabilized versions of this intrinsic are available on the integer
1295 /// primitives via the `saturating_add` method. For example,
1296 /// [`std::u32::saturating_add`](../../std/primitive.u32.html#method.saturating_add)
1297 pub fn saturating_add<T>(a: T, b: T) -> T;
1298 /// Computes `a - b`, while saturating at numeric bounds.
1299 /// The stabilized versions of this intrinsic are available on the integer
1300 /// primitives via the `saturating_sub` method. For example,
1301 /// [`std::u32::saturating_sub`](../../std/primitive.u32.html#method.saturating_sub)
1302 pub fn saturating_sub<T>(a: T, b: T) -> T;
1304 /// Returns the value of the discriminant for the variant in 'v',
1305 /// cast to a `u64`; if `T` has no discriminant, returns 0.
1306 pub fn discriminant_value<T>(v: &T) -> u64;
1308 /// Rust's "try catch" construct which invokes the function pointer `f` with
1309 /// the data pointer `data`.
1311 /// The third pointer is a target-specific data pointer which is filled in
1312 /// with the specifics of the exception that occurred. For examples on Unix
1313 /// platforms this is a `*mut *mut T` which is filled in by the compiler and
1314 /// on MSVC it's `*mut [usize; 2]`. For more information see the compiler's
1315 /// source as well as std's catch implementation.
1316 pub fn r#try(f: fn(*mut u8), data: *mut u8, local_ptr: *mut u8) -> i32;
1318 /// Emits a `!nontemporal` store according to LLVM (see their docs).
1319 /// Probably will never become stable.
1320 pub fn nontemporal_store<T>(ptr: *mut T, val: T);
1323 // Some functions are defined here because they accidentally got made
1324 // available in this module on stable. See <https://github.com/rust-lang/rust/issues/15702>.
1325 // (`transmute` also falls into this category, but it cannot be wrapped due to the
1326 // check that `T` and `U` have the same size.)
1328 /// Checks whether `ptr` is properly aligned with respect to
1329 /// `align_of::<T>()`.
1330 pub(crate) fn is_aligned_and_not_null<T>(ptr: *const T) -> bool {
1331 !ptr.is_null() && ptr as usize % mem::align_of::<T>() == 0
1334 /// Checks whether the regions of memory starting at `src` and `dst` of size
1335 /// `count * size_of::<T>()` overlap.
1336 fn overlaps<T>(src: *const T, dst: *const T, count: usize) -> bool {
1337 let src_usize = src as usize;
1338 let dst_usize = dst as usize;
1339 let size = mem::size_of::<T>().checked_mul(count).unwrap();
1340 let diff = if src_usize > dst_usize {
1341 src_usize - dst_usize
1343 dst_usize - src_usize
1348 /// Copies `count * size_of::<T>()` bytes from `src` to `dst`. The source
1349 /// and destination must *not* overlap.
1351 /// For regions of memory which might overlap, use [`copy`] instead.
1353 /// `copy_nonoverlapping` is semantically equivalent to C's [`memcpy`], but
1354 /// with the argument order swapped.
1356 /// [`copy`]: ./fn.copy.html
1357 /// [`memcpy`]: https://en.cppreference.com/w/c/string/byte/memcpy
1361 /// Behavior is undefined if any of the following conditions are violated:
1363 /// * `src` must be [valid] for reads of `count * size_of::<T>()` bytes.
1365 /// * `dst` must be [valid] for writes of `count * size_of::<T>()` bytes.
1367 /// * Both `src` and `dst` must be properly aligned.
1369 /// * The region of memory beginning at `src` with a size of `count *
1370 /// size_of::<T>()` bytes must *not* overlap with the region of memory
1371 /// beginning at `dst` with the same size.
1373 /// Like [`read`], `copy_nonoverlapping` creates a bitwise copy of `T`, regardless of
1374 /// whether `T` is [`Copy`]. If `T` is not [`Copy`], using *both* the values
1375 /// in the region beginning at `*src` and the region beginning at `*dst` can
1376 /// [violate memory safety][read-ownership].
1378 /// Note that even if the effectively copied size (`count * size_of::<T>()`) is
1379 /// `0`, the pointers must be non-NULL and properly aligned.
1381 /// [`Copy`]: ../marker/trait.Copy.html
1382 /// [`read`]: ../ptr/fn.read.html
1383 /// [read-ownership]: ../ptr/fn.read.html#ownership-of-the-returned-value
1384 /// [valid]: ../ptr/index.html#safety
1388 /// Manually implement [`Vec::append`]:
1393 /// /// Moves all the elements of `src` into `dst`, leaving `src` empty.
1394 /// fn append<T>(dst: &mut Vec<T>, src: &mut Vec<T>) {
1395 /// let src_len = src.len();
1396 /// let dst_len = dst.len();
1398 /// // Ensure that `dst` has enough capacity to hold all of `src`.
1399 /// dst.reserve(src_len);
1402 /// // The call to offset is always safe because `Vec` will never
1403 /// // allocate more than `isize::MAX` bytes.
1404 /// let dst_ptr = dst.as_mut_ptr().offset(dst_len as isize);
1405 /// let src_ptr = src.as_ptr();
1407 /// // Truncate `src` without dropping its contents. We do this first,
1408 /// // to avoid problems in case something further down panics.
1411 /// // The two regions cannot overlap because mutable references do
1412 /// // not alias, and two different vectors cannot own the same
1414 /// ptr::copy_nonoverlapping(src_ptr, dst_ptr, src_len);
1416 /// // Notify `dst` that it now holds the contents of `src`.
1417 /// dst.set_len(dst_len + src_len);
1421 /// let mut a = vec!['r'];
1422 /// let mut b = vec!['u', 's', 't'];
1424 /// append(&mut a, &mut b);
1426 /// assert_eq!(a, &['r', 'u', 's', 't']);
1427 /// assert!(b.is_empty());
1430 /// [`Vec::append`]: ../../std/vec/struct.Vec.html#method.append
1431 #[stable(feature = "rust1", since = "1.0.0")]
1433 pub unsafe fn copy_nonoverlapping<T>(src: *const T, dst: *mut T, count: usize) {
1434 extern "rust-intrinsic" {
1435 fn copy_nonoverlapping<T>(src: *const T, dst: *mut T, count: usize);
1438 debug_assert!(is_aligned_and_not_null(src), "attempt to copy from unaligned or null pointer");
1439 debug_assert!(is_aligned_and_not_null(dst), "attempt to copy to unaligned or null pointer");
1440 debug_assert!(!overlaps(src, dst, count), "attempt to copy to overlapping memory");
1441 copy_nonoverlapping(src, dst, count)
1444 /// Copies `count * size_of::<T>()` bytes from `src` to `dst`. The source
1445 /// and destination may overlap.
1447 /// If the source and destination will *never* overlap,
1448 /// [`copy_nonoverlapping`] can be used instead.
1450 /// `copy` is semantically equivalent to C's [`memmove`], but with the argument
1451 /// order swapped. Copying takes place as if the bytes were copied from `src`
1452 /// to a temporary array and then copied from the array to `dst`.
1454 /// [`copy_nonoverlapping`]: ./fn.copy_nonoverlapping.html
1455 /// [`memmove`]: https://en.cppreference.com/w/c/string/byte/memmove
1459 /// Behavior is undefined if any of the following conditions are violated:
1461 /// * `src` must be [valid] for reads of `count * size_of::<T>()` bytes.
1463 /// * `dst` must be [valid] for writes of `count * size_of::<T>()` bytes.
1465 /// * Both `src` and `dst` must be properly aligned.
1467 /// Like [`read`], `copy` creates a bitwise copy of `T`, regardless of
1468 /// whether `T` is [`Copy`]. If `T` is not [`Copy`], using both the values
1469 /// in the region beginning at `*src` and the region beginning at `*dst` can
1470 /// [violate memory safety][read-ownership].
1472 /// Note that even if the effectively copied size (`count * size_of::<T>()`) is
1473 /// `0`, the pointers must be non-NULL and properly aligned.
1475 /// [`Copy`]: ../marker/trait.Copy.html
1476 /// [`read`]: ../ptr/fn.read.html
1477 /// [read-ownership]: ../ptr/fn.read.html#ownership-of-the-returned-value
1478 /// [valid]: ../ptr/index.html#safety
1482 /// Efficiently create a Rust vector from an unsafe buffer:
1487 /// # #[allow(dead_code)]
1488 /// unsafe fn from_buf_raw<T>(ptr: *const T, elts: usize) -> Vec<T> {
1489 /// let mut dst = Vec::with_capacity(elts);
1490 /// dst.set_len(elts);
1491 /// ptr::copy(ptr, dst.as_mut_ptr(), elts);
1495 #[stable(feature = "rust1", since = "1.0.0")]
1497 pub unsafe fn copy<T>(src: *const T, dst: *mut T, count: usize) {
1498 extern "rust-intrinsic" {
1499 fn copy<T>(src: *const T, dst: *mut T, count: usize);
1502 debug_assert!(is_aligned_and_not_null(src), "attempt to copy from unaligned or null pointer");
1503 debug_assert!(is_aligned_and_not_null(dst), "attempt to copy to unaligned or null pointer");
1504 copy(src, dst, count)
1507 /// Sets `count * size_of::<T>()` bytes of memory starting at `dst` to
1510 /// `write_bytes` is similar to C's [`memset`], but sets `count *
1511 /// size_of::<T>()` bytes to `val`.
1513 /// [`memset`]: https://en.cppreference.com/w/c/string/byte/memset
1517 /// Behavior is undefined if any of the following conditions are violated:
1519 /// * `dst` must be [valid] for writes of `count * size_of::<T>()` bytes.
1521 /// * `dst` must be properly aligned.
1523 /// Additionally, the caller must ensure that writing `count *
1524 /// size_of::<T>()` bytes to the given region of memory results in a valid
1525 /// value of `T`. Using a region of memory typed as a `T` that contains an
1526 /// invalid value of `T` is undefined behavior.
1528 /// Note that even if the effectively copied size (`count * size_of::<T>()`) is
1529 /// `0`, the pointer must be non-NULL and properly aligned.
1531 /// [valid]: ../ptr/index.html#safety
1540 /// let mut vec = vec![0u32; 4];
1542 /// let vec_ptr = vec.as_mut_ptr();
1543 /// ptr::write_bytes(vec_ptr, 0xfe, 2);
1545 /// assert_eq!(vec, [0xfefefefe, 0xfefefefe, 0, 0]);
1548 /// Creating an invalid value:
1553 /// let mut v = Box::new(0i32);
1556 /// // Leaks the previously held value by overwriting the `Box<T>` with
1557 /// // a null pointer.
1558 /// ptr::write_bytes(&mut v as *mut Box<i32>, 0, 1);
1561 /// // At this point, using or dropping `v` results in undefined behavior.
1562 /// // drop(v); // ERROR
1564 /// // Even leaking `v` "uses" it, and hence is undefined behavior.
1565 /// // mem::forget(v); // ERROR
1567 /// // In fact, `v` is invalid according to basic type layout invariants, so *any*
1568 /// // operation touching it is undefined behavior.
1569 /// // let v2 = v; // ERROR
1572 /// // Let us instead put in a valid value
1573 /// ptr::write(&mut v as *mut Box<i32>, Box::new(42i32));
1576 /// // Now the box is fine
1577 /// assert_eq!(*v, 42);
1579 #[stable(feature = "rust1", since = "1.0.0")]
1581 pub unsafe fn write_bytes<T>(dst: *mut T, val: u8, count: usize) {
1582 extern "rust-intrinsic" {
1583 fn write_bytes<T>(dst: *mut T, val: u8, count: usize);
1586 debug_assert!(is_aligned_and_not_null(dst), "attempt to write to unaligned or null pointer");
1587 write_bytes(dst, val, count)