From 0889e73d627eb1ef6e6b9750f69c5e769bdc51bb Mon Sep 17 00:00:00 2001 From: SuperSamus <40663462+SuperSamus@users.noreply.github.com> Date: Sat, 28 Dec 2019 16:57:32 +0100 Subject: [PATCH 1/3] Update ch19-01-unsafe-rust.md Fixes #2042. --- src/ch19-01-unsafe-rust.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ch19-01-unsafe-rust.md b/src/ch19-01-unsafe-rust.md index c7956e8627..fad896b557 100644 --- a/src/ch19-01-unsafe-rust.md +++ b/src/ch19-01-unsafe-rust.md @@ -292,7 +292,7 @@ fn split_at_mut(slice: &mut [i32], mid: usize) -> (&mut [i32], &mut [i32]) { unsafe { (slice::from_raw_parts_mut(ptr, mid), - slice::from_raw_parts_mut(ptr.offset(mid as isize), len - mid)) + slice::from_raw_parts_mut(ptr.add(mid), len - mid)) } } ``` From 05f8008f5d17f2a94de219971ff447408653eaa1 Mon Sep 17 00:00:00 2001 From: SuperSamus <40663462+SuperSamus@users.noreply.github.com> Date: Thu, 30 Jan 2020 12:06:14 +0100 Subject: [PATCH 2/3] Update ch19-01-unsafe-rust.md --- src/ch19-01-unsafe-rust.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/ch19-01-unsafe-rust.md b/src/ch19-01-unsafe-rust.md index fad896b557..d2b29264a4 100644 --- a/src/ch19-01-unsafe-rust.md +++ b/src/ch19-01-unsafe-rust.md @@ -310,16 +310,16 @@ mutable slice to `i32` values, `as_mut_ptr` returns a raw pointer with the type We keep the assertion that the `mid` index is within the slice. Then we get to the unsafe code: the `slice::from_raw_parts_mut` function takes a raw pointer and a length, and it creates a slice. We use this function to create a slice -that starts from `ptr` and is `mid` items long. Then we call the `offset` +that starts from `ptr` and is `mid` items long. Then we call the `add` method on `ptr` with `mid` as an argument to get a raw pointer that starts at `mid`, and we create a slice using that pointer and the remaining number of items after `mid` as the length. The function `slice::from_raw_parts_mut` is unsafe because it takes a raw -pointer and must trust that this pointer is valid. The `offset` method on raw +pointer and must trust that this pointer is valid. The `add` method on raw pointers is also unsafe, because it must trust that the offset location is also a valid pointer. Therefore, we had to put an `unsafe` block around our calls to -`slice::from_raw_parts_mut` and `offset` so we could call them. By looking at +`slice::from_raw_parts_mut` and `add` so we could call them. By looking at the code and by adding the assertion that `mid` must be less than or equal to `len`, we can tell that all the raw pointers used within the `unsafe` block will be valid pointers to data within the slice. This is an acceptable and From f58d93901423b40cdd701838bece345aa3c79256 Mon Sep 17 00:00:00 2001 From: SuperSamus <40663462+SuperSamus@users.noreply.github.com> Date: Thu, 30 Jan 2020 18:34:13 +0100 Subject: [PATCH 3/3] Use ptr.add instad of offset --- listings/ch19-advanced-features/listing-19-06/src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/listings/ch19-advanced-features/listing-19-06/src/main.rs b/listings/ch19-advanced-features/listing-19-06/src/main.rs index 2bc2e5c70c..f25cbf49d9 100644 --- a/listings/ch19-advanced-features/listing-19-06/src/main.rs +++ b/listings/ch19-advanced-features/listing-19-06/src/main.rs @@ -10,7 +10,7 @@ fn split_at_mut(slice: &mut [i32], mid: usize) -> (&mut [i32], &mut [i32]) { unsafe { ( slice::from_raw_parts_mut(ptr, mid), - slice::from_raw_parts_mut(ptr.offset(mid as isize), len - mid), + slice::from_raw_parts_mut(ptr.add(mid), len - mid), ) } }