1 // Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 //! rustc compiler intrinsics.
13 //! The corresponding definitions are in librustc_trans/intrinsic.rs.
17 //! The volatile intrinsics provide operations intended to act on I/O
18 //! memory, which are guaranteed to not be reordered by the compiler
19 //! across other volatile intrinsics. See the LLVM documentation on
22 //! [volatile]: http://llvm.org/docs/LangRef.html#volatile-memory-accesses
26 //! The atomic intrinsics provide common atomic operations on machine
27 //! words, with multiple possible memory orderings. They obey the same
28 //! semantics as C++11. See the LLVM documentation on [[atomics]].
30 //! [atomics]: http://llvm.org/docs/Atomics.html
32 //! A quick refresher on memory ordering:
34 //! * Acquire - a barrier for acquiring a lock. Subsequent reads and writes
35 //! take place after the barrier.
36 //! * Release - a barrier for releasing a lock. Preceding reads and writes
37 //! take place before the barrier.
38 //! * Sequentially consistent - sequentially consistent operations are
39 //! guaranteed to happen in order. This is the standard mode for working
40 //! with atomic types and is equivalent to Java's `volatile`.
42 #![unstable(feature = "core_intrinsics",
43 reason
= "intrinsics are unlikely to ever be stabilized, instead \
44 they should be used through stabilized interfaces \
45 in the rest of the standard library",
47 #![allow(missing_docs)]
49 extern "rust-intrinsic" {
51 // NB: These intrinsics take raw pointers because they mutate aliased
52 // memory, which is not valid for either `&` or `&mut`.
54 /// Stores a value if the current value is the same as the `old` value.
55 /// The stabilized version of this intrinsic is available on the
56 /// `std::sync::atomic` types via the `compare_exchange` method by passing
57 /// [`Ordering::SeqCst`](../../std/sync/atomic/enum.Ordering.html)
58 /// as both the `success` and `failure` parameters. For example,
59 /// [`AtomicBool::compare_exchange`]
60 /// (../../std/sync/atomic/struct.AtomicBool.html#method.compare_exchange).
61 pub fn atomic_cxchg
<T
>(dst
: *mut T
, old
: T
, src
: T
) -> (T
, bool
);
62 /// Stores a value if the current value is the same as the `old` value.
63 /// The stabilized version of this intrinsic is available on the
64 /// `std::sync::atomic` types via the `compare_exchange` method by passing
65 /// [`Ordering::Acquire`](../../std/sync/atomic/enum.Ordering.html)
66 /// as both the `success` and `failure` parameters. For example,
67 /// [`AtomicBool::compare_exchange`]
68 /// (../../std/sync/atomic/struct.AtomicBool.html#method.compare_exchange).
69 pub fn atomic_cxchg_acq
<T
>(dst
: *mut T
, old
: T
, src
: T
) -> (T
, bool
);
70 /// Stores a value if the current value is the same as the `old` value.
71 /// The stabilized version of this intrinsic is available on the
72 /// `std::sync::atomic` types via the `compare_exchange` method by passing
73 /// [`Ordering::Release`](../../std/sync/atomic/enum.Ordering.html)
74 /// as the `success` and
75 /// [`Ordering::Relaxed`](../../std/sync/atomic/enum.Ordering.html)
76 /// as the `failure` parameters. For example,
77 /// [`AtomicBool::compare_exchange`]
78 /// (../../std/sync/atomic/struct.AtomicBool.html#method.compare_exchange).
79 pub fn atomic_cxchg_rel
<T
>(dst
: *mut T
, old
: T
, src
: T
) -> (T
, bool
);
80 /// Stores a value if the current value is the same as the `old` value.
81 /// The stabilized version of this intrinsic is available on the
82 /// `std::sync::atomic` types via the `compare_exchange` method by passing
83 /// [`Ordering::AcqRel`](../../std/sync/atomic/enum.Ordering.html)
84 /// as the `success` and
85 /// [`Ordering::Acquire`](../../std/sync/atomic/enum.Ordering.html)
86 /// as the `failure` parameters. For example,
87 /// [`AtomicBool::compare_exchange`]
88 /// (../../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`]
96 /// (../../std/sync/atomic/struct.AtomicBool.html#method.compare_exchange).
97 pub fn atomic_cxchg_relaxed
<T
>(dst
: *mut T
, old
: T
, src
: T
) -> (T
, bool
);
98 /// Stores a value if the current value is the same as the `old` value.
99 /// The stabilized version of this intrinsic is available on the
100 /// `std::sync::atomic` types via the `compare_exchange` method by passing
101 /// [`Ordering::SeqCst`](../../std/sync/atomic/enum.Ordering.html)
102 /// as the `success` and
103 /// [`Ordering::Relaxed`](../../std/sync/atomic/enum.Ordering.html)
104 /// as the `failure` parameters. For example,
105 /// [`AtomicBool::compare_exchange`]
106 /// (../../std/sync/atomic/struct.AtomicBool.html#method.compare_exchange).
107 pub fn atomic_cxchg_failrelaxed
<T
>(dst
: *mut T
, old
: T
, src
: T
) -> (T
, bool
);
108 /// Stores a value if the current value is the same as the `old` value.
109 /// The stabilized version of this intrinsic is available on the
110 /// `std::sync::atomic` types via the `compare_exchange` method by passing
111 /// [`Ordering::SeqCst`](../../std/sync/atomic/enum.Ordering.html)
112 /// as the `success` and
113 /// [`Ordering::Acquire`](../../std/sync/atomic/enum.Ordering.html)
114 /// as the `failure` parameters. For example,
115 /// [`AtomicBool::compare_exchange`]
116 /// (../../std/sync/atomic/struct.AtomicBool.html#method.compare_exchange).
117 pub fn atomic_cxchg_failacq
<T
>(dst
: *mut T
, old
: T
, src
: T
) -> (T
, bool
);
118 /// Stores a value if the current value is the same as the `old` value.
119 /// The stabilized version of this intrinsic is available on the
120 /// `std::sync::atomic` types via the `compare_exchange` method by passing
121 /// [`Ordering::Acquire`](../../std/sync/atomic/enum.Ordering.html)
122 /// as the `success` and
123 /// [`Ordering::Relaxed`](../../std/sync/atomic/enum.Ordering.html)
124 /// as the `failure` parameters. For example,
125 /// [`AtomicBool::compare_exchange`]
126 /// (../../std/sync/atomic/struct.AtomicBool.html#method.compare_exchange).
127 pub fn atomic_cxchg_acq_failrelaxed
<T
>(dst
: *mut T
, old
: T
, src
: T
) -> (T
, bool
);
128 /// Stores a value if the current value is the same as the `old` value.
129 /// The stabilized version of this intrinsic is available on the
130 /// `std::sync::atomic` types via the `compare_exchange` method by passing
131 /// [`Ordering::AcqRel`](../../std/sync/atomic/enum.Ordering.html)
132 /// as the `success` and
133 /// [`Ordering::Relaxed`](../../std/sync/atomic/enum.Ordering.html)
134 /// as the `failure` parameters. For example,
135 /// [`AtomicBool::compare_exchange`]
136 /// (../../std/sync/atomic/struct.AtomicBool.html#method.compare_exchange).
137 pub fn atomic_cxchg_acqrel_failrelaxed
<T
>(dst
: *mut T
, old
: T
, src
: T
) -> (T
, bool
);
139 /// Stores a value if the current value is the same as the `old` value.
140 /// The stabilized version of this intrinsic is available on the
141 /// `std::sync::atomic` types via the `compare_exchange_weak` method by passing
142 /// [`Ordering::SeqCst`](../../std/sync/atomic/enum.Ordering.html)
143 /// as both the `success` and `failure` parameters. For example,
144 /// [`AtomicBool::compare_exchange_weak`]
145 /// (../../std/sync/atomic/struct.AtomicBool.html#method.compare_exchange_weak).
146 pub fn atomic_cxchgweak
<T
>(dst
: *mut T
, old
: T
, src
: T
) -> (T
, bool
);
147 /// Stores a value if the current value is the same as the `old` value.
148 /// The stabilized version of this intrinsic is available on the
149 /// `std::sync::atomic` types via the `compare_exchange_weak` method by passing
150 /// [`Ordering::Acquire`](../../std/sync/atomic/enum.Ordering.html)
151 /// as both the `success` and `failure` parameters. For example,
152 /// [`AtomicBool::compare_exchange_weak`]
153 /// (../../std/sync/atomic/struct.AtomicBool.html#method.compare_exchange_weak).
154 pub fn atomic_cxchgweak_acq
<T
>(dst
: *mut T
, old
: T
, src
: T
) -> (T
, bool
);
155 /// Stores a value if the current value is the same as the `old` value.
156 /// The stabilized version of this intrinsic is available on the
157 /// `std::sync::atomic` types via the `compare_exchange_weak` method by passing
158 /// [`Ordering::Release`](../../std/sync/atomic/enum.Ordering.html)
159 /// as the `success` and
160 /// [`Ordering::Relaxed`](../../std/sync/atomic/enum.Ordering.html)
161 /// as the `failure` parameters. For example,
162 /// [`AtomicBool::compare_exchange_weak`]
163 /// (../../std/sync/atomic/struct.AtomicBool.html#method.compare_exchange_weak).
164 pub fn atomic_cxchgweak_rel
<T
>(dst
: *mut T
, old
: T
, src
: T
) -> (T
, bool
);
165 /// Stores a value if the current value is the same as the `old` value.
166 /// The stabilized version of this intrinsic is available on the
167 /// `std::sync::atomic` types via the `compare_exchange_weak` method by passing
168 /// [`Ordering::AcqRel`](../../std/sync/atomic/enum.Ordering.html)
169 /// as the `success` and
170 /// [`Ordering::Acquire`](../../std/sync/atomic/enum.Ordering.html)
171 /// as the `failure` parameters. For example,
172 /// [`AtomicBool::compare_exchange_weak`]
173 /// (../../std/sync/atomic/struct.AtomicBool.html#method.compare_exchange_weak).
174 pub fn atomic_cxchgweak_acqrel
<T
>(dst
: *mut T
, old
: T
, src
: T
) -> (T
, bool
);
175 /// Stores a value if the current value is the same as the `old` value.
176 /// The stabilized version of this intrinsic is available on the
177 /// `std::sync::atomic` types via the `compare_exchange_weak` method by passing
178 /// [`Ordering::Relaxed`](../../std/sync/atomic/enum.Ordering.html)
179 /// as both the `success` and `failure` parameters. For example,
180 /// [`AtomicBool::compare_exchange_weak`]
181 /// (../../std/sync/atomic/struct.AtomicBool.html#method.compare_exchange_weak).
182 pub fn atomic_cxchgweak_relaxed
<T
>(dst
: *mut T
, old
: T
, src
: T
) -> (T
, bool
);
183 /// Stores a value if the current value is the same as the `old` value.
184 /// The stabilized version of this intrinsic is available on the
185 /// `std::sync::atomic` types via the `compare_exchange_weak` method by passing
186 /// [`Ordering::SeqCst`](../../std/sync/atomic/enum.Ordering.html)
187 /// as the `success` and
188 /// [`Ordering::Relaxed`](../../std/sync/atomic/enum.Ordering.html)
189 /// as the `failure` parameters. For example,
190 /// [`AtomicBool::compare_exchange_weak`]
191 /// (../../std/sync/atomic/struct.AtomicBool.html#method.compare_exchange_weak).
192 pub fn atomic_cxchgweak_failrelaxed
<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::Acquire`](../../std/sync/atomic/enum.Ordering.html)
199 /// as the `failure` parameters. For example,
200 /// [`AtomicBool::compare_exchange_weak`]
201 /// (../../std/sync/atomic/struct.AtomicBool.html#method.compare_exchange_weak).
202 pub fn atomic_cxchgweak_failacq
<T
>(dst
: *mut T
, old
: T
, src
: T
) -> (T
, bool
);
203 /// Stores a value if the current value is the same as the `old` value.
204 /// The stabilized version of this intrinsic is available on the
205 /// `std::sync::atomic` types via the `compare_exchange_weak` method by passing
206 /// [`Ordering::Acquire`](../../std/sync/atomic/enum.Ordering.html)
207 /// as the `success` and
208 /// [`Ordering::Relaxed`](../../std/sync/atomic/enum.Ordering.html)
209 /// as the `failure` parameters. For example,
210 /// [`AtomicBool::compare_exchange_weak`]
211 /// (../../std/sync/atomic/struct.AtomicBool.html#method.compare_exchange_weak).
212 pub fn atomic_cxchgweak_acq_failrelaxed
<T
>(dst
: *mut T
, old
: T
, src
: T
) -> (T
, bool
);
213 /// Stores a value if the current value is the same as the `old` value.
214 /// The stabilized version of this intrinsic is available on the
215 /// `std::sync::atomic` types via the `compare_exchange_weak` method by passing
216 /// [`Ordering::AcqRel`](../../std/sync/atomic/enum.Ordering.html)
217 /// as the `success` and
218 /// [`Ordering::Relaxed`](../../std/sync/atomic/enum.Ordering.html)
219 /// as the `failure` parameters. For example,
220 /// [`AtomicBool::compare_exchange_weak`]
221 /// (../../std/sync/atomic/struct.AtomicBool.html#method.compare_exchange_weak).
222 pub fn atomic_cxchgweak_acqrel_failrelaxed
<T
>(dst
: *mut T
, old
: T
, src
: T
) -> (T
, bool
);
224 /// Loads the current value of the pointer.
225 /// The stabilized version of this intrinsic is available on the
226 /// `std::sync::atomic` types via the `load` method by passing
227 /// [`Ordering::SeqCst`](../../std/sync/atomic/enum.Ordering.html)
228 /// as the `order`. For example,
229 /// [`AtomicBool::load`](../../std/sync/atomic/struct.AtomicBool.html#method.load).
230 pub fn atomic_load
<T
>(src
: *const T
) -> T
;
231 /// Loads the current value of the pointer.
232 /// The stabilized version of this intrinsic is available on the
233 /// `std::sync::atomic` types via the `load` method by passing
234 /// [`Ordering::Acquire`](../../std/sync/atomic/enum.Ordering.html)
235 /// as the `order`. For example,
236 /// [`AtomicBool::load`](../../std/sync/atomic/struct.AtomicBool.html#method.load).
237 pub fn atomic_load_acq
<T
>(src
: *const T
) -> T
;
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::Relaxed`](../../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_relaxed
<T
>(src
: *const T
) -> T
;
245 pub fn atomic_load_unordered
<T
>(src
: *const T
) -> T
;
247 /// Stores the value at the specified memory location.
248 /// The stabilized version of this intrinsic is available on the
249 /// `std::sync::atomic` types via the `store` method by passing
250 /// [`Ordering::SeqCst`](../../std/sync/atomic/enum.Ordering.html)
251 /// as the `order`. For example,
252 /// [`AtomicBool::store`](../../std/sync/atomic/struct.AtomicBool.html#method.store).
253 pub fn atomic_store
<T
>(dst
: *mut T
, val
: T
);
254 /// Stores the value at the specified memory location.
255 /// The stabilized version of this intrinsic is available on the
256 /// `std::sync::atomic` types via the `store` method by passing
257 /// [`Ordering::Release`](../../std/sync/atomic/enum.Ordering.html)
258 /// as the `order`. For example,
259 /// [`AtomicBool::store`](../../std/sync/atomic/struct.AtomicBool.html#method.store).
260 pub fn atomic_store_rel
<T
>(dst
: *mut T
, val
: 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::Relaxed`](../../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_relaxed
<T
>(dst
: *mut T
, val
: T
);
268 pub fn atomic_store_unordered
<T
>(dst
: *mut T
, val
: T
);
270 /// Stores the value at the specified memory location, returning the old value.
271 /// The stabilized version of this intrinsic is available on the
272 /// `std::sync::atomic` types via the `swap` method by passing
273 /// [`Ordering::SeqCst`](../../std/sync/atomic/enum.Ordering.html)
274 /// as the `order`. For example,
275 /// [`AtomicBool::swap`](../../std/sync/atomic/struct.AtomicBool.html#method.swap).
276 pub fn atomic_xchg
<T
>(dst
: *mut T
, src
: T
) -> T
;
277 /// Stores the value at the specified memory location, returning the old value.
278 /// The stabilized version of this intrinsic is available on the
279 /// `std::sync::atomic` types via the `swap` method by passing
280 /// [`Ordering::Acquire`](../../std/sync/atomic/enum.Ordering.html)
281 /// as the `order`. For example,
282 /// [`AtomicBool::swap`](../../std/sync/atomic/struct.AtomicBool.html#method.swap).
283 pub fn atomic_xchg_acq
<T
>(dst
: *mut T
, src
: T
) -> 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::Release`](../../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_rel
<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::AcqRel`](../../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_acqrel
<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::Relaxed`](../../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_relaxed
<T
>(dst
: *mut T
, src
: T
) -> T
;
306 /// Add to the current value, returning the previous value.
307 /// The stabilized version of this intrinsic is available on the
308 /// `std::sync::atomic` types via the `fetch_add` method by passing
309 /// [`Ordering::SeqCst`](../../std/sync/atomic/enum.Ordering.html)
310 /// as the `order`. For example,
311 /// [`AtomicIsize::fetch_add`](../../std/sync/atomic/struct.AtomicIsize.html#method.fetch_add).
312 pub fn atomic_xadd
<T
>(dst
: *mut T
, src
: T
) -> T
;
313 /// Add to the current value, returning the previous value.
314 /// The stabilized version of this intrinsic is available on the
315 /// `std::sync::atomic` types via the `fetch_add` method by passing
316 /// [`Ordering::Acquire`](../../std/sync/atomic/enum.Ordering.html)
317 /// as the `order`. For example,
318 /// [`AtomicIsize::fetch_add`](../../std/sync/atomic/struct.AtomicIsize.html#method.fetch_add).
319 pub fn atomic_xadd_acq
<T
>(dst
: *mut T
, src
: T
) -> T
;
320 /// Add 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::Release`](../../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_rel
<T
>(dst
: *mut T
, src
: T
) -> T
;
327 /// Add 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::AcqRel`](../../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_acqrel
<T
>(dst
: *mut T
, src
: T
) -> T
;
334 /// Add 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::Relaxed`](../../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_relaxed
<T
>(dst
: *mut T
, src
: T
) -> T
;
342 /// Subtract from the current value, returning the previous value.
343 /// The stabilized version of this intrinsic is available on the
344 /// `std::sync::atomic` types via the `fetch_sub` method by passing
345 /// [`Ordering::SeqCst`](../../std/sync/atomic/enum.Ordering.html)
346 /// as the `order`. For example,
347 /// [`AtomicIsize::fetch_sub`](../../std/sync/atomic/struct.AtomicIsize.html#method.fetch_sub).
348 pub fn atomic_xsub
<T
>(dst
: *mut T
, src
: T
) -> T
;
349 /// Subtract from the current value, returning the previous value.
350 /// The stabilized version of this intrinsic is available on the
351 /// `std::sync::atomic` types via the `fetch_sub` method by passing
352 /// [`Ordering::Acquire`](../../std/sync/atomic/enum.Ordering.html)
353 /// as the `order`. For example,
354 /// [`AtomicIsize::fetch_sub`](../../std/sync/atomic/struct.AtomicIsize.html#method.fetch_sub).
355 pub fn atomic_xsub_acq
<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::Release`](../../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_rel
<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::AcqRel`](../../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_acqrel
<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::Relaxed`](../../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_relaxed
<T
>(dst
: *mut T
, src
: T
) -> T
;
378 /// Bitwise and with the current value, returning the previous value.
379 /// The stabilized version of this intrinsic is available on the
380 /// `std::sync::atomic` types via the `fetch_and` method by passing
381 /// [`Ordering::SeqCst`](../../std/sync/atomic/enum.Ordering.html)
382 /// as the `order`. For example,
383 /// [`AtomicBool::fetch_and`](../../std/sync/atomic/struct.AtomicBool.html#method.fetch_and).
384 pub fn atomic_and
<T
>(dst
: *mut T
, src
: T
) -> T
;
385 /// Bitwise and with the current value, returning the previous value.
386 /// The stabilized version of this intrinsic is available on the
387 /// `std::sync::atomic` types via the `fetch_and` method by passing
388 /// [`Ordering::Acquire`](../../std/sync/atomic/enum.Ordering.html)
389 /// as the `order`. For example,
390 /// [`AtomicBool::fetch_and`](../../std/sync/atomic/struct.AtomicBool.html#method.fetch_and).
391 pub fn atomic_and_acq
<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::Release`](../../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_rel
<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::AcqRel`](../../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_acqrel
<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::Relaxed`](../../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_relaxed
<T
>(dst
: *mut T
, src
: T
) -> T
;
414 /// Bitwise nand with the current value, returning the previous value.
415 /// The stabilized version of this intrinsic is available on the
416 /// `std::sync::atomic::AtomicBool` type via the `fetch_nand` method by passing
417 /// [`Ordering::SeqCst`](../../std/sync/atomic/enum.Ordering.html)
418 /// as the `order`. For example,
419 /// [`AtomicBool::fetch_nand`](../../std/sync/atomic/struct.AtomicBool.html#method.fetch_nand).
420 pub fn atomic_nand
<T
>(dst
: *mut T
, src
: T
) -> T
;
421 /// Bitwise nand with the current value, returning the previous value.
422 /// The stabilized version of this intrinsic is available on the
423 /// `std::sync::atomic::AtomicBool` type via the `fetch_nand` method by passing
424 /// [`Ordering::Acquire`](../../std/sync/atomic/enum.Ordering.html)
425 /// as the `order`. For example,
426 /// [`AtomicBool::fetch_nand`](../../std/sync/atomic/struct.AtomicBool.html#method.fetch_nand).
427 pub fn atomic_nand_acq
<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::Release`](../../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_rel
<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::AcqRel`](../../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_acqrel
<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::Relaxed`](../../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_relaxed
<T
>(dst
: *mut T
, src
: T
) -> T
;
450 /// Bitwise or with the current value, returning the previous value.
451 /// The stabilized version of this intrinsic is available on the
452 /// `std::sync::atomic` types via the `fetch_or` method by passing
453 /// [`Ordering::SeqCst`](../../std/sync/atomic/enum.Ordering.html)
454 /// as the `order`. For example,
455 /// [`AtomicBool::fetch_or`](../../std/sync/atomic/struct.AtomicBool.html#method.fetch_or).
456 pub fn atomic_or
<T
>(dst
: *mut T
, src
: T
) -> T
;
457 /// Bitwise or with the current value, returning the previous value.
458 /// The stabilized version of this intrinsic is available on the
459 /// `std::sync::atomic` types via the `fetch_or` method by passing
460 /// [`Ordering::Acquire`](../../std/sync/atomic/enum.Ordering.html)
461 /// as the `order`. For example,
462 /// [`AtomicBool::fetch_or`](../../std/sync/atomic/struct.AtomicBool.html#method.fetch_or).
463 pub fn atomic_or_acq
<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::Release`](../../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_rel
<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::AcqRel`](../../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_acqrel
<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::Relaxed`](../../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_relaxed
<T
>(dst
: *mut T
, src
: T
) -> T
;
486 /// Bitwise xor with the current value, returning the previous value.
487 /// The stabilized version of this intrinsic is available on the
488 /// `std::sync::atomic` types via the `fetch_xor` method by passing
489 /// [`Ordering::SeqCst`](../../std/sync/atomic/enum.Ordering.html)
490 /// as the `order`. For example,
491 /// [`AtomicBool::fetch_xor`](../../std/sync/atomic/struct.AtomicBool.html#method.fetch_xor).
492 pub fn atomic_xor
<T
>(dst
: *mut T
, src
: T
) -> T
;
493 /// Bitwise xor with the current value, returning the previous value.
494 /// The stabilized version of this intrinsic is available on the
495 /// `std::sync::atomic` types via the `fetch_xor` method by passing
496 /// [`Ordering::Acquire`](../../std/sync/atomic/enum.Ordering.html)
497 /// as the `order`. For example,
498 /// [`AtomicBool::fetch_xor`](../../std/sync/atomic/struct.AtomicBool.html#method.fetch_xor).
499 pub fn atomic_xor_acq
<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::Release`](../../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_rel
<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::AcqRel`](../../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_acqrel
<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::Relaxed`](../../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_relaxed
<T
>(dst
: *mut T
, src
: T
) -> T
;
522 pub fn atomic_max
<T
>(dst
: *mut T
, src
: T
) -> T
;
523 pub fn atomic_max_acq
<T
>(dst
: *mut T
, src
: T
) -> T
;
524 pub fn atomic_max_rel
<T
>(dst
: *mut T
, src
: T
) -> T
;
525 pub fn atomic_max_acqrel
<T
>(dst
: *mut T
, src
: T
) -> T
;
526 pub fn atomic_max_relaxed
<T
>(dst
: *mut T
, src
: T
) -> T
;
528 pub fn atomic_min
<T
>(dst
: *mut T
, src
: T
) -> T
;
529 pub fn atomic_min_acq
<T
>(dst
: *mut T
, src
: T
) -> T
;
530 pub fn atomic_min_rel
<T
>(dst
: *mut T
, src
: T
) -> T
;
531 pub fn atomic_min_acqrel
<T
>(dst
: *mut T
, src
: T
) -> T
;
532 pub fn atomic_min_relaxed
<T
>(dst
: *mut T
, src
: T
) -> T
;
534 pub fn atomic_umin
<T
>(dst
: *mut T
, src
: T
) -> T
;
535 pub fn atomic_umin_acq
<T
>(dst
: *mut T
, src
: T
) -> T
;
536 pub fn atomic_umin_rel
<T
>(dst
: *mut T
, src
: T
) -> T
;
537 pub fn atomic_umin_acqrel
<T
>(dst
: *mut T
, src
: T
) -> T
;
538 pub fn atomic_umin_relaxed
<T
>(dst
: *mut T
, src
: T
) -> T
;
540 pub fn atomic_umax
<T
>(dst
: *mut T
, src
: T
) -> T
;
541 pub fn atomic_umax_acq
<T
>(dst
: *mut T
, src
: T
) -> T
;
542 pub fn atomic_umax_rel
<T
>(dst
: *mut T
, src
: T
) -> T
;
543 pub fn atomic_umax_acqrel
<T
>(dst
: *mut T
, src
: T
) -> T
;
544 pub fn atomic_umax_relaxed
<T
>(dst
: *mut T
, src
: T
) -> T
;
547 extern "rust-intrinsic" {
549 pub fn atomic_fence();
550 pub fn atomic_fence_acq();
551 pub fn atomic_fence_rel();
552 pub fn atomic_fence_acqrel();
554 /// A compiler-only memory barrier.
556 /// Memory accesses will never be reordered across this barrier by the
557 /// compiler, but no instructions will be emitted for it. This is
558 /// appropriate for operations on the same thread that may be preempted,
559 /// such as when interacting with signal handlers.
560 pub fn atomic_singlethreadfence();
561 pub fn atomic_singlethreadfence_acq();
562 pub fn atomic_singlethreadfence_rel();
563 pub fn atomic_singlethreadfence_acqrel();
565 /// Magic intrinsic that derives its meaning from attributes
566 /// attached to the function.
568 /// For example, dataflow uses this to inject static assertions so
569 /// that `rustc_peek(potentially_uninitialized)` would actually
570 /// double-check that dataflow did indeed compute that it is
571 /// uninitialized at that point in the control flow.
572 pub fn rustc_peek
<T
>(_
: T
) -> T
;
574 /// Aborts the execution of the process.
577 /// Tells LLVM that this point in the code is not reachable,
578 /// enabling further optimizations.
580 /// NB: This is very different from the `unreachable!()` macro!
581 pub fn unreachable() -> !;
583 /// Informs the optimizer that a condition is always true.
584 /// If the condition is false, the behavior is undefined.
586 /// No code is generated for this intrinsic, but the optimizer will try
587 /// to preserve it (and its condition) between passes, which may interfere
588 /// with optimization of surrounding code and reduce performance. It should
589 /// not be used if the invariant can be discovered by the optimizer on its
590 /// own, or if it does not enable any significant optimizations.
591 pub fn assume(b
: bool
);
593 /// Hints to the compiler that branch condition is likely to be true.
594 /// Returns the value passed to it.
596 /// Any use other than with `if` statements will probably not have an effect.
597 pub fn likely(b
: bool
) -> bool
;
599 /// Hints to the compiler that branch condition is likely to be false.
600 /// Returns the value passed to it.
602 /// Any use other than with `if` statements will probably not have an effect.
603 pub fn unlikely(b
: bool
) -> bool
;
605 /// Executes a breakpoint trap, for inspection by a debugger.
608 /// The size of a type in bytes.
610 /// More specifically, this is the offset in bytes between successive
611 /// items of the same type, including alignment padding.
612 pub fn size_of
<T
>() -> usize;
614 /// Moves a value to an uninitialized memory location.
616 /// Drop glue is not run on the destination.
617 pub fn move_val_init
<T
>(dst
: *mut T
, src
: T
);
619 pub fn min_align_of
<T
>() -> usize;
620 pub fn pref_align_of
<T
>() -> usize;
622 pub fn size_of_val
<T
: ?Sized
>(_
: &T
) -> usize;
623 pub fn min_align_of_val
<T
: ?Sized
>(_
: &T
) -> usize;
625 /// Executes the destructor (if any) of the pointed-to value.
627 /// This has two use cases:
629 /// * It is *required* to use `drop_in_place` to drop unsized types like
630 /// trait objects, because they can't be read out onto the stack and
631 /// dropped normally.
633 /// * It is friendlier to the optimizer to do this over `ptr::read` when
634 /// dropping manually allocated memory (e.g. when writing Box/Rc/Vec),
635 /// as the compiler doesn't need to prove that it's sound to elide the
638 /// # Undefined Behavior
640 /// This has all the same safety problems as `ptr::read` with respect to
641 /// invalid pointers, types, and double drops.
642 #[stable(feature = "drop_in_place", since = "1.8.0")]
643 pub fn drop_in_place
<T
: ?Sized
>(to_drop
: *mut T
);
645 /// Gets a static string slice containing the name of a type.
646 pub fn type_name
<T
: ?Sized
>() -> &'
static str;
648 /// Gets an identifier which is globally unique to the specified type. This
649 /// function will return the same value for a type regardless of whichever
650 /// crate it is invoked in.
651 pub fn type_id
<T
: ?Sized
+ '
static>() -> u64;
653 /// Creates a value initialized to zero.
655 /// `init` is unsafe because it returns a zeroed-out datum,
656 /// which is unsafe unless T is `Copy`. Also, even if T is
657 /// `Copy`, an all-zero value may not correspond to any legitimate
658 /// state for the type in question.
659 pub fn init
<T
>() -> T
;
661 /// Creates an uninitialized value.
663 /// `uninit` is unsafe because there is no guarantee of what its
664 /// contents are. In particular its drop-flag may be set to any
665 /// state, which means it may claim either dropped or
666 /// undropped. In the general case one must use `ptr::write` to
667 /// initialize memory previous set to the result of `uninit`.
668 pub fn uninit
<T
>() -> T
;
670 /// Moves a value out of scope without running drop glue.
671 pub fn forget
<T
>(_
: T
) -> ();
673 /// Reinterprets the bits of a value of one type as another type.
675 /// Both types must have the same size. Neither the original, nor the result,
676 /// may be an [invalid value](../../nomicon/meet-safe-and-unsafe.html).
678 /// `transmute` is semantically equivalent to a bitwise move of one type
679 /// into another. It copies the bits from the source value into the
680 /// destination value, then forgets the original. It's equivalent to C's
681 /// `memcpy` under the hood, just like `transmute_copy`.
683 /// `transmute` is **incredibly** unsafe. There are a vast number of ways to
684 /// cause [undefined behavior][ub] with this function. `transmute` should be
685 /// the absolute last resort.
687 /// The [nomicon](../../nomicon/transmutes.html) has additional
690 /// [ub]: ../../reference.html#behavior-considered-undefined
694 /// There are a few things that `transmute` is really useful for.
696 /// Getting the bitpattern of a floating point type (or, more generally,
697 /// type punning, when `T` and `U` aren't pointers):
700 /// let bitpattern = unsafe {
701 /// std::mem::transmute::<f32, u32>(1.0)
703 /// assert_eq!(bitpattern, 0x3F800000);
706 /// Turning a pointer into a function pointer. This is *not* portable to
707 /// machines where function pointers and data pointers have different sizes.
710 /// fn foo() -> i32 {
713 /// let pointer = foo as *const ();
714 /// let function = unsafe {
715 /// std::mem::transmute::<*const (), fn() -> i32>(pointer)
717 /// assert_eq!(function(), 0);
720 /// Extending a lifetime, or shortening an invariant lifetime. This is
721 /// advanced, very unsafe Rust!
724 /// struct R<'a>(&'a i32);
725 /// unsafe fn extend_lifetime<'b>(r: R<'b>) -> R<'static> {
726 /// std::mem::transmute::<R<'b>, R<'static>>(r)
729 /// unsafe fn shorten_invariant_lifetime<'b, 'c>(r: &'b mut R<'static>)
730 /// -> &'b mut R<'c> {
731 /// std::mem::transmute::<&'b mut R<'static>, &'b mut R<'c>>(r)
737 /// Don't despair: many uses of `transmute` can be achieved through other means.
738 /// Below are common applications of `transmute` which can be replaced with safer
741 /// Turning a pointer into a `usize`:
745 /// let ptr_num_transmute = unsafe {
746 /// std::mem::transmute::<&i32, usize>(ptr)
749 /// // Use an `as` cast instead
750 /// let ptr_num_cast = ptr as *const i32 as usize;
753 /// Turning a `*mut T` into an `&mut T`:
756 /// let ptr: *mut i32 = &mut 0;
757 /// let ref_transmuted = unsafe {
758 /// std::mem::transmute::<*mut i32, &mut i32>(ptr)
761 /// // Use a reborrow instead
762 /// let ref_casted = unsafe { &mut *ptr };
765 /// Turning an `&mut T` into an `&mut U`:
768 /// let ptr = &mut 0;
769 /// let val_transmuted = unsafe {
770 /// std::mem::transmute::<&mut i32, &mut u32>(ptr)
773 /// // Now, put together `as` and reborrowing - note the chaining of `as`
774 /// // `as` is not transitive
775 /// let val_casts = unsafe { &mut *(ptr as *mut i32 as *mut u32) };
778 /// Turning an `&str` into an `&[u8]`:
781 /// // this is not a good way to do this.
782 /// let slice = unsafe { std::mem::transmute::<&str, &[u8]>("Rust") };
783 /// assert_eq!(slice, &[82, 117, 115, 116]);
785 /// // You could use `str::as_bytes`
786 /// let slice = "Rust".as_bytes();
787 /// assert_eq!(slice, &[82, 117, 115, 116]);
789 /// // Or, just use a byte string, if you have control over the string
791 /// assert_eq!(b"Rust", &[82, 117, 115, 116]);
794 /// Turning a `Vec<&T>` into a `Vec<Option<&T>>`:
797 /// let store = [0, 1, 2, 3];
798 /// let mut v_orig = store.iter().collect::<Vec<&i32>>();
800 /// // Using transmute: this is Undefined Behavior, and a bad idea.
801 /// // However, it is no-copy.
802 /// let v_transmuted = unsafe {
803 /// std::mem::transmute::<Vec<&i32>, Vec<Option<&i32>>>(
807 /// // This is the suggested, safe way.
808 /// // It does copy the entire vector, though, into a new array.
809 /// let v_collected = v_orig.clone()
811 /// .map(|r| Some(r))
812 /// .collect::<Vec<Option<&i32>>>();
814 /// // The no-copy, unsafe way, still using transmute, but not UB.
815 /// // This is equivalent to the original, but safer, and reuses the
816 /// // same Vec internals. Therefore the new inner type must have the
817 /// // exact same size, and the same or lesser alignment, as the old
818 /// // type. The same caveats exist for this method as transmute, for
819 /// // the original inner type (`&i32`) to the converted inner type
820 /// // (`Option<&i32>`), so read the nomicon pages linked above.
821 /// let v_from_raw = unsafe {
822 /// Vec::from_raw_parts(v_orig.as_mut_ptr(),
824 /// v_orig.capacity())
826 /// std::mem::forget(v_orig);
829 /// Implementing `split_at_mut`:
832 /// use std::{slice, mem};
834 /// // There are multiple ways to do this; and there are multiple problems
835 /// // with the following, transmute, way.
836 /// fn split_at_mut_transmute<T>(slice: &mut [T], mid: usize)
837 /// -> (&mut [T], &mut [T]) {
838 /// let len = slice.len();
839 /// assert!(mid <= len);
841 /// let slice2 = mem::transmute::<&mut [T], &mut [T]>(slice);
842 /// // first: transmute is not typesafe; all it checks is that T and
843 /// // U are of the same size. Second, right here, you have two
844 /// // mutable references pointing to the same memory.
845 /// (&mut slice[0..mid], &mut slice2[mid..len])
849 /// // This gets rid of the typesafety problems; `&mut *` will *only* give
850 /// // you an `&mut T` from an `&mut T` or `*mut T`.
851 /// fn split_at_mut_casts<T>(slice: &mut [T], mid: usize)
852 /// -> (&mut [T], &mut [T]) {
853 /// let len = slice.len();
854 /// assert!(mid <= len);
856 /// let slice2 = &mut *(slice as *mut [T]);
857 /// // however, you still have two mutable references pointing to
858 /// // the same memory.
859 /// (&mut slice[0..mid], &mut slice2[mid..len])
863 /// // This is how the standard library does it. This is the best method, if
864 /// // you need to do something like this
865 /// fn split_at_stdlib<T>(slice: &mut [T], mid: usize)
866 /// -> (&mut [T], &mut [T]) {
867 /// let len = slice.len();
868 /// assert!(mid <= len);
870 /// let ptr = slice.as_mut_ptr();
871 /// // This now has three mutable references pointing at the same
872 /// // memory. `slice`, the rvalue ret.0, and the rvalue ret.1.
873 /// // `slice` is never used after `let ptr = ...`, and so one can
874 /// // treat it as "dead", and therefore, you only have two real
875 /// // mutable slices.
876 /// (slice::from_raw_parts_mut(ptr, mid),
877 /// slice::from_raw_parts_mut(ptr.offset(mid as isize), len - mid))
881 #[stable(feature = "rust1", since = "1.0.0")]
882 pub fn transmute
<T
, U
>(e
: T
) -> U
;
884 /// Returns `true` if the actual type given as `T` requires drop
885 /// glue; returns `false` if the actual type provided for `T`
886 /// implements `Copy`.
888 /// If the actual type neither requires drop glue nor implements
889 /// `Copy`, then may return `true` or `false`.
890 pub fn needs_drop
<T
>() -> bool
;
892 /// Calculates the offset from a pointer.
894 /// This is implemented as an intrinsic to avoid converting to and from an
895 /// integer, since the conversion would throw away aliasing information.
899 /// Both the starting and resulting pointer must be either in bounds or one
900 /// byte past the end of an allocated object. If either pointer is out of
901 /// bounds or arithmetic overflow occurs then any further use of the
902 /// returned value will result in undefined behavior.
903 pub fn offset
<T
>(dst
: *const T
, offset
: isize) -> *const T
;
905 /// Calculates the offset from a pointer, potentially wrapping.
907 /// This is implemented as an intrinsic to avoid converting to and from an
908 /// integer, since the conversion inhibits certain optimizations.
912 /// Unlike the `offset` intrinsic, this intrinsic does not restrict the
913 /// resulting pointer to point into or one byte past the end of an allocated
914 /// object, and it wraps with two's complement arithmetic. The resulting
915 /// value is not necessarily valid to be used to actually access memory.
916 pub fn arith_offset
<T
>(dst
: *const T
, offset
: isize) -> *const T
;
918 /// Copies `count * size_of<T>` bytes from `src` to `dst`. The source
919 /// and destination may *not* overlap.
921 /// `copy_nonoverlapping` is semantically equivalent to C's `memcpy`.
925 /// Beyond requiring that the program must be allowed to access both regions
926 /// of memory, it is Undefined Behavior for source and destination to
927 /// overlap. Care must also be taken with the ownership of `src` and
928 /// `dst`. This method semantically moves the values of `src` into `dst`.
929 /// However it does not drop the contents of `dst`, or prevent the contents
930 /// of `src` from being dropped or used.
934 /// A safe swap function:
940 /// # #[allow(dead_code)]
941 /// fn swap<T>(x: &mut T, y: &mut T) {
943 /// // Give ourselves some scratch space to work with
944 /// let mut t: T = mem::uninitialized();
946 /// // Perform the swap, `&mut` pointers never alias
947 /// ptr::copy_nonoverlapping(x, &mut t, 1);
948 /// ptr::copy_nonoverlapping(y, x, 1);
949 /// ptr::copy_nonoverlapping(&t, y, 1);
951 /// // y and t now point to the same thing, but we need to completely forget `tmp`
952 /// // because it's no longer relevant.
957 #[stable(feature = "rust1", since = "1.0.0")]
958 pub fn copy_nonoverlapping
<T
>(src
: *const T
, dst
: *mut T
, count
: usize);
960 /// Copies `count * size_of<T>` bytes from `src` to `dst`. The source
961 /// and destination may overlap.
963 /// `copy` is semantically equivalent to C's `memmove`.
967 /// Care must be taken with the ownership of `src` and `dst`.
968 /// This method semantically moves the values of `src` into `dst`.
969 /// However it does not drop the contents of `dst`, or prevent the contents of `src`
970 /// from being dropped or used.
974 /// Efficiently create a Rust vector from an unsafe buffer:
979 /// # #[allow(dead_code)]
980 /// unsafe fn from_buf_raw<T>(ptr: *const T, elts: usize) -> Vec<T> {
981 /// let mut dst = Vec::with_capacity(elts);
982 /// dst.set_len(elts);
983 /// ptr::copy(ptr, dst.as_mut_ptr(), elts);
988 #[stable(feature = "rust1", since = "1.0.0")]
989 pub fn copy
<T
>(src
: *const T
, dst
: *mut T
, count
: usize);
991 /// Invokes memset on the specified pointer, setting `count * size_of::<T>()`
992 /// bytes of memory starting at `dst` to `val`.
999 /// let mut vec = vec![0; 4];
1001 /// let vec_ptr = vec.as_mut_ptr();
1002 /// ptr::write_bytes(vec_ptr, b'a', 2);
1004 /// assert_eq!(vec, [b'a', b'a', 0, 0]);
1006 #[stable(feature = "rust1", since = "1.0.0")]
1007 pub fn write_bytes
<T
>(dst
: *mut T
, val
: u8, count
: usize);
1009 /// Equivalent to the appropriate `llvm.memcpy.p0i8.0i8.*` intrinsic, with
1010 /// a size of `count` * `size_of::<T>()` and an alignment of
1011 /// `min_align_of::<T>()`
1013 /// The volatile parameter is set to `true`, so it will not be optimized out.
1014 pub fn volatile_copy_nonoverlapping_memory
<T
>(dst
: *mut T
, src
: *const T
,
1016 /// Equivalent to the appropriate `llvm.memmove.p0i8.0i8.*` intrinsic, with
1017 /// a size of `count` * `size_of::<T>()` and an alignment of
1018 /// `min_align_of::<T>()`
1020 /// The volatile parameter is set to `true`, so it will not be optimized out.
1021 pub fn volatile_copy_memory
<T
>(dst
: *mut T
, src
: *const T
, count
: usize);
1022 /// Equivalent to the appropriate `llvm.memset.p0i8.*` intrinsic, with a
1023 /// size of `count` * `size_of::<T>()` and an alignment of
1024 /// `min_align_of::<T>()`.
1026 /// The volatile parameter is set to `true`, so it will not be optimized out.
1027 pub fn volatile_set_memory
<T
>(dst
: *mut T
, val
: u8, count
: usize);
1029 /// Perform a volatile load from the `src` pointer.
1030 /// The stabilized version of this intrinsic is
1031 /// [`std::ptr::read_volatile`](../../std/ptr/fn.read_volatile.html).
1032 pub fn volatile_load
<T
>(src
: *const T
) -> T
;
1033 /// Perform a volatile store to the `dst` pointer.
1034 /// The stabilized version of this intrinsic is
1035 /// [`std::ptr::write_volatile`](../../std/ptr/fn.write_volatile.html).
1036 pub fn volatile_store
<T
>(dst
: *mut T
, val
: T
);
1038 /// Returns the square root of an `f32`
1039 pub fn sqrtf32(x
: f32) -> f32;
1040 /// Returns the square root of an `f64`
1041 pub fn sqrtf64(x
: f64) -> f64;
1043 /// Raises an `f32` to an integer power.
1044 pub fn powif32(a
: f32, x
: i32) -> f32;
1045 /// Raises an `f64` to an integer power.
1046 pub fn powif64(a
: f64, x
: i32) -> f64;
1048 /// Returns the sine of an `f32`.
1049 pub fn sinf32(x
: f32) -> f32;
1050 /// Returns the sine of an `f64`.
1051 pub fn sinf64(x
: f64) -> f64;
1053 /// Returns the cosine of an `f32`.
1054 pub fn cosf32(x
: f32) -> f32;
1055 /// Returns the cosine of an `f64`.
1056 pub fn cosf64(x
: f64) -> f64;
1058 /// Raises an `f32` to an `f32` power.
1059 pub fn powf32(a
: f32, x
: f32) -> f32;
1060 /// Raises an `f64` to an `f64` power.
1061 pub fn powf64(a
: f64, x
: f64) -> f64;
1063 /// Returns the exponential of an `f32`.
1064 pub fn expf32(x
: f32) -> f32;
1065 /// Returns the exponential of an `f64`.
1066 pub fn expf64(x
: f64) -> f64;
1068 /// Returns 2 raised to the power of an `f32`.
1069 pub fn exp2f32(x
: f32) -> f32;
1070 /// Returns 2 raised to the power of an `f64`.
1071 pub fn exp2f64(x
: f64) -> f64;
1073 /// Returns the natural logarithm of an `f32`.
1074 pub fn logf32(x
: f32) -> f32;
1075 /// Returns the natural logarithm of an `f64`.
1076 pub fn logf64(x
: f64) -> f64;
1078 /// Returns the base 10 logarithm of an `f32`.
1079 pub fn log10f32(x
: f32) -> f32;
1080 /// Returns the base 10 logarithm of an `f64`.
1081 pub fn log10f64(x
: f64) -> f64;
1083 /// Returns the base 2 logarithm of an `f32`.
1084 pub fn log2f32(x
: f32) -> f32;
1085 /// Returns the base 2 logarithm of an `f64`.
1086 pub fn log2f64(x
: f64) -> f64;
1088 /// Returns `a * b + c` for `f32` values.
1089 pub fn fmaf32(a
: f32, b
: f32, c
: f32) -> f32;
1090 /// Returns `a * b + c` for `f64` values.
1091 pub fn fmaf64(a
: f64, b
: f64, c
: f64) -> f64;
1093 /// Returns the absolute value of an `f32`.
1094 pub fn fabsf32(x
: f32) -> f32;
1095 /// Returns the absolute value of an `f64`.
1096 pub fn fabsf64(x
: f64) -> f64;
1098 /// Copies the sign from `y` to `x` for `f32` values.
1099 pub fn copysignf32(x
: f32, y
: f32) -> f32;
1100 /// Copies the sign from `y` to `x` for `f64` values.
1101 pub fn copysignf64(x
: f64, y
: f64) -> f64;
1103 /// Returns the largest integer less than or equal to an `f32`.
1104 pub fn floorf32(x
: f32) -> f32;
1105 /// Returns the largest integer less than or equal to an `f64`.
1106 pub fn floorf64(x
: f64) -> f64;
1108 /// Returns the smallest integer greater than or equal to an `f32`.
1109 pub fn ceilf32(x
: f32) -> f32;
1110 /// Returns the smallest integer greater than or equal to an `f64`.
1111 pub fn ceilf64(x
: f64) -> f64;
1113 /// Returns the integer part of an `f32`.
1114 pub fn truncf32(x
: f32) -> f32;
1115 /// Returns the integer part of an `f64`.
1116 pub fn truncf64(x
: f64) -> f64;
1118 /// Returns the nearest integer to an `f32`. May raise an inexact floating-point exception
1119 /// if the argument is not an integer.
1120 pub fn rintf32(x
: f32) -> f32;
1121 /// Returns the nearest integer to an `f64`. May raise an inexact floating-point exception
1122 /// if the argument is not an integer.
1123 pub fn rintf64(x
: f64) -> f64;
1125 /// Returns the nearest integer to an `f32`.
1126 pub fn nearbyintf32(x
: f32) -> f32;
1127 /// Returns the nearest integer to an `f64`.
1128 pub fn nearbyintf64(x
: f64) -> f64;
1130 /// Returns the nearest integer to an `f32`. Rounds half-way cases away from zero.
1131 pub fn roundf32(x
: f32) -> f32;
1132 /// Returns the nearest integer to an `f64`. Rounds half-way cases away from zero.
1133 pub fn roundf64(x
: f64) -> f64;
1135 /// Float addition that allows optimizations based on algebraic rules.
1136 /// May assume inputs are finite.
1137 pub fn fadd_fast
<T
>(a
: T
, b
: T
) -> T
;
1139 /// Float subtraction that allows optimizations based on algebraic rules.
1140 /// May assume inputs are finite.
1141 pub fn fsub_fast
<T
>(a
: T
, b
: T
) -> T
;
1143 /// Float multiplication that allows optimizations based on algebraic rules.
1144 /// May assume inputs are finite.
1145 pub fn fmul_fast
<T
>(a
: T
, b
: T
) -> T
;
1147 /// Float division that allows optimizations based on algebraic rules.
1148 /// May assume inputs are finite.
1149 pub fn fdiv_fast
<T
>(a
: T
, b
: T
) -> T
;
1151 /// Float remainder that allows optimizations based on algebraic rules.
1152 /// May assume inputs are finite.
1153 pub fn frem_fast
<T
>(a
: T
, b
: T
) -> T
;
1156 /// Returns the number of bits set in an integer type `T`
1157 pub fn ctpop
<T
>(x
: T
) -> T
;
1159 /// Returns the number of leading bits unset in an integer type `T`
1160 pub fn ctlz
<T
>(x
: T
) -> T
;
1162 /// Returns the number of trailing bits unset in an integer type `T`
1163 pub fn cttz
<T
>(x
: T
) -> T
;
1165 /// Reverses the bytes in an integer type `T`.
1166 pub fn bswap
<T
>(x
: T
) -> T
;
1168 /// Performs checked integer addition.
1169 /// The stabilized versions of this intrinsic are available on the integer
1170 /// primitives via the `overflowing_add` method. For example,
1171 /// [`std::u32::overflowing_add`](../../std/primitive.u32.html#method.overflowing_add)
1172 pub fn add_with_overflow
<T
>(x
: T
, y
: T
) -> (T
, bool
);
1174 /// Performs checked integer subtraction
1175 /// The stabilized versions of this intrinsic are available on the integer
1176 /// primitives via the `overflowing_sub` method. For example,
1177 /// [`std::u32::overflowing_sub`](../../std/primitive.u32.html#method.overflowing_sub)
1178 pub fn sub_with_overflow
<T
>(x
: T
, y
: T
) -> (T
, bool
);
1180 /// Performs checked integer multiplication
1181 /// The stabilized versions of this intrinsic are available on the integer
1182 /// primitives via the `overflowing_mul` method. For example,
1183 /// [`std::u32::overflowing_mul`](../../std/primitive.u32.html#method.overflowing_mul)
1184 pub fn mul_with_overflow
<T
>(x
: T
, y
: T
) -> (T
, bool
);
1186 /// Performs an unchecked division, resulting in undefined behavior
1187 /// where y = 0 or x = `T::min_value()` and y = -1
1188 pub fn unchecked_div
<T
>(x
: T
, y
: T
) -> T
;
1189 /// Returns the remainder of an unchecked division, resulting in
1190 /// undefined behavior where y = 0 or x = `T::min_value()` and y = -1
1191 pub fn unchecked_rem
<T
>(x
: T
, y
: T
) -> T
;
1193 /// Returns (a + b) mod 2^N, where N is the width of T in bits.
1194 /// The stabilized versions of this intrinsic are available on the integer
1195 /// primitives via the `wrapping_add` method. For example,
1196 /// [`std::u32::wrapping_add`](../../std/primitive.u32.html#method.wrapping_add)
1197 pub fn overflowing_add
<T
>(a
: T
, b
: T
) -> T
;
1198 /// Returns (a - b) mod 2^N, where N is the width of T in bits.
1199 /// The stabilized versions of this intrinsic are available on the integer
1200 /// primitives via the `wrapping_sub` method. For example,
1201 /// [`std::u32::wrapping_sub`](../../std/primitive.u32.html#method.wrapping_sub)
1202 pub fn overflowing_sub
<T
>(a
: T
, b
: T
) -> T
;
1203 /// Returns (a * b) mod 2^N, where N is the width of T in bits.
1204 /// The stabilized versions of this intrinsic are available on the integer
1205 /// primitives via the `wrapping_mul` method. For example,
1206 /// [`std::u32::wrapping_mul`](../../std/primitive.u32.html#method.wrapping_mul)
1207 pub fn overflowing_mul
<T
>(a
: T
, b
: T
) -> T
;
1209 /// Returns the value of the discriminant for the variant in 'v',
1210 /// cast to a `u64`; if `T` has no discriminant, returns 0.
1211 pub fn discriminant_value
<T
>(v
: &T
) -> u64;
1213 /// Rust's "try catch" construct which invokes the function pointer `f` with
1214 /// the data pointer `data`.
1216 /// The third pointer is a target-specific data pointer which is filled in
1217 /// with the specifics of the exception that occurred. For examples on Unix
1218 /// platforms this is a `*mut *mut T` which is filled in by the compiler and
1219 /// on MSVC it's `*mut [usize; 2]`. For more information see the compiler's
1220 /// source as well as std's catch implementation.
1221 pub fn try(f
: fn(*mut u8), data
: *mut u8, local_ptr
: *mut u8) -> i32;