From 12d501913e398da31dd348fae011206b78cce012 Mon Sep 17 00:00:00 2001 From: Jorge Aparicio <jorge@japaric.io> Date: Tue, 31 Oct 2017 19:22:09 +0100 Subject: [PATCH] use get_unchecked() instead of as_ptr().offset() --- src/ring_buffer/mod.rs | 4 ++-- src/ring_buffer/spsc.rs | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/ring_buffer/mod.rs b/src/ring_buffer/mod.rs index 0b4e0ff..2f9cb62 100644 --- a/src/ring_buffer/mod.rs +++ b/src/ring_buffer/mod.rs @@ -52,7 +52,7 @@ where let buffer: &[T] = unsafe { self.buffer.as_ref() }; if self.head != self.tail { - let item = unsafe { ptr::read(buffer.as_ptr().offset(self.head as isize)) }; + let item = unsafe { ptr::read(buffer.get_unchecked(self.head)) }; self.head = (self.head + 1) % n; Some(item) } else { @@ -71,7 +71,7 @@ where if next_tail != self.head { // NOTE(ptr::write) the memory slot that we are about to write to is uninitialized. We // use `ptr::write` to avoid running `T`'s destructor on the uninitialized memory - unsafe { ptr::write(buffer.as_mut_ptr().offset(self.tail as isize), item) } + unsafe { ptr::write(buffer.get_unchecked_mut(self.tail), item) } self.tail = next_tail; Ok(()) } else { diff --git a/src/ring_buffer/spsc.rs b/src/ring_buffer/spsc.rs index 37c0671..5c6fa0e 100644 --- a/src/ring_buffer/spsc.rs +++ b/src/ring_buffer/spsc.rs @@ -47,7 +47,7 @@ where // NOTE(volatile) the value of `tail` can change at any time in the execution context of the // consumer so we inform this to the compiler using a volatile load if rb.head != unsafe { ptr::read_volatile(&rb.tail) } { - let item = unsafe { ptr::read(buffer.as_ptr().offset(rb.head as isize)) }; + let item = unsafe { ptr::read(buffer.get_unchecked(rb.head)) }; rb.head = (rb.head + 1) % n; Some(item) } else { @@ -91,7 +91,7 @@ where if next_tail != unsafe { ptr::read_volatile(&rb.head) } { // NOTE(ptr::write) the memory slot that we are about to write to is uninitialized. We // use `ptr::write` to avoid running `T`'s destructor on the uninitialized memory - unsafe { ptr::write(buffer.as_mut_ptr().offset(rb.tail as isize), item) } + unsafe { ptr::write(buffer.get_unchecked_mut(rb.tail), item) } rb.tail = next_tail; Ok(()) } else { -- GitLab