Skip to content

Commit 6e8ec85

Browse files
committed
Make documentation of slice_first_last_chunk more consistent
Clarify that these functions return array references. Also change from doing `as` casting to using the less misuseable `.cast()`.
1 parent 55020c6 commit 6e8ec85

File tree

1 file changed

+35
-21
lines changed

1 file changed

+35
-21
lines changed

library/core/src/slice/mod.rs

+35-21
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ impl<T> [T] {
300300
if let [.., last] = self { Some(last) } else { None }
301301
}
302302

303-
/// Returns a mutable pointer to the last item in the slice.
303+
/// Returns a mutable reference to the last item in the slice.
304304
///
305305
/// # Examples
306306
///
@@ -320,7 +320,9 @@ impl<T> [T] {
320320
if let [.., last] = self { Some(last) } else { None }
321321
}
322322

323-
/// Returns the first `N` elements of the slice, or `None` if it has fewer than `N` elements.
323+
/// Return an array reference to the first `N` items in the slice.
324+
///
325+
/// If the slice is not at least `N` in length, this will return `None`.
324326
///
325327
/// # Examples
326328
///
@@ -345,12 +347,13 @@ impl<T> [T] {
345347
} else {
346348
// SAFETY: We explicitly check for the correct number of elements,
347349
// and do not let the reference outlive the slice.
348-
Some(unsafe { &*(self.as_ptr() as *const [T; N]) })
350+
Some(unsafe { &*(self.as_ptr().cast::<[T; N]>()) })
349351
}
350352
}
351353

352-
/// Returns a mutable reference to the first `N` elements of the slice,
353-
/// or `None` if it has fewer than `N` elements.
354+
/// Return a mutable array reference to the first `N` items in the slice.
355+
///
356+
/// If the slice is not at least `N` in length, this will return `None`.
354357
///
355358
/// # Examples
356359
///
@@ -375,12 +378,13 @@ impl<T> [T] {
375378
// SAFETY: We explicitly check for the correct number of elements,
376379
// do not let the reference outlive the slice,
377380
// and require exclusive access to the entire slice to mutate the chunk.
378-
Some(unsafe { &mut *(self.as_mut_ptr() as *mut [T; N]) })
381+
Some(unsafe { &mut *(self.as_mut_ptr().cast::<[T; N]>()) })
379382
}
380383
}
381384

382-
/// Returns the first `N` elements of the slice and the remainder,
383-
/// or `None` if it has fewer than `N` elements.
385+
/// Return an array reference to the first `N` items in the slice and the remaining slice.
386+
///
387+
/// If the slice is not at least `N` in length, this will return `None`.
384388
///
385389
/// # Examples
386390
///
@@ -406,12 +410,14 @@ impl<T> [T] {
406410

407411
// SAFETY: We explicitly check for the correct number of elements,
408412
// and do not let the references outlive the slice.
409-
Some((unsafe { &*(first.as_ptr() as *const [T; N]) }, tail))
413+
Some((unsafe { &*(first.as_ptr().cast::<[T; N]>()) }, tail))
410414
}
411415
}
412416

413-
/// Returns a mutable reference to the first `N` elements of the slice and the remainder,
414-
/// or `None` if it has fewer than `N` elements.
417+
/// Return a mutable array reference to the first `N` items in the slice and the remaining
418+
/// slice.
419+
///
420+
/// If the slice is not at least `N` in length, this will return `None`.
415421
///
416422
/// # Examples
417423
///
@@ -442,12 +448,13 @@ impl<T> [T] {
442448
// SAFETY: We explicitly check for the correct number of elements,
443449
// do not let the reference outlive the slice,
444450
// and enforce exclusive mutability of the chunk by the split.
445-
Some((unsafe { &mut *(first.as_mut_ptr() as *mut [T; N]) }, tail))
451+
Some((unsafe { &mut *(first.as_mut_ptr().cast::<[T; N]>()) }, tail))
446452
}
447453
}
448454

449-
/// Returns the last `N` elements of the slice and the remainder,
450-
/// or `None` if it has fewer than `N` elements.
455+
/// Return an array reference to the last `N` items in the slice and the remaining slice.
456+
///
457+
/// If the slice is not at least `N` in length, this will return `None`.
451458
///
452459
/// # Examples
453460
///
@@ -473,11 +480,14 @@ impl<T> [T] {
473480

474481
// SAFETY: We explicitly check for the correct number of elements,
475482
// and do not let the references outlive the slice.
476-
Some((init, unsafe { &*(last.as_ptr() as *const [T; N]) }))
483+
Some((init, unsafe { &*(last.as_ptr().cast::<[T; N]>()) }))
477484
}
478485
}
479486

480-
/// Returns the last and all the rest of the elements of the slice, or `None` if it is empty.
487+
/// Return a mutable array reference to the last `N` items in the slice and the remaining
488+
/// slice.
489+
///
490+
/// If the slice is not at least `N` in length, this will return `None`.
481491
///
482492
/// # Examples
483493
///
@@ -508,11 +518,13 @@ impl<T> [T] {
508518
// SAFETY: We explicitly check for the correct number of elements,
509519
// do not let the reference outlive the slice,
510520
// and enforce exclusive mutability of the chunk by the split.
511-
Some((init, unsafe { &mut *(last.as_mut_ptr() as *mut [T; N]) }))
521+
Some((init, unsafe { &mut *(last.as_mut_ptr().cast::<[T; N]>()) }))
512522
}
513523
}
514524

515-
/// Returns the last element of the slice, or `None` if it is empty.
525+
/// Return an array reference to the last `N` items in the slice.
526+
///
527+
/// If the slice is not at least `N` in length, this will return `None`.
516528
///
517529
/// # Examples
518530
///
@@ -541,11 +553,13 @@ impl<T> [T] {
541553

542554
// SAFETY: We explicitly check for the correct number of elements,
543555
// and do not let the references outlive the slice.
544-
Some(unsafe { &*(last.as_ptr() as *const [T; N]) })
556+
Some(unsafe { &*(last.as_ptr().cast::<[T; N]>()) })
545557
}
546558
}
547559

548-
/// Returns a mutable pointer to the last item in the slice.
560+
/// Return a mutable array reference to the last `N` items in the slice.
561+
///
562+
/// If the slice is not at least `N` in length, this will return `None`.
549563
///
550564
/// # Examples
551565
///
@@ -574,7 +588,7 @@ impl<T> [T] {
574588
// SAFETY: We explicitly check for the correct number of elements,
575589
// do not let the reference outlive the slice,
576590
// and require exclusive access to the entire slice to mutate the chunk.
577-
Some(unsafe { &mut *(last.as_mut_ptr() as *mut [T; N]) })
591+
Some(unsafe { &mut *(last.as_mut_ptr().cast::<[T; N]>()) })
578592
}
579593
}
580594

0 commit comments

Comments
 (0)