Skip to content

Commit 8c7668a

Browse files
committed
Fix nits
1 parent 7ec44e6 commit 8c7668a

File tree

1 file changed

+25
-16
lines changed

1 file changed

+25
-16
lines changed

src/libcore/intrinsics.rs

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -312,30 +312,34 @@ extern "rust-intrinsic" {
312312
/// are as follows:
313313
///
314314
/// Turning a pointer into a `usize`:
315+
///
315316
/// ```
316317
/// let ptr = &0;
317318
/// let ptr_num_transmute = mem::transmute::<&i32, usize>(ptr);
318-
/// // Use `as` casts instead
319+
/// // Use an `as` cast instead
319320
/// let ptr_num_cast = ptr as *const i32 as usize;
320321
/// ```
321322
///
322323
/// Turning a `*mut T` into an `&mut T`:
324+
///
323325
/// ```
324326
/// let ptr: *mut i32 = &mut 0;
325327
/// let ref_transmuted = mem::transmute::<*mut i32, &mut i32>(ptr);
326-
/// // Use reborrows
328+
/// // Use a reborrow instead
327329
/// let ref_casted = &mut *ptr;
328330
/// ```
329331
///
330332
/// Turning an `&mut T` into an `&mut U`:
333+
///
331334
/// ```
332335
/// let ptr = &mut 0;
333336
/// let val_transmuted = mem::transmute::<&mut i32, &mut u32>(ptr);
334-
/// // Now let's put together `as` and reborrowing
337+
/// // Now, put together `as` and reborrowing
335338
/// let val_casts = &mut *(ptr as *mut i32 as *mut u32);
336339
/// ```
337340
///
338341
/// Turning an `&str` into an `&[u8]`:
342+
///
339343
/// ```
340344
/// // this is not a good way to do this.
341345
/// let slice = unsafe { mem::transmute::<&str, &[u8]>("Rust") };
@@ -349,20 +353,21 @@ extern "rust-intrinsic" {
349353
/// ```
350354
///
351355
/// Turning a `Vec<&T>` into a `Vec<Option<&T>>`:
356+
///
352357
/// ```
353358
/// let store = [0, 1, 2, 3];
354359
/// let v_orig = store.iter().collect::<Vec<&i32>>();
355-
/// // Using transmute: this is Undefined Behavior, and a bad idea
356-
/// // However, it is no-copy
360+
/// // Using transmute: this is Undefined Behavior, and a bad idea.
361+
/// // However, it is no-copy.
357362
/// let v_transmuted = mem::transmute::<Vec<&i32>, Vec<Option<&i32>>>(
358363
/// v_orig.clone());
359-
/// // This is the suggested, safe way
360-
/// // It does copy the entire Vector, though, into a new array
364+
/// // This is the suggested, safe way.
365+
/// // It does copy the entire Vector, though, into a new array.
361366
/// let v_collected = v_orig.clone()
362367
/// .into_iter()
363368
/// .map(|r| Some(r))
364369
/// .collect::<Vec<Option<&i32>>>();
365-
/// // The no-copy, unsafe way, still using transmute, but not UB
370+
/// // The no-copy, unsafe way, still using transmute, but not UB.
366371
/// // This is equivalent to the original, but safer, and reuses the
367372
/// // same Vec internals. Therefore the new inner type must have the
368373
/// // exact same size, and the same or lesser alignment, as the old
@@ -375,11 +380,12 @@ extern "rust-intrinsic" {
375380
/// mem::forget(v_orig);
376381
/// ```
377382
///
378-
/// Implemententing `split_at_mut`:
383+
/// Implementing `split_at_mut`:
384+
///
379385
/// ```
380386
/// use std::{slice, mem};
381387
/// // There are multiple ways to do this; and there are multiple problems
382-
/// // with the following, transmute, way
388+
/// // with the following, transmute, way.
383389
/// fn split_at_mut_transmute<T>(slice: &mut [T], index: usize)
384390
/// -> (&mut [T], &mut [T]) {
385391
/// let len = slice.len();
@@ -388,20 +394,20 @@ extern "rust-intrinsic" {
388394
/// let slice2 = mem::transmute::<&mut [T], &mut [T]>(slice);
389395
/// // first: transmute is not typesafe; all it checks is that T and
390396
/// // U are of the same size. Second, right here, you have two
391-
/// // mutable references pointing to the same memory
397+
/// // mutable references pointing to the same memory.
392398
/// (&mut slice[0..index], &mut slice2[index..len])
393399
/// }
394400
/// }
395401
/// // This gets rid of the typesafety problems; `&mut *` will *only* give
396-
/// // you an &mut T from an &mut T or *mut T
402+
/// // you an `&mut T` from an `&mut T` or `*mut T`.
397403
/// fn split_at_mut_casts<T>(slice: &mut [T], index: usize)
398404
/// -> (&mut [T], &mut [T]) {
399405
/// let len = slice.len();
400406
/// assert!(index < len);
401407
/// unsafe {
402408
/// let slice2 = &mut *(slice as *mut [T]);
403409
/// // however, you still have two mutable references pointing to
404-
/// // the same memory
410+
/// // the same memory.
405411
/// (&mut slice[0..index], &mut slice2[index..len])
406412
/// }
407413
/// }
@@ -415,9 +421,9 @@ extern "rust-intrinsic" {
415421
/// assert!(mid <= len);
416422
/// // This now has three mutable references pointing at the same
417423
/// // memory. `slice`, the rvalue ret.0, and the rvalue ret.1.
418-
/// // However, `slice` is never used after `let ptr = ...`, and so
419-
/// // one can treat it as "dead", and therefore, you only have two
420-
/// // real mutable slices.
424+
/// // `slice` is never used after `let ptr = ...`, and so one can
425+
/// // treat it as "dead", and therefore, you only have two real
426+
/// // mutable slices.
421427
/// (slice::from_raw_parts_mut(ptr, mid),
422428
/// slice::from_raw_parts_mut(ptr.offset(mid as isize), len - mid))
423429
/// }
@@ -429,13 +435,15 @@ extern "rust-intrinsic" {
429435
/// There are valid uses of transmute, though they are few and far between.
430436
///
431437
/// Getting the bitpattern of a floating point type:
438+
///
432439
/// ```
433440
/// let bitpattern = std::mem::transmute::<f32, u32>(1.0);
434441
/// assert_eq!(bitpattern, 0x3F800000);
435442
/// ```
436443
///
437444
/// Turning a pointer into a function pointer (this isn't guaranteed to
438445
/// work in Rust, although, for example, Linux does make this guarantee):
446+
///
439447
/// ```
440448
/// fn foo() -> i32 {
441449
/// 0
@@ -447,6 +455,7 @@ extern "rust-intrinsic" {
447455
///
448456
/// Extending a lifetime, or shortening an invariant an invariant lifetime;
449457
/// this is advanced, very unsafe rust:
458+
///
450459
/// ```
451460
/// use std::mem;
452461
///

0 commit comments

Comments
 (0)