Skip to content

Commit 97003e5

Browse files
committed
Switch around Examples and Alternatives
1 parent 7eabff5 commit 97003e5

File tree

1 file changed

+48
-45
lines changed

1 file changed

+48
-45
lines changed

src/libcore/intrinsics.rs

+48-45
Original file line numberDiff line numberDiff line change
@@ -294,11 +294,56 @@ extern "rust-intrinsic" {
294294
/// The [nomicon](../../nomicon/transmutes.html) has additional
295295
/// documentation.
296296
///
297+
/// # Examples
298+
///
299+
/// There are a few things that `transmute` is really useful for.
300+
///
301+
/// Getting the bitpattern of a floating point type (or, more generally,
302+
/// type punning, when T and U aren't pointers):
303+
///
304+
/// ```
305+
/// let bitpattern = unsafe {
306+
/// std::mem::transmute::<f32, u32>(1.0)
307+
/// };
308+
/// assert_eq!(bitpattern, 0x3F800000);
309+
/// ```
310+
///
311+
/// Turning a pointer into a function pointer:
312+
///
313+
/// ```
314+
/// fn foo() -> i32 {
315+
/// 0
316+
/// }
317+
/// let pointer = foo as *const ();
318+
/// let function = unsafe {
319+
/// std::mem::transmute::<*const (), fn() -> i32>(pointer)
320+
/// };
321+
/// assert_eq!(function(), 0);
322+
/// ```
323+
///
324+
/// Extending a lifetime, or shortening an invariant lifetime; this is
325+
/// advanced, very unsafe rust:
326+
///
327+
/// ```
328+
/// struct R<'a>(&'a i32);
329+
/// unsafe fn extend_lifetime<'b>(r: R<'b>) -> R<'static> {
330+
/// std::mem::transmute::<R<'b>, R<'static>>(r)
331+
/// }
332+
///
333+
/// unsafe fn shorten_invariant_lifetime<'b, 'c>(r: &'b mut R<'static>)
334+
/// -> &'b mut R<'c> {
335+
/// std::mem::transmute::<&'b mut R<'static>, &'b mut R<'c>>(r)
336+
/// }
337+
/// ```
338+
///
297339
/// # Alternatives
298340
///
299-
/// There are very few good cases for `transmute`. Most can be achieved
300-
/// through other means. Some more or less common uses, and a better way,
301-
/// are as follows:
341+
/// However, many uses of `transmute` can be achieved through other means.
342+
/// This is unfortunate because either `transmute` isn't guaranteed to work
343+
/// in that case, and only does because of rustc's current implemenation;
344+
/// or, more commonly, `transmute` is just too powerful. It can transform
345+
/// any type into any other, with just the caveat that they're the same
346+
/// size. Some more or less common uses, and a better way, are as follows:
302347
///
303348
/// Turning a pointer into a `usize`:
304349
///
@@ -428,48 +473,6 @@ extern "rust-intrinsic" {
428473
/// }
429474
/// }
430475
/// ```
431-
///
432-
/// # Examples
433-
///
434-
/// There are valid uses of transmute, though they are few and far between.
435-
///
436-
/// Getting the bitpattern of a floating point type:
437-
///
438-
/// ```
439-
/// let bitpattern = unsafe {
440-
/// std::mem::transmute::<f32, u32>(1.0)
441-
/// };
442-
/// assert_eq!(bitpattern, 0x3F800000);
443-
/// ```
444-
///
445-
/// Turning a pointer into a function pointer (this is not guaranteed to
446-
/// work in Rust, although, for example, Linux does make this guarantee):
447-
///
448-
/// ```
449-
/// fn foo() -> i32 {
450-
/// 0
451-
/// }
452-
/// let pointer = foo as *const ();
453-
/// let function = unsafe {
454-
/// std::mem::transmute::<*const (), fn() -> i32>(pointer)
455-
/// };
456-
/// assert_eq!(function(), 0);
457-
/// ```
458-
///
459-
/// Extending a lifetime, or shortening an invariant lifetime; this is
460-
/// advanced, very unsafe rust:
461-
///
462-
/// ```
463-
/// struct R<'a>(&'a i32);
464-
/// unsafe fn extend_lifetime<'b>(r: R<'b>) -> R<'static> {
465-
/// std::mem::transmute::<R<'b>, R<'static>>(r)
466-
/// }
467-
///
468-
/// unsafe fn shorten_invariant_lifetime<'b, 'c>(r: &'b mut R<'static>)
469-
/// -> &'b mut R<'c> {
470-
/// std::mem::transmute::<&'b mut R<'static>, &'b mut R<'c>>(r)
471-
/// }
472-
/// ```
473476
#[stable(feature = "rust1", since = "1.0.0")]
474477
pub fn transmute<T, U>(e: T) -> U;
475478

0 commit comments

Comments
 (0)