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 /// Gets a reference to a static `Location` indicating where it was called.
700 pub fn caller_location() -> &'static crate::panic::Location<'static>;
702 /// Creates a value initialized to zero.
704 /// `init` is unsafe because it returns a zeroed-out datum,
705 /// which is unsafe unless `T` is `Copy`. Also, even if T is
706 /// `Copy`, an all-zero value may not correspond to any legitimate
707 /// state for the type in question.
708 #[unstable(feature = "core_intrinsics",
709 reason = "intrinsics are unlikely to ever be stabilized, instead \
710 they should be used through stabilized interfaces \
711 in the rest of the standard library",
713 #[rustc_deprecated(reason = "superseded by MaybeUninit, removal planned",
715 pub fn init<T>() -> T;
717 /// Creates an uninitialized value.
719 /// `uninit` is unsafe because there is no guarantee of what its
720 /// contents are. In particular its drop-flag may be set to any
721 /// state, which means it may claim either dropped or
722 /// undropped. In the general case one must use `ptr::write` to
723 /// initialize memory previous set to the result of `uninit`.
724 #[unstable(feature = "core_intrinsics",
725 reason = "intrinsics are unlikely to ever be stabilized, instead \
726 they should be used through stabilized interfaces \
727 in the rest of the standard library",
729 #[rustc_deprecated(reason = "superseded by MaybeUninit, removal planned",
731 pub fn uninit<T>() -> T;
733 /// Moves a value out of scope without running drop glue.
734 pub fn forget<T: ?Sized>(_: T);
736 /// Reinterprets the bits of a value of one type as another type.
738 /// Both types must have the same size. Neither the original, nor the result,
739 /// may be an [invalid value](../../nomicon/what-unsafe-does.html).
741 /// `transmute` is semantically equivalent to a bitwise move of one type
742 /// into another. It copies the bits from the source value into the
743 /// destination value, then forgets the original. It's equivalent to C's
744 /// `memcpy` under the hood, just like `transmute_copy`.
746 /// `transmute` is **incredibly** unsafe. There are a vast number of ways to
747 /// cause [undefined behavior][ub] with this function. `transmute` should be
748 /// the absolute last resort.
750 /// The [nomicon](../../nomicon/transmutes.html) has additional
753 /// [ub]: ../../reference/behavior-considered-undefined.html
757 /// There are a few things that `transmute` is really useful for.
759 /// Turning a pointer into a function pointer. This is *not* portable to
760 /// machines where function pointers and data pointers have different sizes.
763 /// fn foo() -> i32 {
766 /// let pointer = foo as *const ();
767 /// let function = unsafe {
768 /// std::mem::transmute::<*const (), fn() -> i32>(pointer)
770 /// assert_eq!(function(), 0);
773 /// Extending a lifetime, or shortening an invariant lifetime. This is
774 /// advanced, very unsafe Rust!
777 /// struct R<'a>(&'a i32);
778 /// unsafe fn extend_lifetime<'b>(r: R<'b>) -> R<'static> {
779 /// std::mem::transmute::<R<'b>, R<'static>>(r)
782 /// unsafe fn shorten_invariant_lifetime<'b, 'c>(r: &'b mut R<'static>)
783 /// -> &'b mut R<'c> {
784 /// std::mem::transmute::<&'b mut R<'static>, &'b mut R<'c>>(r)
790 /// Don't despair: many uses of `transmute` can be achieved through other means.
791 /// Below are common applications of `transmute` which can be replaced with safer
794 /// Turning a pointer into a `usize`:
798 /// let ptr_num_transmute = unsafe {
799 /// std::mem::transmute::<&i32, usize>(ptr)
802 /// // Use an `as` cast instead
803 /// let ptr_num_cast = ptr as *const i32 as usize;
806 /// Turning a `*mut T` into an `&mut T`:
809 /// let ptr: *mut i32 = &mut 0;
810 /// let ref_transmuted = unsafe {
811 /// std::mem::transmute::<*mut i32, &mut i32>(ptr)
814 /// // Use a reborrow instead
815 /// let ref_casted = unsafe { &mut *ptr };
818 /// Turning an `&mut T` into an `&mut U`:
821 /// let ptr = &mut 0;
822 /// let val_transmuted = unsafe {
823 /// std::mem::transmute::<&mut i32, &mut u32>(ptr)
826 /// // Now, put together `as` and reborrowing - note the chaining of `as`
827 /// // `as` is not transitive
828 /// let val_casts = unsafe { &mut *(ptr as *mut i32 as *mut u32) };
831 /// Turning an `&str` into an `&[u8]`:
834 /// // this is not a good way to do this.
835 /// let slice = unsafe { std::mem::transmute::<&str, &[u8]>("Rust") };
836 /// assert_eq!(slice, &[82, 117, 115, 116]);
838 /// // You could use `str::as_bytes`
839 /// let slice = "Rust".as_bytes();
840 /// assert_eq!(slice, &[82, 117, 115, 116]);
842 /// // Or, just use a byte string, if you have control over the string
844 /// assert_eq!(b"Rust", &[82, 117, 115, 116]);
847 /// Turning a `Vec<&T>` into a `Vec<Option<&T>>`:
850 /// let store = [0, 1, 2, 3];
851 /// let v_orig = store.iter().collect::<Vec<&i32>>();
853 /// // clone the vector as we will reuse them later
854 /// let v_clone = v_orig.clone();
856 /// // Using transmute: this is Undefined Behavior, and a bad idea.
857 /// // However, it is no-copy.
858 /// let v_transmuted = unsafe {
859 /// std::mem::transmute::<Vec<&i32>, Vec<Option<&i32>>>(v_clone)
862 /// let v_clone = v_orig.clone();
864 /// // This is the suggested, safe way.
865 /// // It does copy the entire vector, though, into a new array.
866 /// let v_collected = v_clone.into_iter()
868 /// .collect::<Vec<Option<&i32>>>();
870 /// let v_clone = v_orig.clone();
872 /// // The no-copy, unsafe way, still using transmute, but not UB.
873 /// // This is equivalent to the original, but safer, and reuses the
874 /// // same `Vec` internals. Therefore, the new inner type must have the
875 /// // exact same size, and the same alignment, as the old type.
876 /// // The same caveats exist for this method as transmute, for
877 /// // the original inner type (`&i32`) to the converted inner type
878 /// // (`Option<&i32>`), so read the nomicon pages linked above.
879 /// let v_from_raw = unsafe {
880 // FIXME Update this when vec_into_raw_parts is stabilized
881 /// // Ensure the original vector is not dropped.
882 /// let mut v_clone = std::mem::ManuallyDrop::new(v_clone);
883 /// Vec::from_raw_parts(v_clone.as_mut_ptr() as *mut Option<&i32>,
885 /// v_clone.capacity())
889 /// Implementing `split_at_mut`:
892 /// use std::{slice, mem};
894 /// // There are multiple ways to do this, and there are multiple problems
895 /// // with the following (transmute) way.
896 /// fn split_at_mut_transmute<T>(slice: &mut [T], mid: usize)
897 /// -> (&mut [T], &mut [T]) {
898 /// let len = slice.len();
899 /// assert!(mid <= len);
901 /// let slice2 = mem::transmute::<&mut [T], &mut [T]>(slice);
902 /// // first: transmute is not typesafe; all it checks is that T and
903 /// // U are of the same size. Second, right here, you have two
904 /// // mutable references pointing to the same memory.
905 /// (&mut slice[0..mid], &mut slice2[mid..len])
909 /// // This gets rid of the typesafety problems; `&mut *` will *only* give
910 /// // you an `&mut T` from an `&mut T` or `*mut T`.
911 /// fn split_at_mut_casts<T>(slice: &mut [T], mid: usize)
912 /// -> (&mut [T], &mut [T]) {
913 /// let len = slice.len();
914 /// assert!(mid <= len);
916 /// let slice2 = &mut *(slice as *mut [T]);
917 /// // however, you still have two mutable references pointing to
918 /// // the same memory.
919 /// (&mut slice[0..mid], &mut slice2[mid..len])
923 /// // This is how the standard library does it. This is the best method, if
924 /// // you need to do something like this
925 /// fn split_at_stdlib<T>(slice: &mut [T], mid: usize)
926 /// -> (&mut [T], &mut [T]) {
927 /// let len = slice.len();
928 /// assert!(mid <= len);
930 /// let ptr = slice.as_mut_ptr();
931 /// // This now has three mutable references pointing at the same
932 /// // memory. `slice`, the rvalue ret.0, and the rvalue ret.1.
933 /// // `slice` is never used after `let ptr = ...`, and so one can
934 /// // treat it as "dead", and therefore, you only have two real
935 /// // mutable slices.
936 /// (slice::from_raw_parts_mut(ptr, mid),
937 /// slice::from_raw_parts_mut(ptr.add(mid), len - mid))
941 #[stable(feature = "rust1", since = "1.0.0")]
942 #[rustc_const_unstable(feature = "const_transmute", issue = "53605")]
943 pub fn transmute<T, U>(e: T) -> U;
945 /// Returns `true` if the actual type given as `T` requires drop
946 /// glue; returns `false` if the actual type provided for `T`
947 /// implements `Copy`.
949 /// If the actual type neither requires drop glue nor implements
950 /// `Copy`, then may return `true` or `false`.
952 /// The stabilized version of this intrinsic is
953 /// [`std::mem::needs_drop`](../../std/mem/fn.needs_drop.html).
954 pub fn needs_drop<T>() -> bool;
956 /// Calculates the offset from a pointer.
958 /// This is implemented as an intrinsic to avoid converting to and from an
959 /// integer, since the conversion would throw away aliasing information.
963 /// Both the starting and resulting pointer must be either in bounds or one
964 /// byte past the end of an allocated object. If either pointer is out of
965 /// bounds or arithmetic overflow occurs then any further use of the
966 /// returned value will result in undefined behavior.
967 pub fn offset<T>(dst: *const T, offset: isize) -> *const T;
969 /// Calculates the offset from a pointer, potentially wrapping.
971 /// This is implemented as an intrinsic to avoid converting to and from an
972 /// integer, since the conversion inhibits certain optimizations.
976 /// Unlike the `offset` intrinsic, this intrinsic does not restrict the
977 /// resulting pointer to point into or one byte past the end of an allocated
978 /// object, and it wraps with two's complement arithmetic. The resulting
979 /// value is not necessarily valid to be used to actually access memory.
980 pub fn arith_offset<T>(dst: *const T, offset: isize) -> *const T;
982 /// Equivalent to the appropriate `llvm.memcpy.p0i8.0i8.*` intrinsic, with
983 /// a size of `count` * `size_of::<T>()` and an alignment of
984 /// `min_align_of::<T>()`
986 /// The volatile parameter is set to `true`, so it will not be optimized out
987 /// unless size is equal to zero.
988 pub fn volatile_copy_nonoverlapping_memory<T>(dst: *mut T, src: *const T,
990 /// Equivalent to the appropriate `llvm.memmove.p0i8.0i8.*` intrinsic, with
991 /// a size of `count` * `size_of::<T>()` and an alignment of
992 /// `min_align_of::<T>()`
994 /// The volatile parameter is set to `true`, so it will not be optimized out
995 /// unless size is equal to zero.
996 pub fn volatile_copy_memory<T>(dst: *mut T, src: *const T, count: usize);
997 /// Equivalent to the appropriate `llvm.memset.p0i8.*` intrinsic, with a
998 /// size of `count` * `size_of::<T>()` and an alignment of
999 /// `min_align_of::<T>()`.
1001 /// The volatile parameter is set to `true`, so it will not be optimized out
1002 /// unless size is equal to zero.
1003 pub fn volatile_set_memory<T>(dst: *mut T, val: u8, count: usize);
1005 /// Performs a volatile load from the `src` pointer.
1006 /// The stabilized version of this intrinsic is
1007 /// [`std::ptr::read_volatile`](../../std/ptr/fn.read_volatile.html).
1008 pub fn volatile_load<T>(src: *const T) -> T;
1009 /// Performs a volatile store to the `dst` pointer.
1010 /// The stabilized version of this intrinsic is
1011 /// [`std::ptr::write_volatile`](../../std/ptr/fn.write_volatile.html).
1012 pub fn volatile_store<T>(dst: *mut T, val: T);
1014 /// Performs a volatile load from the `src` pointer
1015 /// The pointer is not required to be aligned.
1016 pub fn unaligned_volatile_load<T>(src: *const T) -> T;
1017 /// Performs a volatile store to the `dst` pointer.
1018 /// The pointer is not required to be aligned.
1019 pub fn unaligned_volatile_store<T>(dst: *mut T, val: T);
1021 /// Returns the square root of an `f32`
1022 pub fn sqrtf32(x: f32) -> f32;
1023 /// Returns the square root of an `f64`
1024 pub fn sqrtf64(x: f64) -> f64;
1026 /// Raises an `f32` to an integer power.
1027 pub fn powif32(a: f32, x: i32) -> f32;
1028 /// Raises an `f64` to an integer power.
1029 pub fn powif64(a: f64, x: i32) -> f64;
1031 /// Returns the sine of an `f32`.
1032 pub fn sinf32(x: f32) -> f32;
1033 /// Returns the sine of an `f64`.
1034 pub fn sinf64(x: f64) -> f64;
1036 /// Returns the cosine of an `f32`.
1037 pub fn cosf32(x: f32) -> f32;
1038 /// Returns the cosine of an `f64`.
1039 pub fn cosf64(x: f64) -> f64;
1041 /// Raises an `f32` to an `f32` power.
1042 pub fn powf32(a: f32, x: f32) -> f32;
1043 /// Raises an `f64` to an `f64` power.
1044 pub fn powf64(a: f64, x: f64) -> f64;
1046 /// Returns the exponential of an `f32`.
1047 pub fn expf32(x: f32) -> f32;
1048 /// Returns the exponential of an `f64`.
1049 pub fn expf64(x: f64) -> f64;
1051 /// Returns 2 raised to the power of an `f32`.
1052 pub fn exp2f32(x: f32) -> f32;
1053 /// Returns 2 raised to the power of an `f64`.
1054 pub fn exp2f64(x: f64) -> f64;
1056 /// Returns the natural logarithm of an `f32`.
1057 pub fn logf32(x: f32) -> f32;
1058 /// Returns the natural logarithm of an `f64`.
1059 pub fn logf64(x: f64) -> f64;
1061 /// Returns the base 10 logarithm of an `f32`.
1062 pub fn log10f32(x: f32) -> f32;
1063 /// Returns the base 10 logarithm of an `f64`.
1064 pub fn log10f64(x: f64) -> f64;
1066 /// Returns the base 2 logarithm of an `f32`.
1067 pub fn log2f32(x: f32) -> f32;
1068 /// Returns the base 2 logarithm of an `f64`.
1069 pub fn log2f64(x: f64) -> f64;
1071 /// Returns `a * b + c` for `f32` values.
1072 pub fn fmaf32(a: f32, b: f32, c: f32) -> f32;
1073 /// Returns `a * b + c` for `f64` values.
1074 pub fn fmaf64(a: f64, b: f64, c: f64) -> f64;
1076 /// Returns the absolute value of an `f32`.
1077 pub fn fabsf32(x: f32) -> f32;
1078 /// Returns the absolute value of an `f64`.
1079 pub fn fabsf64(x: f64) -> f64;
1081 /// Returns the minimum of two `f32` values.
1082 pub fn minnumf32(x: f32, y: f32) -> f32;
1083 /// Returns the minimum of two `f64` values.
1084 pub fn minnumf64(x: f64, y: f64) -> f64;
1085 /// Returns the maximum of two `f32` values.
1086 pub fn maxnumf32(x: f32, y: f32) -> f32;
1087 /// Returns the maximum of two `f64` values.
1088 pub fn maxnumf64(x: f64, y: f64) -> f64;
1090 /// Copies the sign from `y` to `x` for `f32` values.
1091 pub fn copysignf32(x: f32, y: f32) -> f32;
1092 /// Copies the sign from `y` to `x` for `f64` values.
1093 pub fn copysignf64(x: f64, y: f64) -> f64;
1095 /// Returns the largest integer less than or equal to an `f32`.
1096 pub fn floorf32(x: f32) -> f32;
1097 /// Returns the largest integer less than or equal to an `f64`.
1098 pub fn floorf64(x: f64) -> f64;
1100 /// Returns the smallest integer greater than or equal to an `f32`.
1101 pub fn ceilf32(x: f32) -> f32;
1102 /// Returns the smallest integer greater than or equal to an `f64`.
1103 pub fn ceilf64(x: f64) -> f64;
1105 /// Returns the integer part of an `f32`.
1106 pub fn truncf32(x: f32) -> f32;
1107 /// Returns the integer part of an `f64`.
1108 pub fn truncf64(x: f64) -> f64;
1110 /// Returns the nearest integer to an `f32`. May raise an inexact floating-point exception
1111 /// if the argument is not an integer.
1112 pub fn rintf32(x: f32) -> f32;
1113 /// Returns the nearest integer to an `f64`. May raise an inexact floating-point exception
1114 /// if the argument is not an integer.
1115 pub fn rintf64(x: f64) -> f64;
1117 /// Returns the nearest integer to an `f32`.
1118 pub fn nearbyintf32(x: f32) -> f32;
1119 /// Returns the nearest integer to an `f64`.
1120 pub fn nearbyintf64(x: f64) -> f64;
1122 /// Returns the nearest integer to an `f32`. Rounds half-way cases away from zero.
1123 pub fn roundf32(x: f32) -> f32;
1124 /// Returns the nearest integer to an `f64`. Rounds half-way cases away from zero.
1125 pub fn roundf64(x: f64) -> f64;
1127 /// Float addition that allows optimizations based on algebraic rules.
1128 /// May assume inputs are finite.
1129 pub fn fadd_fast<T>(a: T, b: T) -> T;
1131 /// Float subtraction that allows optimizations based on algebraic rules.
1132 /// May assume inputs are finite.
1133 pub fn fsub_fast<T>(a: T, b: T) -> T;
1135 /// Float multiplication that allows optimizations based on algebraic rules.
1136 /// May assume inputs are finite.
1137 pub fn fmul_fast<T>(a: T, b: T) -> T;
1139 /// Float division that allows optimizations based on algebraic rules.
1140 /// May assume inputs are finite.
1141 pub fn fdiv_fast<T>(a: T, b: T) -> T;
1143 /// Float remainder that allows optimizations based on algebraic rules.
1144 /// May assume inputs are finite.
1145 pub fn frem_fast<T>(a: T, b: T) -> T;
1147 /// Convert with LLVM’s fptoui/fptosi, which may return undef for values out of range
1148 /// https://github.com/rust-lang/rust/issues/10184
1149 pub fn float_to_int_approx_unchecked<Float, Int>(value: Float) -> Int;
1152 /// Returns the number of bits set in an integer type `T`
1153 pub fn ctpop<T>(x: T) -> T;
1155 /// Returns the number of leading unset bits (zeroes) in an integer type `T`.
1160 /// #![feature(core_intrinsics)]
1162 /// use std::intrinsics::ctlz;
1164 /// let x = 0b0001_1100_u8;
1165 /// let num_leading = ctlz(x);
1166 /// assert_eq!(num_leading, 3);
1169 /// An `x` with value `0` will return the bit width of `T`.
1172 /// #![feature(core_intrinsics)]
1174 /// use std::intrinsics::ctlz;
1177 /// let num_leading = ctlz(x);
1178 /// assert_eq!(num_leading, 16);
1180 pub fn ctlz<T>(x: T) -> T;
1182 /// Like `ctlz`, but extra-unsafe as it returns `undef` when
1183 /// given an `x` with value `0`.
1188 /// #![feature(core_intrinsics)]
1190 /// use std::intrinsics::ctlz_nonzero;
1192 /// let x = 0b0001_1100_u8;
1193 /// let num_leading = unsafe { ctlz_nonzero(x) };
1194 /// assert_eq!(num_leading, 3);
1196 pub fn ctlz_nonzero<T>(x: T) -> T;
1198 /// Returns the number of trailing unset bits (zeroes) in an integer type `T`.
1203 /// #![feature(core_intrinsics)]
1205 /// use std::intrinsics::cttz;
1207 /// let x = 0b0011_1000_u8;
1208 /// let num_trailing = cttz(x);
1209 /// assert_eq!(num_trailing, 3);
1212 /// An `x` with value `0` will return the bit width of `T`:
1215 /// #![feature(core_intrinsics)]
1217 /// use std::intrinsics::cttz;
1220 /// let num_trailing = cttz(x);
1221 /// assert_eq!(num_trailing, 16);
1223 pub fn cttz<T>(x: T) -> T;
1225 /// Like `cttz`, but extra-unsafe as it returns `undef` when
1226 /// given an `x` with value `0`.
1231 /// #![feature(core_intrinsics)]
1233 /// use std::intrinsics::cttz_nonzero;
1235 /// let x = 0b0011_1000_u8;
1236 /// let num_trailing = unsafe { cttz_nonzero(x) };
1237 /// assert_eq!(num_trailing, 3);
1239 pub fn cttz_nonzero<T>(x: T) -> T;
1241 /// Reverses the bytes in an integer type `T`.
1242 pub fn bswap<T>(x: T) -> T;
1244 /// Reverses the bits in an integer type `T`.
1245 pub fn bitreverse<T>(x: T) -> T;
1247 /// Performs checked integer addition.
1248 /// The stabilized versions of this intrinsic are available on the integer
1249 /// primitives via the `overflowing_add` method. For example,
1250 /// [`std::u32::overflowing_add`](../../std/primitive.u32.html#method.overflowing_add)
1251 pub fn add_with_overflow<T>(x: T, y: T) -> (T, bool);
1253 /// Performs checked integer subtraction
1254 /// The stabilized versions of this intrinsic are available on the integer
1255 /// primitives via the `overflowing_sub` method. For example,
1256 /// [`std::u32::overflowing_sub`](../../std/primitive.u32.html#method.overflowing_sub)
1257 pub fn sub_with_overflow<T>(x: T, y: T) -> (T, bool);
1259 /// Performs checked integer multiplication
1260 /// The stabilized versions of this intrinsic are available on the integer
1261 /// primitives via the `overflowing_mul` method. For example,
1262 /// [`std::u32::overflowing_mul`](../../std/primitive.u32.html#method.overflowing_mul)
1263 pub fn mul_with_overflow<T>(x: T, y: T) -> (T, bool);
1265 /// Performs an exact division, resulting in undefined behavior where
1266 /// `x % y != 0` or `y == 0` or `x == T::min_value() && y == -1`
1267 pub fn exact_div<T>(x: T, y: T) -> T;
1269 /// Performs an unchecked division, resulting in undefined behavior
1270 /// where y = 0 or x = `T::min_value()` and y = -1
1271 pub fn unchecked_div<T>(x: T, y: T) -> T;
1272 /// Returns the remainder of an unchecked division, resulting in
1273 /// undefined behavior where y = 0 or x = `T::min_value()` and y = -1
1274 pub fn unchecked_rem<T>(x: T, y: T) -> T;
1276 /// Performs an unchecked left shift, resulting in undefined behavior when
1277 /// y < 0 or y >= N, where N is the width of T in bits.
1278 pub fn unchecked_shl<T>(x: T, y: T) -> T;
1279 /// Performs an unchecked right shift, resulting in undefined behavior when
1280 /// y < 0 or y >= N, where N is the width of T in bits.
1281 pub fn unchecked_shr<T>(x: T, y: T) -> T;
1283 /// Returns the result of an unchecked addition, resulting in
1284 /// undefined behavior when `x + y > T::max_value()` or `x + y < T::min_value()`.
1285 pub fn unchecked_add<T>(x: T, y: T) -> T;
1287 /// Returns the result of an unchecked subtraction, resulting in
1288 /// undefined behavior when `x - y > T::max_value()` or `x - y < T::min_value()`.
1289 pub fn unchecked_sub<T>(x: T, y: T) -> T;
1291 /// Returns the result of an unchecked multiplication, resulting in
1292 /// undefined behavior when `x * y > T::max_value()` or `x * y < T::min_value()`.
1293 pub fn unchecked_mul<T>(x: T, y: T) -> T;
1295 /// Performs rotate left.
1296 /// The stabilized versions of this intrinsic are available on the integer
1297 /// primitives via the `rotate_left` method. For example,
1298 /// [`std::u32::rotate_left`](../../std/primitive.u32.html#method.rotate_left)
1299 pub fn rotate_left<T>(x: T, y: T) -> T;
1301 /// Performs rotate right.
1302 /// The stabilized versions of this intrinsic are available on the integer
1303 /// primitives via the `rotate_right` method. For example,
1304 /// [`std::u32::rotate_right`](../../std/primitive.u32.html#method.rotate_right)
1305 pub fn rotate_right<T>(x: T, y: T) -> T;
1307 /// Returns (a + b) mod 2<sup>N</sup>, where N is the width of T in bits.
1308 /// The stabilized versions of this intrinsic are available on the integer
1309 /// primitives via the `wrapping_add` method. For example,
1310 /// [`std::u32::wrapping_add`](../../std/primitive.u32.html#method.wrapping_add)
1311 pub fn wrapping_add<T>(a: T, b: T) -> T;
1312 /// Returns (a - b) mod 2<sup>N</sup>, where N is the width of T in bits.
1313 /// The stabilized versions of this intrinsic are available on the integer
1314 /// primitives via the `wrapping_sub` method. For example,
1315 /// [`std::u32::wrapping_sub`](../../std/primitive.u32.html#method.wrapping_sub)
1316 pub fn wrapping_sub<T>(a: T, b: T) -> T;
1317 /// Returns (a * b) mod 2<sup>N</sup>, where N is the width of T in bits.
1318 /// The stabilized versions of this intrinsic are available on the integer
1319 /// primitives via the `wrapping_mul` method. For example,
1320 /// [`std::u32::wrapping_mul`](../../std/primitive.u32.html#method.wrapping_mul)
1321 pub fn wrapping_mul<T>(a: T, b: T) -> T;
1323 /// Computes `a + b`, while saturating at numeric bounds.
1324 /// The stabilized versions of this intrinsic are available on the integer
1325 /// primitives via the `saturating_add` method. For example,
1326 /// [`std::u32::saturating_add`](../../std/primitive.u32.html#method.saturating_add)
1327 pub fn saturating_add<T>(a: T, b: T) -> T;
1328 /// Computes `a - b`, while saturating at numeric bounds.
1329 /// The stabilized versions of this intrinsic are available on the integer
1330 /// primitives via the `saturating_sub` method. For example,
1331 /// [`std::u32::saturating_sub`](../../std/primitive.u32.html#method.saturating_sub)
1332 pub fn saturating_sub<T>(a: T, b: T) -> T;
1334 /// Returns the value of the discriminant for the variant in 'v',
1335 /// cast to a `u64`; if `T` has no discriminant, returns 0.
1336 pub fn discriminant_value<T>(v: &T) -> u64;
1338 /// Rust's "try catch" construct which invokes the function pointer `f` with
1339 /// the data pointer `data`.
1341 /// The third pointer is a target-specific data pointer which is filled in
1342 /// with the specifics of the exception that occurred. For examples on Unix
1343 /// platforms this is a `*mut *mut T` which is filled in by the compiler and
1344 /// on MSVC it's `*mut [usize; 2]`. For more information see the compiler's
1345 /// source as well as std's catch implementation.
1346 pub fn r#try(f: fn(*mut u8), data: *mut u8, local_ptr: *mut u8) -> i32;
1348 /// Emits a `!nontemporal` store according to LLVM (see their docs).
1349 /// Probably will never become stable.
1350 pub fn nontemporal_store<T>(ptr: *mut T, val: T);
1352 /// See documentation of `<*const T>::offset_from` for details.
1353 pub fn ptr_offset_from<T>(ptr: *const T, base: *const T) -> isize;
1355 /// Internal hook used by Miri to implement unwinding.
1356 /// Compiles to a NOP during non-Miri codegen.
1358 /// Perma-unstable: do not use
1359 pub fn miri_start_panic(data: *mut (dyn crate::any::Any + crate::marker::Send)) -> ();
1362 // Some functions are defined here because they accidentally got made
1363 // available in this module on stable. See <https://github.com/rust-lang/rust/issues/15702>.
1364 // (`transmute` also falls into this category, but it cannot be wrapped due to the
1365 // check that `T` and `U` have the same size.)
1367 /// Checks whether `ptr` is properly aligned with respect to
1368 /// `align_of::<T>()`.
1369 pub(crate) fn is_aligned_and_not_null<T>(ptr: *const T) -> bool {
1370 !ptr.is_null() && ptr as usize % mem::align_of::<T>() == 0
1373 /// Checks whether the regions of memory starting at `src` and `dst` of size
1374 /// `count * size_of::<T>()` overlap.
1375 fn overlaps<T>(src: *const T, dst: *const T, count: usize) -> bool {
1376 let src_usize = src as usize;
1377 let dst_usize = dst as usize;
1378 let size = mem::size_of::<T>().checked_mul(count).unwrap();
1379 let diff = if src_usize > dst_usize {
1380 src_usize - dst_usize
1382 dst_usize - src_usize
1387 /// Copies `count * size_of::<T>()` bytes from `src` to `dst`. The source
1388 /// and destination must *not* overlap.
1390 /// For regions of memory which might overlap, use [`copy`] instead.
1392 /// `copy_nonoverlapping` is semantically equivalent to C's [`memcpy`], but
1393 /// with the argument order swapped.
1395 /// [`copy`]: ./fn.copy.html
1396 /// [`memcpy`]: https://en.cppreference.com/w/c/string/byte/memcpy
1400 /// Behavior is undefined if any of the following conditions are violated:
1402 /// * `src` must be [valid] for reads of `count * size_of::<T>()` bytes.
1404 /// * `dst` must be [valid] for writes of `count * size_of::<T>()` bytes.
1406 /// * Both `src` and `dst` must be properly aligned.
1408 /// * The region of memory beginning at `src` with a size of `count *
1409 /// size_of::<T>()` bytes must *not* overlap with the region of memory
1410 /// beginning at `dst` with the same size.
1412 /// Like [`read`], `copy_nonoverlapping` creates a bitwise copy of `T`, regardless of
1413 /// whether `T` is [`Copy`]. If `T` is not [`Copy`], using *both* the values
1414 /// in the region beginning at `*src` and the region beginning at `*dst` can
1415 /// [violate memory safety][read-ownership].
1417 /// Note that even if the effectively copied size (`count * size_of::<T>()`) is
1418 /// `0`, the pointers must be non-NULL and properly aligned.
1420 /// [`Copy`]: ../marker/trait.Copy.html
1421 /// [`read`]: ../ptr/fn.read.html
1422 /// [read-ownership]: ../ptr/fn.read.html#ownership-of-the-returned-value
1423 /// [valid]: ../ptr/index.html#safety
1427 /// Manually implement [`Vec::append`]:
1432 /// /// Moves all the elements of `src` into `dst`, leaving `src` empty.
1433 /// fn append<T>(dst: &mut Vec<T>, src: &mut Vec<T>) {
1434 /// let src_len = src.len();
1435 /// let dst_len = dst.len();
1437 /// // Ensure that `dst` has enough capacity to hold all of `src`.
1438 /// dst.reserve(src_len);
1441 /// // The call to offset is always safe because `Vec` will never
1442 /// // allocate more than `isize::MAX` bytes.
1443 /// let dst_ptr = dst.as_mut_ptr().offset(dst_len as isize);
1444 /// let src_ptr = src.as_ptr();
1446 /// // Truncate `src` without dropping its contents. We do this first,
1447 /// // to avoid problems in case something further down panics.
1450 /// // The two regions cannot overlap because mutable references do
1451 /// // not alias, and two different vectors cannot own the same
1453 /// ptr::copy_nonoverlapping(src_ptr, dst_ptr, src_len);
1455 /// // Notify `dst` that it now holds the contents of `src`.
1456 /// dst.set_len(dst_len + src_len);
1460 /// let mut a = vec!['r'];
1461 /// let mut b = vec!['u', 's', 't'];
1463 /// append(&mut a, &mut b);
1465 /// assert_eq!(a, &['r', 'u', 's', 't']);
1466 /// assert!(b.is_empty());
1469 /// [`Vec::append`]: ../../std/vec/struct.Vec.html#method.append
1470 #[stable(feature = "rust1", since = "1.0.0")]
1472 pub unsafe fn copy_nonoverlapping<T>(src: *const T, dst: *mut T, count: usize) {
1473 extern "rust-intrinsic" {
1474 fn copy_nonoverlapping<T>(src: *const T, dst: *mut T, count: usize);
1477 debug_assert!(is_aligned_and_not_null(src), "attempt to copy from unaligned or null pointer");
1478 debug_assert!(is_aligned_and_not_null(dst), "attempt to copy to unaligned or null pointer");
1479 debug_assert!(!overlaps(src, dst, count), "attempt to copy to overlapping memory");
1480 copy_nonoverlapping(src, dst, count)
1483 /// Copies `count * size_of::<T>()` bytes from `src` to `dst`. The source
1484 /// and destination may overlap.
1486 /// If the source and destination will *never* overlap,
1487 /// [`copy_nonoverlapping`] can be used instead.
1489 /// `copy` is semantically equivalent to C's [`memmove`], but with the argument
1490 /// order swapped. Copying takes place as if the bytes were copied from `src`
1491 /// to a temporary array and then copied from the array to `dst`.
1493 /// [`copy_nonoverlapping`]: ./fn.copy_nonoverlapping.html
1494 /// [`memmove`]: https://en.cppreference.com/w/c/string/byte/memmove
1498 /// Behavior is undefined if any of the following conditions are violated:
1500 /// * `src` must be [valid] for reads of `count * size_of::<T>()` bytes.
1502 /// * `dst` must be [valid] for writes of `count * size_of::<T>()` bytes.
1504 /// * Both `src` and `dst` must be properly aligned.
1506 /// Like [`read`], `copy` creates a bitwise copy of `T`, regardless of
1507 /// whether `T` is [`Copy`]. If `T` is not [`Copy`], using both the values
1508 /// in the region beginning at `*src` and the region beginning at `*dst` can
1509 /// [violate memory safety][read-ownership].
1511 /// Note that even if the effectively copied size (`count * size_of::<T>()`) is
1512 /// `0`, the pointers must be non-NULL and properly aligned.
1514 /// [`Copy`]: ../marker/trait.Copy.html
1515 /// [`read`]: ../ptr/fn.read.html
1516 /// [read-ownership]: ../ptr/fn.read.html#ownership-of-the-returned-value
1517 /// [valid]: ../ptr/index.html#safety
1521 /// Efficiently create a Rust vector from an unsafe buffer:
1526 /// # #[allow(dead_code)]
1527 /// unsafe fn from_buf_raw<T>(ptr: *const T, elts: usize) -> Vec<T> {
1528 /// let mut dst = Vec::with_capacity(elts);
1529 /// dst.set_len(elts);
1530 /// ptr::copy(ptr, dst.as_mut_ptr(), elts);
1534 #[stable(feature = "rust1", since = "1.0.0")]
1536 pub unsafe fn copy<T>(src: *const T, dst: *mut T, count: usize) {
1537 extern "rust-intrinsic" {
1538 fn copy<T>(src: *const T, dst: *mut T, count: usize);
1541 debug_assert!(is_aligned_and_not_null(src), "attempt to copy from unaligned or null pointer");
1542 debug_assert!(is_aligned_and_not_null(dst), "attempt to copy to unaligned or null pointer");
1543 copy(src, dst, count)
1546 /// Sets `count * size_of::<T>()` bytes of memory starting at `dst` to
1549 /// `write_bytes` is similar to C's [`memset`], but sets `count *
1550 /// size_of::<T>()` bytes to `val`.
1552 /// [`memset`]: https://en.cppreference.com/w/c/string/byte/memset
1556 /// Behavior is undefined if any of the following conditions are violated:
1558 /// * `dst` must be [valid] for writes of `count * size_of::<T>()` bytes.
1560 /// * `dst` must be properly aligned.
1562 /// Additionally, the caller must ensure that writing `count *
1563 /// size_of::<T>()` bytes to the given region of memory results in a valid
1564 /// value of `T`. Using a region of memory typed as a `T` that contains an
1565 /// invalid value of `T` is undefined behavior.
1567 /// Note that even if the effectively copied size (`count * size_of::<T>()`) is
1568 /// `0`, the pointer must be non-NULL and properly aligned.
1570 /// [valid]: ../ptr/index.html#safety
1579 /// let mut vec = vec![0u32; 4];
1581 /// let vec_ptr = vec.as_mut_ptr();
1582 /// ptr::write_bytes(vec_ptr, 0xfe, 2);
1584 /// assert_eq!(vec, [0xfefefefe, 0xfefefefe, 0, 0]);
1587 /// Creating an invalid value:
1592 /// let mut v = Box::new(0i32);
1595 /// // Leaks the previously held value by overwriting the `Box<T>` with
1596 /// // a null pointer.
1597 /// ptr::write_bytes(&mut v as *mut Box<i32>, 0, 1);
1600 /// // At this point, using or dropping `v` results in undefined behavior.
1601 /// // drop(v); // ERROR
1603 /// // Even leaking `v` "uses" it, and hence is undefined behavior.
1604 /// // mem::forget(v); // ERROR
1606 /// // In fact, `v` is invalid according to basic type layout invariants, so *any*
1607 /// // operation touching it is undefined behavior.
1608 /// // let v2 = v; // ERROR
1611 /// // Let us instead put in a valid value
1612 /// ptr::write(&mut v as *mut Box<i32>, Box::new(42i32));
1615 /// // Now the box is fine
1616 /// assert_eq!(*v, 42);
1618 #[stable(feature = "rust1", since = "1.0.0")]
1620 pub unsafe fn write_bytes<T>(dst: *mut T, val: u8, count: usize) {
1621 extern "rust-intrinsic" {
1622 fn write_bytes<T>(dst: *mut T, val: u8, count: usize);
1625 debug_assert!(is_aligned_and_not_null(dst), "attempt to write to unaligned or null pointer");
1626 write_bytes(dst, val, count)