@@ -1992,6 +1992,7 @@ mod type_keyword {}
19921992/// ```rust
19931993/// # #![allow(dead_code)]
19941994/// #![deny(unsafe_op_in_unsafe_fn)]
1995+ ///
19951996/// /// Dereference the given pointer.
19961997/// ///
19971998/// /// # Safety
@@ -2020,22 +2021,22 @@ mod type_keyword {}
20202021/// ///
20212022/// /// `make_even` must return an even number.
20222023/// unsafe trait MakeEven {
2023- /// fn make_even(&self) -> i32;
2024+ /// fn make_even(&self) -> i32;
20242025/// }
20252026///
20262027/// // SAFETY: Our `make_even` always returns something even.
20272028/// unsafe impl MakeEven for i32 {
2028- /// fn make_even(&self) -> i32 {
2029- /// self << 1
2030- /// }
2029+ /// fn make_even(&self) -> i32 {
2030+ /// self << 1
2031+ /// }
20312032/// }
20322033///
20332034/// fn use_make_even(x: impl MakeEven) {
2034- /// if x.make_even() % 2 == 1 {
2035- /// // SAFETY: this can never happen, because all `MakeEven` implementations
2036- /// // ensure that `make_even` returns something even.
2037- /// unsafe { std::hint::unreachable_unchecked() };
2038- /// }
2035+ /// if x.make_even() % 2 == 1 {
2036+ /// // SAFETY: this can never happen, because all `MakeEven` implementations
2037+ /// // ensure that `make_even` returns something even.
2038+ /// unsafe { std::hint::unreachable_unchecked() };
2039+ /// }
20392040/// }
20402041/// ```
20412042///
@@ -2053,55 +2054,55 @@ mod type_keyword {}
20532054/// #![deny(unsafe_op_in_unsafe_fn)]
20542055///
20552056/// trait Indexable {
2056- /// const LEN: usize;
2057+ /// const LEN: usize;
20572058///
2058- /// /// # Safety
2059- /// ///
2060- /// /// The caller must ensure that `idx < LEN`.
2061- /// unsafe fn idx_unchecked(&self, idx: usize) -> i32;
2059+ /// /// # Safety
2060+ /// ///
2061+ /// /// The caller must ensure that `idx < LEN`.
2062+ /// unsafe fn idx_unchecked(&self, idx: usize) -> i32;
20622063/// }
20632064///
20642065/// // The implementation for `i32` doesn't need to do any contract reasoning.
20652066/// impl Indexable for i32 {
2066- /// const LEN: usize = 1;
2067+ /// const LEN: usize = 1;
20672068///
2068- /// unsafe fn idx_unchecked(&self, idx: usize) -> i32 {
2069- /// debug_assert_eq!(idx, 0);
2070- /// *self
2071- /// }
2069+ /// unsafe fn idx_unchecked(&self, idx: usize) -> i32 {
2070+ /// debug_assert_eq!(idx, 0);
2071+ /// *self
2072+ /// }
20722073/// }
20732074///
20742075/// // The implementation for arrays exploits the function contract to
20752076/// // make use of `get_unchecked` on slices and avoid a run-time check.
20762077/// impl Indexable for [i32; 42] {
2077- /// const LEN: usize = 42;
2078+ /// const LEN: usize = 42;
20782079///
2079- /// unsafe fn idx_unchecked(&self, idx: usize) -> i32 {
2080- /// // SAFETY: As per this trait's documentation, the caller ensures
2081- /// // that `idx < 42`.
2082- /// unsafe { *self.get_unchecked(idx) }
2083- /// }
2080+ /// unsafe fn idx_unchecked(&self, idx: usize) -> i32 {
2081+ /// // SAFETY: As per this trait's documentation, the caller ensures
2082+ /// // that `idx < 42`.
2083+ /// unsafe { *self.get_unchecked(idx) }
2084+ /// }
20842085/// }
20852086///
20862087/// // The implementation for the never type declares a length of 0,
20872088/// // which means `idx_unchecked` can never be called.
20882089/// impl Indexable for ! {
2089- /// const LEN: usize = 0;
2090+ /// const LEN: usize = 0;
20902091///
2091- /// unsafe fn idx_unchecked(&self, idx: usize) -> i32 {
2092- /// // SAFETY: As per this trait's documentation, the caller ensures
2093- /// // that `idx < 0`, which is impossible, so this is dead code.
2094- /// unsafe { std::hint::unreachable_unchecked() }
2095- /// }
2092+ /// unsafe fn idx_unchecked(&self, idx: usize) -> i32 {
2093+ /// // SAFETY: As per this trait's documentation, the caller ensures
2094+ /// // that `idx < 0`, which is impossible, so this is dead code.
2095+ /// unsafe { std::hint::unreachable_unchecked() }
2096+ /// }
20962097/// }
20972098///
20982099/// fn use_indexable<I: Indexable>(x: I, idx: usize) -> i32 {
2099- /// if idx < I::LEN {
2100- /// // SAFETY: We have checked that `idx < I::LEN`.
2101- /// unsafe { x.idx_unchecked(idx) }
2102- /// } else {
2103- /// panic!("index out-of-bounds")
2104- /// }
2100+ /// if idx < I::LEN {
2101+ /// // SAFETY: We have checked that `idx < I::LEN`.
2102+ /// unsafe { x.idx_unchecked(idx) }
2103+ /// } else {
2104+ /// panic!("index out-of-bounds")
2105+ /// }
21052106/// }
21062107/// ```
21072108///
@@ -2115,11 +2116,11 @@ mod type_keyword {}
21152116/// is not implicitly an unsafe block.) For that purpose it can make use of the contract that all
21162117/// its callers must uphold -- the fact that `idx < LEN`.
21172118///
2118- /// Formally speaking, an `unsafe fn` in a trait is a function with extra
2119- /// *preconditions* (such as `idx < LEN`), whereas an `unsafe trait` can declare
2120- /// that some of its functions have extra *postconditions* (such as returning an
2121- /// even integer). If a trait needs a function with both extra precondition and
2122- /// extra postcondition, then it needs an `unsafe fn` in an `unsafe trait`.
2119+ /// Formally speaking, an `unsafe fn` in a trait is a function with *preconditions* that go beyond
2120+ /// those encoded by the argument types (such as `idx < LEN`), whereas an `unsafe trait` can declare
2121+ /// that some of its functions have *postconditions* that go beyond those encoded in the return type
2122+ /// (such as returning an even integer). If a trait needs a function with both extra precondition
2123+ /// and extra postcondition, then it needs an `unsafe fn` in an `unsafe trait`.
21232124///
21242125/// [`extern`]: keyword.extern.html
21252126/// [`trait`]: keyword.trait.html
0 commit comments