diff --git a/src/ring_buffer/mod.rs b/src/ring_buffer/mod.rs index 0b4e0ff6e1b0143a532226448fffc8b3111e7318..2f9cb62193c5f31ac412f5dec6e04978debbc783 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 37c06716b55433386f2c86d1511d15608539fbfb..5c6fa0ed0ca103695b64ac9275f317fb98d4c37f 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 {