From 09682676ae1c484ebaefa59c4895452b0bccc58c Mon Sep 17 00:00:00 2001
From: Jorge Aparicio <jorge@japaric.io>
Date: Tue, 31 Oct 2017 14:50:26 +0100
Subject: [PATCH] don't use indexing to elide bound checks

---
 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 199e82e..5647f77 100644
--- a/src/ring_buffer/mod.rs
+++ b/src/ring_buffer/mod.rs
@@ -48,7 +48,7 @@ where
         let buffer: &[T] = unsafe { self.buffer.as_ref() };
 
         if self.head != self.tail {
-            let item = unsafe { ptr::read(&buffer[self.head]) };
+            let item = unsafe { ptr::read(buffer.as_ptr().offset(self.head as isize)) };
             self.head = (self.head + 1) % n;
             Some(item)
         } else {
@@ -64,7 +64,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(&mut buffer[self.tail], item) }
+            unsafe { ptr::write(buffer.as_mut_ptr().offset(self.tail as isize), item) }
             self.tail = next_tail;
             Ok(())
         } else {
diff --git a/src/ring_buffer/spsc.rs b/src/ring_buffer/spsc.rs
index f12f48d..dc6d0f5 100644
--- a/src/ring_buffer/spsc.rs
+++ b/src/ring_buffer/spsc.rs
@@ -43,7 +43,7 @@ where
         // NOTE(volatile) the value of `tail` can change at any time in the 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[rb.head]) };
+            let item = unsafe { ptr::read(buffer.as_ptr().offset(rb.head as isize)) };
             rb.head = (rb.head + 1) % n;
             Some(item)
         } else {
@@ -78,7 +78,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(&mut buffer[rb.tail], item) }
+            unsafe { ptr::write(buffer.as_mut_ptr().offset(rb.tail as isize), item) }
             rb.tail = next_tail;
             Ok(())
         } else {
-- 
GitLab