Skip to content

Commit 3a36251

Browse files
committed
fix: add a dedicated function to get to the base of the buffer #141
1 parent 70832ae commit 3a36251

File tree

3 files changed

+27
-3
lines changed

3 files changed

+27
-3
lines changed

src/ringbuffer_trait.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,7 @@ macro_rules! impl_ringbuffer {
498498
/// Implement various functions on implementors of [`RingBuffer`].
499499
/// This is to avoid duplicate code.
500500
macro_rules! impl_ringbuffer_ext {
501-
($get_unchecked: ident, $get_unchecked_mut: ident, $readptr: ident, $writeptr: ident, $mask: expr) => {
501+
($get_base_ptr: ident, $get_base_mut_ptr: ident, $get_unchecked: ident, $get_unchecked_mut: ident, $readptr: ident, $writeptr: ident, $mask: expr) => {
502502
#[inline]
503503
fn get_signed(&self, index: isize) -> Option<&T> {
504504
use core::ops::Not;
@@ -597,7 +597,7 @@ macro_rules! impl_ringbuffer_ext {
597597
return;
598598
}
599599

600-
let base: *const T = $get_unchecked(rb, 0);
600+
let base: *const T = $get_base_ptr(rb);
601601
let size = Self::ptr_buffer_size(rb);
602602
let offset_readptr = (*rb).$readptr + offset;
603603

@@ -640,7 +640,7 @@ macro_rules! impl_ringbuffer_ext {
640640
return;
641641
}
642642

643-
let base: *mut T = $get_unchecked_mut(rb, 0);
643+
let base: *mut T = $get_base_mut_ptr(rb);
644644
let size = Self::ptr_buffer_size(rb);
645645
let offset_readptr = (*rb).$readptr + offset;
646646

src/with_alloc/alloc_ringbuffer.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,8 @@ unsafe impl<T> RingBuffer<T> for AllocRingBuffer<T> {
274274
}
275275

276276
impl_ringbuffer_ext!(
277+
get_base_ptr,
278+
get_base_mut_ptr,
277279
get_unchecked,
278280
get_unchecked_mut,
279281
readptr,
@@ -332,6 +334,16 @@ impl<T> AllocRingBuffer<T> {
332334
}
333335
}
334336

337+
/// Get a const pointer to the buffer
338+
unsafe fn get_base_ptr<T>(rb: *const AllocRingBuffer<T>) -> *const T {
339+
(*rb).buf.cast()
340+
}
341+
342+
/// Get a mut pointer to the buffer
343+
unsafe fn get_base_mut_ptr<T>(rb: *mut AllocRingBuffer<T>) -> *mut T {
344+
(*rb).buf
345+
}
346+
335347
/// Get a reference from the buffer without checking it is initialized.
336348
///
337349
/// Caller must be sure the index is in bounds, or this will panic.

src/with_const_generics.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,16 @@ impl<T, const CAP: usize> ConstGenericRingBuffer<T, CAP> {
189189
}
190190
}
191191

192+
/// Get a const pointer to the buffer
193+
unsafe fn get_base_ptr<T, const N: usize>(rb: *const ConstGenericRingBuffer<T, N>) -> *const T {
194+
(*rb).buf.as_ptr().cast()
195+
}
196+
197+
/// Get a mut pointer to the buffer
198+
unsafe fn get_base_mut_ptr<T, const N: usize>(rb: *mut ConstGenericRingBuffer<T, N>) -> *mut T {
199+
(*rb).buf.as_mut_ptr().cast()
200+
}
201+
192202
/// Get a reference from the buffer without checking it is initialized
193203
/// Caller MUST be sure this index is initialized, or undefined behavior will happen
194204
unsafe fn get_unchecked<'a, T, const N: usize>(
@@ -305,6 +315,8 @@ unsafe impl<T, const CAP: usize> RingBuffer<T> for ConstGenericRingBuffer<T, CAP
305315
}
306316

307317
impl_ringbuffer_ext!(
318+
get_base_ptr,
319+
get_base_mut_ptr,
308320
get_unchecked,
309321
get_unchecked_mut,
310322
readptr,

0 commit comments

Comments
 (0)