From 56590e360b02890de820c951c98080c6ea40ca7f Mon Sep 17 00:00:00 2001 From: Ben Kimock Date: Fri, 4 Feb 2022 17:51:16 -0500 Subject: [PATCH] Fix aliasing in sort doctest In Stacked Borrows 2.1, the existing code is UB because the second argument does mutable indexing into a slice, which mutably borrows the entire contents of the slice and thus invalidates all pointers into it. Fortunately there is a safe API for the operation we need here, so we can use it and not even worry about what exactly the aliasing model is. --- src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 8391bc3..e2683a3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -153,7 +153,7 @@ //! if *hole_guard.value >= hole_guard.v[i] { //! // move the element back and the hole forward //! let index = hole_guard.index; -//! ptr::copy_nonoverlapping(&hole_guard.v[index + 1], &mut hole_guard.v[index], 1); +//! hole_guard.v.swap(index, index + 1); //! hole_guard.index += 1; //! } else { //! break;