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 #[cfg_attr(bootstrap, rustc_const_unstable(feature = "const_transmute"))]
945 rustc_const_unstable(feature
= "const_transmute", issue
= "53605"),
947 pub fn transmute
<T
, U
>(e
: T
) -> U
;
949 /// Returns `true` if the actual type given as `T` requires drop
950 /// glue; returns `false` if the actual type provided for `T`
951 /// implements `Copy`.
953 /// If the actual type neither requires drop glue nor implements
954 /// `Copy`, then may return `true` or `false`.
956 /// The stabilized version of this intrinsic is
957 /// [`std::mem::needs_drop`](../../std/mem/fn.needs_drop.html).
958 pub fn needs_drop
<T
>() -> bool
;
960 /// Calculates the offset from a pointer.
962 /// This is implemented as an intrinsic to avoid converting to and from an
963 /// integer, since the conversion would throw away aliasing information.
967 /// Both the starting and resulting pointer must be either in bounds or one
968 /// byte past the end of an allocated object. If either pointer is out of
969 /// bounds or arithmetic overflow occurs then any further use of the
970 /// returned value will result in undefined behavior.
971 pub fn offset
<T
>(dst
: *const T
, offset
: isize) -> *const T
;
973 /// Calculates the offset from a pointer, potentially wrapping.
975 /// This is implemented as an intrinsic to avoid converting to and from an
976 /// integer, since the conversion inhibits certain optimizations.
980 /// Unlike the `offset` intrinsic, this intrinsic does not restrict the
981 /// resulting pointer to point into or one byte past the end of an allocated
982 /// object, and it wraps with two's complement arithmetic. The resulting
983 /// value is not necessarily valid to be used to actually access memory.
984 pub fn arith_offset
<T
>(dst
: *const T
, offset
: isize) -> *const T
;
986 /// Equivalent to the appropriate `llvm.memcpy.p0i8.0i8.*` intrinsic, with
987 /// a size of `count` * `size_of::<T>()` and an alignment of
988 /// `min_align_of::<T>()`
990 /// The volatile parameter is set to `true`, so it will not be optimized out
991 /// unless size is equal to zero.
992 pub fn volatile_copy_nonoverlapping_memory
<T
>(dst
: *mut T
, src
: *const T
,
994 /// Equivalent to the appropriate `llvm.memmove.p0i8.0i8.*` intrinsic, with
995 /// a size of `count` * `size_of::<T>()` and an alignment of
996 /// `min_align_of::<T>()`
998 /// The volatile parameter is set to `true`, so it will not be optimized out
999 /// unless size is equal to zero.
1000 pub fn volatile_copy_memory
<T
>(dst
: *mut T
, src
: *const T
, count
: usize);
1001 /// Equivalent to the appropriate `llvm.memset.p0i8.*` intrinsic, with a
1002 /// size of `count` * `size_of::<T>()` and an alignment of
1003 /// `min_align_of::<T>()`.
1005 /// The volatile parameter is set to `true`, so it will not be optimized out
1006 /// unless size is equal to zero.
1007 pub fn volatile_set_memory
<T
>(dst
: *mut T
, val
: u8, count
: usize);
1009 /// Performs a volatile load from the `src` pointer.
1010 /// The stabilized version of this intrinsic is
1011 /// [`std::ptr::read_volatile`](../../std/ptr/fn.read_volatile.html).
1012 pub fn volatile_load
<T
>(src
: *const T
) -> T
;
1013 /// Performs a volatile store to the `dst` pointer.
1014 /// The stabilized version of this intrinsic is
1015 /// [`std::ptr::write_volatile`](../../std/ptr/fn.write_volatile.html).
1016 pub fn volatile_store
<T
>(dst
: *mut T
, val
: T
);
1018 /// Performs a volatile load from the `src` pointer
1019 /// The pointer is not required to be aligned.
1020 pub fn unaligned_volatile_load
<T
>(src
: *const T
) -> T
;
1021 /// Performs a volatile store to the `dst` pointer.
1022 /// The pointer is not required to be aligned.
1023 pub fn unaligned_volatile_store
<T
>(dst
: *mut T
, val
: T
);
1025 /// Returns the square root of an `f32`
1026 pub fn sqrtf32(x
: f32) -> f32;
1027 /// Returns the square root of an `f64`
1028 pub fn sqrtf64(x
: f64) -> f64;
1030 /// Raises an `f32` to an integer power.
1031 pub fn powif32(a
: f32, x
: i32) -> f32;
1032 /// Raises an `f64` to an integer power.
1033 pub fn powif64(a
: f64, x
: i32) -> f64;
1035 /// Returns the sine of an `f32`.
1036 pub fn sinf32(x
: f32) -> f32;
1037 /// Returns the sine of an `f64`.
1038 pub fn sinf64(x
: f64) -> f64;
1040 /// Returns the cosine of an `f32`.
1041 pub fn cosf32(x
: f32) -> f32;
1042 /// Returns the cosine of an `f64`.
1043 pub fn cosf64(x
: f64) -> f64;
1045 /// Raises an `f32` to an `f32` power.
1046 pub fn powf32(a
: f32, x
: f32) -> f32;
1047 /// Raises an `f64` to an `f64` power.
1048 pub fn powf64(a
: f64, x
: f64) -> f64;
1050 /// Returns the exponential of an `f32`.
1051 pub fn expf32(x
: f32) -> f32;
1052 /// Returns the exponential of an `f64`.
1053 pub fn expf64(x
: f64) -> f64;
1055 /// Returns 2 raised to the power of an `f32`.
1056 pub fn exp2f32(x
: f32) -> f32;
1057 /// Returns 2 raised to the power of an `f64`.
1058 pub fn exp2f64(x
: f64) -> f64;
1060 /// Returns the natural logarithm of an `f32`.
1061 pub fn logf32(x
: f32) -> f32;
1062 /// Returns the natural logarithm of an `f64`.
1063 pub fn logf64(x
: f64) -> f64;
1065 /// Returns the base 10 logarithm of an `f32`.
1066 pub fn log10f32(x
: f32) -> f32;
1067 /// Returns the base 10 logarithm of an `f64`.
1068 pub fn log10f64(x
: f64) -> f64;
1070 /// Returns the base 2 logarithm of an `f32`.
1071 pub fn log2f32(x
: f32) -> f32;
1072 /// Returns the base 2 logarithm of an `f64`.
1073 pub fn log2f64(x
: f64) -> f64;
1075 /// Returns `a * b + c` for `f32` values.
1076 pub fn fmaf32(a
: f32, b
: f32, c
: f32) -> f32;
1077 /// Returns `a * b + c` for `f64` values.
1078 pub fn fmaf64(a
: f64, b
: f64, c
: f64) -> f64;
1080 /// Returns the absolute value of an `f32`.
1081 pub fn fabsf32(x
: f32) -> f32;
1082 /// Returns the absolute value of an `f64`.
1083 pub fn fabsf64(x
: f64) -> f64;
1085 /// Returns the minimum of two `f32` values.
1086 pub fn minnumf32(x
: f32, y
: f32) -> f32;
1087 /// Returns the minimum of two `f64` values.
1088 pub fn minnumf64(x
: f64, y
: f64) -> f64;
1089 /// Returns the maximum of two `f32` values.
1090 pub fn maxnumf32(x
: f32, y
: f32) -> f32;
1091 /// Returns the maximum of two `f64` values.
1092 pub fn maxnumf64(x
: f64, y
: f64) -> f64;
1094 /// Copies the sign from `y` to `x` for `f32` values.
1095 pub fn copysignf32(x
: f32, y
: f32) -> f32;
1096 /// Copies the sign from `y` to `x` for `f64` values.
1097 pub fn copysignf64(x
: f64, y
: f64) -> f64;
1099 /// Returns the largest integer less than or equal to an `f32`.
1100 pub fn floorf32(x
: f32) -> f32;
1101 /// Returns the largest integer less than or equal to an `f64`.
1102 pub fn floorf64(x
: f64) -> f64;
1104 /// Returns the smallest integer greater than or equal to an `f32`.
1105 pub fn ceilf32(x
: f32) -> f32;
1106 /// Returns the smallest integer greater than or equal to an `f64`.
1107 pub fn ceilf64(x
: f64) -> f64;
1109 /// Returns the integer part of an `f32`.
1110 pub fn truncf32(x
: f32) -> f32;
1111 /// Returns the integer part of an `f64`.
1112 pub fn truncf64(x
: f64) -> f64;
1114 /// Returns the nearest integer to an `f32`. May raise an inexact floating-point exception
1115 /// if the argument is not an integer.
1116 pub fn rintf32(x
: f32) -> f32;
1117 /// Returns the nearest integer to an `f64`. May raise an inexact floating-point exception
1118 /// if the argument is not an integer.
1119 pub fn rintf64(x
: f64) -> f64;
1121 /// Returns the nearest integer to an `f32`.
1122 pub fn nearbyintf32(x
: f32) -> f32;
1123 /// Returns the nearest integer to an `f64`.
1124 pub fn nearbyintf64(x
: f64) -> f64;
1126 /// Returns the nearest integer to an `f32`. Rounds half-way cases away from zero.
1127 pub fn roundf32(x
: f32) -> f32;
1128 /// Returns the nearest integer to an `f64`. Rounds half-way cases away from zero.
1129 pub fn roundf64(x
: f64) -> f64;
1131 /// Float addition that allows optimizations based on algebraic rules.
1132 /// May assume inputs are finite.
1133 pub fn fadd_fast
<T
>(a
: T
, b
: T
) -> T
;
1135 /// Float subtraction that allows optimizations based on algebraic rules.
1136 /// May assume inputs are finite.
1137 pub fn fsub_fast
<T
>(a
: T
, b
: T
) -> T
;
1139 /// Float multiplication that allows optimizations based on algebraic rules.
1140 /// May assume inputs are finite.
1141 pub fn fmul_fast
<T
>(a
: T
, b
: T
) -> T
;
1143 /// Float division that allows optimizations based on algebraic rules.
1144 /// May assume inputs are finite.
1145 pub fn fdiv_fast
<T
>(a
: T
, b
: T
) -> T
;
1147 /// Float remainder that allows optimizations based on algebraic rules.
1148 /// May assume inputs are finite.
1149 pub fn frem_fast
<T
>(a
: T
, b
: T
) -> T
;
1151 /// Convert with LLVM’s fptoui/fptosi, which may return undef for values out of range
1152 /// https://github.com/rust-lang/rust/issues/10184
1153 #[cfg(not(bootstrap))]
1154 pub fn float_to_int_approx_unchecked
<Float
, Int
>(value
: Float
) -> Int
;
1157 /// Returns the number of bits set in an integer type `T`
1158 pub fn ctpop
<T
>(x
: T
) -> T
;
1160 /// Returns the number of leading unset bits (zeroes) in an integer type `T`.
1165 /// #![feature(core_intrinsics)]
1167 /// use std::intrinsics::ctlz;
1169 /// let x = 0b0001_1100_u8;
1170 /// let num_leading = ctlz(x);
1171 /// assert_eq!(num_leading, 3);
1174 /// An `x` with value `0` will return the bit width of `T`.
1177 /// #![feature(core_intrinsics)]
1179 /// use std::intrinsics::ctlz;
1182 /// let num_leading = ctlz(x);
1183 /// assert_eq!(num_leading, 16);
1185 pub fn ctlz
<T
>(x
: T
) -> T
;
1187 /// Like `ctlz`, but extra-unsafe as it returns `undef` when
1188 /// given an `x` with value `0`.
1193 /// #![feature(core_intrinsics)]
1195 /// use std::intrinsics::ctlz_nonzero;
1197 /// let x = 0b0001_1100_u8;
1198 /// let num_leading = unsafe { ctlz_nonzero(x) };
1199 /// assert_eq!(num_leading, 3);
1201 pub fn ctlz_nonzero
<T
>(x
: T
) -> T
;
1203 /// Returns the number of trailing unset bits (zeroes) in an integer type `T`.
1208 /// #![feature(core_intrinsics)]
1210 /// use std::intrinsics::cttz;
1212 /// let x = 0b0011_1000_u8;
1213 /// let num_trailing = cttz(x);
1214 /// assert_eq!(num_trailing, 3);
1217 /// An `x` with value `0` will return the bit width of `T`:
1220 /// #![feature(core_intrinsics)]
1222 /// use std::intrinsics::cttz;
1225 /// let num_trailing = cttz(x);
1226 /// assert_eq!(num_trailing, 16);
1228 pub fn cttz
<T
>(x
: T
) -> T
;
1230 /// Like `cttz`, but extra-unsafe as it returns `undef` when
1231 /// given an `x` with value `0`.
1236 /// #![feature(core_intrinsics)]
1238 /// use std::intrinsics::cttz_nonzero;
1240 /// let x = 0b0011_1000_u8;
1241 /// let num_trailing = unsafe { cttz_nonzero(x) };
1242 /// assert_eq!(num_trailing, 3);
1244 pub fn cttz_nonzero
<T
>(x
: T
) -> T
;
1246 /// Reverses the bytes in an integer type `T`.
1247 pub fn bswap
<T
>(x
: T
) -> T
;
1249 /// Reverses the bits in an integer type `T`.
1250 pub fn bitreverse
<T
>(x
: T
) -> T
;
1252 /// Performs checked integer addition.
1253 /// The stabilized versions of this intrinsic are available on the integer
1254 /// primitives via the `overflowing_add` method. For example,
1255 /// [`std::u32::overflowing_add`](../../std/primitive.u32.html#method.overflowing_add)
1256 pub fn add_with_overflow
<T
>(x
: T
, y
: T
) -> (T
, bool
);
1258 /// Performs checked integer subtraction
1259 /// The stabilized versions of this intrinsic are available on the integer
1260 /// primitives via the `overflowing_sub` method. For example,
1261 /// [`std::u32::overflowing_sub`](../../std/primitive.u32.html#method.overflowing_sub)
1262 pub fn sub_with_overflow
<T
>(x
: T
, y
: T
) -> (T
, bool
);
1264 /// Performs checked integer multiplication
1265 /// The stabilized versions of this intrinsic are available on the integer
1266 /// primitives via the `overflowing_mul` method. For example,
1267 /// [`std::u32::overflowing_mul`](../../std/primitive.u32.html#method.overflowing_mul)
1268 pub fn mul_with_overflow
<T
>(x
: T
, y
: T
) -> (T
, bool
);
1270 /// Performs an exact division, resulting in undefined behavior where
1271 /// `x % y != 0` or `y == 0` or `x == T::min_value() && y == -1`
1272 pub fn exact_div
<T
>(x
: T
, y
: T
) -> T
;
1274 /// Performs an unchecked division, resulting in undefined behavior
1275 /// where y = 0 or x = `T::min_value()` and y = -1
1276 pub fn unchecked_div
<T
>(x
: T
, y
: T
) -> T
;
1277 /// Returns the remainder of an unchecked division, resulting in
1278 /// undefined behavior where y = 0 or x = `T::min_value()` and y = -1
1279 pub fn unchecked_rem
<T
>(x
: T
, y
: T
) -> T
;
1281 /// Performs an unchecked left shift, resulting in undefined behavior when
1282 /// y < 0 or y >= N, where N is the width of T in bits.
1283 pub fn unchecked_shl
<T
>(x
: T
, y
: T
) -> T
;
1284 /// Performs an unchecked right shift, resulting in undefined behavior when
1285 /// y < 0 or y >= N, where N is the width of T in bits.
1286 pub fn unchecked_shr
<T
>(x
: T
, y
: T
) -> T
;
1288 /// Returns the result of an unchecked addition, resulting in
1289 /// undefined behavior when `x + y > T::max_value()` or `x + y < T::min_value()`.
1290 pub fn unchecked_add
<T
>(x
: T
, y
: T
) -> T
;
1292 /// Returns the result of an unchecked subtraction, resulting in
1293 /// undefined behavior when `x - y > T::max_value()` or `x - y < T::min_value()`.
1294 pub fn unchecked_sub
<T
>(x
: T
, y
: T
) -> T
;
1296 /// Returns the result of an unchecked multiplication, resulting in
1297 /// undefined behavior when `x * y > T::max_value()` or `x * y < T::min_value()`.
1298 pub fn unchecked_mul
<T
>(x
: T
, y
: T
) -> T
;
1300 /// Performs rotate left.
1301 /// The stabilized versions of this intrinsic are available on the integer
1302 /// primitives via the `rotate_left` method. For example,
1303 /// [`std::u32::rotate_left`](../../std/primitive.u32.html#method.rotate_left)
1304 pub fn rotate_left
<T
>(x
: T
, y
: T
) -> T
;
1306 /// Performs rotate right.
1307 /// The stabilized versions of this intrinsic are available on the integer
1308 /// primitives via the `rotate_right` method. For example,
1309 /// [`std::u32::rotate_right`](../../std/primitive.u32.html#method.rotate_right)
1310 pub fn rotate_right
<T
>(x
: T
, y
: 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_add` method. For example,
1315 /// [`std::u32::wrapping_add`](../../std/primitive.u32.html#method.wrapping_add)
1316 pub fn wrapping_add
<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_sub` method. For example,
1320 /// [`std::u32::wrapping_sub`](../../std/primitive.u32.html#method.wrapping_sub)
1321 pub fn wrapping_sub
<T
>(a
: T
, b
: T
) -> T
;
1322 /// Returns (a * b) mod 2<sup>N</sup>, where N is the width of T in bits.
1323 /// The stabilized versions of this intrinsic are available on the integer
1324 /// primitives via the `wrapping_mul` method. For example,
1325 /// [`std::u32::wrapping_mul`](../../std/primitive.u32.html#method.wrapping_mul)
1326 pub fn wrapping_mul
<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_add` method. For example,
1331 /// [`std::u32::saturating_add`](../../std/primitive.u32.html#method.saturating_add)
1332 pub fn saturating_add
<T
>(a
: T
, b
: T
) -> T
;
1333 /// Computes `a - b`, while saturating at numeric bounds.
1334 /// The stabilized versions of this intrinsic are available on the integer
1335 /// primitives via the `saturating_sub` method. For example,
1336 /// [`std::u32::saturating_sub`](../../std/primitive.u32.html#method.saturating_sub)
1337 pub fn saturating_sub
<T
>(a
: T
, b
: T
) -> T
;
1339 /// Returns the value of the discriminant for the variant in 'v',
1340 /// cast to a `u64`; if `T` has no discriminant, returns 0.
1341 pub fn discriminant_value
<T
>(v
: &T
) -> u64;
1343 /// Rust's "try catch" construct which invokes the function pointer `f` with
1344 /// the data pointer `data`.
1346 /// The third pointer is a target-specific data pointer which is filled in
1347 /// with the specifics of the exception that occurred. For examples on Unix
1348 /// platforms this is a `*mut *mut T` which is filled in by the compiler and
1349 /// on MSVC it's `*mut [usize; 2]`. For more information see the compiler's
1350 /// source as well as std's catch implementation.
1351 pub fn r
#try(f: fn(*mut u8), data: *mut u8, local_ptr: *mut u8) -> i32;
1353 /// Emits a `!nontemporal` store according to LLVM (see their docs).
1354 /// Probably will never become stable.
1355 pub fn nontemporal_store
<T
>(ptr
: *mut T
, val
: T
);
1357 /// See documentation of `<*const T>::offset_from` for details.
1358 pub fn ptr_offset_from
<T
>(ptr
: *const T
, base
: *const T
) -> isize;
1360 /// Internal hook used by Miri to implement unwinding.
1361 /// Compiles to a NOP during non-Miri codegen.
1363 /// Perma-unstable: do not use
1364 #[cfg(not(bootstrap))]
1365 pub fn miri_start_panic(data
: *mut (dyn crate::any
::Any
+ crate::marker
::Send
)) -> ();
1368 // Some functions are defined here because they accidentally got made
1369 // available in this module on stable. See <https://github.com/rust-lang/rust/issues/15702>.
1370 // (`transmute` also falls into this category, but it cannot be wrapped due to the
1371 // check that `T` and `U` have the same size.)
1373 /// Checks whether `ptr` is properly aligned with respect to
1374 /// `align_of::<T>()`.
1375 pub(crate) fn is_aligned_and_not_null
<T
>(ptr
: *const T
) -> bool
{
1376 !ptr
.is_null() && ptr
as usize % mem
::align_of
::<T
>() == 0
1379 /// Checks whether the regions of memory starting at `src` and `dst` of size
1380 /// `count * size_of::<T>()` overlap.
1381 fn overlaps
<T
>(src
: *const T
, dst
: *const T
, count
: usize) -> bool
{
1382 let src_usize
= src
as usize;
1383 let dst_usize
= dst
as usize;
1384 let size
= mem
::size_of
::<T
>().checked_mul(count
).unwrap();
1385 let diff
= if src_usize
> dst_usize
{
1386 src_usize
- dst_usize
1388 dst_usize
- src_usize
1393 /// Copies `count * size_of::<T>()` bytes from `src` to `dst`. The source
1394 /// and destination must *not* overlap.
1396 /// For regions of memory which might overlap, use [`copy`] instead.
1398 /// `copy_nonoverlapping` is semantically equivalent to C's [`memcpy`], but
1399 /// with the argument order swapped.
1401 /// [`copy`]: ./fn.copy.html
1402 /// [`memcpy`]: https://en.cppreference.com/w/c/string/byte/memcpy
1406 /// Behavior is undefined if any of the following conditions are violated:
1408 /// * `src` must be [valid] for reads of `count * size_of::<T>()` bytes.
1410 /// * `dst` must be [valid] for writes of `count * size_of::<T>()` bytes.
1412 /// * Both `src` and `dst` must be properly aligned.
1414 /// * The region of memory beginning at `src` with a size of `count *
1415 /// size_of::<T>()` bytes must *not* overlap with the region of memory
1416 /// beginning at `dst` with the same size.
1418 /// Like [`read`], `copy_nonoverlapping` creates a bitwise copy of `T`, regardless of
1419 /// whether `T` is [`Copy`]. If `T` is not [`Copy`], using *both* the values
1420 /// in the region beginning at `*src` and the region beginning at `*dst` can
1421 /// [violate memory safety][read-ownership].
1423 /// Note that even if the effectively copied size (`count * size_of::<T>()`) is
1424 /// `0`, the pointers must be non-NULL and properly aligned.
1426 /// [`Copy`]: ../marker/trait.Copy.html
1427 /// [`read`]: ../ptr/fn.read.html
1428 /// [read-ownership]: ../ptr/fn.read.html#ownership-of-the-returned-value
1429 /// [valid]: ../ptr/index.html#safety
1433 /// Manually implement [`Vec::append`]:
1438 /// /// Moves all the elements of `src` into `dst`, leaving `src` empty.
1439 /// fn append<T>(dst: &mut Vec<T>, src: &mut Vec<T>) {
1440 /// let src_len = src.len();
1441 /// let dst_len = dst.len();
1443 /// // Ensure that `dst` has enough capacity to hold all of `src`.
1444 /// dst.reserve(src_len);
1447 /// // The call to offset is always safe because `Vec` will never
1448 /// // allocate more than `isize::MAX` bytes.
1449 /// let dst_ptr = dst.as_mut_ptr().offset(dst_len as isize);
1450 /// let src_ptr = src.as_ptr();
1452 /// // Truncate `src` without dropping its contents. We do this first,
1453 /// // to avoid problems in case something further down panics.
1456 /// // The two regions cannot overlap because mutable references do
1457 /// // not alias, and two different vectors cannot own the same
1459 /// ptr::copy_nonoverlapping(src_ptr, dst_ptr, src_len);
1461 /// // Notify `dst` that it now holds the contents of `src`.
1462 /// dst.set_len(dst_len + src_len);
1466 /// let mut a = vec!['r'];
1467 /// let mut b = vec!['u', 's', 't'];
1469 /// append(&mut a, &mut b);
1471 /// assert_eq!(a, &['r', 'u', 's', 't']);
1472 /// assert!(b.is_empty());
1475 /// [`Vec::append`]: ../../std/vec/struct.Vec.html#method.append
1476 #[stable(feature = "rust1", since = "1.0.0")]
1478 pub unsafe fn copy_nonoverlapping
<T
>(src
: *const T
, dst
: *mut T
, count
: usize) {
1479 extern "rust-intrinsic" {
1480 fn copy_nonoverlapping
<T
>(src
: *const T
, dst
: *mut T
, count
: usize);
1483 debug_assert
!(is_aligned_and_not_null(src
), "attempt to copy from unaligned or null pointer");
1484 debug_assert
!(is_aligned_and_not_null(dst
), "attempt to copy to unaligned or null pointer");
1485 debug_assert
!(!overlaps(src
, dst
, count
), "attempt to copy to overlapping memory");
1486 copy_nonoverlapping(src
, dst
, count
)
1489 /// Copies `count * size_of::<T>()` bytes from `src` to `dst`. The source
1490 /// and destination may overlap.
1492 /// If the source and destination will *never* overlap,
1493 /// [`copy_nonoverlapping`] can be used instead.
1495 /// `copy` is semantically equivalent to C's [`memmove`], but with the argument
1496 /// order swapped. Copying takes place as if the bytes were copied from `src`
1497 /// to a temporary array and then copied from the array to `dst`.
1499 /// [`copy_nonoverlapping`]: ./fn.copy_nonoverlapping.html
1500 /// [`memmove`]: https://en.cppreference.com/w/c/string/byte/memmove
1504 /// Behavior is undefined if any of the following conditions are violated:
1506 /// * `src` must be [valid] for reads of `count * size_of::<T>()` bytes.
1508 /// * `dst` must be [valid] for writes of `count * size_of::<T>()` bytes.
1510 /// * Both `src` and `dst` must be properly aligned.
1512 /// Like [`read`], `copy` creates a bitwise copy of `T`, regardless of
1513 /// whether `T` is [`Copy`]. If `T` is not [`Copy`], using both the values
1514 /// in the region beginning at `*src` and the region beginning at `*dst` can
1515 /// [violate memory safety][read-ownership].
1517 /// Note that even if the effectively copied size (`count * size_of::<T>()`) is
1518 /// `0`, the pointers must be non-NULL and properly aligned.
1520 /// [`Copy`]: ../marker/trait.Copy.html
1521 /// [`read`]: ../ptr/fn.read.html
1522 /// [read-ownership]: ../ptr/fn.read.html#ownership-of-the-returned-value
1523 /// [valid]: ../ptr/index.html#safety
1527 /// Efficiently create a Rust vector from an unsafe buffer:
1532 /// # #[allow(dead_code)]
1533 /// unsafe fn from_buf_raw<T>(ptr: *const T, elts: usize) -> Vec<T> {
1534 /// let mut dst = Vec::with_capacity(elts);
1535 /// dst.set_len(elts);
1536 /// ptr::copy(ptr, dst.as_mut_ptr(), elts);
1540 #[stable(feature = "rust1", since = "1.0.0")]
1542 pub unsafe fn copy
<T
>(src
: *const T
, dst
: *mut T
, count
: usize) {
1543 extern "rust-intrinsic" {
1544 fn copy
<T
>(src
: *const T
, dst
: *mut T
, count
: usize);
1547 debug_assert
!(is_aligned_and_not_null(src
), "attempt to copy from unaligned or null pointer");
1548 debug_assert
!(is_aligned_and_not_null(dst
), "attempt to copy to unaligned or null pointer");
1549 copy(src
, dst
, count
)
1552 /// Sets `count * size_of::<T>()` bytes of memory starting at `dst` to
1555 /// `write_bytes` is similar to C's [`memset`], but sets `count *
1556 /// size_of::<T>()` bytes to `val`.
1558 /// [`memset`]: https://en.cppreference.com/w/c/string/byte/memset
1562 /// Behavior is undefined if any of the following conditions are violated:
1564 /// * `dst` must be [valid] for writes of `count * size_of::<T>()` bytes.
1566 /// * `dst` must be properly aligned.
1568 /// Additionally, the caller must ensure that writing `count *
1569 /// size_of::<T>()` bytes to the given region of memory results in a valid
1570 /// value of `T`. Using a region of memory typed as a `T` that contains an
1571 /// invalid value of `T` is undefined behavior.
1573 /// Note that even if the effectively copied size (`count * size_of::<T>()`) is
1574 /// `0`, the pointer must be non-NULL and properly aligned.
1576 /// [valid]: ../ptr/index.html#safety
1585 /// let mut vec = vec![0u32; 4];
1587 /// let vec_ptr = vec.as_mut_ptr();
1588 /// ptr::write_bytes(vec_ptr, 0xfe, 2);
1590 /// assert_eq!(vec, [0xfefefefe, 0xfefefefe, 0, 0]);
1593 /// Creating an invalid value:
1598 /// let mut v = Box::new(0i32);
1601 /// // Leaks the previously held value by overwriting the `Box<T>` with
1602 /// // a null pointer.
1603 /// ptr::write_bytes(&mut v as *mut Box<i32>, 0, 1);
1606 /// // At this point, using or dropping `v` results in undefined behavior.
1607 /// // drop(v); // ERROR
1609 /// // Even leaking `v` "uses" it, and hence is undefined behavior.
1610 /// // mem::forget(v); // ERROR
1612 /// // In fact, `v` is invalid according to basic type layout invariants, so *any*
1613 /// // operation touching it is undefined behavior.
1614 /// // let v2 = v; // ERROR
1617 /// // Let us instead put in a valid value
1618 /// ptr::write(&mut v as *mut Box<i32>, Box::new(42i32));
1621 /// // Now the box is fine
1622 /// assert_eq!(*v, 42);
1624 #[stable(feature = "rust1", since = "1.0.0")]
1626 pub unsafe fn write_bytes
<T
>(dst
: *mut T
, val
: u8, count
: usize) {
1627 extern "rust-intrinsic" {
1628 fn write_bytes
<T
>(dst
: *mut T
, val
: u8, count
: usize);
1631 debug_assert
!(is_aligned_and_not_null(dst
), "attempt to write to unaligned or null pointer");
1632 write_bytes(dst
, val
, count
)