Skip to content
Snippets Groups Projects
Commit f7ca3b5e authored by homunkulus's avatar homunkulus
Browse files

Auto merge of #7 - japaric:refactor, r=japaric

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

None
parents 10c5542c 12d50191
Branches
Tags
No related merge requests found
...@@ -52,7 +52,7 @@ where ...@@ -52,7 +52,7 @@ where
let buffer: &[T] = unsafe { self.buffer.as_ref() }; let buffer: &[T] = unsafe { self.buffer.as_ref() };
if self.head != self.tail { 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; self.head = (self.head + 1) % n;
Some(item) Some(item)
} else { } else {
...@@ -71,7 +71,7 @@ where ...@@ -71,7 +71,7 @@ where
if next_tail != self.head { if next_tail != self.head {
// NOTE(ptr::write) the memory slot that we are about to write to is uninitialized. We // 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 // 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; self.tail = next_tail;
Ok(()) Ok(())
} else { } else {
......
...@@ -47,7 +47,7 @@ where ...@@ -47,7 +47,7 @@ where
// NOTE(volatile) the value of `tail` can change at any time in the execution context of the // 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 // consumer so we inform this to the compiler using a volatile load
if rb.head != unsafe { ptr::read_volatile(&rb.tail) } { 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; rb.head = (rb.head + 1) % n;
Some(item) Some(item)
} else { } else {
...@@ -91,7 +91,7 @@ where ...@@ -91,7 +91,7 @@ where
if next_tail != unsafe { ptr::read_volatile(&rb.head) } { 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 // 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 // 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; rb.tail = next_tail;
Ok(()) Ok(())
} else { } else {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment