use get_unchecked() instead of as_ptr().offset()

This commit is contained in:
Jorge Aparicio 2017-10-31 19:22:09 +01:00
parent 10c5542c98
commit 12d501913e
2 changed files with 4 additions and 4 deletions

View File

@ -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 {

View File

@ -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 {